Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"یک حلقهی تعلیم کامل","local":"a-full-training","sections":[{"title":"آمادهسازی برای تعلیم","local":"prepare-for-training","sections":[],"depth":3},{"title":"حلقهی تعلیم","local":"the-training-loop","sections":[],"depth":3},{"title":"حلقهی ارزیابی","local":"the-evaluation-loop","sections":[],"depth":3},{"title":"حلقهی تعلیم خود را با 🤗 Accelerate تقویت کنید","local":"supercharge-your-training-loop-with-accelerate","sections":[],"depth":3},{"title":"گامهای بعدی و بهترین روشها","local":"next-steps-and-best-practices","sections":[],"depth":3},{"title":"آزمون بخش","local":"section-quiz","sections":[{"title":"۱. تفاوت اصلی بهینهسازهای Adam و AdamW چیست؟","local":"۱-تفاوت-اصلی-بهینهسازهای-adam-و-adamw-چیست","sections":[],"depth":3},{"title":"۲. در یک حلقهی تعلیم، ترتیب درست عملیات چیست؟","local":"۲-در-یک-حلقهی-تعلیم-ترتیب-درست-عملیات-چیست","sections":[],"depth":3},{"title":"۳. کتابخانهی 🤗 Accelerate در درجهی اول به چه چیزی کمک میکند؟","local":"۳-کتابخانهی--accelerate-در-درجهی-اول-به-چه-چیزی-کمک-میکند","sections":[],"depth":3},{"title":"۴. چرا در حلقهی تعلیم بتچها را به دستگاه منتقل میکنیم؟","local":"۴-چرا-در-حلقهی-تعلیم-بتچها-را-به-دستگاه-منتقل-میکنیم","sections":[],"depth":3},{"title":"۵. model.eval() پیش از ارزیابی چه کاری انجام میدهد؟","local":"۵-modeleval-پیش-از-ارزیابی-چه-کاری-انجام-میدهد","sections":[],"depth":3},{"title":"۶. هدف torch.no_grad() هنگام ارزیابی چیست؟","local":"۶-هدف-torchnograd-هنگام-ارزیابی-چیست","sections":[],"depth":3},{"title":"۷. هنگام استفاده از 🤗 Accelerate در حلقهی تعلیم چه چیزی تغییر میکند؟","local":"۷-هنگام-استفاده-از--accelerate-در-حلقهی-تعلیم-چه-چیزی-تغییر-میکند","sections":[],"depth":3}],"depth":2}],"depth":1}"/> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/entry/start.CqZXmSrv.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/oyrVqMhC.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/B3UUDp28.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/entry/app.B4UvgX5H.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/BioDZtGL.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/NIl2lQjl.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/DN6uzRQe.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/nodes/0.DcFj1eGj.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/u5kFQ1Zb.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/nodes/49.B1U7l76G.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/BjCHz3cj.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/BPNwLiYb.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/B8VBSgkq.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/DFIrGYWg.js" rel="modulepreload"> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/chunks/NA1Q7nSg.js" rel="modulepreload"> | |
| <!--w5ab0a--><meta name="hf:doc:metadata" content="{"title":"یک حلقهی تعلیم کامل","local":"a-full-training","sections":[{"title":"آمادهسازی برای تعلیم","local":"prepare-for-training","sections":[],"depth":3},{"title":"حلقهی تعلیم","local":"the-training-loop","sections":[],"depth":3},{"title":"حلقهی ارزیابی","local":"the-evaluation-loop","sections":[],"depth":3},{"title":"حلقهی تعلیم خود را با 🤗 Accelerate تقویت کنید","local":"supercharge-your-training-loop-with-accelerate","sections":[],"depth":3},{"title":"گامهای بعدی و بهترین روشها","local":"next-steps-and-best-practices","sections":[],"depth":3},{"title":"آزمون بخش","local":"section-quiz","sections":[{"title":"۱. تفاوت اصلی بهینهسازهای Adam و AdamW چیست؟","local":"۱-تفاوت-اصلی-بهینهسازهای-adam-و-adamw-چیست","sections":[],"depth":3},{"title":"۲. در یک حلقهی تعلیم، ترتیب درست عملیات چیست؟","local":"۲-در-یک-حلقهی-تعلیم-ترتیب-درست-عملیات-چیست","sections":[],"depth":3},{"title":"۳. کتابخانهی 🤗 Accelerate در درجهی اول به چه چیزی کمک میکند؟","local":"۳-کتابخانهی--accelerate-در-درجهی-اول-به-چه-چیزی-کمک-میکند","sections":[],"depth":3},{"title":"۴. چرا در حلقهی تعلیم بتچها را به دستگاه منتقل میکنیم؟","local":"۴-چرا-در-حلقهی-تعلیم-بتچها-را-به-دستگاه-منتقل-میکنیم","sections":[],"depth":3},{"title":"۵. model.eval() پیش از ارزیابی چه کاری انجام میدهد؟","local":"۵-modeleval-پیش-از-ارزیابی-چه-کاری-انجام-میدهد","sections":[],"depth":3},{"title":"۶. هدف torch.no_grad() هنگام ارزیابی چیست؟","local":"۶-هدف-torchnograd-هنگام-ارزیابی-چیست","sections":[],"depth":3},{"title":"۷. هنگام استفاده از 🤗 Accelerate در حلقهی تعلیم چه چیزی تغییر میکند؟","local":"۷-هنگام-استفاده-از--accelerate-در-حلقهی-تعلیم-چه-چیزی-تغییر-میکند","sections":[],"depth":3}],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div dir="rtl"><div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>یک حلقهی تعلیم کامل</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><!----> <!--[0--><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><!--]--> <!--[0--><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>حالا میبینیم چگونه بدون استفاده از کلاس <code>Trainer</code> و با پیادهسازی یک حلقهی تعلیم از صفر، با بهکارگیری بهترین روشهای امروزی پایتورچ، به همان نتایجی برسیم که در بخش قبل به دست آوردیم. دوباره فرض میکنیم پردازش دادهها را در بخش ۲ انجام دادهاید. در اینجا خلاصهی کوتاهی از همهی چیزهایی که نیاز خواهید داشت ارائه میکنیم:</p> <blockquote class="tip"><p>🏗️ <strong>تعلیم از صفر</strong>: این بخش بر محتوای قبلی بنا شده است. برای راهنمایی جامع دربارهی حلقههای تعلیم پایتورچ و بهترین روشها، <a href="https://huggingface.co/docs/transformers/main/en/training#train-in-native-pytorch" rel="nofollow">مستندات تعلیم 🤗 Transformers</a> و <a href="https://huggingface.co/learn/cookbook/en/fine_tuning_code_llm_on_single_gpu#model" rel="nofollow">کتاب آشپزی تعلیم سفارشی</a> را ببینید.</p></blockquote> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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><!----></div> <!--[2--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>آمادهسازی برای تعلیم</span></h3><!--]--><!----> <p>پیش از آنکه حلقهی تعلیم را واقعاً بنویسیم، باید چند شیء را تعریف کنیم. اولین موارد، دیتالودرهایی هستند که برای پیمایش بتچها از آنها استفاده میکنیم. اما پیش از تعریف دیتالودرها، باید کمی پسپردازش روی <code>tokenized_datasets</code> انجام دهیم تا برخی کارهایی را که <code>Trainer</code> بهصورت خودکار انجام میداد، مدیریت کنیم. بهطور مشخص، باید:</p> <div dir="rtl"><ul><li>ستونهای مربوط به مقادیری را که مدل انتظار ندارد حذف کنیم (مانند ستونهای <code>sentence1</code> و <code>sentence2</code>).</li> <li>نام ستون <code>label</code> را به <code>labels</code> تغییر دهیم (چون مدل انتظار دارد نام آرگومان <code>labels</code> باشد).</li> <li>قالب دیتاستها را طوری تنظیم کنیم که بهجای لیست، تِنسورهای پایتورچ برگردانند.</li></ul></div> <p><code>tokenized_datasets</code> برای هرکدام از این مراحل یک روش دارد:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!---->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><!----></div> <p>سپس میتوانیم بررسی کنیم که نتیجه فقط شامل ستونهایی است که مدل ما میپذیرد:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!---->[<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><!----></div> <p>حالا که این کار انجام شده است، میتوانیم دیتالودرهای خود را بهسادگی تعریف کنیم:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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><!----></div> <p>برای آنکه سریع بررسی کنیم پردازش دادهها اشتباهی ندارد، میتوانیم یک بتچ را اینگونه بررسی کنیم:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!---->{<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><!----></div> <p>توجه کنید شکلهای واقعی احتمالاً برای شما کمی متفاوت خواهند بود، چون برای دیتالودر تعلیم <code>shuffle=True</code> تعیین کردهایم و داخل بتچ، ورودیها را تا بیشترین طول همطولسازی میکنیم.</p> <p>حالا که پردازش دادهها کاملاً تمام شده است (هدف رضایتبخش اما دستنیافتنی برای هر متخصص یادگیری ماشین)، به سراغ مدل برویم. آن را دقیقاً مانند بخش قبل نمونهسازی میکنیم:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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><!----></div> <p>برای آنکه مطمئن شویم همهچیز هنگام تعلیم بهخوبی پیش میرود، بتچ خود را به این مدل میدهیم:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!---->tensor(<span class="hljs-number">0.5441</span>, grad_fn=<NllLossBackward>) torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">2</span>])<!----></pre></div><!----></div> <p>همهی مدلهای 🤗 Transformers وقتی <code>labels</code> ارائه شوند، مقدار هزینه را برمیگردانند و همچنین لوجیتها را دریافت میکنیم (برای هر ورودی در بتچ ما دو لوجیت، یعنی یک تِنسور با اندازهی ۸ در ۲).</p> <p>تقریباً آمادهی نوشتن حلقهی تعلیم هستیم! فقط دو چیز کم داریم: یک بهینهساز و یک زمانبند نرخ یادگیری. چون تلاش میکنیم کاری را که <code>Trainer</code> انجام میداد بهصورت دستی تکرار کنیم، از همان مقدارهای پیشفرض استفاده خواهیم کرد. بهینهسازی که <code>Trainer</code> استفاده میکند <code>AdamW</code> است؛ این بهینهساز همان Adam است، اما برای منظمسازی کاهش وزن تفاوتی دارد (به مقالهی <a href="https://arxiv.org/abs/1711.05101" rel="nofollow">«منظمسازی کاهش وزن جداشده»</a> نوشتهی Ilya Loshchilov و Frank Hutter مراجعه کنید):</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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><!----></div> <blockquote class="tip"><p>💡 <strong>نکات بهینهسازی مدرن</strong>: برای عملکرد حتی بهتر، میتوانید این موارد را امتحان کنید:</p> <ul><li><strong>AdamW با کاهش وزن</strong>: <code>AdamW(model.parameters(), lr=5e-5, weight_decay=0.01)</code></li> <li><strong>Adam هشتبیتی</strong>: برای بهینهسازی کارآمد از نظر حافظه از <code>bitsandbytes</code> استفاده کنید</li> <li><strong>نرخهای یادگیری متفاوت</strong>: نرخهای یادگیری کمتر (از ۱e-۵ تا ۳e-۵) اغلب برای مدلهای بزرگ بهتر عمل میکنند</li></ul> <p>🚀 <strong>منابع بهینهسازی</strong>: در <a href="https://huggingface.co/docs/transformers/main/en/performance#optimizer" rel="nofollow">راهنمای بهینهسازی 🤗 Transformers</a> دربارهی بهینهسازها و راهبردهای تعلیم بیشتر بیاموزید.</p></blockquote> <p>در نهایت، زمانبند نرخ یادگیری که بهصورت پیشفرض استفاده میشود، فقط یک کاهش خطی از بیشترین مقدار (۵e-۵) تا صفر است. برای تعریف درست آن، باید تعداد گامهای تعلیم را بدانیم؛ این تعداد برابر است با تعداد دورههایی که میخواهیم اجرا کنیم ضربدر تعداد بتچهای تعلیم (که همان طول دیتالودر تعلیم ماست). <code>Trainer</code> بهصورت پیشفرض از سه دوره استفاده میکند، بنابراین ما نیز همین کار را انجام میدهیم:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-number">1377</span><!----></pre></div><!----></div> <!--[2--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>حلقهی تعلیم</span></h3><!--]--><!----> <p>یک نکتهی آخر: اگر به GPU دسترسی داشته باشیم، میخواهیم از آن استفاده کنیم (تعلیم روی CPU ممکن است بهجای چند دقیقه، چند ساعت طول بکشد). برای این کار، یک <code>device</code> تعریف میکنیم که مدل و بتچهایمان را روی آن قرار میدهیم:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!---->device(<span class="hljs-built_in">type</span>=<span class="hljs-string">'cuda'</span>)<!----></pre></div><!----></div> <p>حالا آمادهی تعلیم هستیم! برای آنکه بدانیم تعلیم چه زمانی تمام میشود، با استفاده از کتابخانهی <code>tqdm</code> روی تعداد گامهای تعلیم یک نوار پیشرفت اضافه میکنیم:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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><!----></div> <blockquote class="tip"><p>💡 <strong>بهینهسازیهای مدرن تعلیم</strong>: برای کارآمدتر کردن حلقهی تعلیم، این موارد را در نظر بگیرید:</p> <ul><li><strong>بُرش گرادیان</strong>: پیش از <code>optimizer.step()</code>، عبارت <code>torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)</code> را اضافه کنید</li> <li><strong>دقت ترکیبی</strong>: برای تعلیم سریعتر از <code>torch.cuda.amp.autocast()</code> و <code>GradScaler</code> استفاده کنید</li> <li><strong>انباشت گرادیان</strong>: گرادیانها را روی چند بتچ انباشته کنید تا بتچهای بزرگتر را شبیهسازی کنید</li> <li><strong>نقطههای تعلیم</strong>: برای ادامهی تعلیم پس از وقفه، بهصورت دورهای نقطههای تعلیم مدل را ذخیره کنید</li></ul> <p>🔧 <strong>راهنمای پیادهسازی</strong>: برای نمونههای دقیق این بهینهسازیها، <a href="https://huggingface.co/docs/transformers/main/en/perf_train_gpu_one" rel="nofollow">راهنمای تعلیم کارآمد 🤗 Transformers</a> و <a href="https://huggingface.co/docs/transformers/main/en/optimizers" rel="nofollow">مجموعهی بهینهسازها</a> را ببینید.</p></blockquote> <p>میبینید که هستهی حلقهی تعلیم بسیار شبیه حلقهی مقدمه است. ما هیچ گزارشی درخواست نکردیم، بنابراین این حلقهی تعلیم چیزی دربارهی عملکرد مدل به ما نمیگوید. باید یک حلقهی ارزیابی نیز به آن اضافه کنیم.</p> <!--[2--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>حلقهی ارزیابی</span></h3><!--]--><!----> <p>همانطور که قبلاً انجام دادیم، از معیاری استفاده میکنیم که کتابخانهی 🤗 Evaluate ارائه میدهد. روش <code>metric.compute()</code> را قبلاً دیدهایم، اما معیارها میتوانند با روش <code>add_batch()</code> هنگام پیمایش حلقهی پیشبینی، بتچها را برای ما جمع کنند. پس از جمعآوری همهی بتچها، میتوانیم با <code>metric.compute()</code> نتیجهی نهایی را دریافت کنیم. در اینجا نحوهی پیادهسازی همهی این موارد در یک حلقهی ارزیابی را میبینید:</p> <blockquote class="tip"><p>📊 <strong>بهترین روشهای ارزیابی</strong>: برای راهبردها و معیارهای ارزیابی پیشرفتهتر، <a href="https://huggingface.co/docs/evaluate/" rel="nofollow">مستندات 🤗 Evaluate</a> و <a href="https://github.com/huggingface/evaluation-guidebook" rel="nofollow">کتاب آشپزی جامع ارزیابی</a> را بررسی کنید.</p></blockquote> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!---->{<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><!----></div> <p>دوباره، نتایج شما بهدلیل تصادفی بودن مقداردهی اولیهی سَر مدل و درهمریختن ترتیب دادهها، کمی متفاوت خواهد بود، اما باید در همان محدوده باشد.</p> <blockquote class="tip"><p>✏️ <strong>امتحان کنید!</strong> حلقهی تعلیم قبلی را تغییر دهید تا مدل خود را روی دیتاست SST-2 کوک کنید.</p></blockquote> <!--[2--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>حلقهی تعلیم خود را با 🤗 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>حلقهی تعلیمی که پیشتر تعریف کردیم، روی یک CPU یا GPU بهخوبی کار میکند. اما با استفاده از کتابخانهی <a href="https://github.com/huggingface/accelerate" rel="nofollow">🤗 Accelerate</a>، تنها با چند تغییر میتوانیم تعلیم توزیعشده روی چند GPU یا TPU را فعال کنیم. 🤗 Accelerate پیچیدگی تعلیم توزیعشده، دقت ترکیبی و قرار دادن روی دستگاه را بهصورت خودکار مدیریت میکند. از نقطهی ایجاد دیتالودرهای تعلیم و اعتبارسنجی شروع کنیم؛ در اینجا حلقهی تعلیم دستی ما چنین است:</p> <blockquote class="tip"><p>⚡ <strong>بررسی عمیق Accelerate</strong>: در <a href="https://huggingface.co/docs/accelerate/" rel="nofollow">مستندات 🤗 Accelerate</a> دربارهی تعلیم توزیعشده، دقت ترکیبی و بهینهسازی سختافزار بیشتر بیاموزید و نمونههای عملی را در <a href="https://huggingface.co/docs/transformers/main/en/accelerate" rel="nofollow">مستندات ترنسفورمرها</a> بررسی کنید.</p></blockquote> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator | |
| <span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> 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><!----></div> <p>اولین خطی که باید اضافه کنیم، خط import است. خط دوم یک شیء <code>Accelerator</code> نمونهسازی میکند که محیط را بررسی کرده و راهاندازی مناسب توزیعشده را انجام میدهد. 🤗 Accelerate قرار دادن روی دستگاه را برای شما مدیریت میکند؛ بنابراین میتوانید خطهایی را که مدل را روی دستگاه قرار میدهند حذف کنید (یا اگر ترجیح میدهید، آنها را تغییر دهید تا بهجای <code>device</code> از <code>accelerator.device</code> استفاده کنند).</p> <p>بخش اصلی کار در خطی انجام میشود که دیتالودرها، مدل و بهینهساز را به <code>accelerator.prepare()</code> میفرستد. این تابع آن شیءها را در محفظهی مناسب قرار میدهد تا تعلیم توزیعشدهی شما همانطور که باید کار کند. تغییرات باقیمانده شامل حذف خطی است که بتچ را روی <code>device</code> قرار میدهد (اگر میخواهید آن را نگه دارید، فقط میتوانید تغییرش دهید تا از <code>accelerator.device</code> استفاده کند) و جایگزین کردن <code>loss.backward()</code> با <code>accelerator.backward(loss)</code>.</p> <blockquote class="tip"><p>⚠️ برای بهرهمندی از افزایش سرعت ارائهشده توسط Cloud TPUها، پیشنهاد میکنیم نمونهها را با استفاده از آرگومانهای <code>padding="max_length"</code> و <code>max_length</code> توکِنایزر، تا طولی ثابت همطولسازی کنید.</p></blockquote> <p>اگر میخواهید این کد را کپی و اجرا کنید، در اینجا حلقهی تعلیم کامل با 🤗 Accelerate آمده است:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator | |
| <span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> 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><!----></div> <p>قرار دادن این کد در یک اسکریپت <code>train.py</code> باعث میشود اسکریپت روی هر نوع راهاندازی توزیعشدهای قابل اجرا باشد. برای امتحان کردن آن در راهاندازی توزیعشدهی خود، این دستور را اجرا کنید:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-bash "><!---->accelerate config<!----></pre></div><!----></div> <p>این دستور از شما میخواهد به چند پرسش پاسخ دهید و پاسخهایتان را در فایل پیکربندیای ذخیره میکند که این دستور از آن استفاده خواهد کرد:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class=" "><!---->accelerate <span class="hljs-built_in">launch</span> train.py<!----></pre></div><!----></div> <p>این دستور تعلیم توزیعشده را اجرا میکند.</p> <p>اگر میخواهید این کار را در یک نوتبوک امتحان کنید (برای نمونه، برای آزمودن TPUها در کولَب)، کافی است کد را داخل یک <code>training_function()</code> قرار دهید و یک سلول آخر با این محتوا اجرا کنید:</p> <div dir="ltr"><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> notebook_launcher | |
| notebook_launcher(training_function)<!----></pre></div><!----></div> <p>نمونههای بیشتری را میتوانید در <a href="https://github.com/huggingface/accelerate/tree/main/examples" rel="nofollow">مخزن 🤗 Accelerate</a> پیدا کنید.</p> <blockquote class="tip"><p>🌐 <strong>تعلیم توزیعشده</strong>: برای پوشش جامع تعلیم چند GPU و چند گره، <a href="https://huggingface.co/docs/transformers/main/en/perf_train_gpu_many" rel="nofollow">راهنمای تعلیم توزیعشدهی 🤗 Transformers</a> و <a href="https://huggingface.co/docs/transformers/main/en/accelerate" rel="nofollow">کتاب آشپزی مقیاسبندی تعلیم</a> را ببینید.</p></blockquote> <!--[2--><h3 class="relative group"><a id="next-steps-and-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="#next-steps-and-best-practices"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>گامهای بعدی و بهترین روشها</span></h3><!--]--><!----> <p>حالا که پیادهسازی تعلیم از صفر را یاد گرفتهاید، در اینجا چند ملاحظهی دیگر برای استفاده در محیط تولید آمده است:</p> <p><strong>ارزیابی مدل</strong>: همیشه مدل خود را با چندین معیار، نه فقط دقت، ارزیابی کنید. برای ارزیابی جامع از کتابخانهی 🤗 Evaluate استفاده کنید.</p> <p><strong>تنظیم ابرپارامترها</strong>: برای بهینهسازی نظاممند ابرپارامترها، استفاده از کتابخانههایی مانند Optuna یا Ray Tune را در نظر بگیرید.</p> <p><strong>پایش مدل</strong>: معیارهای تعلیم، منحنیهای یادگیری و عملکرد اعتبارسنجی را در سراسر فرایند تعلیم دنبال کنید.</p> <p><strong>اشتراکگذاری مدل</strong>: پس از تعلیم، مدل خود را در هاب هاگینگفِیس به اشتراک بگذارید تا برای جامعه کاربران در دسترس باشد.</p> <p><strong>کارایی</strong>: برای مدلهای بزرگ، تکنیکهایی مانند نقطهگذاری گرادیان، کوک کارآمد از نظر پارامتر (LoRA، AdaLoRA) یا روشهای کوانتیزهسازی را در نظر بگیرید.</p> <p>این بررسی عمیق ما دربارهی کوک کردن با حلقههای تعلیم سفارشی به پایان میرسد. مهارتهایی که در اینجا آموختید، زمانی که به کنترل کامل بر فرایند تعلیم نیاز دارید یا میخواهید منطق تعلیم سفارشیای فراتر از آنچه API <code>Trainer</code> ارائه میدهد پیادهسازی کنید، بسیار به کارتان خواهند آمد.</p> <!--[1--><h2 class="relative group"><a id="section-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="#section-quiz"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>آزمون بخش</span></h2><!--]--><!----> <p>درک خود را از حلقههای تعلیم سفارشی و تکنیکهای پیشرفتهی تعلیم بیازمایید:</p> <!--[2--><h3 class="relative group"><a id="۱-تفاوت-اصلی-بهینهسازهای-adam-و-adamw-چیست" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۱-تفاوت-اصلی-بهینهسازهای-adam-و-adamw-چیست"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۱. تفاوت اصلی بهینهسازهای Adam و AdamW چیست؟</span></h3><!--]--><!----> <div><form><!--[--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"/> <!---->AdamW از زمانبندی نرخ یادگیری متفاوتی استفاده میکند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"/> <!---->AdamW شامل منظمسازی کاهش وزن جداشده است.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"/> <!---->AdamW فقط با مدلهای ترنسفورمر کار میکند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"/> <!---->AdamW به حافظهی کمتری از Adam نیاز دارد.<!----></label> <!--[-1--><!--]--><!--]--> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> <!--[-1--><!--]--></div></form></div><!----> <!--[2--><h3 class="relative group"><a id="۲-در-یک-حلقهی-تعلیم-ترتیب-درست-عملیات-چیست" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۲-در-یک-حلقهی-تعلیم-ترتیب-درست-عملیات-چیست"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۲. در یک حلقهی تعلیم، ترتیب درست عملیات چیست؟</span></h3><!--]--><!----> <div><form><!--[--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"/> <!---->عبور رو به جلو → عبور رو به عقب → گام بهینهساز → صفر کردن گرادیانها<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"/> <!---->عبور رو به جلو → عبور رو به عقب → گام بهینهساز → گام زمانبند → صفر کردن گرادیانها<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"/> <!---->صفر کردن گرادیانها → عبور رو به جلو → گام بهینهساز → عبور رو به عقب<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"/> <!---->عبور رو به جلو → صفر کردن گرادیانها → عبور رو به عقب → گام بهینهساز<!----></label> <!--[-1--><!--]--><!--]--> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> <!--[-1--><!--]--></div></form></div><!----> <!--[2--><h3 class="relative group"><a id="۳-کتابخانهی--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="#۳-کتابخانهی--accelerate-در-درجهی-اول-به-چه-چیزی-کمک-میکند"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۳. کتابخانهی 🤗 Accelerate در درجهی اول به چه چیزی کمک میکند؟</span></h3><!--]--><!----> <div><form><!--[--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"/> <!---->با بهینهسازی عبور رو به جلو، تعلیم مدلهایتان را سریعتر میکند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"/> <!---->بهصورت خودکار بهترین ابرپارامترها را انتخاب میکند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"/> <!---->با حداقل تغییرات کد، تعلیم توزیعشده روی چندین GPU/TPU را ممکن میکند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"/> <!---->مدلها را به فریمورکهای مختلفی مانند TensorFlow تبدیل میکند.<!----></label> <!--[-1--><!--]--><!--]--> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> <!--[-1--><!--]--></div></form></div><!----> <!--[2--><h3 class="relative group"><a id="۴-چرا-در-حلقهی-تعلیم-بتچها-را-به-دستگاه-منتقل-میکنیم" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۴-چرا-در-حلقهی-تعلیم-بتچها-را-به-دستگاه-منتقل-میکنیم"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۴. چرا در حلقهی تعلیم بتچها را به دستگاه منتقل میکنیم؟</span></h3><!--]--><!----> <div><form><!--[--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"/> <!---->برای آنکه تعلیم سریعتر شود.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"/> <!---->چون مدل و دادهها برای محاسبه باید روی یک دستگاه یکسان (CPU/GPU) باشند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"/> <!---->برای صرفهجویی در حافظه.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"/> <!---->DataLoader به آن نیاز دارد.<!----></label> <!--[-1--><!--]--><!--]--> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> <!--[-1--><!--]--></div></form></div><!----> <!--[2--><h3 class="relative group"><a id="۵-modeleval-پیش-از-ارزیابی-چه-کاری-انجام-میدهد" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۵-modeleval-پیش-از-ارزیابی-چه-کاری-انجام-میدهد"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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.eval() پیش از ارزیابی چه کاری انجام میدهد؟</span></h3><!--]--><!----> <div><form><!--[--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"/> <!---->پارامترهای مدل را ثابت میکند تا نتوان آنها را بهروزرسانی کرد.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"/> <!---->رفتار لایههایی مانند dropout و batch normalization را برای استنتاج تغییر میدهد.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"/> <!---->محاسبهی گرادیان را برای معیارهای ارزیابی فعال میکند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"/> <!---->معیارهای ارزیابی را بهصورت خودکار محاسبه میکند.<!----></label> <!--[-1--><!--]--><!--]--> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> <!--[-1--><!--]--></div></form></div><!----> <!--[2--><h3 class="relative group"><a id="۶-هدف-torchnograd-هنگام-ارزیابی-چیست" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#۶-هدف-torchnograd-هنگام-ارزیابی-چیست"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۶. هدف torch.no_grad() هنگام ارزیابی چیست؟</span></h3><!--]--><!----> <div><form><!--[--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"/> <!---->جلوگیری از پیشبینی کردن مدل.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"/> <!---->با غیرفعال کردن ردیابی گرادیان، حافظه را ذخیره و محاسبه را سریعتر میکند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"/> <!---->حالت ارزیابی مدل را فعال میکند.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"/> <!---->نتایج یکسان در اجراهای مختلف را تضمین میکند.<!----></label> <!--[-1--><!--]--><!--]--> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> <!--[-1--><!--]--></div></form></div><!----> <!--[2--><h3 class="relative group"><a id="۷-هنگام-استفاده-از--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="#۷-هنگام-استفاده-از--accelerate-در-حلقهی-تعلیم-چه-چیزی-تغییر-میکند"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>۷. هنگام استفاده از 🤗 Accelerate در حلقهی تعلیم چه چیزی تغییر میکند؟</span></h3><!--]--><!----> <div><form><!--[--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"/> <!---->باید کل حلقهی تعلیم را از صفر بازنویسی کنید.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"/> <!---->اشیای اصلی را با `accelerator.prepare()` دربرمیگیرید و بهجای `loss.backward()` از `accelerator.backward()` استفاده میکنید.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"/> <!---->باید تعداد GPUها را در کد خود مشخص کنید.<!----></label> <!--[-1--><!--]--><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"/> <!---->باید از بهینهساز و زمانبند متفاوتی استفاده کنید.<!----></label> <!--[-1--><!--]--><!--]--> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> <!--[-1--><!--]--></div></form></div><!----> <blockquote class="tip"><p>💡 <strong>نکات کلیدی:</strong></p> <ul><li>حلقههای تعلیم دستی کنترل کاملی به شما میدهند، اما باید ترتیب درست را بدانید: عبور رو به جلو → عبور رو به عقب → گام بهینهساز → گام زمانبند → صفر کردن گرادیانها</li> <li>AdamW با کاهش وزن، بهینهساز پیشنهادی برای مدلهای ترنسفورمر است</li> <li>برای رفتار درست و کارایی بهتر، هنگام ارزیابی همیشه از <code>model.eval()</code> و <code>torch.no_grad()</code> استفاده کنید</li> <li>🤗 Accelerate تعلیم توزیعشده را با حداقل تغییرات کد در دسترس قرار میدهد</li> <li>مدیریت دستگاه (انتقال تِنسورها به GPU/CPU) برای عملیات پایتورچ ضروری است</li> <li>تکنیکهای مدرن مانند دقت ترکیبی، انباشت گرادیان و بُرش گرادیان میتوانند کارایی تعلیم را بهطور چشمگیری بهبود دهند</li></ul></blockquote></div> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/course/blob/main/chapters/fa/chapter3/4.mdx" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_pfm8ny = { | |
| base: "/docs/course/pr_1364/fa", | |
| assets: "/docs/course/pr_1364/fa" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/course/pr_1364/fa/_app/immutable/entry/start.CqZXmSrv.js"), | |
| import("/docs/course/pr_1364/fa/_app/immutable/entry/app.B4UvgX5H.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 49], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 96.8 kB
- Xet hash:
- 59cb2a15d7af1d6574055f78b523bc0665f103141f456a2af46109413aba24fa
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.