import{s as na,f as la,o as Ma,n as pe}from"../chunks/scheduler.37c15a92.js";import{S as pa,i as ra,g as j,s as p,r as m,A as ya,h as d,f as e,c as r,j as ct,u as T,x as b,k as V,y as ca,a,v as w,t as i,b as ps,d as c,w as u,e as sa,p as rs}from"../chunks/index.2bf4358c.js";import{T as ys}from"../chunks/Tip.363c041f.js";import{Y as wt}from"../chunks/Youtube.1e50a667.js";import{C as h}from"../chunks/CodeBlock.4e987730.js";import{D as aa}from"../chunks/DocNotebookDropdown.efc1fb7c.js";import{F as ia}from"../chunks/FrameworkSwitchCourse.8d4d4ab6.js";import{H as Te,E as Ja}from"../chunks/index.b8688d36.js";function oa(I){let t,y;return t=new aa({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/chapter7/section6_tf.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter7/section6_tf.ipynb"}]}}),{c(){m(t.$$.fragment)},l(n){T(t.$$.fragment,n)},m(n,J){w(t,n,J),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){u(t,n)}}}function Ua(I){let t,y;return t=new aa({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/chapter7/section6_pt.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter7/section6_pt.ipynb"}]}}),{c(){m(t.$$.fragment)},l(n){T(t.$$.fragment,n)},m(n,J){w(t,n,J),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){u(t,n)}}}function ma(I){let t,y="預訓練語言模型需要一段時間。我們建議您首先通過取消註釋以上兩行的註釋對數據樣本運行訓練循環，並確保訓練成功完成並存儲模型。沒有什麼比最後一步的訓練失敗更令人沮喪的了，因為你忘記創建一個文件夾或者因為保存路徑在訓練循環結束時有一個錯字！";return{c(){t=j("p"),t.textContent=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-1sd9p9o"&&(t.textContent=y)},m(n,J){a(n,t,J)},p:pe,d(n){n&&e(t)}}}function Ta(I){let t,y="✏️ <strong>試試看！</strong> 擺脫所有小於上下文大小的塊在這裡並不是什麼大問題，因為我們使用的是小上下文窗口。隨著上下文大小的增加（或者如果您有一個短文檔語料庫），被丟棄的塊的比例也會增加。準備數據的更有效方法是將所有標記化的樣本加入一個批次中，每個語料之間有一個<code>eos_token_id</code> 標記, 然後對連接的序列執行分塊。作為練習，修改 <code>tokenize()</code>函數以使用該方法。請注意，您需要設置<code>truncation=False</code> 和刪除標記生成器中的其他參數以獲取完整的標記 ID 序列。";return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-1mwfx5t"&&(t.innerHTML=y)},m(n,J){a(n,t,J)},p:pe,d(n){n&&e(t)}}}function wa(I){let t,y,n,J="通過該配置，我們可以加載新模型。請注意，這是我們剛開始不使用<code>from_pretrained()</code>函數，因為我們實際上是在自己初始化模型：",_,f,g,$,Z;return t=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMkMlMjBURkdQVDJMTUhlYWRNb2RlbCUyQyUyMEF1dG9Db25maWclMEElMEFjb25maWclMjAlM0QlMjBBdXRvQ29uZmlnLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJncHQyJTIyJTJDJTBBJTIwJTIwJTIwJTIwdm9jYWJfc2l6ZSUzRGxlbih0b2tlbml6ZXIpJTJDJTBBJTIwJTIwJTIwJTIwbl9jdHglM0Rjb250ZXh0X2xlbmd0aCUyQyUwQSUyMCUyMCUyMCUyMGJvc190b2tlbl9pZCUzRHRva2VuaXplci5ib3NfdG9rZW5faWQlMkMlMEElMjAlMjAlMjAlMjBlb3NfdG9rZW5faWQlM0R0b2tlbml6ZXIuZW9zX3Rva2VuX2lkJTJDJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, TFGPT2LMHeadModel, AutoConfig

config = AutoConfig.from_pretrained(
    <span class="hljs-string">&quot;gpt2&quot;</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,
)`,wrap:!1}}),f=new h({props:{code:"bW9kZWwlMjAlM0QlMjBURkdQVDJMTUhlYWRNb2RlbChjb25maWcpJTBBbW9kZWwobW9kZWwuZHVtbXlfaW5wdXRzKSUyMCUyMCUyMyUyMEJ1aWxkcyUyMHRoZSUyMG1vZGVsJTBBbW9kZWwuc3VtbWFyeSgp",highlighted:`model = TFGPT2LMHeadModel(config)
model(model.dummy_inputs)  <span class="hljs-comment"># Builds the model</span>
model.summary()`,wrap:!1}}),$=new h({props:{code:"X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18lMEFMYXllciUyMCh0eXBlKSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyME91dHB1dCUyMFNoYXBlJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwUGFyYW0lMjAlMjMlMjAlMjAlMjAlMEElM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlMEF0cmFuc2Zvcm1lciUyMChURkdQVDJNYWluTGF5ZXIlMjBtdWx0aXBsZSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMDEyNDI0MjQzMiUyMCUwQSUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUwQVRvdGFsJTIwcGFyYW1zJTNBJTIwMTI0JTJDMjQyJTJDNDMyJTBBVHJhaW5hYmxlJTIwcGFyYW1zJTNBJTIwMTI0JTJDMjQyJTJDNDMyJTBBTm9uLXRyYWluYWJsZSUyMHBhcmFtcyUzQSUyMDAlMEFfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fXw==",highlighted:`_________________________________________________________________
Layer (<span class="hljs-built_in">type</span>)                 Output Shape              Param <span class="hljs-comment">#   </span>
=================================================================
transformer (TFGPT2MainLayer multiple                  <span class="hljs-number">124242432</span> 
=================================================================
Total params: <span class="hljs-number">124</span>,<span class="hljs-number">242</span>,<span class="hljs-number">432</span>
Trainable params: <span class="hljs-number">124</span>,<span class="hljs-number">242</span>,<span class="hljs-number">432</span>
Non-trainable params: <span class="hljs-number">0</span>
_________________________________________________________________`,wrap:!1}}),{c(){m(t.$$.fragment),y=p(),n=j("p"),n.innerHTML=J,_=p(),m(f.$$.fragment),g=p(),m($.$$.fragment)},l(o){T(t.$$.fragment,o),y=r(o),n=d(o,"P",{"data-svelte-h":!0}),b(n)!=="svelte-18op4cm"&&(n.innerHTML=J),_=r(o),T(f.$$.fragment,o),g=r(o),T($.$$.fragment,o)},m(o,C){w(t,o,C),a(o,y,C),a(o,n,C),a(o,_,C),w(f,o,C),a(o,g,C),w($,o,C),Z=!0},i(o){Z||(c(t.$$.fragment,o),c(f.$$.fragment,o),c($.$$.fragment,o),Z=!0)},o(o){i(t.$$.fragment,o),i(f.$$.fragment,o),i($.$$.fragment,o),Z=!1},d(o){o&&(e(y),e(n),e(_),e(g)),u(t,o),u(f,o),u($,o)}}}function ua(I){let t,y,n,J="使用該配置，我們可以加載一個新模型。請注意，這是我們第一次不使用 <code>from_pretrained()</code> 函數，因為我們實際上是在自己初始化模型",_,f,g,$,Z;return t=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMkMlMjBHUFQyTE1IZWFkTW9kZWwlMkMlMjBBdXRvQ29uZmlnJTBBJTBBY29uZmlnJTIwJTNEJTIwQXV0b0NvbmZpZy5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyZ3B0MiUyMiUyQyUwQSUyMCUyMCUyMCUyMHZvY2FiX3NpemUlM0RsZW4odG9rZW5pemVyKSUyQyUwQSUyMCUyMCUyMCUyMG5fY3R4JTNEY29udGV4dF9sZW5ndGglMkMlMEElMjAlMjAlMjAlMjBib3NfdG9rZW5faWQlM0R0b2tlbml6ZXIuYm9zX3Rva2VuX2lkJTJDJTBBJTIwJTIwJTIwJTIwZW9zX3Rva2VuX2lkJTNEdG9rZW5pemVyLmVvc190b2tlbl9pZCUyQyUwQSk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, GPT2LMHeadModel, AutoConfig

config = AutoConfig.from_pretrained(
    <span class="hljs-string">&quot;gpt2&quot;</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,
)`,wrap:!1}}),f=new h({props:{code:"bW9kZWwlMjAlM0QlMjBHUFQyTE1IZWFkTW9kZWwoY29uZmlnKSUwQW1vZGVsX3NpemUlMjAlM0QlMjBzdW0odC5udW1lbCgpJTIwZm9yJTIwdCUyMGluJTIwbW9kZWwucGFyYW1ldGVycygpKSUwQXByaW50KGYlMjJHUFQtMiUyMHNpemUlM0ElMjAlN0Jtb2RlbF9zaXplJTJGMTAwMCoqMiUzQS4xZiU3RE0lMjBwYXJhbWV0ZXJzJTIyKQ==",highlighted:`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&quot;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&quot;</span>)`,wrap:!1}}),$=new h({props:{code:"R1BULTIlMjBzaXplJTNBJTIwMTI0LjJNJTIwcGFyYW1ldGVycw==",highlighted:'GPT-<span class="hljs-number">2</span> size: <span class="hljs-number">124.2</span>M parameters',wrap:!1}}),{c(){m(t.$$.fragment),y=p(),n=j("p"),n.innerHTML=J,_=p(),m(f.$$.fragment),g=p(),m($.$$.fragment)},l(o){T(t.$$.fragment,o),y=r(o),n=d(o,"P",{"data-svelte-h":!0}),b(n)!=="svelte-1lln40o"&&(n.innerHTML=J),_=r(o),T(f.$$.fragment,o),g=r(o),T($.$$.fragment,o)},m(o,C){w(t,o,C),a(o,y,C),a(o,n,C),a(o,_,C),w(f,o,C),a(o,g,C),w($,o,C),Z=!0},i(o){Z||(c(t.$$.fragment,o),c(f.$$.fragment,o),c($.$$.fragment,o),Z=!0)},o(o){i(t.$$.fragment,o),i(f.$$.fragment,o),i($.$$.fragment,o),Z=!1},d(o){o&&(e(y),e(n),e(_),e(g)),u(t,o),u(f,o),u($,o)}}}function ja(I){let t,y;return t=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvckZvckxhbmd1YWdlTW9kZWxpbmclMEElMEF0b2tlbml6ZXIucGFkX3Rva2VuJTIwJTNEJTIwdG9rZW5pemVyLmVvc190b2tlbiUwQWRhdGFfY29sbGF0b3IlMjAlM0QlMjBEYXRhQ29sbGF0b3JGb3JMYW5ndWFnZU1vZGVsaW5nKHRva2VuaXplciUyQyUyMG1sbSUzREZhbHNlJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJ0ZiUyMik=",highlighted:`<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>, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`,wrap:!1}}),{c(){m(t.$$.fragment)},l(n){T(t.$$.fragment,n)},m(n,J){w(t,n,J),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){u(t,n)}}}function da(I){let t,y;return t=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvckZvckxhbmd1YWdlTW9kZWxpbmclMEElMEF0b2tlbml6ZXIucGFkX3Rva2VuJTIwJTNEJTIwdG9rZW5pemVyLmVvc190b2tlbiUwQWRhdGFfY29sbGF0b3IlMjAlM0QlMjBEYXRhQ29sbGF0b3JGb3JMYW5ndWFnZU1vZGVsaW5nKHRva2VuaXplciUyQyUyMG1sbSUzREZhbHNlKQ==",highlighted:`<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>)`,wrap:!1}}),{c(){m(t.$$.fragment)},l(n){T(t.$$.fragment,n)},m(n,J){w(t,n,J),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){u(t,n)}}}function ba(I){let t,y;return t=new h({props:{code:"aW5wdXRfaWRzJTIwc2hhcGUlM0ElMjAoNSUyQyUyMDEyOCklMEFhdHRlbnRpb25fbWFzayUyMHNoYXBlJTNBJTIwKDUlMkMlMjAxMjgpJTBBbGFiZWxzJTIwc2hhcGUlM0ElMjAoNSUyQyUyMDEyOCk=",highlighted:`input_ids shape: (<span class="hljs-number">5</span>, <span class="hljs-number">128</span>)
attention_mask shape: (<span class="hljs-number">5</span>, <span class="hljs-number">128</span>)
labels shape: (<span class="hljs-number">5</span>, <span class="hljs-number">128</span>)`,wrap:!1}}),{c(){m(t.$$.fragment)},l(n){T(t.$$.fragment,n)},m(n,J){w(t,n,J),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){u(t,n)}}}function fa(I){let t,y;return t=new h({props:{code:"aW5wdXRfaWRzJTIwc2hhcGUlM0ElMjB0b3JjaC5TaXplKCU1QjUlMkMlMjAxMjglNUQpJTBBYXR0ZW50aW9uX21hc2slMjBzaGFwZSUzQSUyMHRvcmNoLlNpemUoJTVCNSUyQyUyMDEyOCU1RCklMEFsYWJlbHMlMjBzaGFwZSUzQSUyMHRvcmNoLlNpemUoJTVCNSUyQyUyMDEyOCU1RCk=",highlighted:`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>])`,wrap:!1}}),{c(){m(t.$$.fragment)},l(n){T(t.$$.fragment,n)},m(n,J){w(t,n,J),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){u(t,n)}}}function ea(I){let t,y="現在，我們可以使用<code>to_tf_dataset()</code>方法，使用上面創建的數據整理器將數據集轉換為TensorFlow數據集：",n,J,_;return J=new h({props:{code:"dGZfdHJhaW5fZGF0YXNldCUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQudG9fdGZfZGF0YXNldCglMEElMjAlMjAlMjAlMjBjb2x1bW5zJTNEJTVCJTIyaW5wdXRfaWRzJTIyJTJDJTIwJTIyYXR0ZW50aW9uX21hc2slMjIlMkMlMjAlMjJsYWJlbHMlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBjb2xsYXRlX2ZuJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMHNodWZmbGUlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hfc2l6ZSUzRDMyJTJDJTBBKSUwQXRmX2V2YWxfZGF0YXNldCUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0JTVCJTIydmFsaWQlMjIlNUQudG9fdGZfZGF0YXNldCglMEElMjAlMjAlMjAlMjBjb2x1bW5zJTNEJTVCJTIyaW5wdXRfaWRzJTIyJTJDJTIwJTIyYXR0ZW50aW9uX21hc2slMjIlMkMlMjAlMjJsYWJlbHMlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBjb2xsYXRlX2ZuJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMHNodWZmbGUlM0RGYWxzZSUyQyUwQSUyMCUyMCUyMCUyMGJhdGNoX3NpemUlM0QzMiUyQyUwQSk=",highlighted:`tf_train_dataset = tokenized_dataset[<span class="hljs-string">&quot;train&quot;</span>].to_tf_dataset(
    columns=[<span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">True</span>,
    batch_size=<span class="hljs-number">32</span>,
)
tf_eval_dataset = tokenized_dataset[<span class="hljs-string">&quot;valid&quot;</span>].to_tf_dataset(
    columns=[<span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">False</span>,
    batch_size=<span class="hljs-number">32</span>,
)`,wrap:!1}}),{c(){t=j("p"),t.innerHTML=y,n=p(),m(J.$$.fragment)},l(f){t=d(f,"P",{"data-svelte-h":!0}),b(t)!=="svelte-1lj0zqi"&&(t.innerHTML=y),n=r(f),T(J.$$.fragment,f)},m(f,g){a(f,t,g),a(f,n,g),w(J,f,g),_=!0},i(f){_||(c(J.$$.fragment,f),_=!0)},o(f){i(J.$$.fragment,f),_=!1},d(f){f&&(e(t),e(n)),u(J,f)}}}function Ca(I){let t,y="⚠️  移動輸入和標籤以對齊它們發生在模型內部，因此數據整理器只需複製輸入以創建標籤。";return{c(){t=j("p"),t.textContent=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-19fppxv"&&(t.textContent=y)},m(n,J){a(n,t,J)},p:pe,d(n){n&&e(t)}}}function ha(I){let t,y="剩下要做的就是配置訓練超參數並調用 <code>compile()</code> 和 <code>fit()</code>。我們將使用帶有一些預熱的學習率調整策略來提高訓練的穩定性：",n,J,_,f,g="現在我們可以調用<code>model.fit()，</code>並等待訓練完成。你是在完整的訓練集還是他的子集上運行，這將分別需要20和2個小時，所以拿一些咖啡和一本好書來閱讀！訓練完成後，我們可以將模型和分詞器推送到中心：",$,Z,o;return J=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMGNyZWF0ZV9vcHRpbWl6ZXIlMEFpbXBvcnQlMjB0ZW5zb3JmbG93JTIwYXMlMjB0ZiUwQSUwQW51bV90cmFpbl9zdGVwcyUyMCUzRCUyMGxlbih0Zl90cmFpbl9kYXRhc2V0KSUwQW9wdGltaXplciUyQyUyMHNjaGVkdWxlJTIwJTNEJTIwY3JlYXRlX29wdGltaXplciglMEElMjAlMjAlMjAlMjBpbml0X2xyJTNENWUtNSUyQyUwQSUyMCUyMCUyMCUyMG51bV93YXJtdXBfc3RlcHMlM0QxXzAwMCUyQyUwQSUyMCUyMCUyMCUyMG51bV90cmFpbl9zdGVwcyUzRG51bV90cmFpbl9zdGVwcyUyQyUwQSUyMCUyMCUyMCUyMHdlaWdodF9kZWNheV9yYXRlJTNEMC4wMSUyQyUwQSklMEFtb2RlbC5jb21waWxlKG9wdGltaXplciUzRG9wdGltaXplciklMEElMEElMjMlMjBUcmFpbiUyMGluJTIwbWl4ZWQtcHJlY2lzaW9uJTIwZmxvYXQxNiUwQXRmLmtlcmFzLm1peGVkX3ByZWNpc2lvbi5zZXRfZ2xvYmFsX3BvbGljeSglMjJtaXhlZF9mbG9hdDE2JTIyKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> create_optimizer
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf

num_train_steps = <span class="hljs-built_in">len</span>(tf_train_dataset)
optimizer, schedule = create_optimizer(
    init_lr=<span class="hljs-number">5e-5</span>,
    num_warmup_steps=<span class="hljs-number">1_000</span>,
    num_train_steps=num_train_steps,
    weight_decay_rate=<span class="hljs-number">0.01</span>,
)
model.<span class="hljs-built_in">compile</span>(optimizer=optimizer)

<span class="hljs-comment"># Train in mixed-precision float16</span>
tf.keras.mixed_precision.set_global_policy(<span class="hljs-string">&quot;mixed_float16&quot;</span>)`,wrap:!1}}),Z=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycy5rZXJhc19jYWxsYmFja3MlMjBpbXBvcnQlMjBQdXNoVG9IdWJDYWxsYmFjayUwQSUwQWNhbGxiYWNrJTIwJTNEJTIwUHVzaFRvSHViQ2FsbGJhY2sob3V0cHV0X2RpciUzRCUyMmNvZGVwYXJyb3QtZHMlMjIlMkMlMjB0b2tlbml6ZXIlM0R0b2tlbml6ZXIpJTBBJTBBbW9kZWwuZml0KHRmX3RyYWluX2RhdGFzZXQlMkMlMjB2YWxpZGF0aW9uX2RhdGElM0R0Zl9ldmFsX2RhdGFzZXQlMkMlMjBjYWxsYmFja3MlM0QlNUJjYWxsYmFjayU1RCk=",highlighted:`<span class="hljs-keyword">from</span> transformers.keras_callbacks <span class="hljs-keyword">import</span> PushToHubCallback

callback = PushToHubCallback(output_dir=<span class="hljs-string">&quot;codeparrot-ds&quot;</span>, tokenizer=tokenizer)

model.fit(tf_train_dataset, validation_data=tf_eval_dataset, callbacks=[callback])`,wrap:!1}}),{c(){t=j("p"),t.innerHTML=y,n=p(),m(J.$$.fragment),_=p(),f=j("p"),f.innerHTML=g,$=p(),m(Z.$$.fragment)},l(C){t=d(C,"P",{"data-svelte-h":!0}),b(t)!=="svelte-ocv7ex"&&(t.innerHTML=y),n=r(C),T(J.$$.fragment,C),_=r(C),f=d(C,"P",{"data-svelte-h":!0}),b(f)!=="svelte-1s0qalu"&&(f.innerHTML=g),$=r(C),T(Z.$$.fragment,C)},m(C,G){a(C,t,G),a(C,n,G),w(J,C,G),a(C,_,G),a(C,f,G),a(C,$,G),w(Z,C,G),o=!0},i(C){o||(c(J.$$.fragment,C),c(Z.$$.fragment,C),o=!0)},o(C){i(J.$$.fragment,C),i(Z.$$.fragment,C),o=!1},d(C){C&&(e(t),e(n),e(_),e(f),e($)),u(J,C),u(Z,C)}}}function Ia(I){let t,y="剩下要做的就是配置訓練參數並啟動 <code>Trainer</code> .我們將使用餘弦學習率，並進行一些Warmup和有效批量大小為 256 ( <code>per_device_train_batch_size</code> * <code>gradient_accumulation_steps</code>）。當單個批次不適合內存時使用梯度累積，並通過多次向前/向後傳遞逐步建立梯度。當我們使用 🤗 Accelerate 創建訓練循環時，我們將看到這一點。",n,J,_,f,g="現在我們可以開始 <code>Trainer</code>並等待訓練完成。根據您是在整個訓練集還是在訓練集的一個子集上運行它，這將分別需要 20 或 2 個小時，因此請喝杯咖啡和一本好書來閱讀！",$,Z,o,C,G="訓練完成後，我們可以將模型和標記器推送到 Hub：",v,E,W;return J=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluZXIlMkMlMjBUcmFpbmluZ0FyZ3VtZW50cyUwQSUwQWFyZ3MlMjAlM0QlMjBUcmFpbmluZ0FyZ3VtZW50cyglMEElMjAlMjAlMjAlMjBvdXRwdXRfZGlyJTNEJTIyY29kZXBhcnJvdC1kcyUyMiUyQyUwQSUyMCUyMCUyMCUyMHBlcl9kZXZpY2VfdHJhaW5fYmF0Y2hfc2l6ZSUzRDMyJTJDJTBBJTIwJTIwJTIwJTIwcGVyX2RldmljZV9ldmFsX2JhdGNoX3NpemUlM0QzMiUyQyUwQSUyMCUyMCUyMCUyMGV2YWx1YXRpb25fc3RyYXRlZ3klM0QlMjJzdGVwcyUyMiUyQyUwQSUyMCUyMCUyMCUyMGV2YWxfc3RlcHMlM0Q1XzAwMCUyQyUwQSUyMCUyMCUyMCUyMGxvZ2dpbmdfc3RlcHMlM0Q1XzAwMCUyQyUwQSUyMCUyMCUyMCUyMGdyYWRpZW50X2FjY3VtdWxhdGlvbl9zdGVwcyUzRDglMkMlMEElMjAlMjAlMjAlMjBudW1fdHJhaW5fZXBvY2hzJTNEMSUyQyUwQSUyMCUyMCUyMCUyMHdlaWdodF9kZWNheSUzRDAuMSUyQyUwQSUyMCUyMCUyMCUyMHdhcm11cF9zdGVwcyUzRDFfMDAwJTJDJTBBJTIwJTIwJTIwJTIwbHJfc2NoZWR1bGVyX3R5cGUlM0QlMjJjb3NpbmUlMjIlMkMlMEElMjAlMjAlMjAlMjBsZWFybmluZ19yYXRlJTNENWUtNCUyQyUwQSUyMCUyMCUyMCUyMHNhdmVfc3RlcHMlM0Q1XzAwMCUyQyUwQSUyMCUyMCUyMCUyMGZwMTYlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwcHVzaF90b19odWIlM0RUcnVlJTJDJTBBKSUwQSUwQXRyYWluZXIlMjAlM0QlMjBUcmFpbmVyKCUwQSUyMCUyMCUyMCUyMG1vZGVsJTNEbW9kZWwlMkMlMEElMjAlMjAlMjAlMjB0b2tlbml6ZXIlM0R0b2tlbml6ZXIlMkMlMEElMjAlMjAlMjAlMjBhcmdzJTNEYXJncyUyQyUwQSUyMCUyMCUyMCUyMGRhdGFfY29sbGF0b3IlM0RkYXRhX2NvbGxhdG9yJTJDJTBBJTIwJTIwJTIwJTIwdHJhaW5fZGF0YXNldCUzRHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwZXZhbF9kYXRhc2V0JTNEdG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydmFsaWQlMjIlNUQlMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer, TrainingArguments

args = TrainingArguments(
    output_dir=<span class="hljs-string">&quot;codeparrot-ds&quot;</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">&quot;steps&quot;</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">&quot;cosine&quot;</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">&quot;train&quot;</span>],
    eval_dataset=tokenized_datasets[<span class="hljs-string">&quot;valid&quot;</span>],
)`,wrap:!1}}),Z=new h({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:"trainer.train()",wrap:!1}}),E=new h({props:{code:"dHJhaW5lci5wdXNoX3RvX2h1Yigp",highlighted:"trainer.push_to_hub()",wrap:!1}}),{c(){t=j("p"),t.innerHTML=y,n=p(),m(J.$$.fragment),_=p(),f=j("p"),f.innerHTML=g,$=p(),m(Z.$$.fragment),o=p(),C=j("p"),C.textContent=G,v=p(),m(E.$$.fragment)},l(k){t=d(k,"P",{"data-svelte-h":!0}),b(t)!=="svelte-119mbhu"&&(t.innerHTML=y),n=r(k),T(J.$$.fragment,k),_=r(k),f=d(k,"P",{"data-svelte-h":!0}),b(f)!=="svelte-27uz21"&&(f.innerHTML=g),$=r(k),T(Z.$$.fragment,k),o=r(k),C=d(k,"P",{"data-svelte-h":!0}),b(C)!=="svelte-1g9wxp9"&&(C.textContent=G),v=r(k),T(E.$$.fragment,k)},m(k,X){a(k,t,X),a(k,n,X),w(J,k,X),a(k,_,X),a(k,f,X),a(k,$,X),w(Z,k,X),a(k,o,X),a(k,C,X),a(k,v,X),w(E,k,X),W=!0},i(k){W||(c(J.$$.fragment,k),c(Z.$$.fragment,k),c(E.$$.fragment,k),W=!0)},o(k){i(J.$$.fragment,k),i(Z.$$.fragment,k),i(E.$$.fragment,k),W=!1},d(k){k&&(e(t),e(n),e(_),e(f),e($),e(o),e(C),e(v)),u(J,k),u(Z,k),u(E,k)}}}function _a(I){let t,y="✏️ <strong>試試看!</strong> 除了<code>TrainingArguments</code> 之外，我們只需要大約30行代碼就可以從原始文本到訓練GPT-2。 用你自己的數據集試試看，看看你能不能得到好的結果！";return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-1h02rte"&&(t.innerHTML=y)},m(n,J){a(n,t,J)},p:pe,d(n){n&&e(t)}}}function ka(I){let t,y='💡 如果您有權訪問具有多個 GPU 的計算機，則可以嘗試使用 <code>MirroredStrategy</code> 上下文來大幅加快訓練速度。您需要創建一個<code>tf.distribute.MirroredStrategy</code>對象，並確保 <code>to_tf_dataset</code> 命令以及模型創建和對 <code>fit()</code>的調用都在其 <code>scope()</code> context. 上下文中運行。您可以查看有關此內容的文檔<a href="https://www.tensorflow.org/guide/distributed_training#use_tfdistributestrategy_with_keras_modelfit" rel="nofollow">here</a>.';return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-e002qg"&&(t.innerHTML=y)},m(n,J){a(n,t,J)},d(n){n&&e(t)}}}function Za(I){let t,y="💡 如果您可以訪問具有多個 GPU 的機器，請嘗試在那裡運行代碼。 <code>Trainer</code>自動管理多臺機器，這可以極大地加快訓練速度。";return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-1buw6v8"&&(t.innerHTML=y)},m(n,J){a(n,t,J)},d(n){n&&e(t)}}}function $a(I){let t;function y(_,f){return _[0]==="pt"?Za:ka}let n=y(I),J=n(I);return{c(){J.c(),t=sa()},l(_){J.l(_),t=sa()},m(_,f){J.m(_,f),a(_,t,f)},p(_,f){n!==(n=y(_))&&(J.d(1),J=n(_),J&&(J.c(),J.m(t.parentNode,t)))},d(_){_&&e(t),J.d(_)}}}function ga(I){let t,y;return t=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBY291cnNlX21vZGVsJTIwJTNEJTIwVEZHUFQyTE1IZWFkTW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUyMmh1Z2dpbmdmYWNlLWNvdXJzZSUyRmNvZGVwYXJyb3QtZHMlMjIpJTBBY291cnNlX3Rva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKCUyMmh1Z2dpbmdmYWNlLWNvdXJzZSUyRmNvZGVwYXJyb3QtZHMlMjIpJTBBcGlwZSUyMCUzRCUyMHBpcGVsaW5lKCUwQSUyMCUyMCUyMCUyMCUyMnRleHQtZ2VuZXJhdGlvbiUyMiUyQyUyMG1vZGVsJTNEY291cnNlX21vZGVsJTJDJTIwdG9rZW5pemVyJTNEY291cnNlX3Rva2VuaXplciUyQyUyMGRldmljZSUzRDAlMEEp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

course_model = TFGPT2LMHeadModel.from_pretrained(<span class="hljs-string">&quot;huggingface-course/codeparrot-ds&quot;</span>)
course_tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;huggingface-course/codeparrot-ds&quot;</span>)
pipe = pipeline(
    <span class="hljs-string">&quot;text-generation&quot;</span>, model=course_model, tokenizer=course_tokenizer, device=<span class="hljs-number">0</span>
)`,wrap:!1}}),{c(){m(t.$$.fragment)},l(n){T(t.$$.fragment,n)},m(n,J){w(t,n,J),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){u(t,n)}}}function Ga(I){let t,y;return t=new h({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwcGlwZWxpbmUlMEElMEFkZXZpY2UlMjAlM0QlMjB0b3JjaC5kZXZpY2UoJTIyY3VkYSUyMiklMjBpZiUyMHRvcmNoLmN1ZGEuaXNfYXZhaWxhYmxlKCklMjBlbHNlJTIwdG9yY2guZGV2aWNlKCUyMmNwdSUyMiklMEFwaXBlJTIwJTNEJTIwcGlwZWxpbmUoJTBBJTIwJTIwJTIwJTIwJTIydGV4dC1nZW5lcmF0aW9uJTIyJTJDJTIwbW9kZWwlM0QlMjJodWdnaW5nZmFjZS1jb3Vyc2UlMkZjb2RlcGFycm90LWRzJTIyJTJDJTIwZGV2aWNlJTNEZGV2aWNlJTBBKQ==",highlighted:`<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">&quot;cuda&quot;</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">&quot;cpu&quot;</span>)
pipe = pipeline(
    <span class="hljs-string">&quot;text-generation&quot;</span>, model=<span class="hljs-string">&quot;huggingface-course/codeparrot-ds&quot;</span>, device=device
)`,wrap:!1}}),{c(){m(t.$$.fragment)},l(n){T(t.$$.fragment,n)},m(n,J){w(t,n,J),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){u(t,n)}}}function Xa(I){let t,y="從這幾個例子來看，模型似乎已經學習了 Python 數據科學堆棧的一些語法（當然，在將模型部署到現實世界之前，我們需要對其進行更全面的評估）。然而，有時需要對模型訓練進行更多定製才能實現給定用例的必要性能。例如，如果我們想動態更新批量大小或有一個條件訓練循環來即時跳過壞示例怎麼辦？一種選擇是將 <code>Trainer</code> 並添加必要的更改，但有時從頭開始編寫訓練循環會更簡單。這就是🤗 Accelerate 的用武之地。";return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-n880tn"&&(t.innerHTML=y)},m(n,J){a(n,t,J)},d(n){n&&e(t)}}}function Ba(I){let t,y="看看這幾個例子，似乎模型已經學習了Python數據科學堆棧的一些語法。當然，在將模型部署到現實世界之前，我們需要更徹底地評估模型，但這仍然是一個令人印象深刻的原型。";return{c(){t=j("p"),t.textContent=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-18hwf7y"&&(t.textContent=y)},m(n,J){a(n,t,J)},d(n){n&&e(t)}}}function ta(I){let t,y,n,J="我們已經看到了如何使用 <code>Trainer</code> ，這可以允許一些自定義。然而，有時我們想要完全控制訓練循環，或者我們想要進行一些奇特的更改。在這種情況下 🤗 Accelerate 是一個不錯的選擇，在本節中，我們將逐步介紹使用它來訓練我們的模型的步驟。為了讓事情變得更有趣，我們還將在訓練循環中添加一些修改。",_,f,g,$,Z="由於我們主要對數據科學庫的合理自動填充感興趣，因此對更多使用這些庫的訓練樣本給予更多權重是有意義的。我們可以通過使用關鍵字輕鬆識別這些示例，例如 <code>plt</code>、<code>pd</code>、<code>sk</code>、<code>fit</code>和<code>predict</code>等關鍵字，我們可以很容易地識別這些示例，這些關鍵字是matplotlib最常用的導入名稱。<code>Pyplot</code>, <code>pandas</code>和<code>sklearn</code>以及後者的擬合/預測模式。如果這些都表示為單個標記，我們可以輕鬆檢查它們是否出現在輸入序列中。標記可能有一個空格前綴，因此我們還將在標記器詞彙表中檢查這些版本。為了驗證它是否有效，我們將添加一個測試token ，該token 應拆分為多個tokens：",o,C,G,v,E,W,k="太好了，這似乎很好用！我們現在可以編寫一個自定義損失函數，它將輸入序列、logits 和我們剛剛選擇的關​​鍵標記作為輸入。首先，我們需要對齊 logits 和輸入：向右移動一個的輸入序列形成標籤，因為下一個標記是當前標記的標籤。我們可以通過從輸入序列的第二個標記開始標記來實現這一點，因為模型無論如何都不會對第一個標記進行預測。然後我們切斷最後一個 logit，因為我們沒有完整輸入序列之後的標記的標籤。有了這個，我們可以計算每個樣本的損失並計算每個樣本中所有關鍵字的出現次數。最後，我們使用出現次數作為權重計算所有樣本的加權平均值。由於我們不想扔掉所有沒有關鍵字的樣本，我們將權重加1：",X,x,Vl,F,we="在我們開始使用這個很棒的新損失函數進行訓練之前，我們需要準備一些東西：",zl,B,ue="<li>我們需要數據加載器來批量加載數據。</li> <li>我們需要設置權重衰減參數。</li> <li>有時我們想要求值，因此將求值代碼包裝在一個函數中是有意義的。</li>",Nl,R,je="讓我們從數據加載器開始。我們只需要將數據集的格式設置為 <code>&quot;torch&quot;</code>，然後我們可以將它傳遞給 PyTorch <code>DataLoader</code> ,同時設置適當的批量大小：",vl,Y,cs,tl,gl="接下來，我們對參數進行分組，以便優化器知道哪些將獲得額外的權重衰減。通常，所有偏差和 LayerNorm 權重項都不受此限制；以下我們如何做到這一點：",xl,H,is,al,Gl="由於我們希望在訓練期間定期在驗證集上評估模型，因此我們也為此編寫一個函數。它只是運行評估數據加載器並收集跨進程的所有損失：",Js,nl,Ml,pl,Xl='通過 <code>evaluate()</code> 函數我們定期可以獲取損失值和<a href="/course/chapter7/3">perplexity</a>。接下來，我們重新定義我們的模型以確保我們再次從頭開始訓練：',os,rl,yl,cl,Bl="然後我們可以定義我們的優化器，使用之前的函數來分割權重衰減的參數：",Fl,S,Us,il,Rl="現在讓我們準備模型、優化器和數據加載器，以便我們可以開始訓練：",Yl,q,ms,Jl,ol,Ul,Wl="現在我們已經發送了我們的 <code>train_dataloader</code>到 <code>accelerator.prepare()</code> ，我們可以使用它的長度來計算訓練步驟的數量。請記住，我們應該始終在準備好dataloader後執行此操作，因為該方法會改變其長度。我們使用經典線性學習率調度：",Hl,D,Ts,ml,Ql="最後，要將我們的模型推送到 Hub，我們需要創建一個 <code>Repository</code> 工作文件夾中的對象。如果您尚未登錄，請先登錄 Hugging Face。我們將從我們想要為模型提供的模型 ID 中確定存儲庫名稱（您可以自由地用自己的選擇替換 <code>repo_name</code> ；它只需要包含您的用戶名，可以使用<code>get_full_repo_name()</code>函數的查看目前的repo_name）：",Sl,L,ql,z,Dl,K,de="然後我們可以在本地文件夾中克隆該存儲庫。如果它已經存在，這個本地文件夾應該是我們正在使用的存儲庫的克隆：",Ll,P,Kl,O,ws="我們現在可以上傳我們保存的任何內容 <code>output_dir</code> 通過調用 <code>repo.push_to_hub()</code> 方法。這將幫助我們在每個 epoch 結束時上傳中間模型。在我們訓練之前，讓我們運行一個快速測試，看看評估函數是否正常工作：",Tl,Al,Pl,ll,Ol,sl,us="這些損失和困惑度的值非常高，但這並不奇怪，因為我們還沒有訓練過模型。有了這個，我們已經準備好編寫訓練腳本的核心部分：訓練循環。在訓練循環中，我們遍歷數據加載器並將批次傳遞給模型。有了 logits，我們就可以評估我們的自定義損失函數。我們通過梯度累積步驟的數量來縮放損失，以便在聚合更多步驟時不會產生更大的損失。在我們優化之前，我們還剪輯了梯度以獲得更好的收斂性。最後，每隔幾步，我們就會使用新的 <code>evaluate()</code> 函數評估模型：",wl,El,ls,el,be="就是這樣 - 您現在擁有自己的因果語言模型（例如 GPT-2）的自定義訓練循環，您可以根據自己的需要進一步自定義。",ss,N,js,ul,jl;return t=new Te({props:{title:"用 🤗 Accelerate 訓練",local:"用--accelerate-訓練",headingTag:"h2"}}),f=new wt({props:{id:"Hm8_PgVTFuc"}}),C=new h({props:{code:"a2V5dG9rZW5faWRzJTIwJTNEJTIwJTVCJTVEJTBBZm9yJTIwa2V5d29yZCUyMGluJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTIycGx0JTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIycGQlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJzayUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMmZpdCUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMnByZWRpY3QlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIlMjBwbHQlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIlMjBwZCUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMiUyMHNrJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyJTIwZml0JTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyJTIwcHJlZGljdCUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMnRlc3R0ZXN0JTIyJTJDJTBBJTVEJTNBJTBBJTIwJTIwJTIwJTIwaWRzJTIwJTNEJTIwdG9rZW5pemVyKCU1QmtleXdvcmQlNUQpLmlucHV0X2lkcyU1QjAlNUQlMEElMjAlMjAlMjAlMjBpZiUyMGxlbihpZHMpJTIwJTNEJTNEJTIwMSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGtleXRva2VuX2lkcy5hcHBlbmQoaWRzJTVCMCU1RCklMEElMjAlMjAlMjAlMjBlbHNlJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJpbnQoZiUyMktleXdvcmQlMjBoYXMlMjBub3QlMjBzaW5nbGUlMjB0b2tlbiUzQSUyMCU3QmtleXdvcmQlN0QlMjIp",highlighted:`keytoken_ids = []
<span class="hljs-keyword">for</span> keyword <span class="hljs-keyword">in</span> [
    <span class="hljs-string">&quot;plt&quot;</span>,
    <span class="hljs-string">&quot;pd&quot;</span>,
    <span class="hljs-string">&quot;sk&quot;</span>,
    <span class="hljs-string">&quot;fit&quot;</span>,
    <span class="hljs-string">&quot;predict&quot;</span>,
    <span class="hljs-string">&quot; plt&quot;</span>,
    <span class="hljs-string">&quot; pd&quot;</span>,
    <span class="hljs-string">&quot; sk&quot;</span>,
    <span class="hljs-string">&quot; fit&quot;</span>,
    <span class="hljs-string">&quot; predict&quot;</span>,
    <span class="hljs-string">&quot;testtest&quot;</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&quot;Keyword has not single token: <span class="hljs-subst">{keyword}</span>&quot;</span>)`,wrap:!1}}),v=new h({props:{code:"J0tleXdvcmQlMjBoYXMlMjBub3QlMjBzaW5nbGUlMjB0b2tlbiUzQSUyMHRlc3R0ZXN0Jw==",highlighted:'<span class="hljs-string">&#x27;Keyword has not single token: testtest&#x27;</span>',wrap:!1}}),x=new h({props:{code:"ZnJvbSUyMHRvcmNoLm5uJTIwaW1wb3J0JTIwQ3Jvc3NFbnRyb3B5TG9zcyUwQWltcG9ydCUyMHRvcmNoJTBBJTBBJTBBZGVmJTIwa2V5dG9rZW5fd2VpZ2h0ZWRfbG9zcyhpbnB1dHMlMkMlMjBsb2dpdHMlMkMlMjBrZXl0b2tlbl9pZHMlMkMlMjBhbHBoYSUzRDEuMCklM0ElMEElMjAlMjAlMjAlMjAlMjMlMjBTaGlmdCUyMHNvJTIwdGhhdCUyMHRva2VucyUyMCUzQyUyMG4lMjBwcmVkaWN0JTIwbiUwQSUyMCUyMCUyMCUyMHNoaWZ0X2xhYmVscyUyMCUzRCUyMGlucHV0cyU1Qi4uLiUyQyUyMDElM0ElNUQuY29udGlndW91cygpJTBBJTIwJTIwJTIwJTIwc2hpZnRfbG9naXRzJTIwJTNEJTIwbG9naXRzJTVCLi4uJTJDJTIwJTNBLTElMkMlMjAlM0ElNUQuY29udGlndW91cygpJTBBJTIwJTIwJTIwJTIwJTIzJTIwQ2FsY3VsYXRlJTIwcGVyLXRva2VuJTIwbG9zcyUwQSUyMCUyMCUyMCUyMGxvc3NfZmN0JTIwJTNEJTIwQ3Jvc3NFbnRyb3B5TG9zcyhyZWR1Y2UlM0RGYWxzZSklMEElMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwbG9zc19mY3Qoc2hpZnRfbG9naXRzLnZpZXcoLTElMkMlMjBzaGlmdF9sb2dpdHMuc2l6ZSgtMSkpJTJDJTIwc2hpZnRfbGFiZWxzLnZpZXcoLTEpKSUwQSUyMCUyMCUyMCUyMCUyMyUyMFJlc2l6ZSUyMGFuZCUyMGF2ZXJhZ2UlMjBsb3NzJTIwcGVyJTIwc2FtcGxlJTBBJTIwJTIwJTIwJTIwbG9zc19wZXJfc2FtcGxlJTIwJTNEJTIwbG9zcy52aWV3KHNoaWZ0X2xvZ2l0cy5zaXplKDApJTJDJTIwc2hpZnRfbG9naXRzLnNpemUoMSkpLm1lYW4oYXhpcyUzRDEpJTBBJTIwJTIwJTIwJTIwJTIzJTIwQ2FsY3VsYXRlJTIwYW5kJTIwc2NhbGUlMjB3ZWlnaHRpbmclMEElMjAlMjAlMjAlMjB3ZWlnaHRzJTIwJTNEJTIwdG9yY2guc3RhY2soJTVCKGlucHV0cyUyMCUzRCUzRCUyMGt0KS5mbG9hdCgpJTIwZm9yJTIwa3QlMjBpbiUyMGtleXRva2VuX2lkcyU1RCkuc3VtKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGF4aXMlM0QlNUIwJTJDJTIwMiU1RCUwQSUyMCUyMCUyMCUyMCklMEElMjAlMjAlMjAlMjB3ZWlnaHRzJTIwJTNEJTIwYWxwaGElMjAqJTIwKDEuMCUyMCUyQiUyMHdlaWdodHMpJTBBJTIwJTIwJTIwJTIwJTIzJTIwQ2FsY3VsYXRlJTIwd2VpZ2h0ZWQlMjBhdmVyYWdlJTBBJTIwJTIwJTIwJTIwd2VpZ2h0ZWRfbG9zcyUyMCUzRCUyMChsb3NzX3Blcl9zYW1wbGUlMjAqJTIwd2VpZ2h0cykubWVhbigpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwd2VpZ2h0ZWRfbG9zcw==",highlighted:`<span class="hljs-keyword">from</span> torch.nn <span class="hljs-keyword">import</span> CrossEntropyLoss
<span class="hljs-keyword">import</span> torch


<span class="hljs-keyword">def</span> <span class="hljs-title function_">keytoken_weighted_loss</span>(<span class="hljs-params">inputs, logits, keytoken_ids, alpha=<span class="hljs-number">1.0</span></span>):
    <span class="hljs-comment"># Shift so that tokens &lt; n predict n</span>
    shift_labels = inputs[..., <span class="hljs-number">1</span>:].contiguous()
    shift_logits = logits[..., :-<span class="hljs-number">1</span>, :].contiguous()
    <span class="hljs-comment"># Calculate per-token loss</span>
    loss_fct = CrossEntropyLoss(reduce=<span class="hljs-literal">False</span>)
    loss = loss_fct(shift_logits.view(-<span class="hljs-number">1</span>, shift_logits.size(-<span class="hljs-number">1</span>)), shift_labels.view(-<span class="hljs-number">1</span>))
    <span class="hljs-comment"># Resize and average loss per sample</span>
    loss_per_sample = loss.view(shift_logits.size(<span class="hljs-number">0</span>), shift_logits.size(<span class="hljs-number">1</span>)).mean(axis=<span class="hljs-number">1</span>)
    <span class="hljs-comment"># Calculate and scale weighting</span>
    weights = torch.stack([(inputs == kt).<span class="hljs-built_in">float</span>() <span class="hljs-keyword">for</span> kt <span class="hljs-keyword">in</span> keytoken_ids]).<span class="hljs-built_in">sum</span>(
        axis=[<span class="hljs-number">0</span>, <span class="hljs-number">2</span>]
    )
    weights = alpha * (<span class="hljs-number">1.0</span> + weights)
    <span class="hljs-comment"># Calculate weighted average</span>
    weighted_loss = (loss_per_sample * weights).mean()
    <span class="hljs-keyword">return</span> weighted_loss`,wrap:!1}}),Y=new h({props:{code:"ZnJvbSUyMHRvcmNoLnV0aWxzLmRhdGEuZGF0YWxvYWRlciUyMGltcG9ydCUyMERhdGFMb2FkZXIlMEElMEF0b2tlbml6ZWRfZGF0YXNldC5zZXRfZm9ybWF0KCUyMnRvcmNoJTIyKSUwQXRyYWluX2RhdGFsb2FkZXIlMjAlM0QlMjBEYXRhTG9hZGVyKHRva2VuaXplZF9kYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQlMkMlMjBiYXRjaF9zaXplJTNEMzIlMkMlMjBzaHVmZmxlJTNEVHJ1ZSklMEFldmFsX2RhdGFsb2FkZXIlMjAlM0QlMjBEYXRhTG9hZGVyKHRva2VuaXplZF9kYXRhc2V0JTVCJTIydmFsaWQlMjIlNUQlMkMlMjBiYXRjaF9zaXplJTNEMzIp",highlighted:`<span class="hljs-keyword">from</span> torch.utils.data.dataloader <span class="hljs-keyword">import</span> DataLoader

tokenized_dataset.set_format(<span class="hljs-string">&quot;torch&quot;</span>)
train_dataloader = DataLoader(tokenized_dataset[<span class="hljs-string">&quot;train&quot;</span>], batch_size=<span class="hljs-number">32</span>, shuffle=<span class="hljs-literal">True</span>)
eval_dataloader = DataLoader(tokenized_dataset[<span class="hljs-string">&quot;valid&quot;</span>], batch_size=<span class="hljs-number">32</span>)`,wrap:!1}}),H=new h({props:{code:"d2VpZ2h0X2RlY2F5JTIwJTNEJTIwMC4xJTBBJTBBJTBBZGVmJTIwZ2V0X2dyb3VwZWRfcGFyYW1zKG1vZGVsJTJDJTIwbm9fZGVjYXklM0QlNUIlMjJiaWFzJTIyJTJDJTIwJTIyTGF5ZXJOb3JtLndlaWdodCUyMiU1RCklM0ElMEElMjAlMjAlMjAlMjBwYXJhbXNfd2l0aF93ZCUyQyUyMHBhcmFtc193aXRob3V0X3dkJTIwJTNEJTIwJTVCJTVEJTJDJTIwJTVCJTVEJTBBJTIwJTIwJTIwJTIwZm9yJTIwbiUyQyUyMHAlMjBpbiUyMG1vZGVsLm5hbWVkX3BhcmFtZXRlcnMoKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGlmJTIwYW55KG5kJTIwaW4lMjBuJTIwZm9yJTIwbmQlMjBpbiUyMG5vX2RlY2F5KSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHBhcmFtc193aXRob3V0X3dkLmFwcGVuZChwKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGVsc2UlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwYXJhbXNfd2l0aF93ZC5hcHBlbmQocCklMEElMjAlMjAlMjAlMjByZXR1cm4lMjAlNUIlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlN0IlMjJwYXJhbXMlMjIlM0ElMjBwYXJhbXNfd2l0aF93ZCUyQyUyMCUyMndlaWdodF9kZWNheSUyMiUzQSUyMHdlaWdodF9kZWNheSU3RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU3QiUyMnBhcmFtcyUyMiUzQSUyMHBhcmFtc193aXRob3V0X3dkJTJDJTIwJTIyd2VpZ2h0X2RlY2F5JTIyJTNBJTIwMC4wJTdEJTJDJTBBJTIwJTIwJTIwJTIwJTVE",highlighted:`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">&quot;bias&quot;</span>, <span class="hljs-string">&quot;LayerNorm.weight&quot;</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">&quot;params&quot;</span>: params_with_wd, <span class="hljs-string">&quot;weight_decay&quot;</span>: weight_decay},
        {<span class="hljs-string">&quot;params&quot;</span>: params_without_wd, <span class="hljs-string">&quot;weight_decay&quot;</span>: <span class="hljs-number">0.0</span>},
    ]`,wrap:!1}}),nl=new h({props:{code:"ZGVmJTIwZXZhbHVhdGUoKSUzQSUwQSUyMCUyMCUyMCUyMG1vZGVsLmV2YWwoKSUwQSUyMCUyMCUyMCUyMGxvc3NlcyUyMCUzRCUyMCU1QiU1RCUwQSUyMCUyMCUyMCUyMGZvciUyMHN0ZXAlMkMlMjBiYXRjaCUyMGluJTIwZW51bWVyYXRlKGV2YWxfZGF0YWxvYWRlciklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB3aXRoJTIwdG9yY2gubm9fZ3JhZCgpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwb3V0cHV0cyUyMCUzRCUyMG1vZGVsKGJhdGNoJTVCJTIyaW5wdXRfaWRzJTIyJTVEJTJDJTIwbGFiZWxzJTNEYmF0Y2glNUIlMjJpbnB1dF9pZHMlMjIlNUQpJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbG9zc2VzLmFwcGVuZChhY2NlbGVyYXRvci5nYXRoZXIob3V0cHV0cy5sb3NzKSklMEElMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwdG9yY2gubWVhbih0b3JjaC5jYXQobG9zc2VzKSklMEElMjAlMjAlMjAlMjB0cnklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwZXJwbGV4aXR5JTIwJTNEJTIwdG9yY2guZXhwKGxvc3MpJTBBJTIwJTIwJTIwJTIwZXhjZXB0JTIwT3ZlcmZsb3dFcnJvciUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHBlcnBsZXhpdHklMjAlM0QlMjBmbG9hdCglMjJpbmYlMjIpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwbG9zcy5pdGVtKCklMkMlMjBwZXJwbGV4aXR5Lml0ZW0oKQ==",highlighted:`<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">&quot;input_ids&quot;</span>], labels=batch[<span class="hljs-string">&quot;input_ids&quot;</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">&quot;inf&quot;</span>)
    <span class="hljs-keyword">return</span> loss.item(), perplexity.item()`,wrap:!1}}),rl=new h({props:{code:"bW9kZWwlMjAlM0QlMjBHUFQyTE1IZWFkTW9kZWwoY29uZmlnKQ==",highlighted:"model = GPT2LMHeadModel(config)",wrap:!1}}),S=new h({props:{code:"ZnJvbSUyMHRvcmNoLm9wdGltJTIwaW1wb3J0JTIwQWRhbVclMEElMEFvcHRpbWl6ZXIlMjAlM0QlMjBBZGFtVyhnZXRfZ3JvdXBlZF9wYXJhbXMobW9kZWwpJTJDJTIwbHIlM0Q1ZS00KQ==",highlighted:`<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>)`,wrap:!1}}),q=new h({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUwQWFjY2VsZXJhdG9yJTIwJTNEJTIwQWNjZWxlcmF0b3IoZnAxNiUzRFRydWUpJTBBJTBBbW9kZWwlMkMlMjBvcHRpbWl6ZXIlMkMlMjB0cmFpbl9kYXRhbG9hZGVyJTJDJTIwZXZhbF9kYXRhbG9hZGVyJTIwJTNEJTIwYWNjZWxlcmF0b3IucHJlcGFyZSglMEElMjAlMjAlMjAlMjBtb2RlbCUyQyUyMG9wdGltaXplciUyQyUyMHRyYWluX2RhdGFsb2FkZXIlMkMlMjBldmFsX2RhdGFsb2FkZXIlMEEp",highlighted:`<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
)`,wrap:!1}}),Jl=new ys({props:{$$slots:{default:[Ra]},$$scope:{ctx:I}}}),D=new h({props:{code:"bnVtX3RyYWluX2Vwb2NocyUyMCUzRCUyMDElMEFudW1fdXBkYXRlX3N0ZXBzX3Blcl9lcG9jaCUyMCUzRCUyMGxlbih0cmFpbl9kYXRhbG9hZGVyKSUwQW51bV90cmFpbmluZ19zdGVwcyUyMCUzRCUyMG51bV90cmFpbl9lcG9jaHMlMjAqJTIwbnVtX3VwZGF0ZV9zdGVwc19wZXJfZXBvY2glMEElMEFscl9zY2hlZHVsZXIlMjAlM0QlMjBnZXRfc2NoZWR1bGVyKCUwQSUyMCUyMCUyMCUyMG5hbWUlM0QlMjJsaW5lYXIlMjIlMkMlMEElMjAlMjAlMjAlMjBvcHRpbWl6ZXIlM0RvcHRpbWl6ZXIlMkMlMEElMjAlMjAlMjAlMjBudW1fd2FybXVwX3N0ZXBzJTNEMV8wMDAlMkMlMEElMjAlMjAlMjAlMjBudW1fdHJhaW5pbmdfc3RlcHMlM0RudW1fdHJhaW5pbmdfc3RlcHMlMkMlMEEp",highlighted:`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">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">1_000</span>,
    num_training_steps=num_training_steps,
)`,wrap:!1}}),L=new h({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMFJlcG9zaXRvcnklMkMlMjBnZXRfZnVsbF9yZXBvX25hbWUlMEElMEFtb2RlbF9uYW1lJTIwJTNEJTIwJTIyY29kZXBhcnJvdC1kcy1hY2NlbGVyYXRlJTIyJTBBcmVwb19uYW1lJTIwJTNEJTIwZ2V0X2Z1bGxfcmVwb19uYW1lKG1vZGVsX25hbWUpJTBBcmVwb19uYW1l",highlighted:`<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">&quot;codeparrot-ds-accelerate&quot;</span>
repo_name = get_full_repo_name(model_name)
repo_name`,wrap:!1}}),z=new h({props:{code:"J3NndWdnZXIlMkZjb2RlcGFycm90LWRzLWFjY2VsZXJhdGUn",highlighted:'<span class="hljs-string">&#x27;sgugger/codeparrot-ds-accelerate&#x27;</span>',wrap:!1}}),P=new h({props:{code:"b3V0cHV0X2RpciUyMCUzRCUyMCUyMmNvZGVwYXJyb3QtZHMtYWNjZWxlcmF0ZSUyMiUwQXJlcG8lMjAlM0QlMjBSZXBvc2l0b3J5KG91dHB1dF9kaXIlMkMlMjBjbG9uZV9mcm9tJTNEcmVwb19uYW1lKQ==",highlighted:`output_dir = <span class="hljs-string">&quot;codeparrot-ds-accelerate&quot;</span>
repo = Repository(output_dir, clone_from=repo_name)`,wrap:!1}}),Al=new h({props:{code:"ZXZhbHVhdGUoKQ==",highlighted:"evaluate()",wrap:!1}}),ll=new h({props:{code:"KDEwLjkzNDEyNjg1Mzk0Mjg3MSUyQyUyMDU2MDU3LjE0NDUzMTI1KQ==",highlighted:'(<span class="hljs-number">10.934126853942871</span>, <span class="hljs-number">56057.14453125</span>)',wrap:!1}}),El=new h({props:{code:"ZnJvbSUyMHRxZG0ubm90ZWJvb2slMjBpbXBvcnQlMjB0cWRtJTBBJTBBZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTIwJTNEJTIwOCUwQWV2YWxfc3RlcHMlMjAlM0QlMjA1XzAwMCUwQSUwQW1vZGVsLnRyYWluKCklMEFjb21wbGV0ZWRfc3RlcHMlMjAlM0QlMjAwJTBBZm9yJTIwZXBvY2glMjBpbiUyMHJhbmdlKG51bV90cmFpbl9lcG9jaHMpJTNBJTBBJTIwJTIwJTIwJTIwZm9yJTIwc3RlcCUyQyUyMGJhdGNoJTIwaW4lMjB0cWRtKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGVudW1lcmF0ZSh0cmFpbl9kYXRhbG9hZGVyJTJDJTIwc3RhcnQlM0QxKSUyQyUyMHRvdGFsJTNEbGVuKHRyYWluX2RhdGFsb2FkZXIpJTBBJTIwJTIwJTIwJTIwKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxvZ2l0cyUyMCUzRCUyMG1vZGVsKGJhdGNoJTVCJTIyaW5wdXRfaWRzJTIyJTVEKS5sb2dpdHMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwa2V5dG9rZW5fd2VpZ2h0ZWRfbG9zcyhiYXRjaCU1QiUyMmlucHV0X2lkcyUyMiU1RCUyQyUyMGxvZ2l0cyUyQyUyMGtleXRva2VuX2lkcyklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpZiUyMHN0ZXAlMjAlMjUlMjAxMDAlMjAlM0QlM0QlMjAwJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IucHJpbnQoJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIybHIlMjIlM0ElMjBnZXRfbHIoKSUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnNhbXBsZXMlMjIlM0ElMjBzdGVwJTIwKiUyMHNhbXBsZXNfcGVyX3N0ZXAlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJzdGVwcyUyMiUzQSUyMGNvbXBsZXRlZF9zdGVwcyUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMmxvc3MlMkZ0cmFpbiUyMiUzQSUyMGxvc3MuaXRlbSgpJTIwKiUyMGdyYWRpZW50X2FjY3VtdWxhdGlvbl9zdGVwcyUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU3RCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwbG9zcyUyMCUyRiUyMGdyYWRpZW50X2FjY3VtdWxhdGlvbl9zdGVwcyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGFjY2VsZXJhdG9yLmJhY2t3YXJkKGxvc3MpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaWYlMjBzdGVwJTIwJTI1JTIwZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTIwJTNEJTNEJTIwMCUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGFjY2VsZXJhdG9yLmNsaXBfZ3JhZF9ub3JtXyhtb2RlbC5wYXJhbWV0ZXJzKCklMkMlMjAxLjApJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwb3B0aW1pemVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxyX3NjaGVkdWxlci5zdGVwKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuemVyb19ncmFkKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjb21wbGV0ZWRfc3RlcHMlMjAlMkIlM0QlMjAxJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaWYlMjAoc3RlcCUyMCUyNSUyMChldmFsX3N0ZXBzJTIwKiUyMGdyYWRpZW50X2FjY3VtdWxhdGlvbl9zdGVwcykpJTIwJTNEJTNEJTIwMCUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGV2YWxfbG9zcyUyQyUyMHBlcnBsZXhpdHklMjAlM0QlMjBldmFsdWF0ZSgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IucHJpbnQoJTdCJTIybG9zcyUyRmV2YWwlMjIlM0ElMjBldmFsX2xvc3MlMkMlMjAlMjJwZXJwbGV4aXR5JTIyJTNBJTIwcGVycGxleGl0eSU3RCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBtb2RlbC50cmFpbigpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3Iud2FpdF9mb3JfZXZlcnlvbmUoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHVud3JhcHBlZF9tb2RlbCUyMCUzRCUyMGFjY2VsZXJhdG9yLnVud3JhcF9tb2RlbChtb2RlbCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB1bndyYXBwZWRfbW9kZWwuc2F2ZV9wcmV0cmFpbmVkKG91dHB1dF9kaXIlMkMlMjBzYXZlX2Z1bmN0aW9uJTNEYWNjZWxlcmF0b3Iuc2F2ZSklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpZiUyMGFjY2VsZXJhdG9yLmlzX21haW5fcHJvY2VzcyUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRva2VuaXplci5zYXZlX3ByZXRyYWluZWQob3V0cHV0X2RpciklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXBvLnB1c2hfdG9faHViKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNvbW1pdF9tZXNzYWdlJTNEZiUyMlRyYWluaW5nJTIwaW4lMjBwcm9ncmVzcyUyMHN0ZXAlMjAlN0JzdGVwJTdEJTIyJTJDJTIwYmxvY2tpbmclM0RGYWxzZSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCk=",highlighted:`<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=<span class="hljs-built_in">len</span>(train_dataloader)
    ):
        logits = model(batch[<span class="hljs-string">&quot;input_ids&quot;</span>]).logits
        loss = keytoken_weighted_loss(batch[<span class="hljs-string">&quot;input_ids&quot;</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">&quot;lr&quot;</span>: get_lr(),
                    <span class="hljs-string">&quot;samples&quot;</span>: step * samples_per_step,
                    <span class="hljs-string">&quot;steps&quot;</span>: completed_steps,
                    <span class="hljs-string">&quot;loss/train&quot;</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">&quot;loss/eval&quot;</span>: eval_loss, <span class="hljs-string">&quot;perplexity&quot;</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&quot;Training in progress step <span class="hljs-subst">{step}</span>&quot;</span>, blocking=<span class="hljs-literal">False</span>
                )`,wrap:!1}}),N=new ys({props:{$$slots:{default:[Wa]},$$scope:{ctx:I}}}),ul=new ys({props:{$$slots:{default:[Qa]},$$scope:{ctx:I}}}),{c(){m(t.$$.fragment),y=p(),n=j("p"),n.innerHTML=J,_=p(),m(f.$$.fragment),g=p(),$=j("p"),$.innerHTML=Z,o=p(),m(C.$$.fragment),G=p(),m(v.$$.fragment),E=p(),W=j("p"),W.textContent=k,X=p(),m(x.$$.fragment),Vl=p(),F=j("p"),F.textContent=we,zl=p(),B=j("ul"),B.innerHTML=ue,Nl=p(),R=j("p"),R.innerHTML=je,vl=p(),m(Y.$$.fragment),cs=p(),tl=j("p"),tl.textContent=gl,xl=p(),m(H.$$.fragment),is=p(),al=j("p"),al.textContent=Gl,Js=p(),m(nl.$$.fragment),Ml=p(),pl=j("p"),pl.innerHTML=Xl,os=p(),m(rl.$$.fragment),yl=p(),cl=j("p"),cl.textContent=Bl,Fl=p(),m(S.$$.fragment),Us=p(),il=j("p"),il.textContent=Rl,Yl=p(),m(q.$$.fragment),ms=p(),m(Jl.$$.fragment),ol=p(),Ul=j("p"),Ul.innerHTML=Wl,Hl=p(),m(D.$$.fragment),Ts=p(),ml=j("p"),ml.innerHTML=Ql,Sl=p(),m(L.$$.fragment),ql=p(),m(z.$$.fragment),Dl=p(),K=j("p"),K.textContent=de,Ll=p(),m(P.$$.fragment),Kl=p(),O=j("p"),O.innerHTML=ws,Tl=p(),m(Al.$$.fragment),Pl=p(),m(ll.$$.fragment),Ol=p(),sl=j("p"),sl.innerHTML=us,wl=p(),m(El.$$.fragment),ls=p(),el=j("p"),el.textContent=be,ss=p(),m(N.$$.fragment),js=p(),m(ul.$$.fragment)},l(s){T(t.$$.fragment,s),y=r(s),n=d(s,"P",{"data-svelte-h":!0}),b(n)!=="svelte-1vyez8"&&(n.innerHTML=J),_=r(s),T(f.$$.fragment,s),g=r(s),$=d(s,"P",{"data-svelte-h":!0}),b($)!=="svelte-1p47x73"&&($.innerHTML=Z),o=r(s),T(C.$$.fragment,s),G=r(s),T(v.$$.fragment,s),E=r(s),W=d(s,"P",{"data-svelte-h":!0}),b(W)!=="svelte-1bnwbrb"&&(W.textContent=k),X=r(s),T(x.$$.fragment,s),Vl=r(s),F=d(s,"P",{"data-svelte-h":!0}),b(F)!=="svelte-4480va"&&(F.textContent=we),zl=r(s),B=d(s,"UL",{"data-svelte-h":!0}),b(B)!=="svelte-1hho8k5"&&(B.innerHTML=ue),Nl=r(s),R=d(s,"P",{"data-svelte-h":!0}),b(R)!=="svelte-1jnyd3b"&&(R.innerHTML=je),vl=r(s),T(Y.$$.fragment,s),cs=r(s),tl=d(s,"P",{"data-svelte-h":!0}),b(tl)!=="svelte-k4ws43"&&(tl.textContent=gl),xl=r(s),T(H.$$.fragment,s),is=r(s),al=d(s,"P",{"data-svelte-h":!0}),b(al)!=="svelte-lbudkm"&&(al.textContent=Gl),Js=r(s),T(nl.$$.fragment,s),Ml=r(s),pl=d(s,"P",{"data-svelte-h":!0}),b(pl)!=="svelte-lubkiv"&&(pl.innerHTML=Xl),os=r(s),T(rl.$$.fragment,s),yl=r(s),cl=d(s,"P",{"data-svelte-h":!0}),b(cl)!=="svelte-1dz6iug"&&(cl.textContent=Bl),Fl=r(s),T(S.$$.fragment,s),Us=r(s),il=d(s,"P",{"data-svelte-h":!0}),b(il)!=="svelte-182mazh"&&(il.textContent=Rl),Yl=r(s),T(q.$$.fragment,s),ms=r(s),T(Jl.$$.fragment,s),ol=r(s),Ul=d(s,"P",{"data-svelte-h":!0}),b(Ul)!=="svelte-1os5172"&&(Ul.innerHTML=Wl),Hl=r(s),T(D.$$.fragment,s),Ts=r(s),ml=d(s,"P",{"data-svelte-h":!0}),b(ml)!=="svelte-1gs3lys"&&(ml.innerHTML=Ql),Sl=r(s),T(L.$$.fragment,s),ql=r(s),T(z.$$.fragment,s),Dl=r(s),K=d(s,"P",{"data-svelte-h":!0}),b(K)!=="svelte-1fj9jwz"&&(K.textContent=de),Ll=r(s),T(P.$$.fragment,s),Kl=r(s),O=d(s,"P",{"data-svelte-h":!0}),b(O)!=="svelte-ew2ylh"&&(O.innerHTML=ws),Tl=r(s),T(Al.$$.fragment,s),Pl=r(s),T(ll.$$.fragment,s),Ol=r(s),sl=d(s,"P",{"data-svelte-h":!0}),b(sl)!=="svelte-1vo8k3"&&(sl.innerHTML=us),wl=r(s),T(El.$$.fragment,s),ls=r(s),el=d(s,"P",{"data-svelte-h":!0}),b(el)!=="svelte-ep83k9"&&(el.textContent=be),ss=r(s),T(N.$$.fragment,s),js=r(s),T(ul.$$.fragment,s)},m(s,U){w(t,s,U),a(s,y,U),a(s,n,U),a(s,_,U),w(f,s,U),a(s,g,U),a(s,$,U),a(s,o,U),w(C,s,U),a(s,G,U),w(v,s,U),a(s,E,U),a(s,W,U),a(s,X,U),w(x,s,U),a(s,Vl,U),a(s,F,U),a(s,zl,U),a(s,B,U),a(s,Nl,U),a(s,R,U),a(s,vl,U),w(Y,s,U),a(s,cs,U),a(s,tl,U),a(s,xl,U),w(H,s,U),a(s,is,U),a(s,al,U),a(s,Js,U),w(nl,s,U),a(s,Ml,U),a(s,pl,U),a(s,os,U),w(rl,s,U),a(s,yl,U),a(s,cl,U),a(s,Fl,U),w(S,s,U),a(s,Us,U),a(s,il,U),a(s,Yl,U),w(q,s,U),a(s,ms,U),w(Jl,s,U),a(s,ol,U),a(s,Ul,U),a(s,Hl,U),w(D,s,U),a(s,Ts,U),a(s,ml,U),a(s,Sl,U),w(L,s,U),a(s,ql,U),w(z,s,U),a(s,Dl,U),a(s,K,U),a(s,Ll,U),w(P,s,U),a(s,Kl,U),a(s,O,U),a(s,Tl,U),w(Al,s,U),a(s,Pl,U),w(ll,s,U),a(s,Ol,U),a(s,sl,U),a(s,wl,U),w(El,s,U),a(s,ls,U),a(s,el,U),a(s,ss,U),w(N,s,U),a(s,js,U),w(ul,s,U),jl=!0},i(s){jl||(c(t.$$.fragment,s),c(f.$$.fragment,s),c(C.$$.fragment,s),c(v.$$.fragment,s),c(x.$$.fragment,s),c(Y.$$.fragment,s),c(H.$$.fragment,s),c(nl.$$.fragment,s),c(rl.$$.fragment,s),c(S.$$.fragment,s),c(q.$$.fragment,s),c(Jl.$$.fragment,s),c(D.$$.fragment,s),c(L.$$.fragment,s),c(z.$$.fragment,s),c(P.$$.fragment,s),c(Al.$$.fragment,s),c(ll.$$.fragment,s),c(El.$$.fragment,s),c(N.$$.fragment,s),c(ul.$$.fragment,s),jl=!0)},o(s){i(t.$$.fragment,s),i(f.$$.fragment,s),i(C.$$.fragment,s),i(v.$$.fragment,s),i(x.$$.fragment,s),i(Y.$$.fragment,s),i(H.$$.fragment,s),i(nl.$$.fragment,s),i(rl.$$.fragment,s),i(S.$$.fragment,s),i(q.$$.fragment,s),i(Jl.$$.fragment,s),i(D.$$.fragment,s),i(L.$$.fragment,s),i(z.$$.fragment,s),i(P.$$.fragment,s),i(Al.$$.fragment,s),i(ll.$$.fragment,s),i(El.$$.fragment,s),i(N.$$.fragment,s),i(ul.$$.fragment,s),jl=!1},d(s){s&&(e(y),e(n),e(_),e(g),e($),e(o),e(G),e(E),e(W),e(X),e(Vl),e(F),e(zl),e(B),e(Nl),e(R),e(vl),e(cs),e(tl),e(xl),e(is),e(al),e(Js),e(Ml),e(pl),e(os),e(yl),e(cl),e(Fl),e(Us),e(il),e(Yl),e(ms),e(ol),e(Ul),e(Hl),e(Ts),e(ml),e(Sl),e(ql),e(Dl),e(K),e(Ll),e(Kl),e(O),e(Tl),e(Pl),e(Ol),e(sl),e(wl),e(ls),e(el),e(ss),e(js)),u(t,s),u(f,s),u(C,s),u(v,s),u(x,s),u(Y,s),u(H,s),u(nl,s),u(rl,s),u(S,s),u(q,s),u(Jl,s),u(D,s),u(L,s),u(z,s),u(P,s),u(Al,s),u(ll,s),u(El,s),u(N,s),u(ul,s)}}}function Ra(I){let t,y='🚨 如果您在 TPU 上進行訓練，則需要將從上面的單元格開始的所有代碼移動到專用的訓練函數中。有關詳細信息，請參閱 <a href="/course/chapter3">第 3 章</a> for more details.';return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-b54156"&&(t.innerHTML=y)},m(n,J){a(n,t,J)},p:pe,d(n){n&&e(t)}}}function Wa(I){let t,y="✏️ <strong>試試看!</strong> 創建適合您的用例的自定義損失函數，或在訓練循環中添加另一個自定義步驟。";return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-1t4u52j"&&(t.innerHTML=y)},m(n,J){a(n,t,J)},p:pe,d(n){n&&e(t)}}}function Qa(I){let t,y="✏️ <strong>試試看!</strong> 在運行長時間的訓練實驗時，最好使用 TensorBoard 或 Weights Biases 等工具記錄重要指標。向訓練循環添加適當的日誌記錄，以便您始終可以檢查訓練的進行情況。going.";return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),b(t)!=="svelte-11x8ujc"&&(t.innerHTML=y)},m(n,J){a(n,t,J)},p:pe,d(n){n&&e(t)}}}function Aa(I){let t,y,n,J,_,f,g,$,Z,o,C,G,v='到目前為止，我們主要使用預訓練模型，並通過重用預訓練的權重來針對新用例對它們進行微調。正如我們在<a href="/course/chapter1">第一章</a>, 這通常稱為遷移學習，這是將 Transformer 模型應用於大多數標記數據稀疏的現實世界用例的非常成功的策略。在本章中，我們將採用不同的方法並從頭開始訓練一個全新的模型。如果您有大量數據並且它與用於可用模型的預訓練數據有很大不同，那麼這是一個很好的方法。然而，它也需要更多的計算資源來預訓練語言模型，而不僅僅是微調現有的模型。訓練新模型有意義的示例包括由音符、分子序列（如 DNA）或編程語言組成的數據集。後者最近受到關注，這要歸功於 TabNine 和 GitHub 的 Copilot 等工具，它們由 OpenAI 的 Codex 模型提供支持，可以生成長代碼序列。這種文本生成任務最好使用自迴歸或因果語言模型（例如 GPT-2）來解決。',E,W,k="在本節中，我們將構建代碼生成模型的縮小版本：我們將使用 Python 代碼的子集專注於單行完成而不是完整的函數或類。在 Python 中處理數據時，您會經常接觸 Python 數據科學堆棧，包括 <code>matplotlib</code> , <code>seaborn</code> , <code>pandas</code> ， 和 <code>scikit-learn</code> 庫。在使用這些框架時，通常需要查找特定的命令，因此如果我們可以使用模型來為我們完成這些調用，那就太好了。",X,x,Vl,F,we='在<a href="/course/chapter6">第六章</a> 我們創建了一個高效的分詞器來處理 Python 源代碼，但我們仍然需要一個大規模數據集來預訓練模型。在這裡，我們將我們的分詞器應用到源自 GitHub 存儲庫的 Python 代碼語料庫。然後我們將使用 <code>Trainer</code> API 和 🤗 Accelerate 來訓練模型。讓我們開始吧！',zl,B,ue,Nl,R,je,vl,Y,cs='這實際上展示了使用本節中訓練並上傳到 Hub 的模型。你可以在<a href="https://huggingface.co/huggingface-course/codeparrot-ds?text=plt.imshow%28" rel="nofollow">這裡</a>找到。請注意，由於在文本生成過程中發生了一些隨機化，您可能會得到略有不同的結果。',tl,gl,xl,H,is='Python 代碼可以從 GitHub 等代碼存儲庫中獲得，我們可以通過抓取每個 Python 存儲庫來使用它們來創建數據集。這是在<a href="https://learning.oreilly.com/library/view/natural-language-processing/9781098103231/" rel="nofollow">Transformers textbook</a>預訓練大型的GPT-2 模型。使用大約 180 GB 的 GitHub 轉儲，其中包含大約 2000 萬個 Python 文件，稱為 <code>codeparrot</code> ，作者構建了一個數據集，然後在<a href="https://huggingface.co/datasets/transformersbook/codeparrot" rel="nofollow">Hugging Face Hub</a>上分享出來了.',al,Gl,Js="然而，對完整語料庫的訓練既耗時又費力，我們只需要與 Python 數據科學堆棧相關的數據集子集。所以，讓我們開始過濾 <code>codeparrot</code> 包含此堆棧中任何庫的所有文件的數據集。由於數據集的太大，我們希望避免下載它；因此反，我們將使用流功能來動態過濾它。為了使用前面提到的庫過濾代碼示例，我們將使用以下函數：",nl,Ml,pl,Xl,os="讓我們用兩個例子來測試一下：",rl,yl,cl,Bl,Fl,S,Us="我們可以使用它來創建一個函數來流式傳輸數據集並過濾我們想要的元素：",il,Rl,Yl,q,ms="然後我們可以簡單地將此函數應用於流數據集：",Jl,ol,Ul,Wl,Hl,D,Ts="這給我們留下了大約 3% 的原始數據集，這個數據集仍然相當可觀——結果數據集有 6 GB，包含 600,000 個 Python 腳本！過濾完整數據集可能需要 2-3 小時，具體取決於您的機器和帶寬。如果您不想自己經歷這個漫長的過程，我們在 Hub 上提供過濾後的數據集供您下載：",ml,Ql,Sl,L,ql,z,Dl,K,de="讓我們看一個來自數據集的例子。我們將只顯示每個字段的前 200 個字符：",Ll,P,Kl,O,ws,Tl,Al="我們可以看到 <code>content</code> 字段包含我們希望我們的模型訓練的代碼。現在我們有了一個數據集，我們需要預處理文本，使其採用適合預訓練的格式。",Pl,ll,Ol,sl,us,wl,El="第一步是對數據進行標記，以便我們可以將其用於訓練。由於我們的目標主要是自動完成短函數調用，因此我們可以保持上下文大小相對較小。這樣做的好處是我們可以更快地訓練模型並且它需要的內存顯著減少。如果您的應用程序擁有更多上下文很重要（例如，如果您希望模型基於具有函數定義的文件編寫單元測試），請確保增加該數量，但請記住，這需要更大的 GPU 內存佔用。現在，讓我們將上下文大小固定為 128 個標記，而不是 GPT-2 或 GPT-3 中分別使用的 1,024 或 2,048 個標記。",ls,el,be='大多數文檔包含超過 128 個標記，因此簡單地將輸入截斷到最大長度將消除我們數據集的很大一部分。相反，我們將使用 <code>return_overflowing_tokens</code> 標記整個輸入並將其分成幾個塊的選項，就像我們在<a href="/course/chapter6/4">第六章</a>. 我們還將使用 <code>return_length</code> 選項自動返回每個創建的塊的長度。通常最後一個塊會小於上下文大小，我們會去掉這些塊以避免填充問題；因為無論如何我們都有大量數據。',ss,N,js='<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."/>',ul,jl,s="讓我們通過查看前兩個示例來確切瞭解這是如何工作的：",U,ds,Ce,bs,he,fs,ut="我們可以看 到，從這兩個示例中我們總共得到了 34 個片段。查看塊長度，我們可以看到兩個文檔末尾的塊都少於 128 個標記（分別為 117 和 41）。這些僅代表我們擁有的數據集的一小部分，因此我們可以安全地將它們扔掉。通過 <code>overflow_to_sample_mapping</code> 字段，我們還可以重建哪些塊屬於哪些輸入樣本。",Ie,Cs,jt='通過這個操作，我們使用了一個方便的🤗 Datasets 中的<code>Dataset.map()</code> 函數，就是不需要一對一的映射；正如我們在<a href="/course/chapter7/3">第三節</a>,我們可以創建具有比輸入批次更多或更少元素的批次。這在執行更改元素數量的數據增強或數據過濾等操作時非常有用。在我們的例子中，當將每個元素標記為指定上下文大小的塊時，我們從每個文檔中創建了許多樣本。我們只需要確保刪除現有的列，因為它們的大小存在衝突。如果我們想保留它們，我們可以適當地重複它們，並在<code>Dataset.map()</code> 調用中返回它們:',_e,hs,ke,Is,Ze,_s,dt="我們現在有 1670 萬個示例，每個示例有 128 個tokens ，總共相當於大約 21 億個tokens 。作為參考，OpenAI 的 GPT-3 和 Codex 模型分別在 300 和 1000 億個tokens 上訓練，其中 Codex 模型從 GPT-3 檢查點初始化。我們在本節中的目標不是與這些模型競爭，這些模型可以生成長而連貫的文本，而是創建一個縮小版本，為數據科學家提供快速自動完成功能。",$e,ks,bt="現在我們已經準備好了數據集，讓我們設置模型！",ge,ts,Ge,Zs,Xe,$s,ft="我們的第一步是新初始化一個 GPT-2 模型。我們將對我們的模型使用與小型 GPT-2 模型相同的配置，因此我們加載預訓練配置，確保分詞器大小與模型詞彙量大小匹配並設置 <code>bos</code> 和 <code>eos</code> （序列的開始和結束）令牌 ID：",Be,dl,bl,re,gs,Ct="我們的模型有 1.24 億個參數，我們必須對其進行調整。在開始訓練之前，我們需要設置一個負責創建批次的數據整理器。我們可以使用 <code>DataCollatorForLanguageModeling</code> ，它是專為語言建模而設計（顧名思義）。除了堆疊和填充批次，它還負責創建語言模型標籤——在因果語言建模中，輸入也用作標籤（只是移動了一個元素），並且這個數據整理器在訓練期間即時創建它們，所以我們不需要複製 <code>input_ids</code>。",Re,Gs,ht="注意 <code>DataCollatorForLanguageModeling</code> 支持掩碼語言建模 (MLM) 和因果語言建模 (CLM)。默認情況下它為 MLM 準備數據，但我們可以通過設置<code>mlm=False</code>參數切換到 CLM ：",We,fl,Cl,ye,Xs,It="讓我們看一個例子：",Qe,Bs,Ae,hl,Il,ce,Rs,_t="我們可以看到示例已經堆疊在一起，並且所有張量都具有相同的形狀。",Ee,ie,as,Ve,Ws,kt="現在我們已經準備好實際訓練我們的模型的一切了——畢竟這不是那麼多工作！在我們開始訓練之前，我們應該登錄 Hugging Face。如果您在筆記本上工作，則可以使用以下實用程序功能：",ze,Qs,Ne,As,Zt="這將顯示一個小部件，您可以在其中輸入您的 Hugging Face 登錄憑據。",ve,Es,$t="如果您不是在notebook上工作，只需在終端中輸入以下行：",xe,Vs,Fe,_l,kl,Je,ns,Ye,Ms,He,zs,Se,Ns,gt="現在是關鍵的部分：讓我們看看經過訓練的模型的實際效果如何！我們可以在日誌中看到損失穩步下降，但為了讓模型進行測試，讓我們看看它在某些測試上的表現如何。為此，我們將模型包裝在文本生成中的<code>pipeline</code> ，如果有可用的，我們會將它放在 GPU 上進行快速生成：",qe,Zl,$l,oe,vs,Gt="讓我們從創建散點圖的簡單任務開始：",De,xs,Le,Fs,Ke,Ys,Xt="結果看起來是正確的。它也適用於 <code>pandas</code> 類型？讓我們看看我們是否使用兩個數組可以創建一個 <code>DataFrame</code> ：",Pe,Hs,Oe,Ss,lt,qs,Bt="很好，這是正確的答案——儘管它隨後再次插入了列 <code>x</code> 。由於生成的token數量有限，以下 <code>for</code> 循環被切斷。讓我們看看我們是否可以做一些更復雜的事情並讓模型幫助我們分組操作：",st,Ds,et,Ls,tt,Ks,Rt="不錯;這是正確的做法。最後，讓我們看看我們是否也可以將其用於 <code>scikit-learn</code> 並建立一個隨機森林模型：",at,Ps,nt,Os,Mt,Ue,me,le,pt,fe,rt;_=new ia({props:{fw:I[0]}}),g=new Te({props:{title:"從頭開始訓練因果語言模型",local:"從頭開始訓練因果語言模型",headingTag:"h1"}});const Wt=[Ua,oa],se=[];function Qt(l,M){return l[0]==="pt"?0:1}Z=Qt(I),o=se[Z]=Wt[Z](I),x=new wt({props:{id:"Vpjb1lu0MDk"}}),gl=new Te({props:{title:"收集數據",local:"收集數據",headingTag:"h2"}}),Ml=new h({props:{code:"ZGVmJTIwYW55X2tleXdvcmRfaW5fc3RyaW5nKHN0cmluZyUyQyUyMGtleXdvcmRzKSUzQSUwQSUyMCUyMCUyMCUyMGZvciUyMGtleXdvcmQlMjBpbiUyMGtleXdvcmRzJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaWYlMjBrZXl3b3JkJTIwaW4lMjBzdHJpbmclM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm4lMjBUcnVlJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwRmFsc2U=",highlighted:`<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>`,wrap:!1}}),yl=new h({props:{code:"ZmlsdGVycyUyMCUzRCUyMCU1QiUyMnBhbmRhcyUyMiUyQyUyMCUyMnNrbGVhcm4lMjIlMkMlMjAlMjJtYXRwbG90bGliJTIyJTJDJTIwJTIyc2VhYm9ybiUyMiU1RCUwQWV4YW1wbGVfMSUyMCUzRCUyMCUyMmltcG9ydCUyMG51bXB5JTIwYXMlMjBucCUyMiUwQWV4YW1wbGVfMiUyMCUzRCUyMCUyMmltcG9ydCUyMHBhbmRhcyUyMGFzJTIwcGQlMjIlMEElMEFwcmludCglMEElMjAlMjAlMjAlMjBhbnlfa2V5d29yZF9pbl9zdHJpbmcoZXhhbXBsZV8xJTJDJTIwZmlsdGVycyklMkMlMjBhbnlfa2V5d29yZF9pbl9zdHJpbmcoZXhhbXBsZV8yJTJDJTIwZmlsdGVycyklMEEp",highlighted:`filters = [<span class="hljs-string">&quot;pandas&quot;</span>, <span class="hljs-string">&quot;sklearn&quot;</span>, <span class="hljs-string">&quot;matplotlib&quot;</span>, <span class="hljs-string">&quot;seaborn&quot;</span>]
example_1 = <span class="hljs-string">&quot;import numpy as np&quot;</span>
example_2 = <span class="hljs-string">&quot;import pandas as pd&quot;</span>

<span class="hljs-built_in">print</span>(
    any_keyword_in_string(example_1, filters), any_keyword_in_string(example_2, filters)
)`,wrap:!1}}),Bl=new h({props:{code:"RmFsc2UlMjBUcnVl",highlighted:'<span class="hljs-literal">False</span> <span class="hljs-literal">True</span>',wrap:!1}}),Rl=new h({props:{code:"ZGVmJTIwZmlsdGVyX3N0cmVhbWluZ19kYXRhc2V0KGRhdGFzZXQlMkMlMjBmaWx0ZXJzKSUzQSUwQSUyMCUyMCUyMCUyMGZpbHRlcmVkX2RpY3QlMjAlM0QlMjBkZWZhdWx0ZGljdChsaXN0KSUwQSUyMCUyMCUyMCUyMHRvdGFsJTIwJTNEJTIwMCUwQSUyMCUyMCUyMCUyMGZvciUyMHNhbXBsZSUyMGluJTIwdHFkbShpdGVyKGRhdGFzZXQpKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRvdGFsJTIwJTJCJTNEJTIwMSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGlmJTIwYW55X2tleXdvcmRfaW5fc3RyaW5nKHNhbXBsZSU1QiUyMmNvbnRlbnQlMjIlNUQlMkMlMjBmaWx0ZXJzKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZvciUyMGslMkMlMjB2JTIwaW4lMjBzYW1wbGUuaXRlbXMoKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZpbHRlcmVkX2RpY3QlNUJrJTVELmFwcGVuZCh2KSUwQSUyMCUyMCUyMCUyMHByaW50KGYlMjIlN0JsZW4oZmlsdGVyZWRfZGljdCU1Qidjb250ZW50JyU1RCklMkZ0b3RhbCUzQS4yJTI1JTdEJTIwb2YlMjBkYXRhJTIwYWZ0ZXIlMjBmaWx0ZXJpbmcuJTIyKSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMERhdGFzZXQuZnJvbV9kaWN0KGZpbHRlcmVkX2RpY3Qp",highlighted:`<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">&quot;content&quot;</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&quot;<span class="hljs-subst">{<span class="hljs-built_in">len</span>(filtered_dict[<span class="hljs-string">&#x27;content&#x27;</span>])/total:<span class="hljs-number">.2</span>%}</span> of data after filtering.&quot;</span>)
    <span class="hljs-keyword">return</span> Dataset.from_dict(filtered_dict)`,wrap:!1}}),ol=new h({props:{code:"JTIzJTIwVGhpcyUyMGNlbGwlMjB3aWxsJTIwdGFrZSUyMGElMjB2ZXJ5JTIwbG9uZyUyMHRpbWUlMjB0byUyMGV4ZWN1dGUlMkMlMjBzbyUyMHlvdSUyMHNob3VsZCUyMHNraXAlMjBpdCUyMGFuZCUyMGdvJTIwdG8lMEElMjMlMjB0aGUlMjBuZXh0JTIwb25lISUwQWZyb20lMjBkYXRhc2V0cyUyMGltcG9ydCUyMGxvYWRfZGF0YXNldCUwQSUwQXNwbGl0JTIwJTNEJTIwJTIydHJhaW4lMjIlMjAlMjAlMjMlMjAlMjJ2YWxpZCUyMiUwQWZpbHRlcnMlMjAlM0QlMjAlNUIlMjJwYW5kYXMlMjIlMkMlMjAlMjJza2xlYXJuJTIyJTJDJTIwJTIybWF0cGxvdGxpYiUyMiUyQyUyMCUyMnNlYWJvcm4lMjIlNUQlMEElMEFkYXRhJTIwJTNEJTIwbG9hZF9kYXRhc2V0KGYlMjJ0cmFuc2Zvcm1lcnNib29rJTJGY29kZXBhcnJvdC0lN0JzcGxpdCU3RCUyMiUyQyUyMHNwbGl0JTNEc3BsaXQlMkMlMjBzdHJlYW1pbmclM0RUcnVlKSUwQWZpbHRlcmVkX2RhdGElMjAlM0QlMjBmaWx0ZXJfc3RyZWFtaW5nX2RhdGFzZXQoZGF0YSUyQyUyMGZpbHRlcnMp",highlighted:`<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">&quot;train&quot;</span>  <span class="hljs-comment"># &quot;valid&quot;</span>
filters = [<span class="hljs-string">&quot;pandas&quot;</span>, <span class="hljs-string">&quot;sklearn&quot;</span>, <span class="hljs-string">&quot;matplotlib&quot;</span>, <span class="hljs-string">&quot;seaborn&quot;</span>]

data = load_dataset(<span class="hljs-string">f&quot;transformersbook/codeparrot-<span class="hljs-subst">{split}</span>&quot;</span>, split=split, streaming=<span class="hljs-literal">True</span>)
filtered_data = filter_streaming_dataset(data, filters)`,wrap:!1}}),Wl=new h({props:{code:"My4yNiUyNSUyMG9mJTIwZGF0YSUyMGFmdGVyJTIwZmlsdGVyaW5nLg==",highlighted:'<span class="hljs-number">3.26</span>% of data after filtering.',wrap:!1}}),Ql=new h({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTJDJTIwRGF0YXNldERpY3QlMEElMEFkc190cmFpbiUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJodWdnaW5nZmFjZS1jb3Vyc2UlMkZjb2RlcGFycm90LWRzLXRyYWluJTIyJTJDJTIwc3BsaXQlM0QlMjJ0cmFpbiUyMiklMEFkc192YWxpZCUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJodWdnaW5nZmFjZS1jb3Vyc2UlMkZjb2RlcGFycm90LWRzLXZhbGlkJTIyJTJDJTIwc3BsaXQlM0QlMjJ0cmFpbiUyMiklMEElMEFyYXdfZGF0YXNldHMlMjAlM0QlMjBEYXRhc2V0RGljdCglMEElMjAlMjAlMjAlMjAlN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJ0cmFpbiUyMiUzQSUyMGRzX3RyYWluJTJDJTIwJTIwJTIzJTIwLnNodWZmbGUoKS5zZWxlY3QocmFuZ2UoNTAwMDApKSUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnZhbGlkJTIyJTNBJTIwZHNfdmFsaWQlMkMlMjAlMjAlMjMlMjAuc2h1ZmZsZSgpLnNlbGVjdChyYW5nZSg1MDApKSUwQSUyMCUyMCUyMCUyMCU3RCUwQSklMEElMEFyYXdfZGF0YXNldHM=",highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset, DatasetDict

ds_train = load_dataset(<span class="hljs-string">&quot;huggingface-course/codeparrot-ds-train&quot;</span>, split=<span class="hljs-string">&quot;train&quot;</span>)
ds_valid = load_dataset(<span class="hljs-string">&quot;huggingface-course/codeparrot-ds-valid&quot;</span>, split=<span class="hljs-string">&quot;train&quot;</span>)

raw_datasets = DatasetDict(
    {
        <span class="hljs-string">&quot;train&quot;</span>: ds_train,  <span class="hljs-comment"># .shuffle().select(range(50000)),</span>
        <span class="hljs-string">&quot;valid&quot;</span>: ds_valid,  <span class="hljs-comment"># .shuffle().select(range(500))</span>
    }
)

raw_datasets`,wrap:!1}}),L=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ3JlcG9fbmFtZSclMkMlMjAncGF0aCclMkMlMjAnY29waWVzJyUyQyUyMCdzaXplJyUyQyUyMCdjb250ZW50JyUyQyUyMCdsaWNlbnNlJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwNjA2NzIwJTBBJTIwJTIwJTIwJTIwJTdEKSUwQSUyMCUyMCUyMCUyMHZhbGlkJTNBJTIwRGF0YXNldCglN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlcyUzQSUyMCU1QidyZXBvX25hbWUnJTJDJTIwJ3BhdGgnJTJDJTIwJ2NvcGllcyclMkMlMjAnc2l6ZSclMkMlMjAnY29udGVudCclMkMlMjAnbGljZW5zZSclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDMzMjIlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTdEKQ==",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;repo_name&#x27;</span>, <span class="hljs-string">&#x27;path&#x27;</span>, <span class="hljs-string">&#x27;copies&#x27;</span>, <span class="hljs-string">&#x27;size&#x27;</span>, <span class="hljs-string">&#x27;content&#x27;</span>, <span class="hljs-string">&#x27;license&#x27;</span>],
        num_rows: <span class="hljs-number">606720</span>
    })
    valid: Dataset({
        features: [<span class="hljs-string">&#x27;repo_name&#x27;</span>, <span class="hljs-string">&#x27;path&#x27;</span>, <span class="hljs-string">&#x27;copies&#x27;</span>, <span class="hljs-string">&#x27;size&#x27;</span>, <span class="hljs-string">&#x27;content&#x27;</span>, <span class="hljs-string">&#x27;license&#x27;</span>],
        num_rows: <span class="hljs-number">3322</span>
    })
})`,wrap:!1}}),z=new ys({props:{$$slots:{default:[ma]},$$scope:{ctx:I}}}),P=new h({props:{code:"Zm9yJTIwa2V5JTIwaW4lMjByYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QjAlNUQlM0ElMEElMjAlMjAlMjAlMjBwcmludChmJTIyJTdCa2V5LnVwcGVyKCklN0QlM0ElMjAlN0JyYXdfZGF0YXNldHMlNUIndHJhaW4nJTVEJTVCMCU1RCU1QmtleSU1RCU1QiUzQTIwMCU1RCU3RCUyMik=",highlighted:`<span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>]:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;<span class="hljs-subst">{key.upper()}</span>: <span class="hljs-subst">{raw_datasets[<span class="hljs-string">&#x27;train&#x27;</span>][<span class="hljs-number">0</span>][key][:<span class="hljs-number">200</span>]}</span>&quot;</span>)`,wrap:!1}}),O=new h({props:{code:"J1JFUE9fTkFNRSUzQSUyMGttaWtlJTJGc2Npa2l0LWxlYXJuJyUwQSdQQVRIJTNBJTIwc2tsZWFybiUyRnV0aWxzJTJGX19pbml0X18ucHknJTBBJ0NPUElFUyUzQSUyMDMnJTBBJ1NJWkUlM0ElMjAxMDA5NCclMEEnJydDT05URU5UJTNBJTIwJTIyJTIyJTIyJTBBVGhlJTIwJTNBbW9kJTNBJTYwc2tsZWFybi51dGlscyU2MCUyMG1vZHVsZSUyMGluY2x1ZGVzJTIwdmFyaW91cyUyMHV0aWxpdGVzLiUwQSUyMiUyMiUyMiUwQSUwQWZyb20lMjBjb2xsZWN0aW9ucyUyMGltcG9ydCUyMFNlcXVlbmNlJTBBJTBBaW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBZnJvbSUyMHNjaXB5LnNwYXJzZSUyMGltcG9ydCUyMGlzc3BhcnNlJTBBaW1wb3J0JTIwd2FybmluZ3MlMEElMEFmcm9tJTIwLm11cm11cmhhc2glMjBpbXBvcnQlMjBtdXJtJTBBTElDRU5TRSUzQSUyMGJzZC0zLWNsYXVzZScnJw==",highlighted:`<span class="hljs-string">&#x27;REPO_NAME: kmike/scikit-learn&#x27;</span>
<span class="hljs-string">&#x27;PATH: sklearn/utils/__init__.py&#x27;</span>
<span class="hljs-string">&#x27;COPIES: 3&#x27;</span>
<span class="hljs-string">&#x27;SIZE: 10094&#x27;</span>
<span class="hljs-string">&#x27;&#x27;&#x27;CONTENT: &quot;&quot;&quot;
The :mod:\`sklearn.utils\` module includes various utilites.
&quot;&quot;&quot;

from collections import Sequence

import numpy as np
from scipy.sparse import issparse
import warnings

from .murmurhash import murm
LICENSE: bsd-3-clause&#x27;&#x27;&#x27;</span>`,wrap:!1}}),ll=new Te({props:{title:"準備數據集",local:"準備數據集",headingTag:"h2"}}),sl=new wt({props:{id:"ma1TrR7gE7I"}}),ds=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEFjb250ZXh0X2xlbmd0aCUyMCUzRCUyMDEyOCUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKCUyMmh1Z2dpbmdmYWNlLWNvdXJzZSUyRmNvZGUtc2VhcmNoLW5ldC10b2tlbml6ZXIlMjIpJTBBJTBBb3V0cHV0cyUyMCUzRCUyMHRva2VuaXplciglMEElMjAlMjAlMjAlMjByYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QiUzQTIlNUQlNUIlMjJjb250ZW50JTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwdHJ1bmNhdGlvbiUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjBtYXhfbGVuZ3RoJTNEY29udGV4dF9sZW5ndGglMkMlMEElMjAlMjAlMjAlMjByZXR1cm5fb3ZlcmZsb3dpbmdfdG9rZW5zJTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMHJldHVybl9sZW5ndGglM0RUcnVlJTJDJTBBKSUwQSUwQXByaW50KGYlMjJJbnB1dCUyMElEcyUyMGxlbmd0aCUzQSUyMCU3QmxlbihvdXRwdXRzJTVCJ2lucHV0X2lkcyclNUQpJTdEJTIyKSUwQXByaW50KGYlMjJJbnB1dCUyMGNodW5rJTIwbGVuZ3RocyUzQSUyMCU3QihvdXRwdXRzJTVCJ2xlbmd0aCclNUQpJTdEJTIyKSUwQXByaW50KGYlMjJDaHVuayUyMG1hcHBpbmclM0ElMjAlN0JvdXRwdXRzJTVCJ292ZXJmbG93X3RvX3NhbXBsZV9tYXBwaW5nJyU1RCU3RCUyMik=",highlighted:`<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">&quot;huggingface-course/code-search-net-tokenizer&quot;</span>)

outputs = tokenizer(
    raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][:<span class="hljs-number">2</span>][<span class="hljs-string">&quot;content&quot;</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&quot;Input IDs length: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(outputs[<span class="hljs-string">&#x27;input_ids&#x27;</span>])}</span>&quot;</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Input chunk lengths: <span class="hljs-subst">{(outputs[<span class="hljs-string">&#x27;length&#x27;</span>])}</span>&quot;</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Chunk mapping: <span class="hljs-subst">{outputs[<span class="hljs-string">&#x27;overflow_to_sample_mapping&#x27;</span>]}</span>&quot;</span>)`,wrap:!1}}),bs=new h({props:{code:"SW5wdXQlMjBJRHMlMjBsZW5ndGglM0ElMjAzNCUwQUlucHV0JTIwY2h1bmslMjBsZW5ndGhzJTNBJTIwJTVCMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTE3JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwMTI4JTJDJTIwNDElNUQlMEFDaHVuayUyMG1hcHBpbmclM0ElMjAlNUIwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTVE",highlighted:`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>]`,wrap:!1}}),hs=new h({props:{code:"ZGVmJTIwdG9rZW5pemUoZWxlbWVudCklM0ElMEElMjAlMjAlMjAlMjBvdXRwdXRzJTIwJTNEJTIwdG9rZW5pemVyKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGVsZW1lbnQlNUIlMjJjb250ZW50JTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdHJ1bmNhdGlvbiUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBtYXhfbGVuZ3RoJTNEY29udGV4dF9sZW5ndGglMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXR1cm5fb3ZlcmZsb3dpbmdfdG9rZW5zJTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJldHVybl9sZW5ndGglM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwKSUwQSUyMCUyMCUyMCUyMGlucHV0X2JhdGNoJTIwJTNEJTIwJTVCJTVEJTBBJTIwJTIwJTIwJTIwZm9yJTIwbGVuZ3RoJTJDJTIwaW5wdXRfaWRzJTIwaW4lMjB6aXAob3V0cHV0cyU1QiUyMmxlbmd0aCUyMiU1RCUyQyUyMG91dHB1dHMlNUIlMjJpbnB1dF9pZHMlMjIlNUQpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaWYlMjBsZW5ndGglMjAlM0QlM0QlMjBjb250ZXh0X2xlbmd0aCUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGlucHV0X2JhdGNoLmFwcGVuZChpbnB1dF9pZHMpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTdCJTIyaW5wdXRfaWRzJTIyJTNBJTIwaW5wdXRfYmF0Y2glN0QlMEElMEElMEF0b2tlbml6ZWRfZGF0YXNldHMlMjAlM0QlMjByYXdfZGF0YXNldHMubWFwKCUwQSUyMCUyMCUyMCUyMHRva2VuaXplJTJDJTIwYmF0Y2hlZCUzRFRydWUlMkMlMjByZW1vdmVfY29sdW1ucyUzRHJhd19kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVELmNvbHVtbl9uYW1lcyUwQSklMEF0b2tlbml6ZWRfZGF0YXNldHM=",highlighted:`<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">&quot;content&quot;</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">&quot;length&quot;</span>], outputs[<span class="hljs-string">&quot;input_ids&quot;</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">&quot;input_ids&quot;</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">&quot;train&quot;</span>].column_names
)
tokenized_datasets`,wrap:!1}}),Is=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2lucHV0X2lkcyclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDE2NzAyMDYxJTBBJTIwJTIwJTIwJTIwJTdEKSUwQSUyMCUyMCUyMCUyMHZhbGlkJTNBJTIwRGF0YXNldCglN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlcyUzQSUyMCU1QidpbnB1dF9pZHMnJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX3Jvd3MlM0ElMjA5MzE2NCUwQSUyMCUyMCUyMCUyMCU3RCklMEElN0Qp",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;input_ids&#x27;</span>],
        num_rows: <span class="hljs-number">16702061</span>
    })
    valid: Dataset({
        features: [<span class="hljs-string">&#x27;input_ids&#x27;</span>],
        num_rows: <span class="hljs-number">93164</span>
    })
})`,wrap:!1}}),ts=new ys({props:{$$slots:{default:[Ta]},$$scope:{ctx:I}}}),Zs=new Te({props:{title:"初始化新模型",local:"初始化新模型",headingTag:"h2"}});const At=[ua,wa],ee=[];function Et(l,M){return l[0]==="pt"?0:1}dl=Et(I),bl=ee[dl]=At[dl](I);const Vt=[da,ja],te=[];function zt(l,M){return l[0]==="pt"?0:1}fl=zt(I),Cl=te[fl]=Vt[fl](I),Bs=new h({props:{code:"b3V0JTIwJTNEJTIwZGF0YV9jb2xsYXRvciglNUJ0b2tlbml6ZWRfZGF0YXNldCU1QiUyMnRyYWluJTIyJTVEJTVCaSU1RCUyMGZvciUyMGklMjBpbiUyMHJhbmdlKDUpJTVEKSUwQWZvciUyMGtleSUyMGluJTIwb3V0JTNBJTBBJTIwJTIwJTIwJTIwcHJpbnQoZiUyMiU3QmtleSU3RCUyMHNoYXBlJTNBJTIwJTdCb3V0JTVCa2V5JTVELnNoYXBlJTdEJTIyKQ==",highlighted:`out = data_collator([tokenized_dataset[<span class="hljs-string">&quot;train&quot;</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&quot;<span class="hljs-subst">{key}</span> shape: <span class="hljs-subst">{out[key].shape}</span>&quot;</span>)`,wrap:!1}});const Nt=[fa,ba],ae=[];function vt(l,M){return l[0]==="pt"?0:1}hl=vt(I),Il=ae[hl]=Nt[hl](I);let Q=I[0]==="tf"&&ea();as=new ys({props:{warning:!0,$$slots:{default:[Ca]},$$scope:{ctx:I}}}),Qs=new h({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`,wrap:!1}}),Vs=new h({props:{code:"aHVnZ2luZ2ZhY2UtY2xpJTIwbG9naW4=",highlighted:"huggingface-cli login",wrap:!1}});const xt=[Ia,ha],ne=[];function Ft(l,M){return l[0]==="pt"?0:1}_l=Ft(I),kl=ne[_l]=xt[_l](I),ns=new ys({props:{$$slots:{default:[_a]},$$scope:{ctx:I}}}),Ms=new ys({props:{$$slots:{default:[$a]},$$scope:{ctx:I}}}),zs=new Te({props:{title:"使用管道生成代碼",local:"使用管道生成代碼",headingTag:"h2"}});const Yt=[Ga,ga],Me=[];function Ht(l,M){return l[0]==="pt"?0:1}Zl=Ht(I),$l=Me[Zl]=Yt[Zl](I),xs=new h({props:{code:"dHh0JTIwJTNEJTIwJTIyJTIyJTIyJTVDJTBBJTIzJTIwY3JlYXRlJTIwc29tZSUyMGRhdGElMEF4JTIwJTNEJTIwbnAucmFuZG9tLnJhbmRuKDEwMCklMEF5JTIwJTNEJTIwbnAucmFuZG9tLnJhbmRuKDEwMCklMEElMEElMjMlMjBjcmVhdGUlMjBzY2F0dGVyJTIwcGxvdCUyMHdpdGglMjB4JTJDJTIweSUwQSUyMiUyMiUyMiUwQXByaW50KHBpcGUodHh0JTJDJTIwbnVtX3JldHVybl9zZXF1ZW5jZXMlM0QxKSU1QjAlNUQlNUIlMjJnZW5lcmF0ZWRfdGV4dCUyMiU1RCk=",highlighted:`txt = <span class="hljs-string">&quot;&quot;&quot;\\
# create some data
x = np.random.randn(100)
y = np.random.randn(100)

# create scatter plot with x, y
&quot;&quot;&quot;</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">&quot;generated_text&quot;</span>])`,wrap:!1}}),Fs=new h({props:{code:"JTIzJTIwY3JlYXRlJTIwc29tZSUyMGRhdGElMEF4JTIwJTNEJTIwbnAucmFuZG9tLnJhbmRuKDEwMCklMEF5JTIwJTNEJTIwbnAucmFuZG9tLnJhbmRuKDEwMCklMEElMEElMjMlMjBjcmVhdGUlMjBzY2F0dGVyJTIwcGxvdCUyMHdpdGglMjB4JTJDJTIweSUwQXBsdC5zY2F0dGVyKHglMkMlMjB5KSUwQSUwQSUyMyUyMGNyZWF0ZSUyMHNjYXR0ZXI=",highlighted:`<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>`,wrap:!1}}),Hs=new h({props:{code:"dHh0JTIwJTNEJTIwJTIyJTIyJTIyJTVDJTBBJTIzJTIwY3JlYXRlJTIwc29tZSUyMGRhdGElMEF4JTIwJTNEJTIwbnAucmFuZG9tLnJhbmRuKDEwMCklMEF5JTIwJTNEJTIwbnAucmFuZG9tLnJhbmRuKDEwMCklMEElMEElMjMlMjBjcmVhdGUlMjBkYXRhZnJhbWUlMjBmcm9tJTIweCUyMGFuZCUyMHklMEElMjIlMjIlMjIlMEFwcmludChwaXBlKHR4dCUyQyUyMG51bV9yZXR1cm5fc2VxdWVuY2VzJTNEMSklNUIwJTVEJTVCJTIyZ2VuZXJhdGVkX3RleHQlMjIlNUQp",highlighted:`txt = <span class="hljs-string">&quot;&quot;&quot;\\
# create some data
x = np.random.randn(100)
y = np.random.randn(100)

# create dataframe from x and y
&quot;&quot;&quot;</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">&quot;generated_text&quot;</span>])`,wrap:!1}}),Ss=new h({props:{code:"JTIzJTIwY3JlYXRlJTIwc29tZSUyMGRhdGElMEF4JTIwJTNEJTIwbnAucmFuZG9tLnJhbmRuKDEwMCklMEF5JTIwJTNEJTIwbnAucmFuZG9tLnJhbmRuKDEwMCklMEElMEElMjMlMjBjcmVhdGUlMjBkYXRhZnJhbWUlMjBmcm9tJTIweCUyMGFuZCUyMHklMEFkZiUyMCUzRCUyMHBkLkRhdGFGcmFtZSglN0IneCclM0ElMjB4JTJDJTIwJ3knJTNBJTIweSU3RCklMEFkZi5pbnNlcnQoMCUyQyd4JyUyQyUyMHgpJTBBZm9y",highlighted:`<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">&#x27;x&#x27;</span>: x, <span class="hljs-string">&#x27;y&#x27;</span>: y})
df.insert(<span class="hljs-number">0</span>,<span class="hljs-string">&#x27;x&#x27;</span>, x)
<span class="hljs-keyword">for</span>`,wrap:!1}}),Ds=new h({props:{code:"dHh0JTIwJTNEJTIwJTIyJTIyJTIyJTVDJTBBJTIzJTIwZGF0YWZyYW1lJTIwd2l0aCUyMHByb2Zlc3Npb24lMkMlMjBpbmNvbWUlMjBhbmQlMjBuYW1lJTBBZGYlMjAlM0QlMjBwZC5EYXRhRnJhbWUoJTdCJ3Byb2Zlc3Npb24nJTNBJTIweCUyQyUyMCdpbmNvbWUnJTNBeSUyQyUyMCduYW1lJyUzQSUyMHolN0QpJTBBJTBBJTIzJTIwY2FsY3VsYXRlJTIwdGhlJTIwbWVhbiUyMGluY29tZSUyMHBlciUyMHByb2Zlc3Npb24lMEElMjIlMjIlMjIlMEFwcmludChwaXBlKHR4dCUyQyUyMG51bV9yZXR1cm5fc2VxdWVuY2VzJTNEMSklNUIwJTVEJTVCJTIyZ2VuZXJhdGVkX3RleHQlMjIlNUQp",highlighted:`txt = <span class="hljs-string">&quot;&quot;&quot;\\
# dataframe with profession, income and name
df = pd.DataFrame({&#x27;profession&#x27;: x, &#x27;income&#x27;:y, &#x27;name&#x27;: z})

# calculate the mean income per profession
&quot;&quot;&quot;</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">&quot;generated_text&quot;</span>])`,wrap:!1}}),Ls=new h({props:{code:"JTIzJTIwZGF0YWZyYW1lJTIwd2l0aCUyMHByb2Zlc3Npb24lMkMlMjBpbmNvbWUlMjBhbmQlMjBuYW1lJTBBZGYlMjAlM0QlMjBwZC5EYXRhRnJhbWUoJTdCJ3Byb2Zlc3Npb24nJTNBJTIweCUyQyUyMCdpbmNvbWUnJTNBeSUyQyUyMCduYW1lJyUzQSUyMHolN0QpJTBBJTBBJTIzJTIwY2FsY3VsYXRlJTIwdGhlJTIwbWVhbiUyMGluY29tZSUyMHBlciUyMHByb2Zlc3Npb24lMEFwcm9mZXNzaW9uJTIwJTNEJTIwZGYuZ3JvdXBieSglNUIncHJvZmVzc2lvbiclNUQpLm1lYW4oKSUwQSUwQSUyMyUyMGNvbXB1dGUlMjB0aGU=",highlighted:`<span class="hljs-comment"># dataframe with profession, income and name</span>
df = pd.DataFrame({<span class="hljs-string">&#x27;profession&#x27;</span>: x, <span class="hljs-string">&#x27;income&#x27;</span>:y, <span class="hljs-string">&#x27;name&#x27;</span>: z})

<span class="hljs-comment"># calculate the mean income per profession</span>
profession = df.groupby([<span class="hljs-string">&#x27;profession&#x27;</span>]).mean()

<span class="hljs-comment"># compute the</span>`,wrap:!1}}),Ps=new h({props:{code:"dHh0JTIwJTNEJTIwJTIyJTIyJTIyJTBBJTIzJTIwaW1wb3J0JTIwcmFuZG9tJTIwZm9yZXN0JTIwcmVncmVzc29yJTIwZnJvbSUyMHNjaWtpdC1sZWFybiUwQWZyb20lMjBza2xlYXJuLmVuc2VtYmxlJTIwaW1wb3J0JTIwUmFuZG9tRm9yZXN0UmVncmVzc29yJTBBJTBBJTIzJTIwZml0JTIwcmFuZG9tJTIwZm9yZXN0JTIwbW9kZWwlMjB3aXRoJTIwMzAwJTIwZXN0aW1hdG9ycyUyMG9uJTIwWCUyQyUyMHklM0ElMEElMjIlMjIlMjIlMEFwcmludChwaXBlKHR4dCUyQyUyMG51bV9yZXR1cm5fc2VxdWVuY2VzJTNEMSklNUIwJTVEJTVCJTIyZ2VuZXJhdGVkX3RleHQlMjIlNUQp",highlighted:`txt = <span class="hljs-string">&quot;&quot;&quot;
# import random forest regressor from scikit-learn
from sklearn.ensemble import RandomForestRegressor

# fit random forest model with 300 estimators on X, y:
&quot;&quot;&quot;</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">&quot;generated_text&quot;</span>])`,wrap:!1}}),Os=new h({props:{code:"JTIzJTIwaW1wb3J0JTIwcmFuZG9tJTIwZm9yZXN0JTIwcmVncmVzc29yJTIwZnJvbSUyMHNjaWtpdC1sZWFybiUwQWZyb20lMjBza2xlYXJuLmVuc2VtYmxlJTIwaW1wb3J0JTIwUmFuZG9tRm9yZXN0UmVncmVzc29yJTBBJTBBJTIzJTIwZml0JTIwcmFuZG9tJTIwZm9yZXN0JTIwbW9kZWwlMjB3aXRoJTIwMzAwJTIwZXN0aW1hdG9ycyUyMG9uJTIwWCUyQyUyMHklM0ElMEFyZiUyMCUzRCUyMFJhbmRvbUZvcmVzdFJlZ3Jlc3NvcihuX2VzdGltYXRvcnMlM0QzMDAlMkMlMjByYW5kb21fc3RhdGUlM0RyYW5kb21fc3RhdGUlMkMlMjBtYXhfZGVwdGglM0QzKSUwQXJmLmZpdChYJTJDJTIweSklMEFyZg==",highlighted:`<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`,wrap:!1}});function St(l,M){return l[0]==="tf"?Ba:Xa}let yt=St(I),es=yt(I),A=I[0]==="pt"&&ta(I);return le=new Ja({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-TW/chapter7/6.mdx"}}),{c(){t=j("meta"),y=p(),n=j("p"),J=p(),m(_.$$.fragment),f=p(),m(g.$$.fragment),$=p(),o.c(),C=p(),G=j("p"),G.innerHTML=v,E=p(),W=j("p"),W.innerHTML=k,X=p(),m(x.$$.fragment),Vl=p(),F=j("p"),F.innerHTML=we,zl=p(),B=j("iframe"),Nl=p(),R=j("iframe"),vl=p(),Y=j("p"),Y.innerHTML=cs,tl=p(),m(gl.$$.fragment),xl=p(),H=j("p"),H.innerHTML=is,al=p(),Gl=j("p"),Gl.innerHTML=Js,nl=p(),m(Ml.$$.fragment),pl=p(),Xl=j("p"),Xl.textContent=os,rl=p(),m(yl.$$.fragment),cl=p(),m(Bl.$$.fragment),Fl=p(),S=j("p"),S.textContent=Us,il=p(),m(Rl.$$.fragment),Yl=p(),q=j("p"),q.textContent=ms,Jl=p(),m(ol.$$.fragment),Ul=p(),m(Wl.$$.fragment),Hl=p(),D=j("p"),D.textContent=Ts,ml=p(),m(Ql.$$.fragment),Sl=p(),m(L.$$.fragment),ql=p(),m(z.$$.fragment),Dl=p(),K=j("p"),K.textContent=de,Ll=p(),m(P.$$.fragment),Kl=p(),m(O.$$.fragment),ws=p(),Tl=j("p"),Tl.innerHTML=Al,Pl=p(),m(ll.$$.fragment),Ol=p(),m(sl.$$.fragment),us=p(),wl=j("p"),wl.textContent=El,ls=p(),el=j("p"),el.innerHTML=be,ss=p(),N=j("div"),N.innerHTML=js,ul=p(),jl=j("p"),jl.textContent=s,U=p(),m(ds.$$.fragment),Ce=p(),m(bs.$$.fragment),he=p(),fs=j("p"),fs.innerHTML=ut,Ie=p(),Cs=j("p"),Cs.innerHTML=jt,_e=p(),m(hs.$$.fragment),ke=p(),m(Is.$$.fragment),Ze=p(),_s=j("p"),_s.textContent=dt,$e=p(),ks=j("p"),ks.textContent=bt,ge=p(),m(ts.$$.fragment),Ge=p(),m(Zs.$$.fragment),Xe=p(),$s=j("p"),$s.innerHTML=ft,Be=p(),bl.c(),re=p(),gs=j("p"),gs.innerHTML=Ct,Re=p(),Gs=j("p"),Gs.innerHTML=ht,We=p(),Cl.c(),ye=p(),Xs=j("p"),Xs.textContent=It,Qe=p(),m(Bs.$$.fragment),Ae=p(),Il.c(),ce=p(),Rs=j("p"),Rs.textContent=_t,Ee=p(),Q&&Q.c(),ie=p(),m(as.$$.fragment),Ve=p(),Ws=j("p"),Ws.textContent=kt,ze=p(),m(Qs.$$.fragment),Ne=p(),As=j("p"),As.textContent=Zt,ve=p(),Es=j("p"),Es.textContent=$t,xe=p(),m(Vs.$$.fragment),Fe=p(),kl.c(),Je=p(),m(ns.$$.fragment),Ye=p(),m(Ms.$$.fragment),He=p(),m(zs.$$.fragment),Se=p(),Ns=j("p"),Ns.innerHTML=gt,qe=p(),$l.c(),oe=p(),vs=j("p"),vs.textContent=Gt,De=p(),m(xs.$$.fragment),Le=p(),m(Fs.$$.fragment),Ke=p(),Ys=j("p"),Ys.innerHTML=Xt,Pe=p(),m(Hs.$$.fragment),Oe=p(),m(Ss.$$.fragment),lt=p(),qs=j("p"),qs.innerHTML=Bt,st=p(),m(Ds.$$.fragment),et=p(),m(Ls.$$.fragment),tt=p(),Ks=j("p"),Ks.innerHTML=Rt,at=p(),m(Ps.$$.fragment),nt=p(),m(Os.$$.fragment),Mt=p(),es.c(),Ue=p(),A&&A.c(),me=p(),m(le.$$.fragment),pt=p(),fe=j("p"),this.h()},l(l){const M=ya("svelte-u9bgzb",document.head);t=d(M,"META",{name:!0,content:!0}),M.forEach(e),y=r(l),n=d(l,"P",{}),ct(n).forEach(e),J=r(l),T(_.$$.fragment,l),f=r(l),T(g.$$.fragment,l),$=r(l),o.l(l),C=r(l),G=d(l,"P",{"data-svelte-h":!0}),b(G)!=="svelte-145q1wm"&&(G.innerHTML=v),E=r(l),W=d(l,"P",{"data-svelte-h":!0}),b(W)!=="svelte-150g0cy"&&(W.innerHTML=k),X=r(l),T(x.$$.fragment,l),Vl=r(l),F=d(l,"P",{"data-svelte-h":!0}),b(F)!=="svelte-10a6fg6"&&(F.innerHTML=we),zl=r(l),B=d(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),ct(B).forEach(e),Nl=r(l),R=d(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),ct(R).forEach(e),vl=r(l),Y=d(l,"P",{"data-svelte-h":!0}),b(Y)!=="svelte-1ttf6yn"&&(Y.innerHTML=cs),tl=r(l),T(gl.$$.fragment,l),xl=r(l),H=d(l,"P",{"data-svelte-h":!0}),b(H)!=="svelte-9g9q7s"&&(H.innerHTML=is),al=r(l),Gl=d(l,"P",{"data-svelte-h":!0}),b(Gl)!=="svelte-11uybe4"&&(Gl.innerHTML=Js),nl=r(l),T(Ml.$$.fragment,l),pl=r(l),Xl=d(l,"P",{"data-svelte-h":!0}),b(Xl)!=="svelte-ee6loh"&&(Xl.textContent=os),rl=r(l),T(yl.$$.fragment,l),cl=r(l),T(Bl.$$.fragment,l),Fl=r(l),S=d(l,"P",{"data-svelte-h":!0}),b(S)!=="svelte-1vcwklb"&&(S.textContent=Us),il=r(l),T(Rl.$$.fragment,l),Yl=r(l),q=d(l,"P",{"data-svelte-h":!0}),b(q)!=="svelte-2ls0p3"&&(q.textContent=ms),Jl=r(l),T(ol.$$.fragment,l),Ul=r(l),T(Wl.$$.fragment,l),Hl=r(l),D=d(l,"P",{"data-svelte-h":!0}),b(D)!=="svelte-1wakzoy"&&(D.textContent=Ts),ml=r(l),T(Ql.$$.fragment,l),Sl=r(l),T(L.$$.fragment,l),ql=r(l),T(z.$$.fragment,l),Dl=r(l),K=d(l,"P",{"data-svelte-h":!0}),b(K)!=="svelte-1lbgl1f"&&(K.textContent=de),Ll=r(l),T(P.$$.fragment,l),Kl=r(l),T(O.$$.fragment,l),ws=r(l),Tl=d(l,"P",{"data-svelte-h":!0}),b(Tl)!=="svelte-1sd617c"&&(Tl.innerHTML=Al),Pl=r(l),T(ll.$$.fragment,l),Ol=r(l),T(sl.$$.fragment,l),us=r(l),wl=d(l,"P",{"data-svelte-h":!0}),b(wl)!=="svelte-tijx3u"&&(wl.textContent=El),ls=r(l),el=d(l,"P",{"data-svelte-h":!0}),b(el)!=="svelte-ak9zu6"&&(el.innerHTML=be),ss=r(l),N=d(l,"DIV",{class:!0,"data-svelte-h":!0}),b(N)!=="svelte-1mf8pz"&&(N.innerHTML=js),ul=r(l),jl=d(l,"P",{"data-svelte-h":!0}),b(jl)!=="svelte-1mnld0g"&&(jl.textContent=s),U=r(l),T(ds.$$.fragment,l),Ce=r(l),T(bs.$$.fragment,l),he=r(l),fs=d(l,"P",{"data-svelte-h":!0}),b(fs)!=="svelte-e7wmzc"&&(fs.innerHTML=ut),Ie=r(l),Cs=d(l,"P",{"data-svelte-h":!0}),b(Cs)!=="svelte-mmvk53"&&(Cs.innerHTML=jt),_e=r(l),T(hs.$$.fragment,l),ke=r(l),T(Is.$$.fragment,l),Ze=r(l),_s=d(l,"P",{"data-svelte-h":!0}),b(_s)!=="svelte-4uq2ft"&&(_s.textContent=dt),$e=r(l),ks=d(l,"P",{"data-svelte-h":!0}),b(ks)!=="svelte-1lk2r1u"&&(ks.textContent=bt),ge=r(l),T(ts.$$.fragment,l),Ge=r(l),T(Zs.$$.fragment,l),Xe=r(l),$s=d(l,"P",{"data-svelte-h":!0}),b($s)!=="svelte-zid2k"&&($s.innerHTML=ft),Be=r(l),bl.l(l),re=r(l),gs=d(l,"P",{"data-svelte-h":!0}),b(gs)!=="svelte-46qnw9"&&(gs.innerHTML=Ct),Re=r(l),Gs=d(l,"P",{"data-svelte-h":!0}),b(Gs)!=="svelte-1jgzakq"&&(Gs.innerHTML=ht),We=r(l),Cl.l(l),ye=r(l),Xs=d(l,"P",{"data-svelte-h":!0}),b(Xs)!=="svelte-lc35bs"&&(Xs.textContent=It),Qe=r(l),T(Bs.$$.fragment,l),Ae=r(l),Il.l(l),ce=r(l),Rs=d(l,"P",{"data-svelte-h":!0}),b(Rs)!=="svelte-8pe4de"&&(Rs.textContent=_t),Ee=r(l),Q&&Q.l(l),ie=r(l),T(as.$$.fragment,l),Ve=r(l),Ws=d(l,"P",{"data-svelte-h":!0}),b(Ws)!=="svelte-157la4h"&&(Ws.textContent=kt),ze=r(l),T(Qs.$$.fragment,l),Ne=r(l),As=d(l,"P",{"data-svelte-h":!0}),b(As)!=="svelte-1oxqs03"&&(As.textContent=Zt),ve=r(l),Es=d(l,"P",{"data-svelte-h":!0}),b(Es)!=="svelte-pruo9h"&&(Es.textContent=$t),xe=r(l),T(Vs.$$.fragment,l),Fe=r(l),kl.l(l),Je=r(l),T(ns.$$.fragment,l),Ye=r(l),T(Ms.$$.fragment,l),He=r(l),T(zs.$$.fragment,l),Se=r(l),Ns=d(l,"P",{"data-svelte-h":!0}),b(Ns)!=="svelte-1dkmacl"&&(Ns.innerHTML=gt),qe=r(l),$l.l(l),oe=r(l),vs=d(l,"P",{"data-svelte-h":!0}),b(vs)!=="svelte-12egf8k"&&(vs.textContent=Gt),De=r(l),T(xs.$$.fragment,l),Le=r(l),T(Fs.$$.fragment,l),Ke=r(l),Ys=d(l,"P",{"data-svelte-h":!0}),b(Ys)!=="svelte-4mdgjy"&&(Ys.innerHTML=Xt),Pe=r(l),T(Hs.$$.fragment,l),Oe=r(l),T(Ss.$$.fragment,l),lt=r(l),qs=d(l,"P",{"data-svelte-h":!0}),b(qs)!=="svelte-1px822b"&&(qs.innerHTML=Bt),st=r(l),T(Ds.$$.fragment,l),et=r(l),T(Ls.$$.fragment,l),tt=r(l),Ks=d(l,"P",{"data-svelte-h":!0}),b(Ks)!=="svelte-mhbtoo"&&(Ks.innerHTML=Rt),at=r(l),T(Ps.$$.fragment,l),nt=r(l),T(Os.$$.fragment,l),Mt=r(l),es.l(l),Ue=r(l),A&&A.l(l),me=r(l),T(le.$$.fragment,l),pt=r(l),fe=d(l,"P",{}),ct(fe).forEach(e),this.h()},h(){V(t,"name","hf:doc:metadata"),V(t,"content",Ea),la(B.src,ue="https://course-demos-codeparrot-ds.hf.space")||V(B,"src",ue),V(B,"frameborder","0"),V(B,"height","300"),V(B,"title","Gradio app"),V(B,"class","block dark:hidden container p-0 flex-grow space-iframe"),V(B,"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"),V(B,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),la(R.src,je="https://course-demos-codeparrot-ds-darkmode.hf.space")||V(R,"src",je),V(R,"frameborder","0"),V(R,"height","300"),V(R,"title","Gradio app"),V(R,"class","hidden dark:block container p-0 flex-grow space-iframe"),V(R,"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"),V(R,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),V(N,"class","flex justify-center")},m(l,M){ca(document.head,t),a(l,y,M),a(l,n,M),a(l,J,M),w(_,l,M),a(l,f,M),w(g,l,M),a(l,$,M),se[Z].m(l,M),a(l,C,M),a(l,G,M),a(l,E,M),a(l,W,M),a(l,X,M),w(x,l,M),a(l,Vl,M),a(l,F,M),a(l,zl,M),a(l,B,M),a(l,Nl,M),a(l,R,M),a(l,vl,M),a(l,Y,M),a(l,tl,M),w(gl,l,M),a(l,xl,M),a(l,H,M),a(l,al,M),a(l,Gl,M),a(l,nl,M),w(Ml,l,M),a(l,pl,M),a(l,Xl,M),a(l,rl,M),w(yl,l,M),a(l,cl,M),w(Bl,l,M),a(l,Fl,M),a(l,S,M),a(l,il,M),w(Rl,l,M),a(l,Yl,M),a(l,q,M),a(l,Jl,M),w(ol,l,M),a(l,Ul,M),w(Wl,l,M),a(l,Hl,M),a(l,D,M),a(l,ml,M),w(Ql,l,M),a(l,Sl,M),w(L,l,M),a(l,ql,M),w(z,l,M),a(l,Dl,M),a(l,K,M),a(l,Ll,M),w(P,l,M),a(l,Kl,M),w(O,l,M),a(l,ws,M),a(l,Tl,M),a(l,Pl,M),w(ll,l,M),a(l,Ol,M),w(sl,l,M),a(l,us,M),a(l,wl,M),a(l,ls,M),a(l,el,M),a(l,ss,M),a(l,N,M),a(l,ul,M),a(l,jl,M),a(l,U,M),w(ds,l,M),a(l,Ce,M),w(bs,l,M),a(l,he,M),a(l,fs,M),a(l,Ie,M),a(l,Cs,M),a(l,_e,M),w(hs,l,M),a(l,ke,M),w(Is,l,M),a(l,Ze,M),a(l,_s,M),a(l,$e,M),a(l,ks,M),a(l,ge,M),w(ts,l,M),a(l,Ge,M),w(Zs,l,M),a(l,Xe,M),a(l,$s,M),a(l,Be,M),ee[dl].m(l,M),a(l,re,M),a(l,gs,M),a(l,Re,M),a(l,Gs,M),a(l,We,M),te[fl].m(l,M),a(l,ye,M),a(l,Xs,M),a(l,Qe,M),w(Bs,l,M),a(l,Ae,M),ae[hl].m(l,M),a(l,ce,M),a(l,Rs,M),a(l,Ee,M),Q&&Q.m(l,M),a(l,ie,M),w(as,l,M),a(l,Ve,M),a(l,Ws,M),a(l,ze,M),w(Qs,l,M),a(l,Ne,M),a(l,As,M),a(l,ve,M),a(l,Es,M),a(l,xe,M),w(Vs,l,M),a(l,Fe,M),ne[_l].m(l,M),a(l,Je,M),w(ns,l,M),a(l,Ye,M),w(Ms,l,M),a(l,He,M),w(zs,l,M),a(l,Se,M),a(l,Ns,M),a(l,qe,M),Me[Zl].m(l,M),a(l,oe,M),a(l,vs,M),a(l,De,M),w(xs,l,M),a(l,Le,M),w(Fs,l,M),a(l,Ke,M),a(l,Ys,M),a(l,Pe,M),w(Hs,l,M),a(l,Oe,M),w(Ss,l,M),a(l,lt,M),a(l,qs,M),a(l,st,M),w(Ds,l,M),a(l,et,M),w(Ls,l,M),a(l,tt,M),a(l,Ks,M),a(l,at,M),w(Ps,l,M),a(l,nt,M),w(Os,l,M),a(l,Mt,M),es.m(l,M),a(l,Ue,M),A&&A.m(l,M),a(l,me,M),w(le,l,M),a(l,pt,M),a(l,fe,M),rt=!0},p(l,[M]){const qt={};M&1&&(qt.fw=l[0]),_.$set(qt);let it=Z;Z=Qt(l),Z!==it&&(rs(),i(se[it],1,1,()=>{se[it]=null}),ps(),o=se[Z],o||(o=se[Z]=Wt[Z](l),o.c()),c(o,1),o.m(C.parentNode,C));const Dt={};M&2&&(Dt.$$scope={dirty:M,ctx:l}),z.$set(Dt);const Lt={};M&2&&(Lt.$$scope={dirty:M,ctx:l}),ts.$set(Lt);let Jt=dl;dl=Et(l),dl!==Jt&&(rs(),i(ee[Jt],1,1,()=>{ee[Jt]=null}),ps(),bl=ee[dl],bl||(bl=ee[dl]=At[dl](l),bl.c()),c(bl,1),bl.m(re.parentNode,re));let ot=fl;fl=zt(l),fl!==ot&&(rs(),i(te[ot],1,1,()=>{te[ot]=null}),ps(),Cl=te[fl],Cl||(Cl=te[fl]=Vt[fl](l),Cl.c()),c(Cl,1),Cl.m(ye.parentNode,ye));let Ut=hl;hl=vt(l),hl!==Ut&&(rs(),i(ae[Ut],1,1,()=>{ae[Ut]=null}),ps(),Il=ae[hl],Il||(Il=ae[hl]=Nt[hl](l),Il.c()),c(Il,1),Il.m(ce.parentNode,ce)),l[0]==="tf"?Q?M&1&&c(Q,1):(Q=ea(),Q.c(),c(Q,1),Q.m(ie.parentNode,ie)):Q&&(rs(),i(Q,1,1,()=>{Q=null}),ps());const Kt={};M&2&&(Kt.$$scope={dirty:M,ctx:l}),as.$set(Kt);let mt=_l;_l=Ft(l),_l!==mt&&(rs(),i(ne[mt],1,1,()=>{ne[mt]=null}),ps(),kl=ne[_l],kl||(kl=ne[_l]=xt[_l](l),kl.c()),c(kl,1),kl.m(Je.parentNode,Je));const Pt={};M&2&&(Pt.$$scope={dirty:M,ctx:l}),ns.$set(Pt);const Ot={};M&3&&(Ot.$$scope={dirty:M,ctx:l}),Ms.$set(Ot);let Tt=Zl;Zl=Ht(l),Zl!==Tt&&(rs(),i(Me[Tt],1,1,()=>{Me[Tt]=null}),ps(),$l=Me[Zl],$l||($l=Me[Zl]=Yt[Zl](l),$l.c()),c($l,1),$l.m(oe.parentNode,oe)),yt!==(yt=St(l))&&(es.d(1),es=yt(l),es&&(es.c(),es.m(Ue.parentNode,Ue))),l[0]==="pt"?A?M&1&&c(A,1):(A=ta(l),A.c(),c(A,1),A.m(me.parentNode,me)):A&&(rs(),i(A,1,1,()=>{A=null}),ps())},i(l){rt||(c(_.$$.fragment,l),c(g.$$.fragment,l),c(o),c(x.$$.fragment,l),c(gl.$$.fragment,l),c(Ml.$$.fragment,l),c(yl.$$.fragment,l),c(Bl.$$.fragment,l),c(Rl.$$.fragment,l),c(ol.$$.fragment,l),c(Wl.$$.fragment,l),c(Ql.$$.fragment,l),c(L.$$.fragment,l),c(z.$$.fragment,l),c(P.$$.fragment,l),c(O.$$.fragment,l),c(ll.$$.fragment,l),c(sl.$$.fragment,l),c(ds.$$.fragment,l),c(bs.$$.fragment,l),c(hs.$$.fragment,l),c(Is.$$.fragment,l),c(ts.$$.fragment,l),c(Zs.$$.fragment,l),c(bl),c(Cl),c(Bs.$$.fragment,l),c(Il),c(Q),c(as.$$.fragment,l),c(Qs.$$.fragment,l),c(Vs.$$.fragment,l),c(kl),c(ns.$$.fragment,l),c(Ms.$$.fragment,l),c(zs.$$.fragment,l),c($l),c(xs.$$.fragment,l),c(Fs.$$.fragment,l),c(Hs.$$.fragment,l),c(Ss.$$.fragment,l),c(Ds.$$.fragment,l),c(Ls.$$.fragment,l),c(Ps.$$.fragment,l),c(Os.$$.fragment,l),c(A),c(le.$$.fragment,l),rt=!0)},o(l){i(_.$$.fragment,l),i(g.$$.fragment,l),i(o),i(x.$$.fragment,l),i(gl.$$.fragment,l),i(Ml.$$.fragment,l),i(yl.$$.fragment,l),i(Bl.$$.fragment,l),i(Rl.$$.fragment,l),i(ol.$$.fragment,l),i(Wl.$$.fragment,l),i(Ql.$$.fragment,l),i(L.$$.fragment,l),i(z.$$.fragment,l),i(P.$$.fragment,l),i(O.$$.fragment,l),i(ll.$$.fragment,l),i(sl.$$.fragment,l),i(ds.$$.fragment,l),i(bs.$$.fragment,l),i(hs.$$.fragment,l),i(Is.$$.fragment,l),i(ts.$$.fragment,l),i(Zs.$$.fragment,l),i(bl),i(Cl),i(Bs.$$.fragment,l),i(Il),i(Q),i(as.$$.fragment,l),i(Qs.$$.fragment,l),i(Vs.$$.fragment,l),i(kl),i(ns.$$.fragment,l),i(Ms.$$.fragment,l),i(zs.$$.fragment,l),i($l),i(xs.$$.fragment,l),i(Fs.$$.fragment,l),i(Hs.$$.fragment,l),i(Ss.$$.fragment,l),i(Ds.$$.fragment,l),i(Ls.$$.fragment,l),i(Ps.$$.fragment,l),i(Os.$$.fragment,l),i(A),i(le.$$.fragment,l),rt=!1},d(l){l&&(e(y),e(n),e(J),e(f),e($),e(C),e(G),e(E),e(W),e(X),e(Vl),e(F),e(zl),e(B),e(Nl),e(R),e(vl),e(Y),e(tl),e(xl),e(H),e(al),e(Gl),e(nl),e(pl),e(Xl),e(rl),e(cl),e(Fl),e(S),e(il),e(Yl),e(q),e(Jl),e(Ul),e(Hl),e(D),e(ml),e(Sl),e(ql),e(Dl),e(K),e(Ll),e(Kl),e(ws),e(Tl),e(Pl),e(Ol),e(us),e(wl),e(ls),e(el),e(ss),e(N),e(ul),e(jl),e(U),e(Ce),e(he),e(fs),e(Ie),e(Cs),e(_e),e(ke),e(Ze),e(_s),e($e),e(ks),e(ge),e(Ge),e(Xe),e($s),e(Be),e(re),e(gs),e(Re),e(Gs),e(We),e(ye),e(Xs),e(Qe),e(Ae),e(ce),e(Rs),e(Ee),e(ie),e(Ve),e(Ws),e(ze),e(Ne),e(As),e(ve),e(Es),e(xe),e(Fe),e(Je),e(Ye),e(He),e(Se),e(Ns),e(qe),e(oe),e(vs),e(De),e(Le),e(Ke),e(Ys),e(Pe),e(Oe),e(lt),e(qs),e(st),e(et),e(tt),e(Ks),e(at),e(nt),e(Mt),e(Ue),e(me),e(pt),e(fe)),e(t),u(_,l),u(g,l),se[Z].d(l),u(x,l),u(gl,l),u(Ml,l),u(yl,l),u(Bl,l),u(Rl,l),u(ol,l),u(Wl,l),u(Ql,l),u(L,l),u(z,l),u(P,l),u(O,l),u(ll,l),u(sl,l),u(ds,l),u(bs,l),u(hs,l),u(Is,l),u(ts,l),u(Zs,l),ee[dl].d(l),te[fl].d(l),u(Bs,l),ae[hl].d(l),Q&&Q.d(l),u(as,l),u(Qs,l),u(Vs,l),ne[_l].d(l),u(ns,l),u(Ms,l),u(zs,l),Me[Zl].d(l),u(xs,l),u(Fs,l),u(Hs,l),u(Ss,l),u(Ds,l),u(Ls,l),u(Ps,l),u(Os,l),es.d(l),A&&A.d(l),u(le,l)}}}const Ea='{"title":"從頭開始訓練因果語言模型","local":"從頭開始訓練因果語言模型","sections":[{"title":"收集數據","local":"收集數據","sections":[],"depth":2},{"title":"準備數據集","local":"準備數據集","sections":[],"depth":2},{"title":"初始化新模型","local":"初始化新模型","sections":[],"depth":2},{"title":"使用管道生成代碼","local":"使用管道生成代碼","sections":[],"depth":2},{"title":"用 🤗 Accelerate 訓練","local":"用--accelerate-訓練","sections":[],"depth":2}],"depth":1}';function Va(I,t,y){let n="pt";return Ma(()=>{const J=new URLSearchParams(window.location.search);y(0,n=J.get("fw")||"pt")}),[n]}class qa extends pa{constructor(t){super(),ra(this,t,Va,Aa,na,{})}}export{qa as component};
