Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Causal Language Model တစ်ခုကို အစကနေ လေ့ကျင့်ခြင်း","local":"training-a-causal-language-model-from-scratch","sections":[{"title":"Data များကို စုဆောင်းခြင်း","local":"gathering-the-data","sections":[],"depth":2},{"title":"Dataset ကို ပြင်ဆင်ခြင်း","local":"preparing-the-dataset","sections":[],"depth":2},{"title":"Model အသစ်တစ်ခုကို စတင်ခြင်း","local":"initializing-a-new-model","sections":[],"depth":2},{"title":"Pipeline ဖြင့် Code Generation","local":"code-generation-with-a-pipeline","sections":[],"depth":2},{"title":"🤗 Accelerate ဖြင့် Training","local":"training-with-accelerate","sections":[],"depth":2},{"title":"ဝေါဟာရ ရှင်းလင်းချက် (Glossary)","local":"ဝဟရ-ရငလငခက-glossary","sections":[],"depth":2}],"depth":1}"> | |
| <link href="/docs/course/pr_1095/my/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/entry/start.8e25cab6.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/scheduler.893fe8c9.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/singletons.ba455c5c.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/index.bce52c8a.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/paths.9a7be869.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/entry/app.b12ce275.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/preload-helper.b5ee8f74.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/index.b1df2166.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/nodes/0.77c840e7.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/nodes/82.5a1bcb0e.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.e6d31e72.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/Youtube.ec5d7916.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/CodeBlock.abb4f40e.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/CourseFloatingBanner.c1c08878.js"> | |
| <link rel="modulepreload" href="/docs/course/pr_1095/my/_app/immutable/chunks/FrameworkSwitchCourse.4480e339.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Causal Language Model တစ်ခုကို အစကနေ လေ့ကျင့်ခြင်း","local":"training-a-causal-language-model-from-scratch","sections":[{"title":"Data များကို စုဆောင်းခြင်း","local":"gathering-the-data","sections":[],"depth":2},{"title":"Dataset ကို ပြင်ဆင်ခြင်း","local":"preparing-the-dataset","sections":[],"depth":2},{"title":"Model အသစ်တစ်ခုကို စတင်ခြင်း","local":"initializing-a-new-model","sections":[],"depth":2},{"title":"Pipeline ဖြင့် Code Generation","local":"code-generation-with-a-pipeline","sections":[],"depth":2},{"title":"🤗 Accelerate ဖြင့် Training","local":"training-with-accelerate","sections":[],"depth":2},{"title":"ဝေါဟာရ ရှင်းလင်းချက် (Glossary)","local":"ဝဟရ-ရငလငခက-glossary","sections":[],"depth":2}],"depth":1}"><!-- HEAD_svelte-u9bgzb_END --> <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 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 max-sm:gap-0.5 h-6 max-sm:h-5 px-2 max-sm:px-1.5 text-[11px] max-sm:text-[9px] 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"><svg class="w-3 h-3 max-sm:w-2.5 max-sm:h-2.5" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></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-6 max-sm:h-5 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 w-3 h-3 max-sm:w-2.5 max-sm:h-2.5 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> </div> <h1 class="relative group"><a id="training-a-causal-language-model-from-scratch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-a-causal-language-model-from-scratch"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Causal Language Model တစ်ခုကို အစကနေ လေ့ကျင့်ခြင်း</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0" style=""><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section6_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section6_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p data-svelte-h="svelte-1rzz6ao">အခုအထိတော့ ကျွန်တော်တို့ဟာ pretrained models တွေကို အများအားဖြင့် အသုံးပြုခဲ့ကြပြီး pretraining ကနေရရှိတဲ့ weights တွေကို ပြန်လည်အသုံးပြုခြင်းဖြင့် use cases အသစ်တွေအတွက် fine-tuning လုပ်ခဲ့ကြပါတယ်။ <a href="/course/chapter1">Chapter 1</a> မှာ ကျွန်တော်တို့ တွေ့ခဲ့ရတဲ့အတိုင်း၊ ဒါကို <em>transfer learning</em> လို့ အများအားဖြင့် ရည်ညွှန်းကြပြီး၊ labeled data ရှားပါးတဲ့ လက်တွေ့ကမ္ဘာသုံး use cases အများစုမှာ Transformer models တွေကို အသုံးချဖို့အတွက် အလွန်အောင်မြင်တဲ့ နည်းဗျူဟာတစ်ခု ဖြစ်ပါတယ်။ ဒီအခန်းမှာ၊ ကျွန်တော်တို့ မတူညီတဲ့ နည်းလမ်းတစ်ခုကို အသုံးပြုပြီး model အသစ်တစ်ခုကို အစကနေ train လုပ်ပါမယ်။ ဒါက သင့်မှာ data အများကြီးရှိပြီး လက်ရှိ models တွေအတွက် အသုံးပြုခဲ့တဲ့ pretraining data တွေနဲ့ အလွန်ကွာခြားတယ်ဆိုရင် ကောင်းမွန်တဲ့ နည်းလမ်းတစ်ခုပါပဲ။ သို့သော်လည်း၊ လက်ရှိ model တစ်ခုကို fine-tune လုပ်တာထက် language model တစ်ခုကို pretrain လုပ်ဖို့အတွက် compute resources တွေ သိသိသာသာ ပိုလိုအပ်ပါတယ်။ model အသစ်တစ်ခုကို train လုပ်တာ အဓိပ္ပာယ်ရှိနိုင်တဲ့ ဥပမာတွေကတော့ musical notes, DNA လို molecular sequences ဒါမှမဟုတ် programming languages တွေ ပါဝင်တဲ့ datasets တွေ ဖြစ်ပါတယ်။ နောက်ဆုံးပြောခဲ့တဲ့အရာတွေက TabNine နဲ့ OpenAI ရဲ့ Codex model က ပံ့ပိုးပေးထားတဲ့ GitHub ရဲ့ Copilot လို tools တွေကြောင့် မကြာသေးခင်က အရှိန်အဟုန် ရရှိလာခဲ့ပါတယ်။ ဒါတွေက code ရဲ့ ရှည်လျားတဲ့ sequences တွေကို generate လုပ်နိုင်ပါတယ်။ text generation ရဲ့ ဒီ task ကို auto-regressive သို့မဟုတ် GPT-2 လို causal language models တွေနဲ့ အကောင်းဆုံး ကိုင်တွယ်ဖြေရှင်းနိုင်ပါတယ်။</p> <p data-svelte-h="svelte-89x9co">ဒီအပိုင်းမှာ ကျွန်တော်တို့ code generation model ရဲ့ scaled-down version တစ်ခုကို တည်ဆောက်ပါမယ်၊ Python code ရဲ့ subset တစ်ခုကို အသုံးပြုပြီး full functions ဒါမှမဟုတ် classes တွေအစား one-line completions တွေအပေါ် အာရုံစိုက်ပါမယ်။ Python မှာ data နဲ့ အလုပ်လုပ်တဲ့အခါ သင်ဟာ <code>matplotlib</code>, <code>seaborn</code>, <code>pandas</code>, နဲ့ <code>scikit-learn</code> libraries တွေ ပါဝင်တဲ့ Python data science stack နဲ့ မကြာခဏ ထိတွေ့နေရပါလိမ့်မယ်။ အဲဒီ frameworks တွေကို အသုံးပြုတဲ့အခါ သီးခြား commands တွေကို ရှာဖွေဖို့ လိုအပ်တာက ပုံမှန်ပါပဲ၊ ဒါကြောင့် ကျွန်တော်တို့အတွက် ဒီ calls တွေကို ဖြည့်စွက်ပေးနိုင်တဲ့ model တစ်ခုကို အသုံးပြုနိုင်ရင် ကောင်းပါလိမ့်မယ်။</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Vpjb1lu0MDk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> <p data-svelte-h="svelte-146v12"><a href="/course/chapter6">Chapter 6</a> မှာ Python source code ကို process လုပ်ဖို့အတွက် ထိရောက်တဲ့ tokenizer တစ်ခုကို ကျွန်တော်တို့ ဖန်တီးခဲ့ပါတယ်၊ ဒါပေမယ့် ကျွန်တော်တို့ လိုအပ်နေသေးတာက model တစ်ခုကို pretrain လုပ်ဖို့အတွက် large-scale dataset တစ်ခုပါပဲ။ ဒီနေရာမှာ၊ ကျွန်တော်တို့ရဲ့ tokenizer ကို GitHub repositories ကနေ ရရှိတဲ့ Python code corpus တစ်ခုပေါ်မှာ အသုံးချပါမယ်။ ပြီးရင် model ကို train လုပ်ဖို့အတွက် <code>Trainer</code> API နဲ့ 🤗 Accelerate ကို အသုံးပြုပါမယ်။ စလိုက်ရအောင်!</p> <iframe src="https://course-demos-codeparrot-ds.hf.space" frameborder="0" height="300" title="Gradio app" class="block dark:hidden container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p data-svelte-h="svelte-9v9hrf">ဒါက ဒီအပိုင်းမှာ ပြသထားတဲ့ code ကို အသုံးပြုပြီး Hub ကို train လုပ်ပြီး upload လုပ်ထားတဲ့ model ကို လက်တွေ့ပြသနေတာ ဖြစ်ပါတယ်။ သင် <a href="https://huggingface.co/huggingface-course/codeparrot-ds?text=plt.imshow%28" rel="nofollow">ဒီနေရာမှာ</a> ရှာတွေ့နိုင်ပါတယ်။ text generation မှာ randomization အချို့ဖြစ်ပေါ်နေတာကြောင့်၊ သင်အနည်းငယ်ကွဲပြားတဲ့ ရလဒ်ကို ရရှိနိုင်မယ်ဆိုတာ မှတ်သားပါ။</p> <h2 class="relative group"><a id="gathering-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#gathering-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Data များကို စုဆောင်းခြင်း</span></h2> <p data-svelte-h="svelte-1uccoas">Python code ကို GitHub လို code repositories တွေကနေ အလွယ်တကူ ရရှိနိုင်ပြီး၊ Python repository တိုင်းကို scraping လုပ်ခြင်းဖြင့် dataset တစ်ခု ဖန်တီးဖို့ ကျွန်တော်တို့ အသုံးပြုနိုင်ပါတယ်။ ဒါက <a href="https://learning.oreilly.com/library/view/natural-language-processing/9781098136789/" rel="nofollow">Transformers textbook</a> မှာ large GPT-2 model တစ်ခုကို pretrain လုပ်ဖို့ အသုံးပြုခဲ့တဲ့ နည်းလမ်းပါပဲ။ <code>codeparrot</code> လို့ခေါ်တဲ့ Python files သန်း ၂၀ ခန့်ပါဝင်တဲ့ 180 GB ခန့်ရှိတဲ့ GitHub dump ကို အသုံးပြုပြီး၊ စာရေးဆရာတွေက dataset တစ်ခုကို တည်ဆောက်ခဲ့ပြီး အဲဒါကို <a href="https://huggingface.co/datasets/transformersbook/codeparrot" rel="nofollow">Hugging Face Hub</a> မှာ မျှဝေခဲ့ပါတယ်။</p> <p data-svelte-h="svelte-14884zb">သို့သော်လည်း၊ full corpus ပေါ်မှာ training လုပ်တာက အချိန်နဲ့ compute-consuming ဖြစ်ပြီး၊ ကျွန်တော်တို့က Python data science stack နဲ့ ပတ်သက်တဲ့ dataset ရဲ့ subset ကိုပဲ လိုအပ်ပါတယ်။ ဒါကြောင့်၊ <code>codeparrot</code> dataset ကို ဒီ stack ထဲက libraries တွေ ပါဝင်တဲ့ files အားလုံးအတွက် filtering လုပ်ခြင်းဖြင့် စတင်ပါမယ်။ dataset ရဲ့ အရွယ်အစားကြောင့်၊ ကျွန်တော်တို့ ဒါကို download လုပ်တာကို ရှောင်ရှားချင်ပါတယ်၊ အစား streaming feature ကို အသုံးပြုပြီး on the fly မှာ filter လုပ်ပါမယ်။ အစောပိုင်းမှာ ကျွန်တော်တို့ ပြောခဲ့တဲ့ libraries တွေကို အသုံးပြုပြီး code samples တွေကို filter လုပ်ရာမှာ အကူအညီဖြစ်စေဖို့၊ အောက်ပါ function ကို အသုံးပြုပါမယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">any_keyword_in_string</span>(<span class="hljs-params">string, keywords</span>): | |
| <span class="hljs-keyword">for</span> keyword <span class="hljs-keyword">in</span> keywords: | |
| <span class="hljs-keyword">if</span> keyword <span class="hljs-keyword">in</span> string: | |
| <span class="hljs-keyword">return</span> <span class="hljs-literal">True</span> | |
| <span class="hljs-keyword">return</span> <span class="hljs-literal">False</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-gpf97p">ဥပမာနှစ်ခုနဲ့ စမ်းသပ်ကြည့်ရအောင်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START -->filters = [<span class="hljs-string">"pandas"</span>, <span class="hljs-string">"sklearn"</span>, <span class="hljs-string">"matplotlib"</span>, <span class="hljs-string">"seaborn"</span>] | |
| example_1 = <span class="hljs-string">"import numpy as np"</span> | |
| example_2 = <span class="hljs-string">"import pandas as pd"</span> | |
| <span class="hljs-built_in">print</span>( | |
| any_keyword_in_string(example_1, filters), any_keyword_in_string(example_2, filters) | |
| )<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-literal">False</span> <span class="hljs-literal">True</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-onhyal">ဒါကို အသုံးပြုပြီး dataset ကို stream လုပ်ပြီး ကျွန်တော်တို့ လိုချင်တဲ့ elements တွေကို filter လုပ်မယ့် function တစ်ခု ဖန်တီးနိုင်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> defaultdict | |
| <span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm | |
| <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">filter_streaming_dataset</span>(<span class="hljs-params">dataset, filters</span>): | |
| filtered_dict = defaultdict(<span class="hljs-built_in">list</span>) | |
| total = <span class="hljs-number">0</span> | |
| <span class="hljs-keyword">for</span> sample <span class="hljs-keyword">in</span> tqdm(<span class="hljs-built_in">iter</span>(dataset)): | |
| total += <span class="hljs-number">1</span> | |
| <span class="hljs-keyword">if</span> any_keyword_in_string(sample[<span class="hljs-string">"content"</span>], filters): | |
| <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> sample.items(): | |
| filtered_dict[k].append(v) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{<span class="hljs-built_in">len</span>(filtered_dict[<span class="hljs-string">'content'</span>])/total:<span class="hljs-number">.2</span>%}</span> of data after filtering."</span>) | |
| <span class="hljs-keyword">return</span> Dataset.from_dict(filtered_dict)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1ep01nl">ပြီးရင် ဒီ function ကို streaming dataset ကို ရိုးရှင်းစွာ အသုံးချနိုင်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># This cell will take a very long time to execute, so you should skip it and go to</span> | |
| <span class="hljs-comment"># the next one!</span> | |
| <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset | |
| split = <span class="hljs-string">"train"</span> <span class="hljs-comment"># "valid"</span> | |
| filters = [<span class="hljs-string">"pandas"</span>, <span class="hljs-string">"sklearn"</span>, <span class="hljs-string">"matplotlib"</span>, <span class="hljs-string">"seaborn"</span>] | |
| data = load_dataset(<span class="hljs-string">f"transformersbook/codeparrot-<span class="hljs-subst">{split}</span>"</span>, split=split, streaming=<span class="hljs-literal">True</span>) | |
| filtered_data = filter_streaming_dataset(data, filters)<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-number">3.26</span>% of data after filtering.<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1jhtlcd">ဒါက ကျွန်တော်တို့ကို မူရင်း dataset ရဲ့ ၃% ခန့် ကျန်ရှိစေပါတယ်၊ ဒါကလည်း အတော်လေး ကြီးမားနေဆဲပါပဲ — ရရှိလာတဲ့ dataset က 6 GB ရှိပြီး Python scripts ၆၀၀,၀၀၀ ပါဝင်ပါတယ်။</p> <p data-svelte-h="svelte-14412sm">full dataset ကို filtering လုပ်တာက သင့် machine နဲ့ bandwidth ပေါ်မူတည်ပြီး ၂-၃ နာရီ ကြာနိုင်ပါတယ်။ ဒီလို အချိန်ကုန်တဲ့ လုပ်ငန်းစဉ်ကို သင်ကိုယ်တိုင် မလုပ်ချင်ဘူးဆိုရင်၊ ကျွန်တော်တို့က Hub ပေါ်မှာ filtered dataset ကို download လုပ်ဖို့ ပံ့ပိုးပေးထားပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset, DatasetDict | |
| ds_train = load_dataset(<span class="hljs-string">"huggingface-course/codeparrot-ds-train"</span>, split=<span class="hljs-string">"train"</span>) | |
| ds_valid = load_dataset(<span class="hljs-string">"huggingface-course/codeparrot-ds-valid"</span>, split=<span class="hljs-string">"validation"</span>) | |
| raw_datasets = DatasetDict( | |
| { | |
| <span class="hljs-string">"train"</span>: ds_train, <span class="hljs-comment"># .shuffle().select(range(50000)),</span> | |
| <span class="hljs-string">"valid"</span>: ds_valid, <span class="hljs-comment"># .shuffle().select(range(500))</span> | |
| } | |
| ) | |
| raw_datasets<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->DatasetDict({ | |
| train: Dataset({ | |
| features: [<span class="hljs-string">'repo_name'</span>, <span class="hljs-string">'path'</span>, <span class="hljs-string">'copies'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">'content'</span>, <span class="hljs-string">'license'</span>], | |
| num_rows: <span class="hljs-number">606720</span> | |
| }) | |
| valid: Dataset({ | |
| features: [<span class="hljs-string">'repo_name'</span>, <span class="hljs-string">'path'</span>, <span class="hljs-string">'copies'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">'content'</span>, <span class="hljs-string">'license'</span>], | |
| num_rows: <span class="hljs-number">3322</span> | |
| }) | |
| })<!-- HTML_TAG_END --></pre></div> <blockquote class="tip" data-svelte-h="svelte-438a8o"><p>Pretraining the language model will take a while. We suggest that you first run the training loop on a sample of the data by uncommenting the two partial lines above, and make sure that the training successfully completes and the models are stored. Nothing is more frustrating than a training run failing at the last step because you forgot to create a folder or because there’s a typo at the end of the training loop!</p></blockquote> <p data-svelte-h="svelte-6ebiw6">dataset ကနေ ဥပမာတစ်ခုကို ကြည့်ရအောင်။ field တစ်ခုစီရဲ့ ပထမဆုံး characters ၂၀၀ ကိုပဲ ပြသပါမယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START --><span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>]: | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{key.upper()}</span>: <span class="hljs-subst">{raw_datasets[<span class="hljs-string">'train'</span>][<span class="hljs-number">0</span>][key][:<span class="hljs-number">200</span>]}</span>"</span>)<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-string">'REPO_NAME: kmike/scikit-learn'</span> | |
| <span class="hljs-string">'PATH: sklearn/utils/__init__.py'</span> | |
| <span class="hljs-string">'COPIES: 3'</span> | |
| <span class="hljs-string">'SIZE: 10094'</span> | |
| <span class="hljs-string">'''CONTENT: """ | |
| The :mod:`sklearn.utils` module includes various utilites. | |
| """ | |
| from collections import Sequence | |
| import numpy as np | |
| from scipy.sparse import issparse | |
| import warnings | |
| from .murmurhash import murm | |
| LICENSE: bsd-3-clause'''</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-p1pnbq"><code>content</code> field က ကျွန်တော်တို့ model train လုပ်ချင်တဲ့ code ကို ပါဝင်နေတာ တွေ့ရပါတယ်။ အခု dataset ရှိပြီဆိုတော့၊ pretraining အတွက် သင့်လျော်တဲ့ format ဖြစ်အောင် texts တွေကို ပြင်ဆင်ဖို့ လိုပါတယ်။</p> <h2 class="relative group"><a id="preparing-the-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-the-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Dataset ကို ပြင်ဆင်ခြင်း</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/ma1TrR7gE7I" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> <p data-svelte-h="svelte-1awil02">ပထမအဆင့်က data ကို tokenize လုပ်မှာ ဖြစ်ပါတယ်၊ ဒါမှ training အတွက် အသုံးပြုနိုင်မှာပါ။ ကျွန်တော်တို့ရဲ့ ပန်းတိုင်က အဓိကအားဖြင့် တိုတောင်းတဲ့ function calls တွေကို autocomplete လုပ်တာဖြစ်တာကြောင့်၊ context size ကို အတော်လေး သေးငယ်အောင် ထားနိုင်ပါတယ်။ ဒါက model ကို အများကြီး ပိုမြန်မြန် train လုပ်နိုင်ပြီး memory အများကြီး သုံးစွဲမှု နည်းပါးစေတဲ့ အကျိုးကျေးဇူး ရှိပါတယ်။ သင့် application အတွက် context ပိုလိုအပ်တယ်ဆိုရင် (ဥပမာ - function definition ပါတဲ့ file တစ်ခုပေါ်မှာ အခြေခံပြီး unit tests တွေ ရေးစေချင်တယ်ဆိုရင်) အဲဒီနံပါတ်ကို သေချာတိုးမြှင့်ပါ၊ ဒါပေမယ့် ဒါက ပိုကြီးမားတဲ့ GPU memory footprint နဲ့ ပါလာနိုင်တယ်ဆိုတာကိုလည်း သတိရပါ။ အခုအတွက်တော့ context size ကို 128 tokens မှာ သတ်မှတ်ထားပါမယ်၊ GPT-2 ဒါမှမဟုတ် GPT-3 မှာ အသုံးပြုတဲ့ 1,024 ဒါမှမဟုတ် 2,048 နဲ့ မတူပါဘူး။</p> <p data-svelte-h="svelte-thwkeb">documents အများစုမှာ 128 tokens ထက် အများကြီး ပိုများတဲ့ tokens တွေ ပါဝင်တာကြောင့်၊ inputs တွေကို maximum length အထိ ရိုးရှင်းစွာ truncate လုပ်တာက ကျွန်တော်တို့ dataset ရဲ့ အစိတ်အပိုင်းကြီးတစ်ခုကို ဖယ်ရှားပစ်ပါလိမ့်မယ်။ အစား၊ ကျွန်တော်တို့ <code>return_overflowing_tokens</code> option ကို အသုံးပြုပြီး input တစ်ခုလုံးကို tokenize လုပ်ပြီး chunks များစွာအဖြစ် ပိုင်းခြားပါမယ်၊ <a href="/course/chapter6/4">Chapter 6</a> မှာ လုပ်ခဲ့တဲ့အတိုင်းပါပဲ။ <code>return_length</code> option ကိုလည်း အသုံးပြုပြီး ဖန်တီးထားတဲ့ chunk တစ်ခုစီရဲ့ length ကို အလိုအလျောက် ပြန်ပေးပါမယ်။ အများအားဖြင့် နောက်ဆုံး chunk က context size ထက် ပိုသေးငယ်နေမှာဖြစ်ပြီး၊ padding ပြဿနာတွေကို ရှောင်ရှားဖို့အတွက် ဒီအပိုင်းတွေကို ဖယ်ရှားပါမယ်၊ ကျွန်တော်တို့မှာ data အလုံအလောက်ရှိနေတာကြောင့် ဒါတွေ တကယ်မလိုအပ်ပါဘူး။</p> <div class="flex justify-center" data-svelte-h="svelte-1mf8pz"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/chunking_texts.svg" alt="Chunking a large texts in several pieces."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/chunking_texts-dark.svg" alt="Chunking a large texts in several pieces."></div> <p data-svelte-h="svelte-16henph">ဒါက ဘယ်လိုအလုပ်လုပ်လဲဆိုတာကို ပထမဆုံးဥပမာနှစ်ခုကို ကြည့်ပြီး အတိအကျ ကြည့်ရအောင်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer | |
| context_length = <span class="hljs-number">128</span> | |
| tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"huggingface-course/code-search-net-tokenizer"</span>) | |
| outputs = tokenizer( | |
| raw_datasets[<span class="hljs-string">"train"</span>][:<span class="hljs-number">2</span>][<span class="hljs-string">"content"</span>], | |
| truncation=<span class="hljs-literal">True</span>, | |
| max_length=context_length, | |
| return_overflowing_tokens=<span class="hljs-literal">True</span>, | |
| return_length=<span class="hljs-literal">True</span>, | |
| ) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Input IDs length: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(outputs[<span class="hljs-string">'input_ids'</span>])}</span>"</span>) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Input chunk lengths: <span class="hljs-subst">{(outputs[<span class="hljs-string">'length'</span>])}</span>"</span>) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Chunk mapping: <span class="hljs-subst">{outputs[<span class="hljs-string">'overflow_to_sample_mapping'</span>]}</span>"</span>)<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->Input IDs length: <span class="hljs-number">34</span> | |
| Input chunk lengths: [<span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">117</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">41</span>] | |
| Chunk mapping: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-lk5nmm">အဲဒီဥပမာနှစ်ခုကနေ segments ၃၄ ခု ရရှိတာကို တွေ့ရပါတယ်။ chunk lengths တွေကို ကြည့်ရင်၊ documents နှစ်ခုလုံးရဲ့ အဆုံးက chunks တွေမှာ 128 tokens ထက် နည်းပါးတာကို တွေ့ရပါတယ် (အသီးသီး 117 နဲ့ 41)။ ဒါတွေက ကျွန်တော်တို့မှာရှိတဲ့ စုစုပေါင်း chunks တွေရဲ့ အစိတ်အပိုင်းအနည်းငယ်မျှသာ ဖြစ်တာကြောင့်၊ ဒါတွေကို လုံခြုံစွာ ဖယ်ရှားနိုင်ပါတယ်။ <code>overflow_to_sample_mapping</code> field နဲ့၊ ဘယ် chunks တွေက ဘယ် input samples တွေနဲ့ သက်ဆိုင်တယ်ဆိုတာကိုလည်း ပြန်လည်တည်ဆောက်နိုင်ပါတယ်။</p> <p data-svelte-h="svelte-xaghyj">ဒီ operation နဲ့ ကျွန်တော်တို့ 🤗 Datasets မှာရှိတဲ့ <code>Dataset.map()</code> function ရဲ့ အသုံးဝင်တဲ့ feature တစ်ခုကို အသုံးပြုနေပါတယ်။ ဒါက one-to-one maps တွေ မလိုအပ်ပါဘူး၊ <a href="/course/chapter7/3">section 3</a> မှာ ကျွန်တော်တို့ တွေ့ခဲ့ရတဲ့အတိုင်း၊ input batch ထက် elements ပိုများတာ ဒါမှမဟုတ် နည်းပါးတာနဲ့ batches တွေကို ဖန်တီးနိုင်ပါတယ်။ ဒါက data augmentation ဒါမှမဟုတ် data filtering လို operations တွေ လုပ်တဲ့အခါ elements အရေအတွက်ကို ပြောင်းလဲတဲ့အခါ အသုံးဝင်ပါတယ်။ ကျွန်တော်တို့ရဲ့ ကိစ္စမှာ၊ element တစ်ခုစီကို သတ်မှတ်ထားတဲ့ context size ရှိတဲ့ chunks တွေအဖြစ် tokenize လုပ်တဲ့အခါ၊ document တစ်ခုစီကနေ samples များစွာကို ဖန်တီးပါတယ်။ ကျွန်တော်တို့က လက်ရှိ columns တွေကို ဖျက်ပစ်ဖို့ပဲ လိုအပ်ပါတယ်၊ ဘာလို့လဲဆိုတော့ ၎င်းတို့မှာ conflicting size ရှိနေလို့ပါ။ အကယ်၍ ဒါတွေကို ဆက်ထားချင်တယ်ဆိုရင်၊ ၎င်းတို့ကို သင့်လျော်စွာ ထပ်ခါတလဲလဲ လုပ်ပြီး <code>Dataset.map()</code> call ထဲကနေ ပြန်ပေးနိုင်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize</span>(<span class="hljs-params">element</span>): | |
| outputs = tokenizer( | |
| element[<span class="hljs-string">"content"</span>], | |
| truncation=<span class="hljs-literal">True</span>, | |
| max_length=context_length, | |
| return_overflowing_tokens=<span class="hljs-literal">True</span>, | |
| return_length=<span class="hljs-literal">True</span>, | |
| ) | |
| input_batch = [] | |
| <span class="hljs-keyword">for</span> length, input_ids <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(outputs[<span class="hljs-string">"length"</span>], outputs[<span class="hljs-string">"input_ids"</span>]): | |
| <span class="hljs-keyword">if</span> length == context_length: | |
| input_batch.append(input_ids) | |
| <span class="hljs-keyword">return</span> {<span class="hljs-string">"input_ids"</span>: input_batch} | |
| tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>( | |
| tokenize, batched=<span class="hljs-literal">True</span>, remove_columns=raw_datasets[<span class="hljs-string">"train"</span>].column_names | |
| ) | |
| tokenized_datasets<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->DatasetDict({ | |
| train: Dataset({ | |
| features: [<span class="hljs-string">'input_ids'</span>], | |
| num_rows: <span class="hljs-number">16702061</span> | |
| }) | |
| valid: Dataset({ | |
| features: [<span class="hljs-string">'input_ids'</span>], | |
| num_rows: <span class="hljs-number">93164</span> | |
| }) | |
| })<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1h3m8t2">ကျွန်တော်တို့မှာ အခု 128 tokens စီပါဝင်တဲ့ examples 16.7 သန်း ရှိနေပြီး၊ ဒါက စုစုပေါင်း 2.1 ဘီလီယံ tokens ခန့်နဲ့ ကိုက်ညီပါတယ်။ ဥပမာအနေနဲ့၊ OpenAI ရဲ့ GPT-3 နဲ့ Codex models တွေကို ဘီလီယံ ၃၀၀ နဲ့ ဘီလီယံ ၁၀၀ tokens အသီးသီးပေါ်မှာ train လုပ်ထားပါတယ်၊ Codex models တွေကို GPT-3 checkpoints တွေကနေ initialize လုပ်ထားတာပါ။ ဒီအပိုင်းမှာ ကျွန်တော်တို့ရဲ့ ပန်းတိုင်က ရှည်လျားပြီး သဟဇာတဖြစ်တဲ့ texts တွေကို generate လုပ်နိုင်တဲ့ ဒီ models တွေနဲ့ ယှဉ်ပြိုင်ဖို့ မဟုတ်ပါဘူး၊ ဒါပေမယ့် data scientists တွေအတွက် မြန်ဆန်တဲ့ autocomplete function ကို ပံ့ပိုးပေးမယ့် scaled-down version တစ်ခုကို ဖန်တီးဖို့ပါပဲ။</p> <p data-svelte-h="svelte-12lee29">အခု dataset အဆင်သင့်ဖြစ်ပြီဆိုတော့၊ model ကို တည်ဆောက်ကြရအောင်!</p> <blockquote class="tip" data-svelte-h="svelte-m85ghe"><p>✏️ <strong>စမ်းသပ်ကြည့်ပါ။</strong> context size ထက် သေးငယ်တဲ့ chunks တွေအားလုံးကို ဖယ်ရှားပစ်တာက ဒီနေရာမှာ သိပ်ပြဿနာမရှိခဲ့ပါဘူး၊ ဘာလို့လဲဆိုတော့ ကျွန်တော်တို့က small context windows တွေကို အသုံးပြုနေလို့ပါ။ သင် context size ကို တိုးမြှင့်တာနဲ့အမျှ (သို့မဟုတ် တိုတောင်းတဲ့ documents corpus တစ်ခုရှိတယ်ဆိုရင်) ဖယ်ရှားပစ်တဲ့ chunks တွေရဲ့ အချိုးအစားလည်း တိုးလာပါလိမ့်မယ်။ data ကို ပြင်ဆင်ဖို့ ပိုထိရောက်တဲ့ နည်းလမ်းက batch တစ်ခုထဲက tokenized samples အားလုံးကို <code>eos_token_id</code> token တစ်ခုနဲ့ ကြားထဲမှာ ပေါင်းစပ်ပြီး၊ ပြီးရင် concatenated sequences တွေပေါ်မှာ chunking ကို လုပ်ဆောင်တာပါပဲ။ လေ့ကျင့်ခန်းအနေနဲ့၊ အဲဒီနည်းလမ်းကို အသုံးပြုဖို့ <code>tokenize()</code> function ကို ပြင်ဆင်ပါ။ <code>truncation=False</code> ကို သတ်မှတ်ပြီး tokenizer ကနေ အခြား arguments တွေကို ဖယ်ရှားပြီး token IDs ရဲ့ full sequence ကို ရယူချင်တယ်ဆိုတာ သတိပြုပါ။</p></blockquote> <h2 class="relative group"><a id="initializing-a-new-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#initializing-a-new-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model အသစ်တစ်ခုကို စတင်ခြင်း</span></h2> <p data-svelte-h="svelte-4y0nxb">ကျွန်တော်တို့ရဲ့ ပထမအဆင့်က GPT-2 model တစ်ခုကို လုံးဝအသစ်ကနေ initialize လုပ်ဖို့ပါပဲ။ ကျွန်တော်တို့ရဲ့ model အတွက် small GPT-2 model အတွက် configuration တူတူကို အသုံးပြုပါမယ်၊ ဒါကြောင့် pretrained configuration ကို load လုပ်ပြီး tokenizer size က model vocabulary size နဲ့ ကိုက်ညီကြောင်း သေချာစေကာ <code>bos</code> နဲ့ <code>eos</code> (beginning and end of sequence) token IDs တွေကို ထည့်ပေးပါမယ်-</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, GPT2LMHeadModel, AutoConfig | |
| config = AutoConfig.from_pretrained( | |
| <span class="hljs-string">"gpt2"</span>, | |
| vocab_size=<span class="hljs-built_in">len</span>(tokenizer), | |
| n_ctx=context_length, | |
| bos_token_id=tokenizer.bos_token_id, | |
| eos_token_id=tokenizer.eos_token_id, | |
| )<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-jv9qtb">အဲဒီ configuration နဲ့ model အသစ်တစ်ခုကို load လုပ်နိုင်ပါတယ်။ ဒါဟာ ကျွန်တော်တို့ <code>from_pretrained()</code> function ကို မသုံးဘဲ model ကို ကိုယ်တိုင် initialize လုပ်တာ ပထမဆုံးအကြိမ် ဖြစ်တယ်ဆိုတာ မှတ်သားပါ။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START -->model = GPT2LMHeadModel(config) | |
| model_size = <span class="hljs-built_in">sum</span>(t.numel() <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> model.parameters()) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"GPT-2 size: <span class="hljs-subst">{model_size/<span class="hljs-number">1000</span>**<span class="hljs-number">2</span>:<span class="hljs-number">.1</span>f}</span>M parameters"</span>)<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->GPT-<span class="hljs-number">2</span> size: <span class="hljs-number">124.2</span>M parameters<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1kz13es">ကျွန်တော်တို့ရဲ့ model မှာ parameters 124M ရှိပြီး ဒါတွေကို tune လုပ်ရပါမယ်။ training မစတင်ခင်၊ batches တွေကို ဖန်တီးပေးမယ့် data collator တစ်ခုကို တည်ဆောက်ဖို့ လိုပါတယ်။ <code>DataCollatorForLanguageModeling</code> collator ကို အသုံးပြုနိုင်ပါတယ်၊ ဒါက language modeling အတွက် အထူးဒီဇိုင်းထုတ်ထားတာပါ (နာမည်က သိမ်မွေ့စွာ အကြံပြုထားတဲ့အတိုင်း)။ batches တွေကို stacking နဲ့ padding လုပ်ခြင်းအပြင်၊ ဒါက language model labels တွေ ဖန်တီးတာကိုပါ ဂရုစိုက်ပေးပါတယ် — causal language modeling မှာ inputs တွေက labels တွေအဖြစ်လည်း လုပ်ဆောင်ပါတယ် (element တစ်ခုနဲ့ ရွှေ့ထားရုံပါပဲ)၊ ပြီးတော့ ဒီ data collator က training လုပ်နေစဉ် on the fly မှာ ဒါတွေကို ဖန်တီးပေးတာကြောင့် <code>input_ids</code> တွေကို ထပ်ပွားဖို့ မလိုအပ်ပါဘူး။</p> <p data-svelte-h="svelte-100iv6i"><code>DataCollatorForLanguageModeling</code> က masked language modeling (MLM) နဲ့ causal language modeling (CLM) နှစ်ခုလုံးကို ထောက်ပံ့ပေးတယ်ဆိုတာ မှတ်သားပါ။ default အားဖြင့် ဒါက MLM အတွက် data ကို ပြင်ဆင်ပေးပါတယ်၊ ဒါပေမယ့် <code>mlm=False</code> argument ကို သတ်မှတ်ခြင်းဖြင့် CLM ကို ပြောင်းနိုင်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForLanguageModeling | |
| tokenizer.pad_token = tokenizer.eos_token | |
| data_collator = DataCollatorForLanguageModeling(tokenizer, mlm=<span class="hljs-literal">False</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-3lqmdo">ဥပမာတစ်ခု ကြည့်ရအောင်…</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START -->out = data_collator([tokenized_datasets[<span class="hljs-string">"train"</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>)]) | |
| <span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> out: | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{key}</span> shape: <span class="hljs-subst">{out[key].shape}</span>"</span>)<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->input_ids shape: torch.Size([<span class="hljs-number">5</span>, <span class="hljs-number">128</span>]) | |
| attention_mask shape: torch.Size([<span class="hljs-number">5</span>, <span class="hljs-number">128</span>]) | |
| labels shape: torch.Size([<span class="hljs-number">5</span>, <span class="hljs-number">128</span>])<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-9autic">examples တွေကို stacking လုပ်ထားပြီး tensors အားလုံးမှာ shape တူတူရှိတာကို တွေ့ရပါတယ်။</p> <blockquote class="warning" data-svelte-h="svelte-1i7l6ok"><p>⚠️ inputs တွေနဲ့ labels တွေကို ချိန်ညှိဖို့ ရွှေ့ပြောင်းတာက model ထဲမှာ ဖြစ်ပေါ်ပါတယ်၊ ဒါကြောင့် data collator က inputs တွေကို labels တွေဖန်တီးဖို့ ကူးယူရုံပါပဲ။</p></blockquote> <p data-svelte-h="svelte-1zilm2">အခု ကျွန်တော်တို့ model ကို train လုပ်ဖို့ အရာအားလုံး အဆင်သင့်ဖြစ်ပါပြီ — ဒီလောက်အလုပ်အများကြီး မဟုတ်ပါလား! training မစတင်ခင် Hugging Face ကို login ဝင်သင့်ပါတယ်။ သင် notebook မှာ အလုပ်လုပ်နေတယ်ဆိုရင်၊ အောက်ပါ utility function နဲ့ လုပ်နိုင်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login | |
| notebook_login()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-euzy3a">ဒါက သင် Hugging Face login credentials တွေ ထည့်သွင်းနိုင်မယ့် widget တစ်ခုကို ပြသပေးပါလိမ့်မယ်။</p> <p data-svelte-h="svelte-dlr1jb">သင် notebook မှာ အလုပ်မလုပ်ဘူးဆိုရင်၊ သင့် terminal မှာ အောက်ပါစာကြောင်းကို ရိုက်ထည့်လိုက်ပါ။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->huggingface-cli login<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-npx20v">ကျန်ရှိတာက training arguments တွေကို configure လုပ်ပြီး <code>Trainer</code> ကို စတင်ဖို့ပါပဲ။ warmup နဲ့ 256 (<code>per_device_train_batch_size</code> * <code>gradient_accumulation_steps</code>) ရဲ့ effective batch size နဲ့ cosine learning rate schedule ကို ကျွန်တော်တို့ အသုံးပြုပါမယ်။ gradient accumulation က single batch တစ်ခု memory ထဲ မဆံ့တဲ့အခါ အသုံးပြုပြီး၊ forward/backward passes များစွာကနေ gradient ကို တဖြည်းဖြည်း တည်ဆောက်ပါတယ်။ 🤗 Accelerate နဲ့ training loop ကို ဖန်တီးတဲ့အခါ ဒါကို လက်တွေ့ မြင်တွေ့ရပါလိမ့်မယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer, TrainingArguments | |
| args = TrainingArguments( | |
| output_dir=<span class="hljs-string">"codeparrot-ds"</span>, | |
| per_device_train_batch_size=<span class="hljs-number">32</span>, | |
| per_device_eval_batch_size=<span class="hljs-number">32</span>, | |
| evaluation_strategy=<span class="hljs-string">"steps"</span>, | |
| eval_steps=<span class="hljs-number">5_000</span>, | |
| logging_steps=<span class="hljs-number">5_000</span>, | |
| gradient_accumulation_steps=<span class="hljs-number">8</span>, | |
| num_train_epochs=<span class="hljs-number">1</span>, | |
| weight_decay=<span class="hljs-number">0.1</span>, | |
| warmup_steps=<span class="hljs-number">1_000</span>, | |
| lr_scheduler_type=<span class="hljs-string">"cosine"</span>, | |
| learning_rate=<span class="hljs-number">5e-4</span>, | |
| save_steps=<span class="hljs-number">5_000</span>, | |
| fp16=<span class="hljs-literal">True</span>, | |
| push_to_hub=<span class="hljs-literal">True</span>, | |
| ) | |
| trainer = Trainer( | |
| model=model, | |
| tokenizer=tokenizer, | |
| args=args, | |
| data_collator=data_collator, | |
| train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], | |
| eval_dataset=tokenized_datasets[<span class="hljs-string">"valid"</span>], | |
| )<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-8yn7wv">အခု ကျွန်တော်တို့ <code>Trainer</code> ကို စတင်ပြီး training ပြီးဆုံးဖို့ စောင့်ဆိုင်းနိုင်ပါပြီ။ ဒါကို training set အပြည့်အစုံ ဒါမှမဟုတ် subset တစ်ခုပေါ်မှာ run ခြင်းပေါ်မူတည်ပြီး ၂၀ နာရီ ဒါမှမဟုတ် ၂ နာရီ အသီးသီး ကြာမြင့်ပါလိမ့်မယ်၊ ဒါကြောင့် ကော်ဖီအချို့နဲ့ ဖတ်ဖို့ ကောင်းမွန်တဲ့ စာအုပ်တစ်အုပ် ယူထားပါ။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->trainer.train()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-11njpwh">training ပြီးဆုံးသွားပြီးနောက်၊ model နဲ့ tokenizer ကို Hub သို့ push လုပ်နိုင်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START -->trainer.push_to_hub()<!-- HTML_TAG_END --></pre></div> <blockquote class="tip" data-svelte-h="svelte-1h0mmqf"><p>✏️ <strong>စမ်းသပ်ကြည့်ပါ။</strong> GPT-2 ကို raw texts ကနေ train လုပ်ဖို့ <code>TrainingArguments</code> အပြင် code လိုင်း ၃၀ ခန့်ပဲ ကျွန်တော်တို့ အသုံးပြုခဲ့ရပါတယ်။ သင့်ကိုယ်ပိုင် dataset နဲ့ စမ်းသပ်ကြည့်ပြီး ကောင်းမွန်တဲ့ ရလဒ်တွေ ရမရ ကြည့်ပါ။</p></blockquote> <blockquote class="tip" data-svelte-h="svelte-62du9i"></blockquote> <blockquote data-svelte-h="svelte-tulhb6"><p>💡 သင့်မှာ multiple GPUs ပါတဲ့ machine တစ်ခုရှိရင်၊ အဲဒီမှာ code ကို run ကြည့်ပါ။ <code>Trainer</code> က multiple machines တွေကို အလိုအလျောက် စီမံခန့်ခွဲပေးပြီး၊ ဒါက training ကို အလွန်အမင်း မြန်ဆန်စေနိုင်ပါတယ်။</p></blockquote> <h2 class="relative group"><a id="code-generation-with-a-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#code-generation-with-a-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pipeline ဖြင့် Code Generation</span></h2> <p data-svelte-h="svelte-18xoqjs">အခုကတော့ အမှန်တရားရဲ့အချိန်ပါပဲ။ train လုပ်ထားတဲ့ model က ဘယ်လောက်ကောင်းကောင်း အလုပ်လုပ်လဲ ကြည့်ရအောင်။ loss က တသမတ်တည်း ကျဆင်းသွားတာကို logs တွေမှာ ကျွန်တော်တို့ မြင်နိုင်ပါတယ်၊ ဒါပေမယ့် model ကို စမ်းသပ်ဖို့ prompts အချို့မှာ ဘယ်လောက်ကောင်းကောင်း အလုပ်လုပ်လဲ ကြည့်ရအောင်။ ဒါကိုလုပ်ဖို့ model ကို text generation <code>pipeline</code> တစ်ခုထဲမှာ ထည့်သွင်းပါမယ်၊ ပြီးတော့ မြန်ဆန်တဲ့ generation တွေအတွက် GPU တစ်ခုရှိရင် GPU ပေါ်မှာ ထားပါမယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline | |
| device = torch.device(<span class="hljs-string">"cuda"</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">"cpu"</span>) | |
| pipe = pipeline( | |
| <span class="hljs-string">"text-generation"</span>, model=<span class="hljs-string">"huggingface-course/codeparrot-ds"</span>, device=device | |
| )<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-7gucp7">scatter plot တစ်ခု ဖန်တီးတဲ့ ရိုးရှင်းတဲ့ task နဲ့ စတင်ကြရအောင်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->txt = <span class="hljs-string">"""\ | |
| # create some data | |
| x = np.random.randn(100) | |
| y = np.random.randn(100) | |
| # create scatter plot with x, y | |
| """</span> | |
| <span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># create some data</span> | |
| x = np.random.randn(<span class="hljs-number">100</span>) | |
| y = np.random.randn(<span class="hljs-number">100</span>) | |
| <span class="hljs-comment"># create scatter plot with x, y</span> | |
| plt.scatter(x, y) | |
| <span class="hljs-comment"># create scatter</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1onl0y9">ရလဒ်က မှန်ကန်ပုံရပါတယ်။ <code>pandas</code> operation တစ်ခုအတွက်လည်း အလုပ်လုပ်သလား။ arrays နှစ်ခုကနေ <code>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 class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START -->txt = <span class="hljs-string">"""\ | |
| # create some data | |
| x = np.random.randn(100) | |
| y = np.random.randn(100) | |
| # create dataframe from x and y | |
| """</span> | |
| <span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># create some data</span> | |
| x = np.random.randn(<span class="hljs-number">100</span>) | |
| y = np.random.randn(<span class="hljs-number">100</span>) | |
| <span class="hljs-comment"># create dataframe from x and y</span> | |
| df = pd.DataFrame({<span class="hljs-string">'x'</span>: x, <span class="hljs-string">'y'</span>: y}) | |
| df.insert(<span class="hljs-number">0</span>,<span class="hljs-string">'x'</span>, x) | |
| <span class="hljs-keyword">for</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-9qu1fq">ကောင်းပါပြီ၊ ဒါက မှန်ကန်တဲ့ အဖြေပါ — ဒါပေမယ့် နောက်မှ column <code>x</code> ကို ထပ်ထည့်ပြန်ပါတယ်။ generate လုပ်တဲ့ tokens အရေအတွက် ကန့်သတ်ထားတာကြောင့်၊ အောက်ပါ <code>for</code> loop က ဖြတ်တောက်ခံထားရပါတယ်။ နည်းနည်းပိုရှုပ်ထွေးတဲ့အရာတစ်ခု လုပ်နိုင်မလား၊ <code>groupby</code> operation ကို အသုံးပြုရာမှာ model က ကျွန်တော်တို့ကို ကူညီနိုင်မလား ကြည့်ရအောင်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->txt = <span class="hljs-string">"""\ | |
| # dataframe with profession, income and name | |
| df = pd.DataFrame({'profession': x, 'income':y, 'name': z}) | |
| # calculate the mean income per profession | |
| """</span> | |
| <span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># dataframe with profession, income and name</span> | |
| df = pd.DataFrame({<span class="hljs-string">'profession'</span>: x, <span class="hljs-string">'income'</span>:y, <span class="hljs-string">'name'</span>: z}) | |
| <span class="hljs-comment"># calculate the mean income per profession</span> | |
| profession = df.groupby([<span class="hljs-string">'profession'</span>]).mean() | |
| <span class="hljs-comment"># compute the</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-141k43d">မဆိုးပါဘူး၊ ဒါက မှန်ကန်တဲ့နည်းလမ်းပါပဲ။ နောက်ဆုံးအနေနဲ့ <code>scikit-learn</code> အတွက်လည်း အသုံးပြုနိုင်မလား၊ Random Forest model တစ်ခုကို တည်ဆောက်နိုင်မလား ကြည့်ရအောင်-</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->txt = <span class="hljs-string">""" | |
| # import random forest regressor from scikit-learn | |
| from sklearn.ensemble import RandomForestRegressor | |
| # fit random forest model with 300 estimators on X, y: | |
| """</span> | |
| <span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># import random forest regressor from scikit-learn</span> | |
| <span class="hljs-keyword">from</span> sklearn.ensemble <span class="hljs-keyword">import</span> RandomForestRegressor | |
| <span class="hljs-comment"># fit random forest model with 300 estimators on X, y:</span> | |
| rf = RandomForestRegressor(n_estimators=<span class="hljs-number">300</span>, random_state=random_state, max_depth=<span class="hljs-number">3</span>) | |
| rf.fit(X, y) | |
| rf<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-cb888b">ဒီဥပမာအချို့ကို ကြည့်ရတာ၊ model ဟာ Python data science stack ရဲ့ syntax အချို့ကို သင်ယူခဲ့ပုံရပါတယ်။ (Oof course, လက်တွေ့ကမ္ဘာမှာ deploy မလုပ်ခင် model ကို ပိုမိုစေ့စပ်သေချာစွာ evaluation လုပ်ဖို့ လိုအပ်ပါလိမ့်မယ်)။ သို့သော်လည်း၊ ပေးထားတဲ့ use case တစ်ခုအတွက် လိုအပ်တဲ့ performance ကို ရရှိဖို့အတွက် model training ကို ပိုမို customize လုပ်ဖို့ လိုအပ်တာတွေလည်း ရှိပါတယ်။ ဥပမာ၊ batch size ကို dynamically update လုပ်ချင်ရင် ဒါမှမဟုတ် bad examples တွေကို on the fly မှာ ကျော်သွားမယ့် conditional training loop တစ်ခုရှိချင်ရင် ဘယ်လိုလုပ်မလဲ။ နည်းလမ်းတစ်ခုကတော့ <code>Trainer</code> ကို subclass လုပ်ပြီး လိုအပ်တဲ့ ပြောင်းလဲမှုတွေကို ထည့်သွင်းတာပါပဲ၊ ဒါပေမယ့် တခါတလေ training loop ကို အစကနေ ရေးတာက ပိုရိုးရှင်းပါတယ်။ အဲဒီနေရာမှာ 🤗 Accelerate က ဝင်လာတာပါ။</p> <h2 class="relative group"><a id="training-with-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-with-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>🤗 Accelerate ဖြင့် Training</span></h2> <p data-svelte-h="svelte-rruem9"><code>Trainer</code> နဲ့ model တစ်ခုကို ဘယ်လို train လုပ်ရမယ်ဆိုတာ ကျွန်တော်တို့ မြင်ခဲ့ရပြီးပါပြီ၊ ဒါက customization အချို့ကို ခွင့်ပြုနိုင်ပါတယ်။ သို့သော်လည်း၊ တခါတလေ ကျွန်တော်တို့ training loop အပေါ် အပြည့်အဝ ထိန်းချုပ်ချင်တာ ဒါမှမဟုတ် ထူးခြားဆန်းကြယ်တဲ့ ပြောင်းလဲမှုအချို့ လုပ်ချင်ပါတယ်။ ဒီလိုကိစ္စမှာ 🤗 Accelerate က အကောင်းဆုံးရွေးချယ်မှုဖြစ်ပြီး၊ ဒီအပိုင်းမှာ ကျွန်တော်တို့ရဲ့ model ကို train လုပ်ဖို့အတွက် အသုံးပြုဖို့ အဆင့်တွေကို ကျွန်တော်တို့ ဖော်ပြပေးပါမယ်။ ပိုစိတ်ဝင်စားဖို့ကောင်းအောင်၊ training loop ကို twist တစ်ခုလည်း ထည့်သွင်းပါမယ်။</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Hm8_PgVTFuc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> <p data-svelte-h="svelte-1tw1fcs">ကျွန်တော်တို့ဟာ data science libraries တွေအတွက် သဘာဝကျတဲ့ autocompletion ကို အဓိက စိတ်ဝင်စားတာကြောင့်၊ ဒီ libraries တွေကို ပိုအသုံးပြုတဲ့ training samples တွေကို ပိုအလေးထားဖို့ အဓိပ္ပာယ်ရှိပါတယ်။ <code>plt</code>, <code>pd</code>, <code>sk</code>, <code>fit</code>, နဲ့ <code>predict</code> လို keywords တွေကို အသုံးပြုခြင်းဖြင့် ဒီ examples တွေကို အလွယ်တကူ ဖော်ထုတ်နိုင်ပါတယ်။ ဒါတွေက <code>matplotlib.pyplot</code>, <code>pandas</code>, နဲ့ <code>sklearn</code> အတွက် အသုံးများဆုံး import names တွေအပြင် နောက်ဆုံးနှစ်ခုရဲ့ fit/predict pattern လည်း ဖြစ်ပါတယ်။ ဒါတွေကို token တစ်ခုစီအဖြစ် ကိုယ်စားပြုထားရင်၊ ၎င်းတို့ input sequence မှာ ပါဝင်ခြင်းရှိမရှိ အလွယ်တကူ စစ်ဆေးနိုင်ပါတယ်။ Tokens တွေမှာ whitespace prefix ရှိနိုင်တာကြောင့်၊ tokenizer vocabulary ထဲက အဲဒီ versions တွေကိုလည်း စစ်ဆေးပါမယ်။ ဒါက အလုပ်လုပ်မလုပ် စစ်ဆေးဖို့အတွက်၊ multiple tokens အဖြစ် ခွဲခြားသင့်တဲ့ test token တစ်ခု ထည့်ပါမယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->keytoken_ids = [] | |
| <span class="hljs-keyword">for</span> keyword <span class="hljs-keyword">in</span> [ | |
| <span class="hljs-string">"plt"</span>, | |
| <span class="hljs-string">"pd"</span>, | |
| <span class="hljs-string">"sk"</span>, | |
| <span class="hljs-string">"fit"</span>, | |
| <span class="hljs-string">"predict"</span>, | |
| <span class="hljs-string">" plt"</span>, | |
| <span class="hljs-string">" pd"</span>, | |
| <span class="hljs-string">" sk"</span>, | |
| <span class="hljs-string">" fit"</span>, | |
| <span class="hljs-string">" predict"</span>, | |
| <span class="hljs-string">"testtest"</span>, | |
| ]: | |
| ids = tokenizer([keyword]).input_ids[<span class="hljs-number">0</span>] | |
| <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(ids) == <span class="hljs-number">1</span>: | |
| keytoken_ids.append(ids[<span class="hljs-number">0</span>]) | |
| <span class="hljs-keyword">else</span>: | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Keyword has not single token: <span class="hljs-subst">{keyword}</span>"</span>)<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-string">'Keyword has not single token: testtest'</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-yzh37i">ကောင်းပါပြီ၊ ဒါက ကောင်းကောင်းအလုပ်လုပ်ပုံရပါတယ်။ အခု input sequence၊ logits နဲ့ ကျွန်တော်တို့ ရွေးချယ်ခဲ့တဲ့ key tokens တွေကို inputs အဖြစ် ယူတဲ့ custom loss function တစ်ခု ရေးနိုင်ပါပြီ။ ပထမဆုံး logits နဲ့ inputs တွေကို ချိန်ညှိဖို့ လိုပါတယ်- input sequence ကို တစ်ခုညာဘက်သို့ ရွှေ့ထားတာက labels တွေ ဖြစ်လာပါတယ်၊ ဘာလို့လဲဆိုတော့ နောက်ဆုံး token က လက်ရှိ token အတွက် label ဖြစ်လို့ပါ။ model က ပထမဆုံး token အတွက် prediction မလုပ်တာကြောင့် input sequence ရဲ့ ဒုတိယ token ကနေ labels တွေကို စတင်ခြင်းဖြင့် ဒါကို ကျွန်တော်တို့ အောင်မြင်နိုင်ပါတယ်။ ပြီးရင် နောက်ဆုံး logit ကို ဖြတ်ပစ်ပါမယ်၊ ဘာလို့လဲဆိုတော့ full input sequence နောက်က လိုက်လာမယ့် token အတွက် label မရှိလို့ပါပဲ။ ဒါတွေနဲ့ sample တစ်ခုစီရဲ့ loss ကို တွက်ချက်နိုင်ပြီး sample တစ်ခုစီမှာရှိတဲ့ keywords တွေရဲ့ ဖြစ်ပေါ်မှုတွေကို ရေတွက်နိုင်ပါတယ်။ နောက်ဆုံးအနေနဲ့၊ ဖြစ်ပေါ်မှုတွေကို weights အဖြစ် အသုံးပြုပြီး samples အားလုံးရဲ့ weighted average ကို တွက်ချက်ပါတယ်။ keywords မရှိတဲ့ samples တွေအားလုံးကို ဖယ်ရှားပစ်ဖို့ မလိုတာကြောင့်၊ weights တွေမှာ ၁ ကို ပေါင်းထည့်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> torch.nn <span class="hljs-keyword">import</span> CrossEntropyLoss | |
| <span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">keytoken_weighted_loss</span>(<span class="hljs-params">inputs, logits, keytoken_ids, alpha=<span class="hljs-number">1.0</span></span>): | |
| <span class="hljs-comment"># Shift so that tokens < n predict n</span> | |
| shift_labels = inputs[..., <span class="hljs-number">1</span>:].contiguous() | |
| shift_logits = logits[..., :-<span class="hljs-number">1</span>, :].contiguous() | |
| <span class="hljs-comment"># Calculate per-token loss</span> | |
| loss_fct = CrossEntropyLoss(reduce=<span class="hljs-literal">False</span>) | |
| loss = loss_fct(shift_logits.view(-<span class="hljs-number">1</span>, shift_logits.size(-<span class="hljs-number">1</span>)), shift_labels.view(-<span class="hljs-number">1</span>)) | |
| <span class="hljs-comment"># Resize and average loss per sample</span> | |
| loss_per_sample = loss.view(shift_logits.size(<span class="hljs-number">0</span>), shift_logits.size(<span class="hljs-number">1</span>)).mean(axis=<span class="hljs-number">1</span>) | |
| <span class="hljs-comment"># Calculate and scale weighting</span> | |
| weights = torch.stack([(inputs == kt).<span class="hljs-built_in">float</span>() <span class="hljs-keyword">for</span> kt <span class="hljs-keyword">in</span> keytoken_ids]).<span class="hljs-built_in">sum</span>( | |
| axis=[<span class="hljs-number">0</span>, <span class="hljs-number">2</span>] | |
| ) | |
| weights = alpha * (<span class="hljs-number">1.0</span> + weights) | |
| <span class="hljs-comment"># Calculate weighted average</span> | |
| weighted_loss = (loss_per_sample * weights).mean() | |
| <span class="hljs-keyword">return</span> weighted_loss<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1y1pmrr">ဒီအံ့သြဖွယ်ကောင်းတဲ့ loss function အသစ်နဲ့ training မစတင်ခင်၊ အချို့အရာတွေကို ပြင်ဆင်ဖို့ လိုပါတယ်။</p> <ul data-svelte-h="svelte-1n72fh6"><li>batches တွေမှာ data ကို load လုပ်ဖို့ dataloaders တွေ လိုအပ်ပါတယ်။</li> <li>weight decay parameters တွေကို တည်ဆောက်ဖို့ လိုပါတယ်။</li> <li>အခါအားလျော်စွာ evaluate လုပ်ချင်တာကြောင့် evaluation code ကို function တစ်ခုထဲမှာ ထည့်သွင်းတာက အဓိပ္ပာယ်ရှိပါတယ်။</li></ul> <p data-svelte-h="svelte-1ig4qrn">dataloaders တွေနဲ့ စတင်ကြရအောင်။ dataset ရဲ့ format ကို <code>"torch"</code> လို့ သတ်မှတ်ဖို့ပဲ လိုအပ်ပြီး၊ ပြီးရင် သင့်လျော်တဲ့ batch size နဲ့ PyTorch <code>DataLoader</code> ကို ပေးနိုင်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> torch.utils.data.dataloader <span class="hljs-keyword">import</span> DataLoader | |
| tokenized_datasets.set_format(<span class="hljs-string">"torch"</span>) | |
| train_dataloader = DataLoader(tokenized_datasets[<span class="hljs-string">"train"</span>], batch_size=<span class="hljs-number">32</span>, shuffle=<span class="hljs-literal">True</span>) | |
| eval_dataloader = DataLoader(tokenized_datasets[<span class="hljs-string">"valid"</span>], batch_size=<span class="hljs-number">32</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1laenkf">နောက်တစ်ခုက parameters တွေကို အုပ်စုဖွဲ့ပါမယ်၊ ဒါမှ optimizer က ဘယ် parameters တွေက ထပ်ဆောင်း weight decay ရမလဲဆိုတာ သိမှာပါ။ ပုံမှန်အားဖြင့်၊ bias နဲ့ LayerNorm weights terms အားလုံးက ဒါကနေ ကင်းလွတ်ပါတယ်၊ ဒါကို ဘယ်လိုလုပ်ရမလဲဆိုတာ ဒီမှာပါ။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START -->weight_decay = <span class="hljs-number">0.1</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">get_grouped_params</span>(<span class="hljs-params">model, no_decay=[<span class="hljs-string">"bias"</span>, <span class="hljs-string">"LayerNorm.weight"</span>]</span>): | |
| params_with_wd, params_without_wd = [], [] | |
| <span class="hljs-keyword">for</span> n, p <span class="hljs-keyword">in</span> model.named_parameters(): | |
| <span class="hljs-keyword">if</span> <span class="hljs-built_in">any</span>(nd <span class="hljs-keyword">in</span> n <span class="hljs-keyword">for</span> nd <span class="hljs-keyword">in</span> no_decay): | |
| params_without_wd.append(p) | |
| <span class="hljs-keyword">else</span>: | |
| params_with_wd.append(p) | |
| <span class="hljs-keyword">return</span> [ | |
| {<span class="hljs-string">"params"</span>: params_with_wd, <span class="hljs-string">"weight_decay"</span>: weight_decay}, | |
| {<span class="hljs-string">"params"</span>: params_without_wd, <span class="hljs-string">"weight_decay"</span>: <span class="hljs-number">0.0</span>}, | |
| ]<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-uobqdw">training လုပ်နေစဉ် validation set ပေါ်မှာ model ကို ပုံမှန် evaluate လုပ်ချင်တာကြောင့်၊ အဲဒါအတွက်လည်း function တစ်ခု ရေးကြရအောင်။ ဒါက evaluation dataloader ကို ဖြတ်ပြီး processes အားလုံးက losses တွေကို စုဆောင်းရုံပါပဲ။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">evaluate</span>(): | |
| model.<span class="hljs-built_in">eval</span>() | |
| losses = [] | |
| <span class="hljs-keyword">for</span> step, batch <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(eval_dataloader): | |
| <span class="hljs-keyword">with</span> torch.no_grad(): | |
| outputs = model(batch[<span class="hljs-string">"input_ids"</span>], labels=batch[<span class="hljs-string">"input_ids"</span>]) | |
| losses.append(accelerator.gather(outputs.loss)) | |
| loss = torch.mean(torch.cat(losses)) | |
| <span class="hljs-keyword">try</span>: | |
| perplexity = torch.exp(loss) | |
| <span class="hljs-keyword">except</span> OverflowError: | |
| perplexity = <span class="hljs-built_in">float</span>(<span class="hljs-string">"inf"</span>) | |
| <span class="hljs-keyword">return</span> loss.item(), perplexity.item()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1tirxr2"><code>evaluate()</code> function နဲ့ loss နဲ့ <a href="/course/chapter7/3">perplexity</a> ကို ပုံမှန်ကြားကာလတွေမှာ report လုပ်နိုင်ပါတယ်။ နောက်တစ်ခုက၊ model ကို အစကနေ train လုပ်တာ သေချာစေဖို့ model ကို ပြန်လည်သတ်မှတ်ပါမယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->model = GPT2LMHeadModel(config)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1cmci08">ပြီးရင် ကျွန်တော်တို့ optimizer ကို သတ်မှတ်နိုင်ပါတယ်၊ weight decay အတွက် parameters တွေကို ခွဲဖို့ ယခင် function ကို အသုံးပြုပြီး-</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW | |
| optimizer = AdamW(get_grouped_params(model), lr=<span class="hljs-number">5e-4</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-105n42f">အခု model, optimizer နဲ့ dataloaders တွေကို ပြင်ဆင်ပြီး training စတင်နိုင်ပါပြီ။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator | |
| accelerator = Accelerator(fp16=<span class="hljs-literal">True</span>) | |
| model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( | |
| model, optimizer, train_dataloader, eval_dataloader | |
| )<!-- HTML_TAG_END --></pre></div> <blockquote class="tip" data-svelte-h="svelte-pscqhu"><p>🚨 သင် TPU ပေါ်မှာ train လုပ်နေတယ်ဆိုရင်၊ အပေါ်က cell ကနေ စတင်တဲ့ code အားလုံးကို dedicated training function တစ်ခုထဲကို ရွှေ့ဖို့ လိုအပ်ပါလိမ့်မယ်။ အသေးစိတ်အချက်အလက်တွေအတွက် <a href="/course/chapter3">Chapter 3</a> ကို ကြည့်ပါ။</p></blockquote> <p data-svelte-h="svelte-rp9ymw">ကျွန်တော်တို့ <code>train_dataloader</code> ကို <code>accelerator.prepare()</code> သို့ ပေးပို့ပြီးတာနဲ့၊ ၎င်းရဲ့ length ကို အသုံးပြုပြီး training steps အရေအတွက်ကို တွက်ချက်နိုင်ပါပြီ။ အဲဒီ method က ၎င်းရဲ့ length ကို ပြောင်းလဲမှာ ဖြစ်တာကြောင့် dataloader ကို ပြင်ဆင်ပြီးနောက် ဒါကို အမြဲလုပ်ဆောင်သင့်တယ်ဆိုတာ မှတ်သားပါ။ learning rate ကနေ ၀ အထိ classic linear schedule ကို ကျွန်တော်တို့ အသုံးပြုပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler | |
| num_train_epochs = <span class="hljs-number">1</span> | |
| num_update_steps_per_epoch = <span class="hljs-built_in">len</span>(train_dataloader) | |
| num_training_steps = num_train_epochs * num_update_steps_per_epoch | |
| lr_scheduler = get_scheduler( | |
| name=<span class="hljs-string">"linear"</span>, | |
| optimizer=optimizer, | |
| num_warmup_steps=<span class="hljs-number">1_000</span>, | |
| num_training_steps=num_training_steps, | |
| )<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1ntqhml">နောက်ဆုံးအနေနဲ့၊ ကျွန်တော်တို့ model ကို Hub ကို push လုပ်ဖို့အတွက်၊ working folder တစ်ခုထဲမှာ <code>Repository</code> object တစ်ခု ဖန်တီးဖို့ လိုအပ်ပါလိမ့်မယ်။ ပထမဆုံး Hugging Face Hub ကို login ဝင်ပါ၊ အကယ်၍ သင် login ဝင်ထားခြင်းမရှိသေးရင်ပေါ့။ ကျွန်တော်တို့ရဲ့ model ကို ပေးချင်တဲ့ model ID ကနေ repository name ကို ကျွန်တော်တို့ ဆုံးဖြတ်ပါမယ် (သင် <code>repo_name</code> ကို သင့်ကိုယ်ပိုင် ရွေးချယ်မှုနဲ့ အစားထိုးနိုင်ပါတယ်၊ ဒါက သင့် username ကို ပါဝင်ဖို့ပဲ လိုအပ်ပြီး၊ ဒါက <code>get_full_repo_name()</code> function က လုပ်ဆောင်တာပါ)။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository, get_full_repo_name | |
| model_name = <span class="hljs-string">"codeparrot-ds-accelerate"</span> | |
| repo_name = get_full_repo_name(model_name) | |
| repo_name<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-string">'sgugger/codeparrot-ds-accelerate'</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-rea7s0">ပြီးရင် အဲဒီ repository ကို local folder တစ်ခုထဲမှာ clone လုပ်နိုင်ပါတယ်။ အကယ်၍ ဒါက ရှိပြီးသားဆိုရင်၊ ဒီ local folder က ကျွန်တော်တို့ အလုပ်လုပ်နေတဲ့ repository ရဲ့ ရှိပြီးသား clone တစ်ခု ဖြစ်သင့်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START -->output_dir = <span class="hljs-string">"codeparrot-ds-accelerate"</span> | |
| repo = Repository(output_dir, clone_from=repo_name)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-momshn">အခု ကျွန်တော်တို့ <code>repo.push_to_hub()</code> method ကို ခေါ်ခြင်းဖြင့် <code>output_dir</code> မှာ သိမ်းဆည်းထားတဲ့ ဘယ်အရာကိုမဆို upload လုပ်နိုင်ပါပြီ။ ဒါက epoch တစ်ခုစီရဲ့ အဆုံးမှာ intermediate models တွေကို upload လုပ်ဖို့ ကူညီပေးပါလိမ့်မယ်။</p> <p data-svelte-h="svelte-1a031bg">training မလုပ်ခင်၊ evaluation function မှန်ကန်စွာ အလုပ်လုပ်မလုပ် စစ်ဆေးဖို့ quick test တစ်ခု run ကြရအောင်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->evaluate()<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->(<span class="hljs-number">10.934126853942871</span>, <span class="hljs-number">56057.14453125</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1jzj03s">ဒါတွေက loss နဲ့ perplexity အတွက် အလွန်မြင့်မားတဲ့ တန်ဖိုးတွေဖြစ်ပေမယ့်၊ model ကို မtrain ရသေးတာကြောင့် အံ့သြစရာ မဟုတ်ပါဘူး။ ဒါတွေနဲ့ training script ရဲ့ core အပိုင်းဖြစ်တဲ့ training loop ကို ရေးဖို့ အရာအားလုံး အဆင်သင့်ဖြစ်ပါပြီ။ training loop ထဲမှာ ကျွန်တော်တို့ dataloader ကို iterate လုပ်ပြီး batches တွေကို model ကို ပေးပို့ပါတယ်။ logits တွေနဲ့၊ ကျွန်တော်တို့ရဲ့ custom loss function ကို evaluate လုပ်နိုင်ပါတယ်။ gradient accumulation steps အရေအတွက်နဲ့ loss ကို scale လုပ်ပါတယ်၊ ဒါမှ steps တွေ ပိုများများ aggregate လုပ်တဲ့အခါ losses တွေ ပိုကြီးလာတာ မဖြစ်အောင်လို့ပါ။ optimize မလုပ်ခင်၊ ပိုမိုကောင်းမွန်တဲ့ convergence အတွက် gradients တွေကို clip လုပ်ပါတယ်။ နောက်ဆုံးအနေနဲ့၊ steps အနည်းငယ်တိုင်းမှာ evaluation set ပေါ်မှာ model ကို ကျွန်တော်တို့ရဲ့ <code>evaluate()</code> function အသစ်နဲ့ evaluate လုပ်ပါတယ်။</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 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=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> tqdm.notebook <span class="hljs-keyword">import</span> tqdm | |
| gradient_accumulation_steps = <span class="hljs-number">8</span> | |
| eval_steps = <span class="hljs-number">5_000</span> | |
| model.train() | |
| completed_steps = <span class="hljs-number">0</span> | |
| <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_train_epochs): | |
| <span class="hljs-keyword">for</span> step, batch <span class="hljs-keyword">in</span> tqdm( | |
| <span class="hljs-built_in">enumerate</span>(train_dataloader, start=<span class="hljs-number">1</span>), total=num_training_steps | |
| ): | |
| logits = model(batch[<span class="hljs-string">"input_ids"</span>]).logits | |
| loss = keytoken_weighted_loss(batch[<span class="hljs-string">"input_ids"</span>], logits, keytoken_ids) | |
| <span class="hljs-keyword">if</span> step % <span class="hljs-number">100</span> == <span class="hljs-number">0</span>: | |
| accelerator.<span class="hljs-built_in">print</span>( | |
| { | |
| <span class="hljs-string">"samples"</span>: step * samples_per_step, | |
| <span class="hljs-string">"steps"</span>: completed_steps, | |
| <span class="hljs-string">"loss/train"</span>: loss.item() * gradient_accumulation_steps, | |
| } | |
| ) | |
| loss = loss / gradient_accumulation_steps | |
| accelerator.backward(loss) | |
| <span class="hljs-keyword">if</span> step % gradient_accumulation_steps == <span class="hljs-number">0</span>: | |
| accelerator.clip_grad_norm_(model.parameters(), <span class="hljs-number">1.0</span>) | |
| optimizer.step() | |
| lr_scheduler.step() | |
| optimizer.zero_grad() | |
| completed_steps += <span class="hljs-number">1</span> | |
| <span class="hljs-keyword">if</span> (step % (eval_steps * gradient_accumulation_steps)) == <span class="hljs-number">0</span>: | |
| eval_loss, perplexity = evaluate() | |
| accelerator.<span class="hljs-built_in">print</span>({<span class="hljs-string">"loss/eval"</span>: eval_loss, <span class="hljs-string">"perplexity"</span>: perplexity}) | |
| model.train() | |
| accelerator.wait_for_everyone() | |
| unwrapped_model = accelerator.unwrap_model(model) | |
| unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) | |
| <span class="hljs-keyword">if</span> accelerator.is_main_process: | |
| tokenizer.save_pretrained(output_dir) | |
| repo.push_to_hub( | |
| commit_message=<span class="hljs-string">f"Training in progress step <span class="hljs-subst">{step}</span>"</span>, blocking=<span class="hljs-literal">False</span> | |
| )<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-kop1uq">ပြီးပါပြီ။ အခု သင်ကိုယ်ပိုင် causal language models တွေဖြစ်တဲ့ GPT-2 လို models တွေအတွက် custom training loop တစ်ခု ရရှိခဲ့ပါပြီ၊ ဒါကို သင့်လိုအပ်ချက်တွေအတိုင်း ထပ်မံ customize လုပ်နိုင်ပါတယ်။</p> <blockquote class="tip" data-svelte-h="svelte-rdt4av"><p>✏️ <strong>စမ်းသပ်ကြည့်ပါ။</strong> သင့် use case နဲ့ ကိုက်ညီတဲ့ သင့်ကိုယ်ပိုင် custom loss function တစ်ခု ဖန်တီးပါ၊ ဒါမှမဟုတ် training loop ထဲကို အခြား custom step တစ်ခု ထည့်ပါ။</p></blockquote> <blockquote class="tip" data-svelte-h="svelte-668ij0"><p>✏️ <strong>စမ်းသပ်ကြည့်ပါ။</strong> training experiments တွေကို အကြာကြီး run တဲ့အခါ TensorBoard ဒါမှမဟုတ် Weights & Biases လို tools တွေကို အသုံးပြုပြီး အရေးကြီးတဲ့ metrics တွေကို log လုပ်တာက ကောင်းမွန်တဲ့ အကြံဥာဏ်တစ်ခုပါ။ training ဘယ်လိုသွားနေလဲဆိုတာ အမြဲစစ်ဆေးနိုင်ဖို့ training loop ထဲကို သင့်လျော်တဲ့ logging ကို ထည့်ပါ။</p></blockquote> <h2 class="relative group"><a id="ဝဟရ-ရငလငခက-glossary" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#ဝဟရ-ရငလငခက-glossary"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>ဝေါဟာရ ရှင်းလင်းချက် (Glossary)</span></h2> <ul data-svelte-h="svelte-120wg91"><li><strong>Causal Language Model</strong>: စာသား sequence တစ်ခုရှိ token တစ်ခုစီ၏ နောက်ဆက်တွဲ token ကို ခန့်မှန်းရန် လေ့ကျင့်ထားသော language model အမျိုးအစား။ ၎င်းသည် auto-regressive model တစ်ခုဖြစ်ပြီး GPT-2, GPT-3 ကဲ့သို့ models တွေမှာ အသုံးပြုသည်။</li> <li><strong>From Scratch</strong>: Model (သို့မဟုတ် tokenizer) တစ်ခုကို မည်သည့် အစောပိုင်းလေ့ကျင့်မှုမျှ မရှိဘဲ လုံးဝအသစ်ကနေ စတင်တည်ဆောက်ခြင်းနှင့် လေ့ကျင့်ခြင်း။</li> <li><strong>Pretrained Models</strong>: အကြီးစား ဒေတာအမြောက်အမြားဖြင့် ကြိုတင်လေ့ကျင့်ထားပြီးဖြစ်သော AI (Artificial Intelligence) မော်ဒယ်။</li> <li><strong>Fine-tuning</strong>: ကြိုတင်လေ့ကျင့်ထားပြီးသား (pre-trained) မော်ဒယ်တစ်ခုကို သီးခြားလုပ်ငန်းတစ်ခု (specific task) အတွက် အနည်းငယ်သော ဒေတာနဲ့ ထပ်မံလေ့ကျင့်ပေးခြင်း။</li> <li><strong>Weights</strong>: Neural network model တစ်ခု၏ layers များကြားရှိ ဆက်သွယ်မှုများ၏ အားသာချက်များကို ကိုယ်စားပြုသော တန်ဖိုးများ။</li> <li><strong>Pretraining</strong>: Model ကို large-scale datasets များဖြင့် အစောပိုင်းလေ့ကျင့်ခြင်း လုပ်ငန်းစဉ်။</li> <li><strong>Transfer Learning</strong>: ကြိုတင်လေ့ကျင့်ထားသော model တစ်ခုမှ သင်ယူထားသော ဗဟုသုတများကို အခြားဆက်စပ်လုပ်ငန်းတစ်ခုသို့ လွှဲပြောင်းအသုံးပြုခြင်း။</li> <li><strong>Transformer Models</strong>: Natural Language Processing (NLP) မှာ အောင်မြင်မှုများစွာရရှိခဲ့တဲ့ deep learning architecture တစ်မျိုးပါ။</li> <li><strong>Labeled Data</strong>: အဖြေမှန်များ သို့မဟုတ် categories များနှင့် သက်ဆိုင်သော labels များ ပါဝင်သော data။</li> <li><strong>Sparse (Data)</strong>: အချက်အလက်နည်းပါးသော သို့မဟုတ် ပြည့်စုံမှုမရှိသော ဒေတာ။</li> <li><strong>Compute Resources</strong>: AI/ML models များကို လေ့ကျင့်ရန်နှင့် အသုံးပြုရန်အတွက် လိုအပ်သော ကွန်ပျူတာစွမ်းအား (ဥပမာ- CPUs, GPUs, TPUs)။</li> <li><strong>Language Model</strong>: လူသားဘာသာစကား၏ ဖြန့်ဝေမှုကို နားလည်ရန် လေ့ကျင့်ထားသော AI မော်ဒယ်တစ်ခု။</li> <li><strong>Musical Notes</strong>: ဂီတသံစဉ်များကို ကိုယ်စားပြုသော သင်္ကေတများ။</li> <li><strong>Molecular Sequences</strong>: DNA သို့မဟုတ် ပရိုတင်းများကဲ့သို့ မော်လီကျူးဖွဲ့စည်းပုံများ၏ အစီအစဉ်များ။</li> <li><strong>Programming Languages</strong>: ကွန်ပျူတာပရိုဂရမ်များကို ရေးသားရန်အတွက် အသုံးပြုသော ဘာသာစကားများ။</li> <li><strong>TabNine</strong>: AI-powered code completion tool။</li> <li><strong>GitHub Copilot</strong>: OpenAI ၏ Codex model က ပံ့ပိုးပေးသော AI-powered code completion tool။</li> <li><strong>OpenAI Codex Model</strong>: OpenAI မှ code generation အတွက် လေ့ကျင့်ထားသော LLM။</li> <li><strong>Text Generation</strong>: AI မော်ဒယ်များကို အသုံးပြု၍ လူသားကဲ့သို့သော စာသားအသစ်များ ဖန်တီးခြင်း။</li> <li><strong>Auto-regressive Models</strong>: input sequence ၏ ယခင် tokens များပေါ်တွင် အခြေခံပြီး နောက်ဆက်တွဲ token များကို တစ်ခုပြီးတစ်ခု ခန့်မှန်းသော model များ။</li> <li><strong>GPT-2</strong>: OpenAI မှ ထုတ်လုပ်ထားသော Transformer-based causal language model။</li> <li><strong>Scaled-down Version</strong>: မူရင်းစနစ် သို့မဟုတ် model ၏ အရွယ်အစား သို့မဟုတ် စွမ်းဆောင်ရည်ကို လျှော့ချထားသော version။</li> <li><strong>One-line Completions</strong>: code ၏ တစ်ကြောင်းတည်းသော အပိုင်းများကို အလိုအလျောက် ဖြည့်စွက်ပေးခြင်း။</li> <li><strong>Python Code</strong>: Python programming language ဖြင့် ရေးသားထားသော code များ။</li> <li><strong>Python Data Science Stack</strong>: Python တွင် data science လုပ်ငန်းများအတွက် အသုံးပြုသော libraries များ (ဥပမာ- matplotlib, seaborn, pandas, scikit-learn)။</li> <li><strong><code>matplotlib</code></strong>: Python အတွက် plotting library။</li> <li><strong><code>seaborn</code></strong>: <code>matplotlib</code> ပေါ်တွင် အခြေခံထားသော data visualization library။</li> <li><strong><code>pandas</code></strong>: Python အတွက် data analysis library။</li> <li><strong><code>scikit-learn</code></strong>: Python အတွက် machine learning library။</li> <li><strong>Tokenizer</strong>: စာသား (သို့မဟုတ် အခြားဒေတာ) ကို AI မော်ဒယ်များ စီမံဆောင်ရွက်နိုင်ရန် tokens တွေအဖြစ် ပိုင်းခြားပေးသည့် ကိရိယာ သို့မဟုတ် လုပ်ငန်းစဉ်။</li> <li><strong>Large-scale Dataset</strong>: အရွယ်အစား ကြီးမားသော dataset။</li> <li><strong>Corpus of Python Code</strong>: Python code များစွာ ပါဝင်သော စာသားအစုအဝေး။</li> <li><strong>GitHub Repositories</strong>: Git version control system ကို အသုံးပြု၍ code များကို သိမ်းဆည်းထားသော online repository များ။</li> <li><strong><code>Trainer</code> API</strong>: Hugging Face Transformers library မှ model များကို ထိရောက်စွာ လေ့ကျင့်ရန်အတွက် ဒီဇိုင်းထုတ်ထားသော မြင့်မားသောအဆင့် API။</li> <li><strong>🤗 Accelerate</strong>: Hugging Face က ထုတ်လုပ်ထားတဲ့ library တစ်ခုဖြစ်ပြီး PyTorch code တွေကို မတူညီတဲ့ training environment (ဥပမာ - GPU အများအပြား၊ distributed training) တွေမှာ အလွယ်တကူ run နိုင်အောင် ကူညီပေးပါတယ်။</li> <li><strong>Gradio App</strong>: Gradio library ကို အသုံးပြုပြီး တည်ဆောက်ထားသော machine learning model ၏ web demo။</li> <li><strong>Model Hub</strong>: Hugging Face Hub ကို ရည်ညွှန်းပြီး AI မော်ဒယ်များ ရှာဖွေ၊ မျှဝေ၊ အသုံးပြုနိုင်သော ဗဟို platform။</li> <li><strong>Scraping</strong>: ဝဘ်ဆိုဒ်များမှ အချက်အလက်များကို အလိုအလျောက် ထုတ်ယူခြင်း။</li> <li><strong>GitHub Dump</strong>: GitHub repositories များမှ data များကို စုဆောင်းထားသော အစုံလိုက် (bulk) ဒေတာ။</li> <li><strong><code>codeparrot</code></strong>: GitHub မှ Python code များဖြင့် ဖွဲ့စည်းထားသော dataset။</li> <li><strong>Streaming Feature</strong>: Dataset တစ်ခုလုံးကို memory ထဲသို့ load မလုပ်ဘဲ လိုအပ်သလို အပိုင်းလိုက် stream လုပ်ခြင်း။</li> <li><strong>On the Fly</strong>: လုပ်ငန်းစဉ်တစ်ခု ဖြစ်ပေါ်နေစဉ်အတွင်း ချက်ချင်း လုပ်ဆောင်ခြင်း။</li> <li><strong>Keywords</strong>: စာသားတစ်ခု၏ အဓိကအချက်အလက်ကို ကိုယ်စားပြုသော စကားလုံးများ။</li> <li><strong><code>defaultdict</code></strong>: <code>collections</code> module မှ dictionary subclass တစ်ခုဖြစ်ပြီး key တစ်ခု မရှိပါက default value တစ်ခုကို အလိုအလျောက် ဖန်တီးပေးသည်။</li> <li><strong><code>tqdm</code></strong>: Python library တစ်ခုဖြစ်ပြီး loops များအတွက် progress bar များကို လှပစွာ ဖော်ပြပေးသည်။</li> <li><strong><code>Dataset</code> (Hugging Face)</strong>: 🤗 Datasets library တွင် dataset တစ်ခုကို ကိုယ်စားပြုသော class။</li> <li><strong><code>filter_streaming_dataset()</code> Function</strong>: streaming dataset မှ အချို့သော အခြေအနေများနှင့် ကိုက်ညီသော data များကို filter လုပ်ရန် ဒီဇိုင်းထုတ်ထားသော function။</li> <li><strong><code>raw_datasets</code></strong>: preprocessing မလုပ်ရသေးသော dataset။</li> <li><strong>Python Scripts</strong>: Python programming language ဖြင့် ရေးသားထားသော code file များ။</li> <li><strong>Pretraining the Language Model</strong>: language model ကို ဒေတာများဖြင့် အစောပိုင်းလေ့ကျင့်ခြင်း။</li> <li><strong>Training Loop</strong>: model တစ်ခုကို လေ့ကျင့်ရန်အတွက် iterations များစွာဖြင့် လုပ်ဆောင်သော အဓိက code အပိုင်း။</li> <li><strong>Model Card</strong>: Hugging Face Hub တွင် မော်ဒယ်တစ်ခုစီအတွက် ပါရှိသော အချက်အလက်များပါသည့် စာမျက်နှာ။</li> <li><strong><code>content</code> Field</strong>: dataset မှ code content ပါဝင်သော field။</li> <li><strong>Truncating Inputs</strong>: input sequence ၏ အရှည်ကို သတ်မှတ်ထားသော maximum length သို့ လျှော့ချခြင်း။</li> <li><strong>Context Size</strong>: model တစ်ခုက prediction ပြုလုပ်ရာတွင် ထည့်သွင်းစဉ်းစားသော input sequence ၏ အရှည် (tokens အရေအတွက်)။</li> <li><strong>GPU Memory Footprint</strong>: GPU memory ပေါ်တွင် model တစ်ခုက သိမ်းပိုက်ထားသော နေရာ။</li> <li><strong>Tokens</strong>: စာသားကို ပိုင်းခြားထားသော အသေးငယ်ဆုံးယူနစ်များ။</li> <li><strong><code>return_overflowing_tokens</code> Option</strong>: tokenizer ကို input sequence ကို chunks များစွာအဖြစ် ပိုင်းခြားရန် ညွှန်ကြားသော option။</li> <li><strong><code>return_length</code> Option</strong>: tokenizer ကို ဖန်တီးထားသော chunk တစ်ခုစီ၏ length ကို ပြန်ပေးရန် ညွှန်ကြားသော option။</li> <li><strong>Padding Issues</strong>: batch အတွင်းရှိ sequences များကို အရှည်တူညီအောင် ဖြည့်စွက်ရာတွင် ဖြစ်ပေါ်နိုင်သော ပြဿနာများ။</li> <li><strong><code>overflow_to_sample_mapping</code> Field</strong>: chunk တစ်ခုစီသည် မူရင်း input sample မည်သည့်တစ်ခုနှင့် သက်ဆိုင်သည်ကို ဖော်ပြပေးသော field။</li> <li><strong><code>Dataset.map()</code> Function</strong>: 🤗 Datasets library မှာ ပါဝင်တဲ့ method တစ်ခုဖြစ်ပြီး dataset ရဲ့ element တစ်ခုစီ ဒါမှမဟုတ် batch တစ်ခုစီပေါ်မှာ function တစ်ခုကို အသုံးပြုနိုင်စေသည်။</li> <li><strong>One-to-one Maps</strong>: input element တစ်ခုစီအတွက် output element တစ်ခုသာ ထုတ်ပေးသော mapping။</li> <li><strong>Data Augmentation</strong>: datasets ၏ အရွယ်အစားကို တိုးမြှင့်ရန်အတွက် လက်ရှိ data မှ ပြောင်းလဲထားသော မိတ္တူအသစ်များ ဖန်တီးခြင်း။</li> <li><strong>Data Filtering</strong>: dataset မှ သတ်မှတ်ထားသော အခြေအနေများနှင့် ကိုက်ညီသော data များကို ရွေးထုတ်ခြင်း။</li> <li><strong>Conflicting Size</strong>: columns များ၏ အရွယ်အစားများ မကိုက်ညီခြင်း။</li> <li><strong><code>remove_columns</code> Argument</strong>: <code>Dataset.map()</code> method တွင် dataset မှ columns များကို ဖယ်ရှားရန် အသုံးပြုသော argument။</li> <li><strong><code>input_ids</code></strong>: Tokenizer မှ ထုတ်ပေးသော tokens တစ်ခုစီ၏ ထူးခြားသော ဂဏန်းဆိုင်ရာ ID များ။</li> <li><strong>GPT-3</strong>: OpenAI မှ ထုတ်လုပ်ထားသော အလွန်ကြီးမားသည့် Transformer-based causal language model။</li> <li><strong>Autocomplete Function</strong>: စာရိုက်နေစဉ် စကားလုံးများ သို့မဟုတ် code များကို အလိုအလျောက် ဖြည့်စွက်ပေးသော လုပ်ဆောင်ချက်။</li> <li><strong>Context Windows</strong>: model က prediction လုပ်ရာတွင် ထည့်သွင်းစဉ်းစားသော input ၏ အပိုင်းအစ။</li> <li><strong><code>eos_token_id</code> Token</strong>: sequence ၏ အဆုံးကို ကိုယ်စားပြုသော end-of-sequence token ၏ ID။</li> <li><strong>Concatenated Sequences</strong>: sequences များစွာကို တစ်ခုတည်းအဖြစ် ပေါင်းစပ်ထားခြင်း။</li> <li><strong><code>truncation=False</code></strong>: tokenizer ကို input sequence ကို truncate မလုပ်ရန် ညွှန်ကြားခြင်း။</li> <li><strong><code>AutoTokenizer</code></strong>: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး မော်ဒယ်အမည်ကို အသုံးပြုပြီး သက်ဆိုင်ရာ tokenizer ကို အလိုအလျောက် load လုပ်ပေးသည်။</li> <li><strong><code>GPT2LMHeadModel</code></strong>: GPT-2 model ၏ causal language modeling head ပါဝင်သော class။</li> <li><strong><code>AutoConfig</code></strong>: Hugging Face Transformers library မှာ ပါဝင်တဲ့ class တစ်ခုဖြစ်ပြီး model အမည်ကို အသုံးပြုပြီး သက်ဆိုင်ရာ configuration ကို အလိုအလျောက် load လုပ်ပေးသည်။</li> <li><strong><code>vocab_size</code></strong>: tokenizer ၏ vocabulary အတွင်းရှိ ထူးခြားသော tokens အရေအတွက်။</li> <li><strong><code>n_ctx</code></strong>: context window ၏ အများဆုံး tokens အရေအတွက် (sequence length)။</li> <li><strong><code>bos_token_id</code></strong>: beginning-of-sequence token ၏ ID။</li> <li><strong><code>eos_token_id</code></strong>: end-of-sequence token ၏ ID။</li> <li><strong><code>from_pretrained()</code> Function</strong>: pretrained model သို့မဟုတ် tokenizer ကို load လုပ်ရန် အသုံးပြုသော function။</li> <li><strong><code>model.parameters()</code></strong>: model ၏ လေ့ကျင့်နိုင်သော parameters (weights နှင့် biases) များကို ပြန်ပေးသော method။</li> <li><strong>Parameters (Model)</strong>: model ၏ လုပ်ဆောင်ချက်ကို သတ်မှတ်ပေးသော အတွင်းပိုင်းတန်ဖိုးများ (weights, biases)။</li> <li><strong><code>TFGPT2LMHeadModel</code></strong>: TensorFlow အတွက် GPT-2 model ၏ causal language modeling head ပါဝင်သော class။</li> <li><strong><code>model.dummy_inputs</code></strong>: model ကို build လုပ်ရန်အတွက် အသုံးပြုသော dummy inputs များ။</li> <li><strong><code>model.summary()</code></strong>: TensorFlow Keras model ၏ layers များ၊ output shapes များ၊ parameter အရေအတွက်တို့ကို ဖော်ပြပေးသော method။</li> <li><strong>Data Collator</strong>: Dataloader မှ ရရှိသော samples များကို batch တစ်ခုအဖြစ် စုစည်းပေးသော function သို့မဟုတ် class။</li> <li><strong><code>DataCollatorForLanguageModeling</code></strong>: language modeling tasks များအတွက် batch များကို ဖန်တီးပေးရန် Hugging Face Transformers library မှ class။</li> <li><strong>Stacking</strong>: multiple tensors များကို တန်ဖိုးအသစ်တစ်ခုအဖြစ် ပေါင်းစပ်ခြင်း။</li> <li><strong>Padding</strong>: batch အတွင်းရှိ sequences များကို အရှည်တူညီအောင် ဖြည့်စွက်ခြင်း။</li> <li><strong>Language Model Labels</strong>: language model training တွင် input ၏ နောက်ဆက်တွဲ token အဖြစ် အသုံးပြုသော target labels များ။</li> <li><strong>Masked Language Modeling (MLM)</strong>: စာကြောင်းတစ်ခုထဲမှ စကားလုံးအချို့ကို ဝှက်ထားပြီး ၎င်းတို့ကို ခန့်မှန်းစေခြင်းဖြင့် model ကို လေ့ကျင့်သော task။</li> <li><strong>Causal Language Modeling (CLM)</strong>: စာကြောင်းတစ်ခု၏ နောက်ဆက်တွဲ token ကို ခန့်မှန်းခြင်းဖြင့် model ကို လေ့ကျင့်သော task။</li> <li><strong><code>mlm=False</code> Argument</strong>: <code>DataCollatorForLanguageModeling</code> ကို causal language modeling အတွက် data ပြင်ဆင်ရန် ညွှန်ကြားသော argument။</li> <li><strong><code>return_tensors="tf"</code></strong>: TensorFlow tensors များကို ပြန်ပေးရန် သတ်မှတ်ခြင်း။</li> <li><strong>Tensor Shapes</strong>: tensor တစ်ခု၏ dimensions များ၏ အရွယ်အစား။</li> <li><strong><code>prepare_tf_dataset()</code> Method</strong>: Keras model ၏ method တစ်ခုဖြစ်ပြီး 🤗 Datasets dataset ကို TensorFlow dataset သို့ ပြောင်းလဲပေးသည်။</li> <li><strong><code>collate_fn</code></strong>: <code>prepare_tf_dataset()</code> တွင် data collator ကို သတ်မှတ်ရန် argument။</li> <li><strong><code>shuffle=True</code></strong>: dataset ကို shuffle လုပ်ရန် ညွှန်ကြားခြင်း။</li> <li><strong><code>batch_size</code></strong>: training သို့မဟုတ် evaluation အတွက် တစ်ပြိုင်နက်တည်း လုပ်ဆောင်မည့် samples အရေအတွက်။</li> <li><strong><code>labels</code></strong>: model training အတွက် အဖြေမှန်များ။</li> <li><strong><code>huggingface_hub.notebook_login()</code></strong>: Jupyter/Colab Notebooks များတွင် Hugging Face Hub သို့ login ဝင်ရန် အသုံးပြုသော function။</li> <li><strong>Widget</strong>: Graphical User Interface (GUI) တွင် အသုံးပြုသူနှင့် အပြန်အလှန်တုံ့ပြန်နိုင်သော အစိတ်အပိုင်းများ။</li> <li><strong><code>huggingface-cli login</code></strong>: Hugging Face CLI (Command Line Interface) မှ Hugging Face Hub သို့ login ဝင်ရန် အသုံးပြုသော command။</li> <li><strong>Training Arguments</strong>: <code>Trainer</code> API တွင် training process ကို configure လုပ်ရန် အသုံးပြုသော parameters များ။</li> <li><strong><code>Trainer</code></strong>: Hugging Face Transformers library မှ model များကို လေ့ကျင့်ရန်အတွက် မြင့်မားသောအဆင့် (high-level) API။</li> <li><strong>Cosine Learning Rate Schedule</strong>: learning rate ကို cosine function ပုံစံဖြင့် အချိန်ကြာလာသည်နှင့်အမျှ လျှော့ချသော schedule။</li> <li><strong>Warmup</strong>: training အစပိုင်းတွင် learning rate ကို တဖြည်းဖြည်းတိုးမြှင့်သော နည်းလမ်း။</li> <li><strong>Effective Batch Size</strong>: <code>per_device_train_batch_size</code> ကို <code>gradient_accumulation_steps</code> ဖြင့် မြှောက်၍ ရရှိသော စုစုပေါင်း batch size။</li> <li><strong><code>per_device_train_batch_size</code></strong>: GPU သို့မဟုတ် TPU တစ်ခုစီတွင် training အတွက် အသုံးပြုသော batch size။</li> <li><strong><code>gradient_accumulation_steps</code></strong>: gradient ကို update မလုပ်ခင် gradient များကို စုဆောင်းမည့် step အရေအတွက်။</li> <li><strong>Gradient Accumulation</strong>: GPU memory ကန့်သတ်ချက်များကြောင့် batch size ကြီးကြီးမားမား အသုံးပြု၍မရသောအခါ gradient များကို batch များစွာမှ စုဆောင်းပြီး တစ်ကြိမ်တည်း update လုပ်သော နည်းလမ်း။</li> <li><strong>Forward/Backward Passes</strong>: neural network တွင် input data ကို ဖြတ်သန်းစေပြီး output ကို တွက်ချက်ခြင်း (forward pass) နှင့် loss function မှ gradient များကို တွက်ချက်ခြင်း (backward pass)။</li> <li><strong><code>output_dir</code></strong>: trained model, logs, checkpoints တို့ကို သိမ်းဆည်းမည့် directory။</li> <li><strong><code>per_device_eval_batch_size</code></strong>: GPU သို့မဟုတ် TPU တစ်ခုစီတွင် evaluation အတွက် အသုံးပြုသော batch size။</li> <li><strong><code>evaluation_strategy="steps"</code></strong>: သတ်မှတ်ထားသော steps အရေအတွက်အလိုက် evaluation လုပ်ရန် ညွှန်ကြားခြင်း။</li> <li><strong><code>eval_steps</code></strong>: evaluation လုပ်မည့် steps အရေအတွက်။</li> <li><strong><code>logging_steps</code></strong>: logs များကို မှတ်တမ်းတင်မည့် steps အရေအတွက်။</li> <li><strong><code>num_train_epochs</code></strong>: training dataset တစ်ခုလုံးဖြင့် model ကို လေ့ကျင့်မည့် အကြိမ်အရေအတွက် (epochs)။</li> <li><strong><code>weight_decay</code></strong>: overfitting ကို လျှော့ချရန်အတွက် model ၏ weights များကို ထိန်းချုပ်သော regularization technique။</li> <li><strong><code>warmup_steps</code></strong>: warmup လုပ်မည့် steps အရေအတွက်။</li> <li><strong><code>lr_scheduler_type="cosine"</code></strong>: learning rate scheduler အမျိုးအစားကို cosine အဖြစ် သတ်မှတ်ခြင်း။</li> <li><strong><code>learning_rate</code></strong>: training လုပ်ငန်းစဉ်အတွင်း model ၏ weights များကို မည်မျှပြောင်းလဲရမည်ကို ထိန်းချုပ်သော parameter။</li> <li><strong><code>save_steps</code></strong>: model ကို သိမ်းဆည်းမည့် steps အရေအတွက်။</li> <li><strong><code>fp16=True</code></strong>: training ကို float16 (half-precision) ဖြင့် လုပ်ဆောင်ရန် ညွှန်ကြားခြင်း (memory နှင့် speed ကို တိုးတက်စေသည်)။</li> <li><strong><code>push_to_hub=True</code></strong>: training ပြီးဆုံးပြီးနောက် model ကို Hugging Face Hub သို့ push လုပ်ရန် ညွှန်ကြားခြင်း။</li> <li><strong><code>eval_dataset</code></strong>: evaluation အတွက် အသုံးပြုမည့် dataset။</li> <li><strong><code>trainer.train()</code></strong>: <code>Trainer</code> ကို အသုံးပြု၍ model ကို train လုပ်ရန်။</li> <li><strong><code>trainer.push_to_hub()</code></strong>: <code>Trainer</code> ကို အသုံးပြု၍ model နှင့် tokenizer ကို Hub သို့ push လုပ်ရန်။</li> <li><strong>Training Hyperparameters</strong>: training process ကို ထိန်းချုပ်သော parameters များ (ဥပမာ- learning rate, batch size)။</li> <li><strong><code>compile()</code> Method</strong>: Keras model ကို training အတွက် ပြင်ဆင်ရန် (optimizer, loss function စသည်)။</li> <li><strong><code>fit()</code> Method</strong>: Keras model ကို train လုပ်ရန်။</li> <li><strong><code>create_optimizer()</code></strong>: Hugging Face Transformers library မှ optimizer နှင့် learning rate schedule ကို ဖန်တီးသော function။</li> <li><strong><code>init_lr</code></strong>: စတင် learning rate။</li> <li><strong><code>num_warmup_steps</code></strong>: warmup steps အရေအတွက်။</li> <li><strong><code>num_train_steps</code></strong>: စုစုပေါင်း training steps အရေအတွက်။</li> <li><strong><code>weight_decay_rate</code></strong>: weight decay ၏နှုန်း။</li> <li><strong><code>tf.keras.mixed_precision.set_global_policy("mixed_float16")</code></strong>: TensorFlow Keras တွင် mixed-precision (float16) training ကို ဖွင့်ရန်။</li> <li><strong><code>tf_train_dataset</code></strong>: TensorFlow format ဖြင့် training dataset။</li> <li><strong><code>tf_eval_dataset</code></strong>: TensorFlow format ဖြင့် evaluation dataset။</li> <li><strong><code>PushToHubCallback</code></strong>: Keras model ကို training လုပ်နေစဉ် Hugging Face Hub သို့ update လုပ်ရန် Hugging Face Transformers library မှ callback။</li> <li><strong><code>output_dir</code></strong>: trained model, logs, checkpoints တို့ကို သိမ်းဆည်းမည့် directory။</li> <li><strong><code>callbacks</code></strong>: Keras <code>fit()</code> method တွင် training လုပ်နေစဉ် လုပ်ဆောင်ချက်များ (ဥပမာ- logging, saving) ကို ထည့်သွင်းရန် argument။</li> <li><strong><code>model.fit()</code></strong>: Keras model ကို train လုပ်ရန်။</li> <li><strong>Multiple GPUs</strong>: ကွန်ပျူတာစနစ်တစ်ခုတွင် GPUs များစွာ အသုံးပြုခြင်း။</li> <li><strong><code>MirroredStrategy</code></strong>: TensorFlow ၏ distributed training strategy တစ်ခုဖြစ်ပြီး multiple GPUs ပေါ်တွင် data-parallel training ကို လုပ်ဆောင်ရန်။</li> <li><strong><code>scope()</code> Context</strong>: TensorFlow distributed training strategy ၏ context manager တစ်ခုဖြစ်ပြီး၊ ၎င်းအတွင်းရှိ code ကို strategy ၏ စည်းမျဉ်းများအတိုင်း လုပ်ဆောင်စေသည်။</li> <li><strong><code>text-generation</code> Pipeline</strong>: text generation task အတွက် ဒီဇိုင်းထုတ်ထားသော pipeline။</li> <li><strong>GPU (Graphics Processing Unit)</strong>: ဂရပ်ဖစ်လုပ်ဆောင်မှုအတွက် အထူးဒီဇိုင်းထုတ်ထားသော processor တစ်မျိုးဖြစ်သော်လည်း AI/ML လုပ်ငန်းများတွင် အရှိန်မြှင့်ရန် အသုံးများသည်။</li> <li><strong><code>torch.device("cuda")</code></strong>: PyTorch တွင် CUDA (GPU) device ကို သတ်မှတ်ခြင်း။</li> <li><strong><code>torch.device("cpu")</code></strong>: PyTorch တွင် CPU device ကို သတ်မှတ်ခြင်း။</li> <li><strong><code>pipeline()</code> Function</strong>: Hugging Face Transformers library မှာ ပါဝင်တဲ့ လုပ်ဆောင်ချက်တစ်ခုဖြစ်ပြီး မော်ဒယ်တွေကို သီးခြားလုပ်ငန်းတာဝန်များ (ဥပမာ- စာသားခွဲခြားသတ်မှတ်ခြင်း၊ စာသားထုတ်လုပ်ခြင်း) အတွက် အသုံးပြုရလွယ်ကူအောင် ပြုလုပ်ပေးပါတယ်။</li> <li><strong><code>num_return_sequences</code></strong>: generate လုပ်မည့် sequences အရေအတွက်။</li> <li><strong><code>generated_text</code></strong>: model မှ generate လုပ်ထားသော စာသား။</li> <li><strong>Scatter Plot</strong>: data points များကို x-axis နှင့် y-axis ပေါ်တွင် ပြသသော graph အမျိုးအစား။</li> <li><strong><code>np.random.randn()</code></strong>: NumPy မှ standard normal distribution မှ ကျပန်းနံပါတ်များ ထုတ်လုပ်သော function။</li> <li><strong><code>plt.scatter(x, y)</code></strong>: Matplotlib မှ scatter plot ကို ဖန်တီးသော function။</li> <li><strong><code>pd.DataFrame</code></strong>: Pandas မှ DataFrame object ကို ဖန်တီးသော class။</li> <li><strong><code>df.insert()</code></strong>: Pandas DataFrame တွင် column အသစ်တစ်ခု ထည့်သွင်းသော method။</li> <li><strong><code>groupby()</code> Operation</strong>: Pandas DataFrame တွင် columns များကို အုပ်စုဖွဲ့ပြီး aggregate လုပ်ဆောင်ချက်များ လုပ်ဆောင်သော method။</li> <li><strong><code>sklearn.ensemble.RandomForestRegressor</code></strong>: scikit-learn မှ Random Forest Regressor model class။</li> <li><strong><code>n_estimators</code></strong>: Random Forest model တွင် အသုံးပြုမည့် trees အရေအတွက်။</li> <li><strong><code>random_state</code></strong>: ကျပန်းနံပါတ် generator အတွက် seed။</li> <li><strong><code>max_depth</code></strong>: tree ၏ အများဆုံး အနက်။</li> <li><strong><code>rf.fit(X, y)</code></strong>: Random Forest model ကို data <code>X</code> နှင့် <code>y</code> ဖြင့် train လုပ်ရန်။</li> <li><strong>Syntax (Python)</strong>: Python programming language ၏ စည်းမျဉ်းများ။</li> <li><strong>Prototype</strong>: ထုတ်ကုန်တစ်ခု၏ ကနဦးမူကြမ်း သို့မဟုတ် စမ်းသပ်ဗားရှင်း။</li> <li><strong>Conditional Training Loop</strong>: သတ်မှတ်ထားသော အခြေအနေများပေါ်မူတည်၍ training steps များကို ကျော်သွားနိုင်သော training loop။</li> <li><strong>Subclass</strong>: လက်ရှိ class တစ်ခုမှ အင်္ဂါရပ်များကို အမွေခံယူပြီး ပြင်ဆင်နိုင်သော class အသစ်တစ်ခု ဖန်တီးခြင်း။</li> <li><strong>Exotic Changes</strong>: ပုံမှန်မဟုတ်သော သို့မဟုတ် ထူးခြားသော ပြောင်းလဲမှုများ။</li> <li><strong>Weighted Loss Function</strong>: data points အချို့ကို အခြား data points များထက် ပိုအလေးထားရန် weights များကို အသုံးပြုသော loss function။</li> <li><strong><code>plt</code> (matplotlib.pyplot)</strong>: Matplotlib library ၏ commonly imported alias။</li> <li><strong><code>pd</code> (pandas)</strong>: Pandas library ၏ commonly imported alias။</li> <li><strong><code>sk</code> (sklearn)</strong>: scikit-learn library ၏ commonly imported alias (တခါတလေ သုံးသည်)။</li> <li><strong><code>fit</code> / <code>predict</code> Pattern</strong>: machine learning model များကို train လုပ်ရန် <code>fit()</code> method နှင့် ခန့်မှန်းချက်များ ပြုလုပ်ရန် <code>predict()</code> method ကို အသုံးပြုသော ပုံစံ။</li> <li><strong>Whitespace Prefix</strong>: token ၏ အရှေ့တွင် ပါဝင်သော space character။</li> <li><strong>Tokenizer Vocabulary</strong>: tokenizer သိရှိသော ထူးခြားသော tokens များစာရင်း။</li> <li><strong><code>tokenizer([keyword]).input_ids[0]</code></strong>: keyword ကို tokenize လုပ်ပြီး ၎င်း၏ input IDs များကို ရယူခြင်း။</li> <li><strong><code>CrossEntropyLoss</code></strong>: PyTorch တွင် classification tasks အတွက် အသုံးများသော loss function။</li> <li><strong><code>torch</code></strong>: PyTorch library။</li> <li><strong><code>inputs[..., 1:].contiguous()</code></strong>: input tensor ၏ ဒုတိယ element မှ စတင်၍ slice လုပ်ပြီး memory တွင် ဆက်စပ်နေစေရန်။</li> <li><strong><code>logits[..., :-1, :].contiguous()</code></strong>: logits tensor ၏ နောက်ဆုံး element ကို ဖယ်ရှားပြီး memory တွင် ဆက်စပ်နေစေရန်။</li> <li><strong><code>loss_fct = CrossEntropyLoss(reduce=False)</code></strong>: per-token loss များကို ပြန်ပေးရန် <code>reduce=False</code> ဖြင့် CrossEntropyLoss ကို ဖန်တီးခြင်း။</li> <li><strong><code>loss.view(-1, shift_logits.size(1)).mean(axis=1)</code></strong>: per-sample loss များကို တွက်ချက်ခြင်း။</li> <li><strong><code>torch.stack()</code></strong>: tensors များစွာကို အတူတကွ stacking လုပ်ခြင်း။</li> <li><strong><code>torch.nn.CrossEntropyLoss</code></strong>: PyTorch တွင် classification tasks အတွက် အသုံးများသော loss function။</li> <li><strong><code>torch.optim.AdamW</code></strong>: PyTorch မှာ အသုံးပြုတဲ့ AdamW optimizer။</li> <li><strong>Dataloaders</strong>: batch အလိုက် data ကို load လုပ်ပေးသော PyTorch utility။</li> <li><strong>Weight Decay Parameters</strong>: model ၏ weights များကို ထိန်းချုပ်ရန် weight decay ကို အသုံးပြုသော parameters များ။</li> <li><strong>Evaluation Code</strong>: model ၏ စွမ်းဆောင်ရည်ကို တိုင်းတာရန်အတွက် code။</li> <li><strong><code>tokenized_datasets.set_format("torch")</code></strong>: dataset ၏ format ကို PyTorch tensors အဖြစ် သတ်မှတ်ခြင်း။</li> <li><strong><code>torch.utils.data.dataloader.DataLoader</code></strong>: PyTorch တွင် dataset မှ batch အလိုက် data များကို load လုပ်ပေးသော class။</li> <li><strong><code>get_grouped_params()</code> Function</strong>: model ၏ parameters များကို weight decay လုပ်ရန် သို့မဟုတ် မလုပ်ရန် ခွဲခြားပေးသော function။</li> <li><strong><code>model.named_parameters()</code></strong>: model ၏ parameters များနှင့် ၎င်းတို့၏ နာမည်များကို ပြန်ပေးသော method။</li> <li><strong><code>model.eval()</code></strong>: model ကို evaluation mode သို့ ပြောင်းလဲခြင်း။</li> <li><strong><code>torch.no_grad()</code></strong>: gradient တွက်ချက်မှုကို ပိတ်ထားရန် PyTorch context manager။</li> <li><strong><code>accelerator.gather()</code></strong>: multiple processes မှ tensors များကို စုစည်းရန် 🤗 Accelerate method။</li> <li><strong><code>torch.cat()</code></strong>: multiple tensors များကို တစ်ခုတည်းအဖြစ် ပေါင်းစပ်ရန်။</li> <li><strong><code>torch.exp()</code></strong>: tensor ၏ element တစ်ခုစီ၏ exponential ကို တွက်ချက်ရန်။</li> <li><strong>OverflowError</strong>: Python တွင် ဂဏန်းတန်ဖိုးများ အလွန်ကြီးမားသောအခါ ဖြစ်ပေါ်သော error။</li> <li><strong>Perplexity</strong>: language model တစ်ခု၏ စွမ်းဆောင်ရည်ကို တိုင်းတာသော metric (ပိုနည်းလေ ပိုကောင်းလေ)။</li> <li><strong><code>accelerator.prepare()</code></strong>: 🤗 Accelerate တွင် model, optimizer, dataloaders များကို distributed training အတွက် ပြင်ဆင်ရန် method။</li> <li><strong>TPU (Tensor Processing Unit)</strong>: Google မှ AI/ML workloads များအတွက် အထူးဒီဇိုင်းထုတ်ထားသော processor တစ်မျိုး။</li> <li><strong>Dedicated Training Function</strong>: training logic အားလုံးပါဝင်သော သီးခြား function။</li> <li><strong>Linear Schedule</strong>: learning rate ကို linear ပုံစံဖြင့် အချိန်ကြာလာသည်နှင့်အမျှ လျှော့ချသော schedule။</li> <li><strong><code>get_scheduler()</code></strong>: Hugging Face Transformers library မှ learning rate scheduler ကို ရယူသော function။</li> <li><strong><code>Repository</code> Object</strong>: <code>huggingface_hub</code> library မှ Git repository များကို ကိုင်တွယ်ရန်အတွက် object။</li> <li><strong>Working Folder</strong>: project files များကို သိမ်းဆည်းထားသော folder။</li> <li><strong><code>get_full_repo_name()</code> Function</strong>: Hugging Face Hub ပေါ်တွင် repository ၏ full name (username/repo_name) ကို ရယူသော function။</li> <li><strong><code>clone_from</code> Argument</strong>: <code>Repository</code> object ကို ဖန်တီးသောအခါ remote repository မှ clone လုပ်ရန် ညွှန်ကြားသော argument။</li> <li><strong><code>repo.push_to_hub()</code> Method</strong>: local repository ၏ ပြောင်းလဲမှုများကို Hugging Face Hub သို့ push လုပ်ရန်။</li> <li><strong>Intermediate Models</strong>: training လုပ်နေစဉ် သိမ်းဆည်းထားသော model versions များ။</li> <li><strong>Perplexity</strong>: language model တစ်ခု၏ စွမ်းဆောင်ရည်ကို တိုင်းတာသော metric (ပိုနည်းလေ ပိုကောင်းလေ)။</li> <li><strong><code>model.train()</code></strong>: model ကို training mode သို့ ပြောင်းလဲခြင်း။</li> <li><strong><code>tqdm.notebook</code></strong>: Jupyter Notebooks အတွက် <code>tqdm</code> progress bar။</li> <li><strong><code>enumerate()</code></strong>: list သို့မဟုတ် iterable တစ်ခု၏ element တစ်ခုစီကို index နှင့်အတူ ပြန်ပေးသော Python function။</li> <li><strong><code>accelerator.print()</code></strong>: distributed training ပတ်ဝန်းကျင်တွင် logs များကို print ထုတ်ရန် 🤗 Accelerate utility။</li> <li><strong><code>loss.item()</code></strong>: PyTorch tensor မှ Python float တန်ဖိုးကို ရယူခြင်း။</li> <li><strong><code>accelerator.backward(loss)</code></strong>: distributed training တွင် loss မှ gradients များကို တွက်ချက်ရန် 🤗 Accelerate method။</li> <li><strong><code>accelerator.clip_grad_norm_()</code></strong>: gradients များကို clip လုပ်ရန် 🤗 Accelerate method။</li> <li><strong><code>optimizer.step()</code></strong>: တွက်ချက်ထားသော gradients များကို အသုံးပြုပြီး model ၏ parameters များကို update လုပ်သော optimizer method။</li> <li><strong><code>lr_scheduler.step()</code></strong>: learning rate scheduler ကို တစ်ဆင့်ချင်းစီ update လုပ်ရန်။</li> <li><strong><code>optimizer.zero_grad()</code></strong>: gradients များကို သုညသို့ ပြန်လည်သတ်မှတ်ရန်။</li> <li><strong><code>accelerator.wait_for_everyone()</code></strong>: distributed training တွင် processes အားလုံး အမှတ်တစ်ခုသို့ ရောက်သည်အထိ စောင့်ဆိုင်းရန် 🤗 Accelerate method။</li> <li><strong><code>accelerator.unwrap_model()</code></strong>: distributed training အတွက် wrap လုပ်ထားသော model မှ မူရင်း model ကို ပြန်လည်ရယူရန် 🤗 Accelerate method။</li> <li><strong><code>unwrapped_model.save_pretrained()</code></strong>: model ကို pretrained format ဖြင့် disk ပေါ်တွင် သိမ်းဆည်းရန်။</li> <li><strong><code>accelerator.save()</code></strong>: distributed training တွင် model ကို သိမ်းဆည်းရန် 🤗 Accelerate utility။</li> <li><strong><code>accelerator.is_main_process</code></strong>: လက်ရှိ process က main process ဟုတ်မဟုတ် စစ်ဆေးရန် 🤗 Accelerate property။</li> <li><strong><code>tokenizer.save_pretrained()</code></strong>: tokenizer ကို disk ပေါ်တွင် သိမ်းဆည်းရန်။</li> <li><strong><code>commit_message</code></strong>: Git commit အတွက် ပေးသော မက်ဆေ့ချ်။</li> <li><strong><code>blocking=False</code></strong>: <code>push_to_hub()</code> ကို asynchronous အဖြစ် run ရန် ညွှန်ကြားခြင်း (main thread ကို မပိတ်မိစေရန်)။</li> <li><strong>TensorBoard</strong>: Google မှ ဖန်တီးထားသော machine learning experiments များကို visualization လုပ်ရန် ကိရိယာ။</li> <li><strong>Weights & Biases</strong>: machine learning experiments များကို ခြေရာခံ၊ visualization လုပ်ရန်နှင့် ပူးပေါင်းဆောင်ရွက်ရန် platform။</li></ul> <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/my/chapter7/6.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 data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p> | |
| <script> | |
| { | |
| __sveltekit_5q47hu = { | |
| assets: "/docs/course/pr_1095/my", | |
| base: "/docs/course/pr_1095/my", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/course/pr_1095/my/_app/immutable/entry/start.8e25cab6.js"), | |
| import("/docs/course/pr_1095/my/_app/immutable/entry/app.b12ce275.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 82], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 214 kB
- Xet hash:
- 5aeb2695ea8e63dcac4cb4c2c5951c1afb01e5d40ad0eef20f9bf02800b5c0ee
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.