Buckets:

hf-doc-build/doc / transformers /main /vi /training.html
mishig's picture
download
raw
36.5 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Tinh chỉnh&quot;,&quot;local&quot;:&quot;fine-tuning&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Tokenization&quot;,&quot;local&quot;:&quot;tokenization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Tải một mô hình&quot;,&quot;local&quot;:&quot;loading-a-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Cấu hình huấn luyện&quot;,&quot;local&quot;:&quot;training-configuration&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Huấn luyện&quot;,&quot;local&quot;:&quot;training&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Các bước tiếp theo&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DSYYoqAA.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BovLmb3z.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/WyYpyc-L.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DYBfcveH.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/0.ByO_lNbj.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/ZtBhN-Ei.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/780.CXFrB0Bs.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BDdFI6Uf.js" rel="modulepreload">
<!--d816z7--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Fine-tuning&quot;,&quot;local&quot;:&quot;fine-tuning&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Tokenization&quot;,&quot;local&quot;:&quot;tokenization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Loading a model&quot;,&quot;local&quot;:&quot;loading-a-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Training configuration&quot;,&quot;local&quot;:&quot;training-configuration&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Training&quot;,&quot;local&quot;:&quot;training&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="fine-tuning" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Tinh chỉnh<!----></span></h1><!--]--><!----> <p>Fine-tuning tiếp tục huấn luyện một mô hình lớn đã được huấn luyện trước trên một tập dữ liệu nhỏ hơn, đặc thù cho một tác vụ hoặc lĩnh vực. Ví dụ, fine-tuning trên một tập dữ liệu gồm các ví dụ về lập trình giúp mô hình trở nên giỏi hơn trong việc lập trình. Fine-tuning giống hệt với pretraining, ngoại trừ việc bạn không bắt đầu với các trọng số ngẫu nhiên. Nó cũng đòi hỏi ít tài nguyên tính toán, dữ liệu và thời gian hơn rất nhiều.</p> <p>Hướng dẫn bên dưới đi qua các bước tinh chỉnh một mô hình ngôn ngữ lớn bằng <a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer">Trainer</a>.</p> <div class="my-6 overflow-hidden rounded-xl border border-gray-200 dark:border-gray-850"><div class="flex items-center justify-between gap-2 border-b border-gray-200 bg-linear-to-t from-gray-100-to-white px-3 py-1.5 dark:border-gray-850"><span class="flex items-center gap-1.5 text-sm font-semibold text-gray-800 dark:text-gray-200"><svg class="text-blue-500" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 12 12" fill="currentColor"><rect width="9.491" height="2.438" x="1.254" y="8.845" opacity=".5" rx="1.219"></rect><path d="M8.068 3.07a2.678 2.678 0 0 1 0 5.355H3.932a2.678 2.678 0 0 1 0-5.355h1.652v.412a.343.343 0 0 0 .684 0V3.07zM4.412 4.85a.898.898 0 1 0 0 1.796.898.898 0 0 0 0-1.795m3.177 0a.898.898 0 1 0 0 1.796.898.898 0 0 0 0-1.795"></path><path d="M6.269 3.48a.342.342 0 0 1-.684 0V2.353a.9.9 0 0 0 .684 0z" opacity=".25"></path><circle cx="5.927" cy="1.526" r=".894"></circle></svg><!----> Agent prompt</span> <button type="button" title="Copy agent prompt" aria-live="polite" class="inline-flex items-center gap-1 h-7 px-2 text-sm font-medium text-gray-800 border rounded-md 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"><svg class="size-3.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><!----> Copy</button></div> <div class="relative"><div class="prose max-w-none px-4 py-3 "><p>Hỗ trợ người dùng lập kế hoạch cho một phiên fine-tuning cho tác vụ của họ. Trước khi đề xuất một cấu hình, hãy xác định tác vụ, mô hình pretrained và revision, các trường dữ liệu của dataset, dữ liệu đánh giá, phiên bản Transformers, phần cứng và bộ nhớ khả dụng. Sử dụng hướng dẫn này để kiểm tra việc chuẩn bị dữ liệu, tải mô hình và thiết lập <code>Trainer</code> áp dụng cho trường hợp của họ. Không giả định rằng dataset hoặc mô hình trong ví dụ phù hợp với một tác vụ khác.</p> <ol><li>Yêu cầu các chi tiết còn thiếu có thể làm thay đổi mô hình, tiền xử lý hoặc cấu hình huấn luyện. Nêu rõ bất kỳ giả định nào.</li> <li>Giữ các ví dụ đánh giá tách biệt khỏi dữ liệu huấn luyện. Chọn một mô hình và tokenizer cho tác vụ, chuẩn bị các đầu vào và nhãn mong đợi của mô hình, và chọn một data collator phù hợp với các đầu vào đó.</li> <li>Đặt <code>TrainingArguments</code> cho phù hợp với phần cứng và mục tiêu của người dùng. Cấu hình đánh giá và lưu checkpoint khi người dùng cần. Chạy một bài kiểm tra nhỏ trước khi thực hiện công việc huấn luyện đầy đủ, và kiểm tra rằng mô hình nhận được các đầu vào như mong đợi, loss hữu hạn, và quá trình đánh giá hoàn tất.</li> <li>Không bắt đầu một lượt huấn luyện đầy đủ hoặc tải lên các sản phẩm (artifacts) trừ khi người dùng yêu cầu. Báo cáo cấu hình đề xuất, bằng chứng từ bất kỳ bài kiểm tra nào, các đánh đổi về tài nguyên dự kiến và bất kỳ điều gì bạn không thể xác minh.</li></ol><!----></div> <!--[-1--><!--]--></div> <!--[-1--><!--]--></div><!----> <p>Đăng nhập vào tài khoản Hugging Face của bạn bằng user token để đẩy mô hình đã fine-tune lên Hub.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> login
login()<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="tokenization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenization"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Tokenization<!----></span></h2><!--]--><!----> <p>Tải một dataset và <a href="./fast_tokenizers">tokenize</a> cột văn bản mà mô hình được huấn luyện trên đó (<code>horoscope</code> trong dataset bên dưới).</p> <iframe src="https://huggingface.co/datasets/karthiksagarn/astro_horoscope/embed/viewer/default/train" frameborder="0" width="100%" height="560px"></iframe> <p>Tokenizer tạo ra các đầu vào cho mô hình, <code>input_ids</code> và <code>attention_mask</code>. Phương thức forward của mô hình chỉ chấp nhận <code>input_ids</code> và <code>attention_mask</code>, vì vậy hãy đặt <code>remove_columns</code> để loại bỏ các cột như <code>horoscope</code> sau khi tokenization.</p> <ul><li>Đặt <code>truncation=True</code> và một <code>max_length</code> để cắt các chuỗi dài hơn xuống độ dài tối đa được chỉ định.</li> <li>Sử dụng phương thức <code>train_test_split</code> để tạo một tập kiểm tra nhằm đánh giá mô hình.</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, DataCollatorForLanguageModeling
model_name = <span class="hljs-string">&quot;Qwen/Qwen3-0.6B&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(model_name)
dataset = load_dataset(<span class="hljs-string">&quot;karthiksagarn/astro_horoscope&quot;</span>, split=<span class="hljs-string">&quot;train&quot;</span>)
<span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize</span>(<span class="hljs-params">batch</span>):
<span class="hljs-keyword">return</span> tokenizer(
batch[<span class="hljs-string">&quot;horoscope&quot;</span>],
truncation=<span class="hljs-literal">True</span>,
max_length=<span class="hljs-number">512</span>,
)
dataset = dataset.<span class="hljs-built_in">map</span>(tokenize, batched=<span class="hljs-literal">True</span>, remove_columns=dataset.column_names)
dataset = dataset.train_test_split(test_size=<span class="hljs-number">0.1</span>)<!----></pre></div><!----> <p>Data collator tập hợp các mẫu từ bộ dữ liệu thành các lô (batch) để mô hình xử lý. <a href="/docs/transformers/main/vi/main_classes/data_collator#transformers.DataCollatorForLanguageModeling">DataCollatorForLanguageModeling</a> <em>động</em> đệm (pad) mỗi lô đến độ dài của chuỗi dài nhất trong lô đó, thay vì đệm mọi chuỗi trong bộ dữ liệu về cùng một độ dài. Điều này tiết kiệm tài nguyên tính toán và bộ nhớ bằng cách tránh việc tính toán các token đệm không cần thiết.</p> <ul><li>Đặt <code>mlm=False</code> để tránh việc che ngẫu nhiên các token.</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->data_collator = DataCollatorForLanguageModeling(tokenizer, mlm=<span class="hljs-literal">False</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="loading-a-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-a-model"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Tải một mô hình<!----></span></h2><!--]--><!----> <p>Tải checkpoint được huấn luyện trước để tinh chỉnh (xem hướng dẫn <a href="./models">Tải mô hình</a> để biết thêm chi tiết về việc tải mô hình).</p> <ul><li>Đặt <code>dtype="auto"</code> để tải trọng số với dtype đã lưu. Nếu không, PyTorch sẽ tải trọng số ở <code>torch.float32</code>, làm tăng gấp đôi bộ nhớ sử dụng nếu trọng số ban đầu là <code>torch.bfloat16</code>.</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, TrainingArguments, Trainer
model_name = <span class="hljs-string">&quot;Qwen/Qwen3-0.6B&quot;</span>
model = AutoModelForCausalLM.from_pretrained(model_name, dtype=<span class="hljs-string">&quot;auto&quot;</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="training-configuration" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-configuration"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Cấu hình huấn luyện<!----></span></h2><!--]--><!----> <p><a href="/docs/transformers/main/vi/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> cung cấp tất cả các tùy chọn để tùy chỉnh một phiên huấn luyện. Chỉ các đối số phổ biến nhất được đề cập ở đây. Mọi thứ khác đều có giá trị mặc định hợp lý hoặc chỉ liên quan đến các kịch bản cụ thể như huấn luyện phân tán. Xem tài liệu API của <a href="/docs/transformers/main/vi/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> để có danh sách đầy đủ các đối số.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">thời lượng huấn luyện</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">các tối ưu hóa huấn luyện</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">đánh giá và lưu checkpoint</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">ghi log</div><!--]--></div> <div class="language-select"><div><ul><li><code>num_train_epochs</code> và <code>per_device_train_batch_size</code> kiểm soát thời lượng huấn luyện và kích thước lô. <code>learning_rate</code> đặt tốc độ học ban đầu cho bộ tối ưu.</li></ul><!----></div><!----> <div hidden=""><ul><li>Đặt <code>bf16=True</code> để huấn luyện độ chính xác hỗn hợp nhanh nếu phần cứng của bạn hỗ trợ (GPU Ampere trở lên). Nếu không, hãy chuyển sang <code>fp16=True</code> trên phần cứng cũ hơn.</li> <li><code>gradient_accumulation_steps</code> mô phỏng kích thước batch hiệu dụng lớn hơn bằng cách tích lũy gradient qua nhiều lần truyền tiến trước khi cập nhật trọng số.</li> <li><code>gradient_checkpointing</code> đánh đổi tính toán lấy bộ nhớ bằng cách tính lại các kích hoạt trung gian trong quá trình truyền ngược thay vì lưu trữ chúng. Để có sự cân bằng giữa bộ nhớ và tốc độ, hãy xem <a href="./grad_checkpointing#partial-checkpointing">partial checkpointing</a>.</li> <li>Khi <code>gradient_checkpointing=True</code>, hãy đặt <code>gradient_checkpointing_kwargs={"offload": True}</code> để giữ các activation đã lưu trong bộ nhớ host được ghim và giảm mức sử dụng bộ nhớ GPU cho các chuỗi dài. Xem <a href="./grad_checkpointing#offloading-the-saved-activations">offloading các activation đã lưu</a> để biết về sự đánh đổi.</li> <li>Đặt <code>train_sampling_strategy="group_by_length"</code> để nhóm các ví dụ có độ dài tương tự vào cùng một batch và giảm padding. Xem <a href="./trainer_recipes#group-samples-by-length">nhóm mẫu theo độ dài</a> cho các tập dữ liệu đa phương thức dựa trên processor và độ dài đã tính trước.</li></ul><!----></div><!----> <div hidden=""><ul><li><code>eval_strategy</code> và <code>save_strategy</code> xác định thời điểm đánh giá mô hình trong quá trình huấn luyện và thời điểm lưu checkpoint.</li> <li><code>load_best_model_at_end</code> sẽ tải checkpoint tốt nhất khi quá trình huấn luyện kết thúc. Tham số này yêu cầu <code>eval_strategy</code> phải được thiết lập.</li></ul><!----></div><!----> <div hidden=""><ul><li><code>logging_steps</code> kiểm soát tần suất cập nhật và trả về loss trong quá trình huấn luyện.</li></ul><!----></div><!----><!----></div><!----> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->training_args = TrainingArguments(
output_dir=<span class="hljs-string">&quot;qwen3-finetuned&quot;</span>,
num_train_epochs=<span class="hljs-number">3</span>,
per_device_train_batch_size=<span class="hljs-number">2</span>,
gradient_accumulation_steps=<span class="hljs-number">8</span>,
gradient_checkpointing=<span class="hljs-literal">True</span>,
gradient_checkpointing_kwargs={<span class="hljs-string">&quot;every_n_layers&quot;</span>: <span class="hljs-number">4</span>},
bf16=<span class="hljs-literal">True</span>,
learning_rate=<span class="hljs-number">2e-5</span>,
logging_steps=<span class="hljs-number">10</span>,
eval_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
save_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
load_best_model_at_end=<span class="hljs-literal">True</span>,
)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Huấn luyện<!----></span></h2><!--]--><!----> <p>Tạo một instance <a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer">Trainer</a> với tất cả các thành phần cần thiết, sau đó gọi <a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer.train">train()</a> để bắt đầu.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset[<span class="hljs-string">&quot;train&quot;</span>],
eval_dataset=dataset[<span class="hljs-string">&quot;test&quot;</span>],
processing_class=tokenizer,
data_collator=DataCollatorForLanguageModeling(tokenizer, mlm=<span class="hljs-literal">False</span>),
)
trainer.train()
trainer.push_to_hub()<!----></pre></div><!----> <p><a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer.push_to_hub">push_to_hub()</a> tải lên Hub các trọng số đã tinh chỉnh, cấu hình sinh, tokenizer và cấu hình mô hình.</p> <!--[1--><h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Các bước tiếp theo<!----></span></h2><!--]--><!----> <ul><li>Đọc hướng dẫn <a href="./trainer_recipes">Các tính năng của Trainer</a> để xem các ví dụ hoạt động tối giản cho các tính năng phổ biến của Trainer như hàm loss tùy chỉnh, đánh giá tiết kiệm bộ nhớ, checkpointing và nhiều hơn nữa.</li> <li>Đọc hướng dẫn <a href="./trainer_customize">Subclassing Trainer methods</a> để tìm hiểu cách subclass các phương thức của <a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer">Trainer</a> nhằm hỗ trợ các tính năng mới và tùy chỉnh.</li> <li>Đọc hướng dẫn <a href="./trainer_callbacks">Callbacks</a> để tìm hiểu cách gắn vào các sự kiện huấn luyện cho việc ghi log, dừng sớm và các hành vi tùy chỉnh khác.</li> <li>Đọc hướng dẫn <a href="./data_collators">Data collators</a> để tìm hiểu cách tùy chỉnh cách các mẫu được tập hợp thành các lô (batches).</li> <li>Chạy một script huấn luyện trên GPU thuê bằng <a href="https://huggingface.co/docs/hub/jobs-training" rel="nofollow">Hugging Face Jobs</a>.</li> <li>Duyệt <a href="./notebooks">notebooks</a> hoặc phần <strong>Resources &gt; Task Recipes</strong> để xem thêm các ví dụ huấn luyện cho các tác vụ khác nhau về văn bản, âm thanh, thị giác và đa phương thức.</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/training.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]-->
<script>
{
__sveltekit_1m6yd5j = {
base: "/docs/transformers/main/en",
assets: "/docs/transformers/main/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 780],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
36.5 kB
·
Xet hash:
66b40b7b294aa60eaab9276424d4101a92695e3f75c2d14d91746e60f0d4ab60

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.