Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"خلاصهسازی","local":"summarization","sections":[{"title":"آمادهسازی یک پیکره چندزبانه","local":"preparing-a-multilingual-corpus","sections":[],"depth":2},{"title":"مدلهای خلاصهسازی متن","local":"models-for-text-summarization","sections":[],"depth":2},{"title":"پیشپردازش دادهها","local":"preprocessing-the-data","sections":[],"depth":2},{"title":"معیارهای خلاصهسازی متن","local":"metrics-for-text-summarization","sections":[{"title":"ایجاد یک خط مبنای قوی","local":"creating-a-strong-baseline","sections":[],"depth":3}],"depth":2},{"title":"کوک کردن mT5 با رابط برنامهنویسی کاربردی Trainer","local":"fine-tuning-mt5-with-the-trainer-api","sections":[],"depth":2},{"title":"کوک دقیق mT5 با Keras","local":"fine-tuning-mt5-with-keras","sections":[],"depth":2},{"title":"کوک کردن mT5 با 🤗 شتابدهی","local":"fine-tuning-mt5-with-accelerate","sections":[{"title":"آمادهسازی همهچیز برای آموزش","local":"preparing-everything-for-training","sections":[],"depth":3},{"title":"حلقه آموزشی","local":"training-loop","sections":[],"depth":3}],"depth":2},{"title":"با استفاده از مدل تنظیمشدهی دقیق شما","local":"using-your-fine-tuned-model","sections":[],"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/82.MJuDFasI.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/DbEBkG5U.js" rel="modulepreload"> | |
| <!--m7mm2j--><meta name="hf:doc:metadata" content="{"title":"خلاصهسازی","local":"summarization","sections":[{"title":"آمادهسازی یک پیکره چندزبانه","local":"preparing-a-multilingual-corpus","sections":[],"depth":2},{"title":"مدلهای خلاصهسازی متن","local":"models-for-text-summarization","sections":[],"depth":2},{"title":"پیشپردازش دادهها","local":"preprocessing-the-data","sections":[],"depth":2},{"title":"معیارهای خلاصهسازی متن","local":"metrics-for-text-summarization","sections":[{"title":"ایجاد یک خط مبنای قوی","local":"creating-a-strong-baseline","sections":[],"depth":3}],"depth":2},{"title":"کوک کردن mT5 با رابط برنامهنویسی کاربردی Trainer","local":"fine-tuning-mt5-with-the-trainer-api","sections":[],"depth":2},{"title":"کوک دقیق mT5 با Keras","local":"fine-tuning-mt5-with-keras","sections":[],"depth":2},{"title":"کوک کردن mT5 با 🤗 شتابدهی","local":"fine-tuning-mt5-with-accelerate","sections":[{"title":"آمادهسازی همهچیز برای آموزش","local":"preparing-everything-for-training","sections":[],"depth":3},{"title":"حلقه آموزشی","local":"training-loop","sections":[],"depth":3}],"depth":2},{"title":"با استفاده از مدل تنظیمشدهی دقیق شما","local":"using-your-fine-tuned-model","sections":[],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/course/pr_1364/fa/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <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><!----> <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="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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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><!--]--><!----> <!--[0--><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><!----> <!--[0--><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><!--]--> <!--[0--><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>در این بخش، نگاهی خواهیم داشت به اینکه چگونه میتوان از مدلهای ترنسفورمر برای خلاصهسازی اسناد طولانی استفاده کرد، کاری که به عنوان خلاصهسازی متن شناخته میشود. این یکی از چالشبرانگیزترین وظایف NLP است زیرا به طیف وسیعی از تواناییها، مانند درک متون طولانی و تولید متن منسجم که موضوعات اصلی یک سند را در بر میگیرد، نیاز دارد. با این حال، اگر خلاصهسازی متن به خوبی انجام شود، ابزاری قدرتمند است که میتواند سرعت را افزایش دهد. فرآیندهای مختلف کسب و کار را با کاهش بار متخصصان حوزه برای خواندن اسناد طولانی با جزئیات، بهبود میبخشد.</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>اگرچه در حال حاضر مدلهای مختلف و دقیقی برای خلاصهسازی در <a href="https://huggingface.co/models?pipeline_tag=summarization&sort=downloads" rel="nofollow">هاگینگفِیس هاب</a> وجود دارد، اما تقریباً همه آنها فقط برای اسناد انگلیسی مناسب هستند. بنابراین، برای اضافه کردن یک تغییر در این بخش، یک مدل دوزبانه برای انگلیسی و اسپانیایی آموزش خواهیم داد. در پایان این بخش، شما یک <a href="https://huggingface.co/huggingface-course/mt5-small-finetuned-amazon-en-es" rel="nofollow">مدل</a> خواهید داشت که میتواند نظرات مشتریان را مانند آنچه در اینجا نشان داده شده است، خلاصه کند:</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>همانطور که خواهیم دید، این خلاصهها مختصر هستند زیرا از عناوینی که مشتریان در بررسیهای محصول خود ارائه میدهند، استخراج شدهاند. بیایید با جمعآوری یک دیتاسِت دوزبانه مناسب برای این کار شروع کنیم.</p> <!--[1--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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>ما از <a href="https://huggingface.co/datasets/amazon_reviews_multi" rel="nofollow">Multilingual Amazon Reviews Corpus</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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> 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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>همانطور که میبینید، برای هر زبان، ۲۰۰۰۰۰ نقد برای تقسیمبندی <code>train</code> و ۵۰۰۰ نقد برای هر یک از تقسیمبندیهای <code>validation</code> و <code>test</code> وجود دارد. اطلاعات نقد و بررسی که ما به آنها علاقهمندیم در ستونهای <code>review_body</code> و <code>review_title</code> قرار دارد. بیایید با ایجاد یک تابع ساده که با تکنیکهایی که در <a href="/course/chapter5">فصل ۵</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">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'>> 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"'>> 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'>> Title: Worked in front position, not rear'</span> | |
| <span class="hljs-string">'>> 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">'>> Title: meh'</span> | |
| <span class="hljs-string">'>> 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">'>> Title: Can\'t beat these for the money'</span> | |
| <span class="hljs-string">'>> 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><!----> <blockquote><p>نکته | |
| ✏️ <strong>امتحانش کنید!</strong> برای بررسی سایر نقدهای موجود در مجموعه، مقدار تصادفی را در دستور <code>Dataset.shuffle()</code> تغییر دهید. اگر اسپانیایی زبان هستید، به برخی از نقدهای موجود در <code>spanish_dataset</code> نگاهی بیندازید تا ببینید آیا عناوین آنها نیز خلاصههای معقولی به نظر میرسند یا خیر.</p></blockquote> <p>این نمونه، تنوع نقدهایی را که معمولاً بهصورت آنلاین پیدا میکنید، از مثبت تا منفی (و هر چیزی بین این دو!) نشان میدهد. اگرچه مثال با عنوان «معمولی» خیلی آموزنده نیست، اما عناوین دیگر خلاصههای مناسبی از خود نقدها به نظر میرسند. آموزش یک مدل خلاصهسازی روی تمام ۴۰۰۰۰۰ نقد روی یک پردازنده گرافیکی واحد، زمان بسیار زیادی میبرد، بنابراین در عوض، ما بر تولید خلاصه برای یک نقد واحد تمرکز خواهیم کرد. دامنه محصولات. برای اینکه بفهمیم از بین چه دامنههایی میتوانیم انتخاب کنیم، بیایید <code>english_dataset</code> را به <code>pandas.DataFrame</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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>محبوبترین محصولات در دیتاسِت انگلیسی مربوط به لوازم خانه، پوشاک و تجهیزات بیسیم هستند. برای ماندن در فضای آمازون، روی خلاصهسازی مرور کتابها تمرکز میکنیم؛ بالاخره شرکت با همین کار آغاز شد! دو دستهی مناسب، <code>book</code> و <code>digital_ebook_purchase</code>، را میبینیم؛ پس دیتاسِتهای هر دو زبان را فقط برای همین محصولات فیلتر میکنیم. همانطور که در <a href="/course/chapter5">فصل ۵</a> دیدیم، متد <code>Dataset.filter()</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">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>حالا وقتی این تابع را روی <code>english_dataset</code> و <code>spanish_dataset</code> اعمال کنیم، نتیجه فقط شامل ردیفهایی خواهد بود که شامل دستههای کتاب هستند. قبل از اعمال فیلتر، بیایید قالب <code>english_dataset</code> را از <code>"pandas"</code> به <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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->english_dataset.reset_format()<!----></pre></div><!----> <p>سپس میتوانیم تابع فیلتر را اعمال کنیم و به عنوان یک بررسی سلامت، نمونهای از نقدها را بررسی کنیم تا ببینیم آیا واقعاً درباره کتابها هستند یا خیر:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'>> Title: I\'m dissapointed.'</span> | |
| <span class="hljs-string">'>> 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">'>> Title: Good art, good price, poor design'</span> | |
| <span class="hljs-string">'>> 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">'>> Title: Helpful'</span> | |
| <span class="hljs-string">'>> 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>خب، میبینیم که نقدها صرفاً مربوط به کتابها نیستند و ممکن است به چیزهایی مانند تقویمها و برنامههای الکترونیکی مانند OneNote اشاره داشته باشند. با این وجود، به نظر میرسد این دامنه برای آموزش یک مدل خلاصهسازی مناسب باشد. قبل از اینکه به مدلهای مختلف مناسب برای این کار نگاه کنیم، یک مرحله آمادهسازی دادهها داریم: ترکیب نقدهای انگلیسی و اسپانیایی به عنوان یک شیء واحد <code>DatasetDict</code>. 🤗 دیتاسِتها یک تابع مفید <code>concatenate_datasets()</code> ارائه میدهد که (همانطور که از نامش پیداست) دو شیء <code>Dataset</code> را روی هم قرار میدهد. بنابراین، برای ایجاد دیتاسِت دوزبانه خود، روی هر تقسیمبندی حلقه میزنیم، دیتاسِتهای مربوط به آن تقسیمبندی را به هم متصل میکنیم و نتیجه را تغییر میدهیم تا مطمئن شویم مدل ما به یک زبان واحد بیشبرازش (overfit) نمیشود:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'>> Title: Easy to follow!!!!'</span> | |
| <span class="hljs-string">'>> 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">'>> Title: PARCIALMENTE DAÑADO'</span> | |
| <span class="hljs-string">'>> 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">'>> Title: no lo he podido descargar'</span> | |
| <span class="hljs-string">'>> Review: igual que el anterior'</span><!----></pre></div><!----> <p>این قطعاً شبیه ترکیبی از نقدهای انگلیسی و اسپانیایی به نظر میرسد! حالا که یک مجموعه آموزشی داریم، یک نکته نهایی که باید بررسی کنیم، توزیع کلمات در نقدها و عناوین آنهاست. این امر به ویژه برای وظایف خلاصهسازی مهم است، جایی که خلاصههای مرجع کوتاه در دادهها میتوانند مدل را به گونهای منحرف کنند که فقط یک یا دو کلمه را در خلاصههای تولید شده نمایش دهد. نمودارهای زیر توزیع کلمات را نشان میدهند و میتوانیم آن را ببینیم. عناوین به شدت به سمت ۱-۲ کلمه متمایل شدهاند:</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>برای مقابله با این مشکل، مثالهایی با عناوین بسیار کوتاه را فیلتر میکنیم تا مدل ما بتواند خلاصههای جالبتری تولید کند. از آنجایی که با متون انگلیسی و اسپانیایی سروکار داریم، میتوانیم از یک روش ابتکاری تقریبی برای تقسیم عناوین بر اساس فضای خالی استفاده کنیم و سپس از روش قابل اعتماد <code>Dataset.filter()</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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()) > <span class="hljs-number">2</span>)<!----></pre></div><!----> <p>حالا که مجموعه مقالاتمان را آماده کردهایم، بیایید نگاهی به چند مدل احتمالی ترنسفورمر بیندازیم که میتوان روی آنها تنظیمات دقیقی انجام داد!</p> <!--[1--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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>اگر در مورد آن فکر کنید، خلاصهسازی متن نوعی کار مشابه ترجمه ماشینی است: ما مجموعهای از متن مانند یک نقد و بررسی داریم که میخواهیم آن را به نسخهای کوتاهتر که ویژگیهای برجسته ورودی را در بر میگیرد، “ترجمه” کنیم. بر این اساس، اکثر مدلهای ترنسفورمر برای خلاصهسازی، معماری رمزگذار-رمزگشا را که برای اولین بار در <a href="/course/chapter1">فصل 1</a> با آن مواجه شدیم، اتخاذ میکنند، اگرچه استثنائاتی مانند … نیز وجود دارد. خانواده مدلهای GPT که میتوانند برای خلاصهسازی در تنظیمات چند مرحلهای نیز استفاده شوند. جدول زیر برخی از مدلهای از پیش تعلیمدیده محبوب را که میتوانند برای خلاصهسازی کوک کردن شوند، فهرست میکند.</p> <table><thead><tr><th align="center">مدل ترنسفورمر</th><th>توضیحات</th><th align="center">چندزبانه؟</th></tr></thead><tbody><tr><td align="center"><a href="https://huggingface.co/gpt2-xl" rel="nofollow">GPT-2</a></td><td>اگرچه به عنوان یک مدل زبانی خود-رگرسیون آموزش دیده است، میتوانید با افزودن “TL;DR” به انتهای متن ورودی، GPT-2 را وادار به تولید خلاصه کنید.</td><td align="center">❌</td></tr><tr><td align="center"><a href="https://huggingface.co/google/pegasus-large" rel="nofollow">PEGASUS</a></td><td>از یک هدف پیشآموزشی برای پیشبینی جملههای ماسکشده در متنهای چندجملهای استفاده میکند. این هدف پیشآموزشی به خلاصهسازی نزدیکتر از مدلسازی زبانی معمولی است و در معیارهای رایج امتیاز بالایی میگیرد.</td><td align="center">❌</td></tr><tr><td align="center"><a href="https://huggingface.co/t5-base" rel="nofollow">T5</a></td><td>یک معماری ترنسفورمر جهانی که تمام وظایف را در یک چارچوب متن به متن فرموله میکند؛ به عنوان مثال، قالب ورودی برای مدل جهت خلاصه کردن یک سند <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>یک نسخه چندزبانه از T5، که از قبل روی دیتاسِتهای چندزبانه Common Crawl (mC4) آموزش دیده و ۱۰۱ زبان را پوشش میدهد.</td><td align="center">✅</td></tr><tr><td align="center"><a href="https://huggingface.co/facebook/bart-base" rel="nofollow">BART</a></td><td>یک معماری ترنسفورمر جدید با پشته رمزگذار و رمزگشا که برای بازسازی ورودی خراب آموزش دیده است و طرحهای پیشتعلیم BERT و 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>یک نسخه چند زبانه از BART، از پیش آموزش دیده بر روی 50 زبان.</td><td align="center">✅</td></tr></tbody></table> <p>همانطور که از این جدول مشاهده میکنید، اکثر مدلهای ترنسفورمر برای خلاصهسازی (و در واقع اکثر وظایف NLP) تکزبانه هستند. اگر وظیفه شما به زبانی با «منابع بالا» مانند انگلیسی یا آلمانی باشد، این عالی است، اما برای هزاران زبان دیگر که در سراسر جهان استفاده میشوند، اینطور نیست. خوشبختانه، دستهای از مدلهای ترنسفورمر چندزبانه، مانند mT5 و mBART، وجود دارند که به کمک میآیند. این مدلها از قبل آموزش دیدهاند با استفاده از مدلسازی زبان، اما با یک تغییر: به جای آموزش روی مجموعهای از یک زبان، آنها به طور مشترک روی متون بیش از ۵۰ زبان به طور همزمان آموزش میبینند!</p> <p>ما بر روی mT5 تمرکز خواهیم کرد، یک معماری جالب مبتنی بر T5 که در یک چارچوب متن به متن از پیش تعلیم داده شده است. در T5، هر وظیفه NLP بر اساس یک پیشوند اعلان مانند <code>summarize:</code> فرموله میشود که مدل را برای تطبیق متن تولید شده با اعلان شرطی میکند. همانطور که در شکل زیر نشان داده شده است، این امر T5 را بسیار متنوع میکند، زیرا میتوانید بسیاری از وظایف را با یک مدل واحد حل کنید!</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 از پیشوندها استفاده نمیکند، اما بسیاری از تطبیقپذیری T5 را به اشتراک میگذارد و از مزیت چندزبانه بودن نیز برخوردار است. اکنون که یک مدل انتخاب کردهایم، بیایید نگاهی به آمادهسازی دادههای خود برای آموزش بیندازیم.</p> <blockquote class="tip"><p>✏️ <strong>امتحانش کنید!</strong> پس از اتمام این بخش، با کوک کردن mT5 با همان تکنیکها، ببینید mBART در مقایسه با mT5 چقدر خوب عمل میکند. برای امتیاز بیشتر، میتوانید T5 را فقط روی نقدهای انگلیسی تنظیم کنید. از آنجایی که T5 یک پیشوند خاص دارد، باید در مراحل پیشپردازش زیر، <code>summarize:</code> را به مثالهای ورودی اضافه کنید.</p></blockquote> <!--[1--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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><!--]--><!----> <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>کار بعدی توکِنسازی و کدگذاری مرورها و عنوانهای آنهاست. طبق معمول، ابتدا توکِنساز مربوط به نقطهی بررسی مدل از پیش تعلیمدیده را بارگذاری میکنیم. از <code>mt5-small</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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> 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><!----> <blockquote><p>نکته | |
| 💡 در مراحل اولیه پروژههای NLP خود، یک روش خوب این است که یک کلاس از مدلهای “کوچک” را روی یک نمونه کوچک از دادهها آموزش دهید. این به شما امکان میدهد تا سریعتر اشکالزدایی کنید و به سمت یک گردش کار سرتاسری تکرار کنید. هنگامی که از نتایج مطمئن شدید، همیشه میتوانید مدل را با تغییر ساده نقطه بررسی مدل، مقیاسبندی کنید!</p></blockquote> <p>بیایید توکنساز 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'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>در اینجا میتوانیم <code>input_ids</code> و <code>attention_mask</code> آشنا را که در اولین آزمایشهای کوک کردن خود در <a href="/course/chapter3">فصل 3</a> با آنها مواجه شدیم، ببینیم. بیایید این شناسههای ورودی را با تابع <code>convert_ids_to_tokens()</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'▁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">'</s>'</span>]<!----></pre></div><!----> <p>کاراکتر یونیکد ویژه <code>▁</code> و توکن پایان توالی <code></s></code> نشان میدهند که ما با توکنساز SentencePiece سروکار داریم که مبتنی بر الگوریتم قطعهبندی Unigram است که در <a href="/course/chapter6">فصل 6</a> مورد بحث قرار گرفته است. Unigram به ویژه برای پیکرههای چندزبانه مفید است زیرا به SentencePiece اجازه میدهد تا نسبت به لهجهها، علائم نگارشی و این واقعیت که بسیاری از زبانها، مانند ژاپنی، کاراکترهای فاصله ندارند، بیتفاوت باشد.</p> <p>برای توکنیزه کردن دیتاسِتهایمان، باید با یک نکتهٔ ظریف مرتبط با خلاصهسازی سر و کار داشته باشیم: از آنجا که برچسبهای ما نیز متن هستند، ممکن است از حداکثر اندازهٔ متن مدل فراتر روند. این بدان معناست که باید هم بر روی نظرات و هم بر روی عناوین آنها کوتاهسازی اعمال کنیم تا مطمئن شویم ورودیهای بیش از حد طولانی به مدل خود ارسال نمیکنیم. توکنایزرها در ترنسفورمرهای 🤗 یک آرگومان <code>text_target</code> ارائه میدهند که به شما امکان میدهد برچسبها را همزمان با ورودیها توکنیزه کنید. در اینجا مثالی از نحوهٔ پردازش ورودیها و اهداف برای 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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>بیایید این کد را بررسی کنیم تا بفهمیم چه اتفاقی میافتد. اولین کاری که انجام دادهایم، تعریف مقادیری برای <code>max_input_length</code> و <code>max_target_length</code> است که حداکثر طول نقدها و عناوین ما را تعیین میکنند. از آنجایی که متن نقد معمولاً بسیار بزرگتر از عنوان است، این مقادیر را بر این اساس مقیاسبندی کردهایم.</p> <p>با <code>preprocess_function()</code>، توکِنسازی کل پیکره با تابع کاربردی <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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->tokenized_datasets = books_dataset.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <p>اکنون که متن پیشپردازش شده است، بیایید نگاهی به برخی از معیارهایی که معمولاً برای خلاصهسازی استفاده میشوند، بیندازیم. همانطور که خواهیم دید، هیچ راه حل قطعی برای اندازهگیری کیفیت متن تولید شده توسط ماشین وجود ندارد.</p> <blockquote><p>نکته | |
| 💡 شاید متوجه شده باشید که ما در تابع <code>Dataset.map()</code> بالا از <code>batched=True</code> استفاده کردیم. این تابع مثالها را در دستههای ۱۰۰۰تایی (بهصورت پیشفرض) کدگذاری میکند و به شما امکان میدهد از قابلیتهای چندنخی توکنایزرهای سریع در ترنسفورمرهای 🤗 استفاده کنید. در صورت امکان، سعی کنید از <code>batched=True</code> استفاده کنید تا از پیشپردازش خود نهایت استفاده را ببرید!</p></blockquote> <!--[1--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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><!--]--><!----> <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>در مقایسه با اکثر وظایف دیگری که در این دوره پوشش دادهایم، اندازهگیری عملکرد وظایف تولید متن مانند خلاصهسازی یا ترجمه به این سادگی نیست. برای مثال، با توجه به نقدی مانند «من عاشق خواندن بازیهای گرسنگی بودم»، چندین خلاصه معتبر وجود دارد، مانند «من عاشق بازیهای گرسنگی بودم» یا «بازیهای گرسنگی یک اثر عالی برای خواندن است». واضح است که اعمال نوعی تطابق دقیق بین خلاصه تولید شده و این برچسب راه حل خوبی نیست — حتی انسانها هم تحت چنین معیاری عملکرد ضعیفی خواهند داشت، زیرا همه ما سبک نوشتاری خودمان را داریم.</p> <p>برای خلاصهسازی، یکی از معیارهای رایج <a href="https://en.wikipedia.org/wiki/ROUGE_(metric)" rel="nofollow">امتیاز ROUGE</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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>یک راه برای مقایسه آنها میتواند شمارش تعداد کلمات همپوشانی باشد که در این مورد ۶ خواهد بود. با این حال، این کمی خام است، بنابراین در عوض ROUGE مبتنی بر محاسبه نمرات <em>precision</em> و <em>recall</em> برای همپوشانی است.</p> <blockquote><p>نکته | |
| 🙋 اگر این اولین باری است که در مورد دقت و فراخوانی میشنوید، نگران نباشید — ما با هم چند مثال واضح را بررسی خواهیم کرد تا همه چیز روشن شود. این معیارها معمولاً در وظایف طبقهبندی با هم مواجه میشوند، بنابراین اگر میخواهید بفهمید که دقت و فراخوانی در آن زمینه چگونه تعریف میشوند، توصیه میکنیم <a href="https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html" rel="nofollow">راهنماهای <code>scikit-learn</code></a> را بررسی کنید.</p></blockquote> <p>برای ROUGE، معیار فراخوانی، میزان خلاصه مرجعی را که توسط خلاصه تولید شده در بر گرفته شده است، اندازهگیری میکند. اگر فقط کلمات را مقایسه کنیم، فراخوانی را میتوان طبق فرمول زیر محاسبه کرد: <!----><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.0778em;">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.0139em;">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.0778em;">of</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.0139em;">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>برای مثال سادهی بالا، این فرمول میزان یادآوری کاملی برابر با ۶/۶ = ۱ ارائه میدهد؛ یعنی تمام کلمات موجود در خلاصهی مرجع توسط مدل تولید شدهاند. این ممکن است عالی به نظر برسد، اما تصور کنید اگر خلاصهی تولید شدهی ما این بود: «من واقعاً واقعاً عاشق خواندن بازیهای گرسنگی در تمام طول شب بودم». این نیز میزان یادآوری کاملی داشت، اما مسلماً خلاصهی بدتری است زیرا طولانی است. برای مقابله با این سناریوها، ما همچنین محاسبه میکنیم دقت، که در زمینه ROUGE میزان مرتبط بودن خلاصه تولید شده را اندازهگیری میکند: <!----><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.0778em;">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.0139em;">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.0778em;">of</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.0139em;">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>اعمال این مورد بر خلاصهی طولانی ما، دقتی برابر با ۶/۱۰ = ۰.۶ را ارائه میدهد که به طور قابل توجهی بدتر از دقت ۶/۷ = ۰.۸۶ است که توسط خلاصهی کوتاهتر ما به دست آمده است. در عمل، معمولاً هم دقت و هم فراخوانی محاسبه میشوند و سپس امتیاز F1 (میانگین هارمونیک دقت و فراخوانی) گزارش میشود. ما میتوانیم این کار را به راحتی در دیتاسِتهای 🤗 با نصب اولیهی بستهی <code>rouge_score</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->!pip install rouge_score<!----></pre></div><!----> <p>و سپس بارگذاری متریک ROUGE به شرح زیر:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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">import</span> evaluate | |
| rouge_score = evaluate.load(<span class="hljs-string">"rouge"</span>)<!----></pre></div><!----> <p>سپس میتوانیم از تابع <code>rouge_score.compute()</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'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>وای، اطلاعات زیادی در این خروجی وجود دارد — معنی همه اینها چیست؟ اول، 🤗 Datasets در واقع فواصل اطمینان را برای دقت، فراخوانی و امتیاز F1 محاسبه میکند؛ اینها ویژگیهای <code>low</code>، <code>mid</code> و <code>high</code> هستند که میتوانید اینجا ببینید. علاوه بر این، 🤗 Datasets انواع مختلفی از امتیازات ROUGE را محاسبه میکند که بر اساس انواع مختلف جزئیات متن هنگام مقایسه خلاصههای تولید شده و مرجع هستند. نوع <code>rouge1</code> همپوشانی یونیگرامها است — این فقط یک روش فانتزی برای بیان همپوشانی کلمات است و دقیقاً همان معیاری است که در بالا در مورد آن صحبت کردیم. برای تأیید این موضوع، بیایید مقدار <code>mid</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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>عالی، اعداد دقت و فراخوانی با هم مطابقت دارند! حالا در مورد سایر نمرات ROUGE چطور؟ <code>rouge2</code> همپوشانی بین دو رشته (همپوشانی جفت کلمات) را اندازهگیری میکند، در حالی که <code>rougeL</code> و <code>rougeLsum</code> طولانیترین توالیهای منطبق از کلمات را با جستجوی طولانیترین زیررشتههای مشترک در خلاصههای تولید شده و مرجع اندازهگیری میکنند. “جمع” در <code>rougeLsum</code> به این واقعیت اشاره دارد که این معیار بر روی کل خلاصه محاسبه میشود، در حالی که <code>rougeL</code> به عنوان میانگین روی تک تک جملات محاسبه میشود.</p> <blockquote class="tip"><p>✏️ <strong>امتحانش کنید!</strong> مثال خودتان را از یک خلاصه تولید شده و مرجع ایجاد کنید و ببینید آیا نمرات ROUGE حاصل با محاسبه دستی بر اساس فرمولهای دقت و فراخوانی مطابقت دارد یا خیر. برای امتیاز بیشتر، متن را به دو قسمت تقسیم کنید و دقت و فراخوانی را برای معیار <code>rouge2</code> مقایسه کنید.</p></blockquote> <p>ما از این نمرات ROUGE برای ردیابی عملکرد مدل خود استفاده خواهیم کرد، اما قبل از انجام این کار، بیایید کاری را انجام دهیم که هر متخصص خوب NLP باید انجام دهد: ایجاد یک خط پایه قوی و در عین حال ساده!</p> <!--[2--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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>یک خط پایهی رایج برای خلاصهسازی متن، گرفتن سه جملهی نخست مقاله است که معمولاً خط پایهی <em>lead-3</em> نامیده میشود. میتوانیم با نقطه پایان جملهها را پیدا کنیم، اما این روش برای سرواژههایی مانند «U.S.» یا «U.N.» شکست میخورد؛ بنابراین از کتابخانهی <code>nltk</code> استفاده میکنیم که الگوریتم بهتری برای این موارد دارد. بسته را با <code>pip</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->!pip install nltk<!----></pre></div><!----> <p>و سپس قواعد نشانهگذاری را دانلود میکنیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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">import</span> nltk | |
| nltk.download(<span class="hljs-string">"punkt"</span>)<!----></pre></div><!----> <p>در مرحله بعد، توکنساز جمله را از <code>nltk</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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> 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'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>به نظر میرسد که این روش جواب میدهد، بنابراین بیایید تابعی پیادهسازی کنیم که این «خلاصهها» را از یک دیتاسِت استخراج کرده و امتیازات ROUGE را برای خط مبنا محاسبه کند:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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">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>سپس با این تابع امتیازهای ROUGE را روی مجموعهی اعتبارسنجی محاسبه و با 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'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>میتوانیم ببینیم که امتیاز <code>rouge2</code> به طور قابل توجهی کمتر از بقیه است؛ این احتمالاً نشان دهنده این واقعیت است که عناوین نقد و بررسی معمولاً مختصر هستند و بنابراین خط پایه لید ۳ بیش از حد طولانی است. حالا که یک خط پایه خوب برای کار داریم، بیایید توجه خود را به کوک کردن mT5 معطوف کنیم!</p> <!--[0--><!--[1--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>کوک کردن mT5 با رابط برنامهنویسی کاربردی Trainer</span></h2><!--]--><!----> <p>کوک کردن مدل برای خلاصهسازی بسیار شبیه وظایف دیگر این فصل است. ابتدا مدل از پیش تعلیمدیده را از نقطهی بررسی <code>mt5-small</code> بارگذاری میکنیم. چون خلاصهسازی وظیفهای دنبالهبهدنباله است، میتوانیم مدل را با کلاس <code>AutoModelForSeq2SeqLM</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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> transformers <span class="hljs-keyword">import</span> AutoModelForSeq2SeqLM | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)<!----></pre></div><!----><!--]--> <blockquote><p>نکته | |
| 💡 اگر از خود میپرسید که چرا هیچ هشداری در مورد کوک کردن مدل در یک وظیفه پاییندستی نمیبینید، به این دلیل است که برای وظایف توالی به توالی، ما تمام وزنهای شبکه را نگه میداریم. این را با مدل طبقهبندی متن ما در <a href="/course/chapter3">فصل 3</a> مقایسه کنید، که در آن سر مدل از پیش تعلیمدیده با یک شبکه با مقداردهی اولیه تصادفی جایگزین شده است.</p></blockquote> <p>کار بعدی که باید انجام دهیم این است که به هاگینگفِیس هاب وارد شویم. اگر این کد را در یک نوتبوک اجرا میکنید، میتوانید این کار را با تابع کاربردی زیر انجام دهید:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> huggingface_hub <span class="hljs-keyword">import</span> notebook_login | |
| notebook_login()<!----></pre></div><!----> <p>که یک ویجت نمایش میدهد که میتوانید اطلاعات کاربری خود را در آن وارد کنید. همچنین میتوانید این دستور را در ترمینال خود اجرا کنید و از آنجا وارد سیستم شوید:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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=" "><!---->huggingface-<span class="hljs-keyword">cli</span> login<!----></pre></div><!----> <!--[0--><p>برای محاسبهی امتیازهای ROUGE هنگام تعلیم باید خلاصه تولید کنیم. خوشبختانه 🤗 Transformers کلاسهای اختصاصی <code>Seq2SeqTrainingArguments</code> و <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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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> 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>در اینجا، آرگومان <code>predict_with_generate</code> طوری تنظیم شده است که نشان دهد ما باید در طول ارزیابی خلاصههایی تولید کنیم تا بتوانیم نمرات ROUGE را برای هر دوره محاسبه کنیم. همانطور که در <a href="/course/chapter1">فصل 1</a> بحث شد، رمزگشا با پیشبینی توکنها به صورت یک به یک، استنتاج را انجام میدهد و این توسط روش <code>generate()</code> مدل پیادهسازی میشود. تنظیم <code>predict_with_generate=True</code> به <code>Seq2SeqTrainer</code> میگوید که از آن روش برای ارزیابی استفاده کند. همچنین ابرپارامترهای پیشفرضی مانند نرخ یادگیری، تعداد دورهها و کاهش وزن را تنظیم کردهایم و گزینهی <code>save_total_limit</code> را طوری تنظیم کردهایم که حداکثر ۳ نقطهی بررسی را در طول تعلیم ذخیره کند؛ زیرا حتی نسخهی «کوچک» mT5 حدود یک گیگابایت از فضای دیسک را مصرف میکند و با محدود کردن تعداد نسخههای ذخیرهشده، کمی فضا آزاد میکنیم.</p> <p>آرگومان <code>push_to_hub=True</code> به ما این امکان را میدهد که مدل را پس از آموزش به هاب ارسال کنیم؛ مخزن را در زیر پروفایل کاربری خود، در مکانی که توسط <code>output_dir</code> تعریف شده است، خواهید یافت. توجه داشته باشید که میتوانید نام مخزنی را که میخواهید به آن ارسال کنید، با آرگومان <code>hub_model_id</code> مشخص کنید (به طور خاص، برای ارسال به یک سازمان باید از این آرگومان استفاده کنید). به عنوان مثال، وقتی مدل را به سازمان <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code></a> ارسال کردیم، ما <code>hub_model_id="huggingface-course/mt5-finetuned-amazon-en-es"</code> به <code>Seq2SeqTrainingArguments</code> اضافه شد.</p> <p>کار بعدی که باید انجام دهیم این است که یک تابع <code>compute_metrics()</code> به مربی ارائه دهیم تا بتوانیم مدل خود را در طول آموزش ارزیابی کنیم. به طور خلاصه، این کار کمی پیچیدهتر از فراخوانی ساده <code>rouge_score.compute()</code> روی پیشبینیهای مدل است، زیرا قبل از محاسبه امتیازات ROUGE، باید خروجیها و برچسبها را به متن <em>رمزگشایی</em> کنیم. تابع زیر دقیقاً همین کار را انجام میدهد و همچنین از تابع <code>sent_tokenize()</code> از <code>nltk</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">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>در مرحله بعد، باید یک تطبیقدهنده داده برای وظیفه توالی به توالی خود تعریف کنیم. از آنجایی که mT5 یک مدل تبدیلکننده رمزگذار-رمزگشا است، یک نکته ظریف در آمادهسازی دستههای ما این است که در طول رمزگشایی باید برچسبها را یکی یکی به سمت راست جابجا کنیم. این کار برای اطمینان از این لازم است که رمزگشا فقط برچسبهای حقیقت پایه قبلی را میبیند و نه برچسبهای فعلی یا آینده را، که به خاطر سپردن آن برای مدل آسان خواهد بود. این شبیه نحوهی اعمال توجهِ خودیِ ماسکشده بر ورودیها در مسئلهای مانند <a href="/course/chapter7/6">مدلسازی زبان علّی</a> است.</p> <p>خوشبختانه، 🤗 ترنسفورمرز یک کامپایلر <code>DataCollatorForSeq2Seq</code> ارائه میدهد که به صورت پویا ورودیها و برچسبها را برای ما لایهگذاری میکند. برای نمونهسازی این کامپایلر، کافی است <code>tokenizer</code> و <code>model</code> را ارائه دهیم:</p> <!--[0--><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> transformers <span class="hljs-keyword">import</span> DataCollatorForSeq2Seq | |
| data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)<!----></pre></div><!--]--> <p>بیایید ببینیم این جمعکننده وقتی به یک دسته کوچک از مثالها داده میشود چه چیزی تولید میکند. ابتدا، باید ستونهای دارای رشته را حذف کنیم زیرا جمعکننده نمیداند چگونه این عناصر را لایهگذاری کند:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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 "><!---->tokenized_datasets = tokenized_datasets.remove_columns( | |
| books_dataset[<span class="hljs-string">"train"</span>].column_names | |
| )<!----></pre></div><!----> <p>چون جمعکننده فهرستی از <code>dict</code>ها میخواهد و هر <code>dict</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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>: 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>نکتهی اصلی این است که نمونهی اول از دوم طولانیتر است؛ بنابراین <code>input_ids</code> و <code>attention_mask</code> نمونهی دوم در سمت راست با توکِن <code>[PAD]</code> (با شناسهی <code>0</code>) همطول شدهاند. همچنین <code>labels</code> با <code>-100</code>ها پر شدهاند تا توکِنهای padding در تابع هزینه نادیده گرفته شوند. در پایان <code>decoder_input_ids</code> تازهای میبینیم که با قرار دادن توکِن <code>[PAD]</code> در ابتدای ورودی، برچسبها را به راست منتقل کرده است.</p> <!--[0--><p>بالاخره تمام مواد لازم برای آموزش را داریم! حالا فقط باید مربی را با آرگومانهای استاندارد نمونهسازی کنیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> 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>و دوره آموزشی خود را آغاز کنیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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 "><!---->trainer.train()<!----></pre></div><!----> <p>در طول آموزش، باید شاهد کاهش تلفات آموزش و افزایش امتیازات ROUGE با هر دوره باشید. پس از اتمام آموزش، میتوانید امتیازات نهایی ROUGE را با اجرای <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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'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>از روی امتیازات میتوانیم ببینیم که مدل ما به راحتی از خط پایهی سوم خود بهتر عمل کرده است — عالی! آخرین کاری که باید انجام دهیم این است که وزنهای مدل را به صورت زیر به هاب منتقل کنیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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=" "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'https://huggingface.co/huggingface-course/mt5-finetuned-amazon-en-es/commit/aa0536b829b28e73e1e4b94b8a5aacec420d40e0'</span><!----></pre></div><!----> <p>این کار، فایلهای Checkpoint و پیکربندی را قبل از آپلود همه فایلها در هاب، در <code>output_dir</code> ذخیره میکند. با مشخص کردن آرگومان <code>tags</code>، همچنین اطمینان حاصل میکنیم که ویجت روی هاب، به جای خط تولید متن پیشفرض مرتبط با معماری mT5، برای خط تولید خلاصهسازی خواهد بود (برای اطلاعات بیشتر در مورد تگهای مدل، به <a href="https://huggingface.co/docs/hub/main#how-is-a-models-type-of-inference-api-and-widget-determined" rel="nofollow">🤗 مستندات هاب</a> مراجعه کنید). خروجی <code>trainer.push_to_hub()</code> یک URL به … است. هش کامیت گیت، بنابراین میتوانید به راحتی تغییراتی که در مخزن مدل ایجاد شده است را مشاهده کنید!</p> <p>برای پایان این بخش، ببینیم چگونه میتوانیم mT5 را با قابلیتهای سطح پایین 🤗 Accelerate نیز کوک کنیم.</p><!--]--> <!--[0--><!--[1--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>کوک کردن mT5 با 🤗 شتابدهی</span></h2><!--]--><!----> <p>کوک کردن مدل با 🤗 Accelerate بسیار شبیه به مثال طبقهبندی متن است که در <a href="/course/chapter3">فصل ۳</a> با آن مواجه شدیم. تفاوتهای اصلی، نیاز به تولید صریح خلاصههای ما در طول آموزش و تعریف نحوه محاسبه امتیازات ROUGE خواهد بود (به یاد داشته باشید که <code>Seq2SeqTrainer</code> تولید را برای ما انجام داد). بیایید نگاهی به نحوه پیادهسازی این دو الزام در 🤗 Accelerate بیندازیم!</p> <!--[2--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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>DataLoader</code> ایجاد کنیم. از آنجایی که بارگذارهای داده PyTorch انتظار دستههایی از تانسورها را دارند، باید قالب را در دیتاسِتهای خود روی <code>"torch"</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->tokenized_datasets.set_format(<span class="hljs-string">"torch"</span>)<!----></pre></div><!----> <p>حالا که دیتاسِتهایی داریم که فقط از تانسورها تشکیل شدهاند، کار بعدی که باید انجام دهیم، نمونهسازی مجدد <code>DataCollatorForSeq2Seq</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)<!----></pre></div><!----> <p>سپس میتوانیم از گردآورندهی دادهها نمونهسازی کنیم و از آن برای تعریف بارگذارهای دادهی خود استفاده کنیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> 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>کار بعدی که باید انجام دهیم، تعریف بهینهساز مورد نظرمان است. همانند مثالهای دیگرمان، از <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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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> torch.optim <span class="hljs-keyword">import</span> AdamW | |
| optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">2e-5</span>)<!----></pre></div><!----> <p>در نهایت، مدل، بهینهساز و بارگذارهای داده خود را به متد <code>accelerator.prepare()</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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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> Accelerator | |
| accelerator = Accelerator() | |
| model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( | |
| model, optimizer, train_dataloader, eval_dataloader | |
| )<!----></pre></div><!----> <blockquote><p>نکته | |
| 🚨 اگر روی یک TPU آموزش میبینید، باید تمام کد بالا را به یک تابع آموزش اختصاصی منتقل کنید. برای جزئیات بیشتر به <a href="/course/chapter3">فصل ۳</a> مراجعه کنید.</p></blockquote> <p>حالا که اشیاء خود را آماده کردهایم، سه کار دیگر باقی مانده است:</p> <ul><li>جدول نرخ یادگیری را تعریف کنید.</li> <li>تابعی را برای پسپردازش خلاصهها جهت ارزیابی پیادهسازی کنید.</li> <li>مخزنی در هاب ایجاد کنید تا بتوانیم مدل خود را در آن بارگذاری کنیم.</li></ul> <p>برای زمانبندی نرخ یادگیری، از زمانبندی خطی استاندارد بخشهای قبلی استفاده خواهیم کرد:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> 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>برای پسپردازش، به تابعی نیاز داریم که خلاصههای تولید شده را به جملاتی تقسیم کند که با خط جدید از هم جدا شدهاند. این فرمتی است که معیار ROUGE انتظار دارد و میتوانیم با قطعه کد زیر به آن دست یابیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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">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>اگر به یاد داشته باشید که چگونه تابع <code>compute_metrics()</code> مربوط به <code>Seq2SeqTrainer</code> را تعریف کردیم، این باید برای شما آشنا به نظر برسد.</p> <p>در نهایت، باید یک مخزن مدل در هاگینگفِیس هاب ایجاد کنیم. برای این کار، میتوانیم از کتابخانه هاب با عنوان مناسب 🤗 استفاده کنیم. فقط باید یک نام برای مخزن خود تعریف کنیم و کتابخانه یک تابع کاربردی برای ترکیب شناسه مخزن با پروفایل کاربر دارد:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'lewtun/mt5-finetuned-amazon-en-es-accelerate'</span><!----></pre></div><!----> <p>اکنون میتوانیم از این نام مخزن برای کلون کردن یک نسخه محلی در دایرکتوری نتایج خود استفاده کنیم که مصنوعات آموزشی را ذخیره میکند:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> 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>این به ما امکان میدهد تا با فراخوانی متد <code>repo.push_to_hub()</code> در طول آموزش، مصنوعات را به هاب برگردانیم! حال بیایید تحلیل خود را با نوشتن حلقه آموزش به پایان برسانیم.</p> <!--[2--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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>حلقه آموزشی برای خلاصهسازی کاملاً شبیه به سایر مثالهای Accelerate 🤗 است که با آنها مواجه شدهایم و تقریباً به چهار مرحله اصلی تقسیم میشود:</p> <p>۱. مدل را با تکرار روی تمام مثالهای موجود در <code>train_dataloader</code> برای هر دوره، آموزش دهید. | |
| ۲. در پایان هر دوره خلاصههای مدل را تولید کنید؛ ابتدا توکنها و سپس آنها (و خلاصههای مرجع) را به متن تبدیل کنید. | |
| ۳. امتیازهای ROUGE را با همان روشهایی که پیشتر دیدیم محاسبه کنید. | |
| ۴. چکپوینتها را ذخیره کنید و همه چیز را به هاب ارسال کنید. در اینجا ما به آرگومان <code>blocking=False</code> از شیء <code>Repository</code> تکیه میکنیم تا بتوانیم چکپوینتها را در هر دوره <em>به صورت ناهمگام</em> ارسال کنیم. این به ما امکان میدهد بدون نیاز به انتظار برای آپلود نسبتاً کند مربوط به یک مدل با اندازه GB، به آموزش ادامه دهیم!</p> <p>این مراحل را میتوانید در بلوک کد زیر مشاهده کنید:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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> 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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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 "><!---->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>و تمام! وقتی این را اجرا کنید، مدل و نتایجی خواهید داشت که بسیار شبیه به نتایجی است که با <code>Trainer</code> به دست آوردیم.</p><!--]--> <!--[1--><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 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 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>وقتی مدل را به هاب ارسال کردید، میتوانید از طریق ویجت استنتاج یا با یک شیء <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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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> 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>میتوانیم چند مثال از مجموعه تست (که مدل ندیده است) را به خط تولید خود وارد کنیم تا کیفیت خلاصهها را ارزیابی کنیم. ابتدا بیایید یک تابع ساده برای نمایش نقد، عنوان و خلاصه تولید شده با هم پیادهسازی کنیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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">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"'>>> Review: <span class="hljs-subst">{review}</span>'"</span>) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"\n'>>> Title: <span class="hljs-subst">{title}</span>'"</span>) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"\n'>>> Summary: <span class="hljs-subst">{summary}</span>'"</span>)<!----></pre></div><!----> <p>بیایید نگاهی به یکی از مثالهای انگلیسی که به دست میآوریم بیندازیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'>>> 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">'>>> Title: Not impressed at all... buy something else'</span> | |
| <span class="hljs-string">'>>> Summary: Nothing special at all about this product'</span><!----></pre></div><!----> <p>بد نیست! میبینیم مدل ما توانسته با افزودن کلمات تازه به بخشهایی از مرور، خلاصهسازی <em>انتزاعی</em> انجام دهد. شاید جالبترین ویژگی مدل دوزبانه بودن آن باشد؛ بنابراین میتوانیم مرورهای اسپانیایی را هم خلاصه کنیم:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-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 "><!---->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 xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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">'>>> 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">'>>> Title: Buena literatura para adolescentes'</span> | |
| <span class="hljs-string">'>>> Summary: Muy facil de leer'</span><!----></pre></div><!----> <p>این خلاصه در انگلیسی به «Very easy to read» ترجمه میشود و میبینیم که در این مورد مستقیماً از مرور استخراج شده است. بااینحال، این مثال انعطافپذیری مدل mT5 و تجربهی کار با پیکرهای چندزبانه را نشان میدهد!</p> <p>در ادامه، توجه خود را به یک کار کمی پیچیدهتر معطوف خواهیم کرد: آموزش یک مدل زبانی از ابتدا.</p></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/chapter7/5.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, 82], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 218 kB
- Xet hash:
- 3c9b0e0e615f55fcf073e6dffe466823519e21da7f7c9ade013e1e29d622c621
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.