import{s as pa,a as ea,o as ra,n as re}from"../chunks/scheduler.cc52f4b9.js";import{S as ya,i as ca,e as j,s as p,c as U,h as ia,a as d,d as e,b as r,f as it,g as m,j as f,k as z,l as oa,m as a,n as T,o as i,u as ps,t as c,p as w,y as ta,v as rs}from"../chunks/index.9480bb2b.js";import{T as ys}from"../chunks/Tip.f7b80743.js";import{C as Ja,H as we,E as Ua}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.52103c90.js";import{Y as ut}from"../chunks/Youtube.7b12d9e8.js";import{C as h}from"../chunks/CodeBlock.c5404643.js";import{D as Ma}from"../chunks/DocNotebookDropdown.ea74f0e0.js";import{F as ma}from"../chunks/FrameworkSwitchCourse.42d12053.js";function Ta(I){let t,y;return t=new Ma({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(){U(t.$$.fragment)},l(n){m(t.$$.fragment,n)},m(n,o){T(t,n,o),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){w(t,n)}}}function wa(I){let t,y;return t=new Ma({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(){U(t.$$.fragment)},l(n){m(t.$$.fragment,n)},m(n,o){T(t,n,o),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){w(t,n)}}}function ua(I){let t,y="預訓練語言模型需要一段時間。我們建議您首先通過取消註釋以上兩行的註釋對數據樣本運行訓練循環，並確保訓練成功完成並存儲模型。沒有什麼比最後一步的訓練失敗更令人沮喪的了，因為你忘記創建一個文件夾或者因為保存路徑在訓練循環結束時有一個錯字！";return{c(){t=j("p"),t.textContent=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),f(t)!=="svelte-1sd9p9o"&&(t.textContent=y)},m(n,o){a(n,t,o)},p:re,d(n){n&&e(t)}}}function ja(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}),f(t)!=="svelte-1mwfx5t"&&(t.innerHTML=y)},m(n,o){a(n,t,o)},p:re,d(n){n&&e(t)}}}function da(I){let t,y,n,o="通過該配置，我們可以加載新模型。請注意，這是我們剛開始不使用<code>from_pretrained()</code>函數，因為我們實際上是在自己初始化模型：",_,C,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}}),C=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}}),Z=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(){U(t.$$.fragment),y=p(),n=j("p"),n.innerHTML=o,_=p(),U(C.$$.fragment),g=p(),U(Z.$$.fragment)},l(u){m(t.$$.fragment,u),y=r(u),n=d(u,"P",{"data-svelte-h":!0}),f(n)!=="svelte-18op4cm"&&(n.innerHTML=o),_=r(u),m(C.$$.fragment,u),g=r(u),m(Z.$$.fragment,u)},m(u,b){T(t,u,b),a(u,y,b),a(u,n,b),a(u,_,b),T(C,u,b),a(u,g,b),T(Z,u,b),$=!0},i(u){$||(c(t.$$.fragment,u),c(C.$$.fragment,u),c(Z.$$.fragment,u),$=!0)},o(u){i(t.$$.fragment,u),i(C.$$.fragment,u),i(Z.$$.fragment,u),$=!1},d(u){u&&(e(y),e(n),e(_),e(g)),w(t,u),w(C,u),w(Z,u)}}}function ba(I){let t,y,n,o="使用該配置，我們可以加載一個新模型。請注意，這是我們第一次不使用 <code>from_pretrained()</code> 函數，因為我們實際上是在自己初始化模型",_,C,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}}),C=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}}),Z=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(){U(t.$$.fragment),y=p(),n=j("p"),n.innerHTML=o,_=p(),U(C.$$.fragment),g=p(),U(Z.$$.fragment)},l(u){m(t.$$.fragment,u),y=r(u),n=d(u,"P",{"data-svelte-h":!0}),f(n)!=="svelte-1lln40o"&&(n.innerHTML=o),_=r(u),m(C.$$.fragment,u),g=r(u),m(Z.$$.fragment,u)},m(u,b){T(t,u,b),a(u,y,b),a(u,n,b),a(u,_,b),T(C,u,b),a(u,g,b),T(Z,u,b),$=!0},i(u){$||(c(t.$$.fragment,u),c(C.$$.fragment,u),c(Z.$$.fragment,u),$=!0)},o(u){i(t.$$.fragment,u),i(C.$$.fragment,u),i(Z.$$.fragment,u),$=!1},d(u){u&&(e(y),e(n),e(_),e(g)),w(t,u),w(C,u),w(Z,u)}}}function fa(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(){U(t.$$.fragment)},l(n){m(t.$$.fragment,n)},m(n,o){T(t,n,o),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){w(t,n)}}}function Ca(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(){U(t.$$.fragment)},l(n){m(t.$$.fragment,n)},m(n,o){T(t,n,o),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){w(t,n)}}}function ha(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(){U(t.$$.fragment)},l(n){m(t.$$.fragment,n)},m(n,o){T(t,n,o),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){w(t,n)}}}function Ia(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(){U(t.$$.fragment)},l(n){m(t.$$.fragment,n)},m(n,o){T(t,n,o),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){w(t,n)}}}function aa(I){let t,y="現在，我們可以使用<code>to_tf_dataset()</code>方法，使用上面創建的數據整理器將數據集轉換為TensorFlow數據集：",n,o,_;return o=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(),U(o.$$.fragment)},l(C){t=d(C,"P",{"data-svelte-h":!0}),f(t)!=="svelte-1lj0zqi"&&(t.innerHTML=y),n=r(C),m(o.$$.fragment,C)},m(C,g){a(C,t,g),a(C,n,g),T(o,C,g),_=!0},i(C){_||(c(o.$$.fragment,C),_=!0)},o(C){i(o.$$.fragment,C),_=!1},d(C){C&&(e(t),e(n)),w(o,C)}}}function _a(I){let t,y="⚠️  移動輸入和標籤以對齊它們發生在模型內部，因此數據整理器只需複製輸入以創建標籤。";return{c(){t=j("p"),t.textContent=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),f(t)!=="svelte-19fppxv"&&(t.textContent=y)},m(n,o){a(n,t,o)},p:re,d(n){n&&e(t)}}}function ka(I){let t,y="剩下要做的就是配置訓練超參數並調用 <code>compile()</code> 和 <code>fit()</code>。我們將使用帶有一些預熱的學習率調整策略來提高訓練的穩定性：",n,o,_,C,g="現在我們可以調用<code>model.fit()，</code>並等待訓練完成。你是在完整的訓練集還是他的子集上運行，這將分別需要20和2個小時，所以拿一些咖啡和一本好書來閱讀！訓練完成後，我們可以將模型和分詞器推送到中心：",Z,$,u;return o=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}}),$=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(),U(o.$$.fragment),_=p(),C=j("p"),C.innerHTML=g,Z=p(),U($.$$.fragment)},l(b){t=d(b,"P",{"data-svelte-h":!0}),f(t)!=="svelte-ocv7ex"&&(t.innerHTML=y),n=r(b),m(o.$$.fragment,b),_=r(b),C=d(b,"P",{"data-svelte-h":!0}),f(C)!=="svelte-1s0qalu"&&(C.innerHTML=g),Z=r(b),m($.$$.fragment,b)},m(b,G){a(b,t,G),a(b,n,G),T(o,b,G),a(b,_,G),a(b,C,G),a(b,Z,G),T($,b,G),u=!0},i(b){u||(c(o.$$.fragment,b),c($.$$.fragment,b),u=!0)},o(b){i(o.$$.fragment,b),i($.$$.fragment,b),u=!1},d(b){b&&(e(t),e(n),e(_),e(C),e(Z)),w(o,b),w($,b)}}}function Za(I){let t,y="剩下要做的就是配置訓練參數並啟動 <code>Trainer</code> .我們將使用餘弦學習率，並進行一些Warmup和有效批量大小為 256 ( <code>per_device_train_batch_size</code> * <code>gradient_accumulation_steps</code>）。當單個批次不適合內存時使用梯度累積，並通過多次向前/向後傳遞逐步建立梯度。當我們使用 🤗 Accelerate 創建訓練循環時，我們將看到這一點。",n,o,_,C,g="現在我們可以開始 <code>Trainer</code>並等待訓練完成。根據您是在整個訓練集還是在訓練集的一個子集上運行它，這將分別需要 20 或 2 個小時，因此請喝杯咖啡和一本好書來閱讀！",Z,$,u,b,G="訓練完成後，我們可以將模型和標記器推送到 Hub：",R,B,v;return o=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}}),$=new h({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:"trainer.train()",wrap:!1}}),B=new h({props:{code:"dHJhaW5lci5wdXNoX3RvX2h1Yigp",highlighted:"trainer.push_to_hub()",wrap:!1}}),{c(){t=j("p"),t.innerHTML=y,n=p(),U(o.$$.fragment),_=p(),C=j("p"),C.innerHTML=g,Z=p(),U($.$$.fragment),u=p(),b=j("p"),b.textContent=G,R=p(),U(B.$$.fragment)},l(k){t=d(k,"P",{"data-svelte-h":!0}),f(t)!=="svelte-119mbhu"&&(t.innerHTML=y),n=r(k),m(o.$$.fragment,k),_=r(k),C=d(k,"P",{"data-svelte-h":!0}),f(C)!=="svelte-27uz21"&&(C.innerHTML=g),Z=r(k),m($.$$.fragment,k),u=r(k),b=d(k,"P",{"data-svelte-h":!0}),f(b)!=="svelte-1g9wxp9"&&(b.textContent=G),R=r(k),m(B.$$.fragment,k)},m(k,X){a(k,t,X),a(k,n,X),T(o,k,X),a(k,_,X),a(k,C,X),a(k,Z,X),T($,k,X),a(k,u,X),a(k,b,X),a(k,R,X),T(B,k,X),v=!0},i(k){v||(c(o.$$.fragment,k),c($.$$.fragment,k),c(B.$$.fragment,k),v=!0)},o(k){i(o.$$.fragment,k),i($.$$.fragment,k),i(B.$$.fragment,k),v=!1},d(k){k&&(e(t),e(n),e(_),e(C),e(Z),e(u),e(b),e(R)),w(o,k),w($,k),w(B,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}),f(t)!=="svelte-1h02rte"&&(t.innerHTML=y)},m(n,o){a(n,t,o)},p:re,d(n){n&&e(t)}}}function ga(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}),f(t)!=="svelte-e002qg"&&(t.innerHTML=y)},m(n,o){a(n,t,o)},d(n){n&&e(t)}}}function Ga(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}),f(t)!=="svelte-1buw6v8"&&(t.innerHTML=y)},m(n,o){a(n,t,o)},d(n){n&&e(t)}}}function Xa(I){let t;function y(_,C){return _[0]==="pt"?Ga:ga}let n=y(I),o=n(I);return{c(){o.c(),t=ta()},l(_){o.l(_),t=ta()},m(_,C){o.m(_,C),a(_,t,C)},p(_,C){n!==(n=y(_))&&(o.d(1),o=n(_),o&&(o.c(),o.m(t.parentNode,t)))},d(_){_&&e(t),o.d(_)}}}function Ba(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(){U(t.$$.fragment)},l(n){m(t.$$.fragment,n)},m(n,o){T(t,n,o),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){w(t,n)}}}function Ra(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(){U(t.$$.fragment)},l(n){m(t.$$.fragment,n)},m(n,o){T(t,n,o),y=!0},i(n){y||(c(t.$$.fragment,n),y=!0)},o(n){i(t.$$.fragment,n),y=!1},d(n){w(t,n)}}}function Wa(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}),f(t)!=="svelte-n880tn"&&(t.innerHTML=y)},m(n,o){a(n,t,o)},d(n){n&&e(t)}}}function Qa(I){let t,y="看看這幾個例子，似乎模型已經學習了Python數據科學堆棧的一些語法。當然，在將模型部署到現實世界之前，我們需要更徹底地評估模型，但這仍然是一個令人印象深刻的原型。";return{c(){t=j("p"),t.textContent=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),f(t)!=="svelte-18hwf7y"&&(t.textContent=y)},m(n,o){a(n,t,o)},d(n){n&&e(t)}}}function na(I){let t,y,n,o="我們已經看到了如何使用 <code>Trainer</code> ，這可以允許一些自定義。然而，有時我們想要完全控制訓練循環，或者我們想要進行一些奇特的更改。在這種情況下 🤗 Accelerate 是一個不錯的選擇，在本節中，我們將逐步介紹使用它來訓練我們的模型的步驟。為了讓事情變得更有趣，我們還將在訓練循環中添加一些修改。",_,C,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：",u,b,G,R,B,v,k="太好了，這似乎很好用！我們現在可以編寫一個自定義損失函數，它將輸入序列、logits 和我們剛剛選擇的關​​鍵標記作為輸入。首先，我們需要對齊 logits 和輸入：向右移動一個的輸入序列形成標籤，因為下一個標記是當前標記的標籤。我們可以通過從輸入序列的第二個標記開始標記來實現這一點，因為模型無論如何都不會對第一個標記進行預測。然後我們切斷最後一個 logit，因為我們沒有完整輸入序列之後的標記的標籤。有了這個，我們可以計算每個樣本的損失並計算每個樣本中所有關鍵字的出現次數。最後，我們使用出現次數作為權重計算所有樣本的加權平均值。由於我們不想扔掉所有沒有關鍵字的樣本，我們將權重加1：",X,kl,El,x,cs="在我們開始使用這個很棒的新損失函數進行訓練之前，我們需要準備一些東西：",nl,Zl,is="<li>我們需要數據加載器來批量加載數據。</li> <li>我們需要設置權重衰減參數。</li> <li>有時我們想要求值，因此將求值代碼包裝在一個函數中是有意義的。</li>",A,$l,os="讓我們從數據加載器開始。我們只需要將數據集的格式設置為 <code>&quot;torch&quot;</code>，然後我們可以將它傳遞給 PyTorch <code>DataLoader</code> ,同時設置適當的批量大小：",E,gl,zl,F,ue="接下來，我們對參數進行分組，以便優化器知道哪些將獲得額外的權重衰減。通常，所有偏差和 LayerNorm 權重項都不受此限制；以下我們如何做到這一點：",Vl,Y,Nl,H,je="由於我們希望在訓練期間定期在驗證集上評估模型，因此我們也為此編寫一個函數。它只是運行評估數據加載器並收集跨進程的所有損失：",vl,S,Js,Ml,Gl='通過 <code>evaluate()</code> 函數我們定期可以獲取損失值和<a href="/course/chapter7/3">perplexity</a>。接下來，我們重新定義我們的模型以確保我們再次從頭開始訓練：',xl,q,Us,pl,Xl="然後我們可以定義我們的優化器，使用之前的函數來分割權重衰減的參數：",Fl,D,Yl,L,de="現在讓我們準備模型、優化器和數據加載器，以便我們可以開始訓練：",Hl,K,Sl,P,ms,rl,Bl="現在我們已經發送了我們的 <code>train_dataloader</code>到 <code>accelerator.prepare()</code> ，我們可以使用它的長度來計算訓練步驟的數量。請記住，我們應該始終在準備好dataloader後執行此操作，因為該方法會改變其長度。我們使用經典線性學習率調度：",ql,O,Dl,ll,be="最後，要將我們的模型推送到 Hub，我們需要創建一個 <code>Repository</code> 工作文件夾中的對象。如果您尚未登錄，請先登錄 Hugging Face。我們將從我們想要為模型提供的模型 ID 中確定存儲庫名稱（您可以自由地用自己的選擇替換 <code>repo_name</code> ；它只需要包含您的用戶名，可以使用<code>get_full_repo_name()</code>函數的查看目前的repo_name）：",Ll,sl,Kl,el,Pl,V,Ts="然後我們可以在本地文件夾中克隆該存儲庫。如果它已經存在，這個本地文件夾應該是我們正在使用的存儲庫的克隆：",yl,Rl,Ol,tl,ws="我們現在可以上傳我們保存的任何內容 <code>output_dir</code> 通過調用 <code>repo.push_to_hub()</code> 方法。這將幫助我們在每個 epoch 結束時上傳中間模型。在我們訓練之前，讓我們運行一個快速測試，看看評估函數是否正常工作：",cl,il,ol,Wl,ls,al,us="這些損失和困惑度的值非常高，但這並不奇怪，因為我們還沒有訓練過模型。有了這個，我們已經準備好編寫訓練腳本的核心部分：訓練循環。在訓練循環中，我們遍歷數據加載器並將批次傳遞給模型。有了 logits，我們就可以評估我們的自定義損失函數。我們通過梯度累積步驟的數量來縮放損失，以便在聚合更多步驟時不會產生更大的損失。在我們優化之前，我們還剪輯了梯度以獲得更好的收斂性。最後，每隔幾步，我們就會使用新的 <code>evaluate()</code> 函數評估模型：",Jl,Ul,ml,Ql,js="就是這樣 - 您現在擁有自己的因果語言模型（例如 GPT-2）的自定義訓練循環，您可以根據自己的需要進一步自定義。",Tl,Al,ss,N,ds;return t=new we({props:{title:"用 🤗 Accelerate 訓練",local:"用--accelerate-訓練",headingTag:"h2"}}),C=new ut({props:{id:"Hm8_PgVTFuc"}}),b=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}}),R=new h({props:{code:"J0tleXdvcmQlMjBoYXMlMjBub3QlMjBzaW5nbGUlMjB0b2tlbiUzQSUyMHRlc3R0ZXN0Jw==",highlighted:'<span class="hljs-string">&#x27;Keyword has not single token: testtest&#x27;</span>',wrap:!1}}),kl=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}}),gl=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}}),Y=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}}),S=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}}),q=new h({props:{code:"bW9kZWwlMjAlM0QlMjBHUFQyTE1IZWFkTW9kZWwoY29uZmlnKQ==",highlighted:"model = GPT2LMHeadModel(config)",wrap:!1}}),D=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}}),K=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}}),P=new ys({props:{$$slots:{default:[Aa]},$$scope:{ctx:I}}}),O=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}}),sl=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}}),el=new h({props:{code:"J3NndWdnZXIlMkZjb2RlcGFycm90LWRzLWFjY2VsZXJhdGUn",highlighted:'<span class="hljs-string">&#x27;sgugger/codeparrot-ds-accelerate&#x27;</span>',wrap:!1}}),Rl=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}}),il=new h({props:{code:"ZXZhbHVhdGUoKQ==",highlighted:"evaluate()",wrap:!1}}),Wl=new h({props:{code:"KDEwLjkzNDEyNjg1Mzk0Mjg3MSUyQyUyMDU2MDU3LjE0NDUzMTI1KQ==",highlighted:'(<span class="hljs-number">10.934126853942871</span>, <span class="hljs-number">56057.14453125</span>)',wrap:!1}}),Ul=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}}),Al=new ys({props:{$$slots:{default:[Ea]},$$scope:{ctx:I}}}),N=new ys({props:{$$slots:{default:[za]},$$scope:{ctx:I}}}),{c(){U(t.$$.fragment),y=p(),n=j("p"),n.innerHTML=o,_=p(),U(C.$$.fragment),g=p(),Z=j("p"),Z.innerHTML=$,u=p(),U(b.$$.fragment),G=p(),U(R.$$.fragment),B=p(),v=j("p"),v.textContent=k,X=p(),U(kl.$$.fragment),El=p(),x=j("p"),x.textContent=cs,nl=p(),Zl=j("ul"),Zl.innerHTML=is,A=p(),$l=j("p"),$l.innerHTML=os,E=p(),U(gl.$$.fragment),zl=p(),F=j("p"),F.textContent=ue,Vl=p(),U(Y.$$.fragment),Nl=p(),H=j("p"),H.textContent=je,vl=p(),U(S.$$.fragment),Js=p(),Ml=j("p"),Ml.innerHTML=Gl,xl=p(),U(q.$$.fragment),Us=p(),pl=j("p"),pl.textContent=Xl,Fl=p(),U(D.$$.fragment),Yl=p(),L=j("p"),L.textContent=de,Hl=p(),U(K.$$.fragment),Sl=p(),U(P.$$.fragment),ms=p(),rl=j("p"),rl.innerHTML=Bl,ql=p(),U(O.$$.fragment),Dl=p(),ll=j("p"),ll.innerHTML=be,Ll=p(),U(sl.$$.fragment),Kl=p(),U(el.$$.fragment),Pl=p(),V=j("p"),V.textContent=Ts,yl=p(),U(Rl.$$.fragment),Ol=p(),tl=j("p"),tl.innerHTML=ws,cl=p(),U(il.$$.fragment),ol=p(),U(Wl.$$.fragment),ls=p(),al=j("p"),al.innerHTML=us,Jl=p(),U(Ul.$$.fragment),ml=p(),Ql=j("p"),Ql.textContent=js,Tl=p(),U(Al.$$.fragment),ss=p(),U(N.$$.fragment)},l(s){m(t.$$.fragment,s),y=r(s),n=d(s,"P",{"data-svelte-h":!0}),f(n)!=="svelte-1vyez8"&&(n.innerHTML=o),_=r(s),m(C.$$.fragment,s),g=r(s),Z=d(s,"P",{"data-svelte-h":!0}),f(Z)!=="svelte-1p47x73"&&(Z.innerHTML=$),u=r(s),m(b.$$.fragment,s),G=r(s),m(R.$$.fragment,s),B=r(s),v=d(s,"P",{"data-svelte-h":!0}),f(v)!=="svelte-1bnwbrb"&&(v.textContent=k),X=r(s),m(kl.$$.fragment,s),El=r(s),x=d(s,"P",{"data-svelte-h":!0}),f(x)!=="svelte-4480va"&&(x.textContent=cs),nl=r(s),Zl=d(s,"UL",{"data-svelte-h":!0}),f(Zl)!=="svelte-1hho8k5"&&(Zl.innerHTML=is),A=r(s),$l=d(s,"P",{"data-svelte-h":!0}),f($l)!=="svelte-1jnyd3b"&&($l.innerHTML=os),E=r(s),m(gl.$$.fragment,s),zl=r(s),F=d(s,"P",{"data-svelte-h":!0}),f(F)!=="svelte-k4ws43"&&(F.textContent=ue),Vl=r(s),m(Y.$$.fragment,s),Nl=r(s),H=d(s,"P",{"data-svelte-h":!0}),f(H)!=="svelte-lbudkm"&&(H.textContent=je),vl=r(s),m(S.$$.fragment,s),Js=r(s),Ml=d(s,"P",{"data-svelte-h":!0}),f(Ml)!=="svelte-lubkiv"&&(Ml.innerHTML=Gl),xl=r(s),m(q.$$.fragment,s),Us=r(s),pl=d(s,"P",{"data-svelte-h":!0}),f(pl)!=="svelte-1dz6iug"&&(pl.textContent=Xl),Fl=r(s),m(D.$$.fragment,s),Yl=r(s),L=d(s,"P",{"data-svelte-h":!0}),f(L)!=="svelte-182mazh"&&(L.textContent=de),Hl=r(s),m(K.$$.fragment,s),Sl=r(s),m(P.$$.fragment,s),ms=r(s),rl=d(s,"P",{"data-svelte-h":!0}),f(rl)!=="svelte-1os5172"&&(rl.innerHTML=Bl),ql=r(s),m(O.$$.fragment,s),Dl=r(s),ll=d(s,"P",{"data-svelte-h":!0}),f(ll)!=="svelte-1gs3lys"&&(ll.innerHTML=be),Ll=r(s),m(sl.$$.fragment,s),Kl=r(s),m(el.$$.fragment,s),Pl=r(s),V=d(s,"P",{"data-svelte-h":!0}),f(V)!=="svelte-1fj9jwz"&&(V.textContent=Ts),yl=r(s),m(Rl.$$.fragment,s),Ol=r(s),tl=d(s,"P",{"data-svelte-h":!0}),f(tl)!=="svelte-ew2ylh"&&(tl.innerHTML=ws),cl=r(s),m(il.$$.fragment,s),ol=r(s),m(Wl.$$.fragment,s),ls=r(s),al=d(s,"P",{"data-svelte-h":!0}),f(al)!=="svelte-1vo8k3"&&(al.innerHTML=us),Jl=r(s),m(Ul.$$.fragment,s),ml=r(s),Ql=d(s,"P",{"data-svelte-h":!0}),f(Ql)!=="svelte-ep83k9"&&(Ql.textContent=js),Tl=r(s),m(Al.$$.fragment,s),ss=r(s),m(N.$$.fragment,s)},m(s,J){T(t,s,J),a(s,y,J),a(s,n,J),a(s,_,J),T(C,s,J),a(s,g,J),a(s,Z,J),a(s,u,J),T(b,s,J),a(s,G,J),T(R,s,J),a(s,B,J),a(s,v,J),a(s,X,J),T(kl,s,J),a(s,El,J),a(s,x,J),a(s,nl,J),a(s,Zl,J),a(s,A,J),a(s,$l,J),a(s,E,J),T(gl,s,J),a(s,zl,J),a(s,F,J),a(s,Vl,J),T(Y,s,J),a(s,Nl,J),a(s,H,J),a(s,vl,J),T(S,s,J),a(s,Js,J),a(s,Ml,J),a(s,xl,J),T(q,s,J),a(s,Us,J),a(s,pl,J),a(s,Fl,J),T(D,s,J),a(s,Yl,J),a(s,L,J),a(s,Hl,J),T(K,s,J),a(s,Sl,J),T(P,s,J),a(s,ms,J),a(s,rl,J),a(s,ql,J),T(O,s,J),a(s,Dl,J),a(s,ll,J),a(s,Ll,J),T(sl,s,J),a(s,Kl,J),T(el,s,J),a(s,Pl,J),a(s,V,J),a(s,yl,J),T(Rl,s,J),a(s,Ol,J),a(s,tl,J),a(s,cl,J),T(il,s,J),a(s,ol,J),T(Wl,s,J),a(s,ls,J),a(s,al,J),a(s,Jl,J),T(Ul,s,J),a(s,ml,J),a(s,Ql,J),a(s,Tl,J),T(Al,s,J),a(s,ss,J),T(N,s,J),ds=!0},i(s){ds||(c(t.$$.fragment,s),c(C.$$.fragment,s),c(b.$$.fragment,s),c(R.$$.fragment,s),c(kl.$$.fragment,s),c(gl.$$.fragment,s),c(Y.$$.fragment,s),c(S.$$.fragment,s),c(q.$$.fragment,s),c(D.$$.fragment,s),c(K.$$.fragment,s),c(P.$$.fragment,s),c(O.$$.fragment,s),c(sl.$$.fragment,s),c(el.$$.fragment,s),c(Rl.$$.fragment,s),c(il.$$.fragment,s),c(Wl.$$.fragment,s),c(Ul.$$.fragment,s),c(Al.$$.fragment,s),c(N.$$.fragment,s),ds=!0)},o(s){i(t.$$.fragment,s),i(C.$$.fragment,s),i(b.$$.fragment,s),i(R.$$.fragment,s),i(kl.$$.fragment,s),i(gl.$$.fragment,s),i(Y.$$.fragment,s),i(S.$$.fragment,s),i(q.$$.fragment,s),i(D.$$.fragment,s),i(K.$$.fragment,s),i(P.$$.fragment,s),i(O.$$.fragment,s),i(sl.$$.fragment,s),i(el.$$.fragment,s),i(Rl.$$.fragment,s),i(il.$$.fragment,s),i(Wl.$$.fragment,s),i(Ul.$$.fragment,s),i(Al.$$.fragment,s),i(N.$$.fragment,s),ds=!1},d(s){s&&(e(y),e(n),e(_),e(g),e(Z),e(u),e(G),e(B),e(v),e(X),e(El),e(x),e(nl),e(Zl),e(A),e($l),e(E),e(zl),e(F),e(Vl),e(Nl),e(H),e(vl),e(Js),e(Ml),e(xl),e(Us),e(pl),e(Fl),e(Yl),e(L),e(Hl),e(Sl),e(ms),e(rl),e(ql),e(Dl),e(ll),e(Ll),e(Kl),e(Pl),e(V),e(yl),e(Ol),e(tl),e(cl),e(ol),e(ls),e(al),e(Jl),e(ml),e(Ql),e(Tl),e(ss)),w(t,s),w(C,s),w(b,s),w(R,s),w(kl,s),w(gl,s),w(Y,s),w(S,s),w(q,s),w(D,s),w(K,s),w(P,s),w(O,s),w(sl,s),w(el,s),w(Rl,s),w(il,s),w(Wl,s),w(Ul,s),w(Al,s),w(N,s)}}}function Aa(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}),f(t)!=="svelte-b54156"&&(t.innerHTML=y)},m(n,o){a(n,t,o)},p:re,d(n){n&&e(t)}}}function Ea(I){let t,y="✏️ <strong>試試看!</strong> 創建適合您的用例的自定義損失函數，或在訓練循環中添加另一個自定義步驟。";return{c(){t=j("p"),t.innerHTML=y},l(n){t=d(n,"P",{"data-svelte-h":!0}),f(t)!=="svelte-1t4u52j"&&(t.innerHTML=y)},m(n,o){a(n,t,o)},p:re,d(n){n&&e(t)}}}function za(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}),f(t)!=="svelte-11x8ujc"&&(t.innerHTML=y)},m(n,o){a(n,t,o)},p:re,d(n){n&&e(t)}}}function Va(I){let t,y,n,o,_,C,g,Z,$,u,b,G,R,B,v='到目前為止，我們主要使用預訓練模型，並通過重用預訓練的權重來針對新用例對它們進行微調。正如我們在<a href="/course/chapter1">第一章</a>, 這通常稱為遷移學習，這是將 Transformer 模型應用於大多數標記數據稀疏的現實世界用例的非常成功的策略。在本章中，我們將採用不同的方法並從頭開始訓練一個全新的模型。如果您有大量數據並且它與用於可用模型的預訓練數據有很大不同，那麼這是一個很好的方法。然而，它也需要更多的計算資源來預訓練語言模型，而不僅僅是微調現有的模型。訓練新模型有意義的示例包括由音符、分子序列（如 DNA）或編程語言組成的數據集。後者最近受到關注，這要歸功於 TabNine 和 GitHub 的 Copilot 等工具，它們由 OpenAI 的 Codex 模型提供支持，可以生成長代碼序列。這種文本生成任務最好使用自迴歸或因果語言模型（例如 GPT-2）來解決。',k,X,kl="在本節中，我們將構建代碼生成模型的縮小版本：我們將使用 Python 代碼的子集專注於單行完成而不是完整的函數或類。在 Python 中處理數據時，您會經常接觸 Python 數據科學堆棧，包括 <code>matplotlib</code> , <code>seaborn</code> , <code>pandas</code> ， 和 <code>scikit-learn</code> 庫。在使用這些框架時，通常需要查找特定的命令，因此如果我們可以使用模型來為我們完成這些調用，那就太好了。",El,x,cs,nl,Zl='在<a href="/course/chapter6">第六章</a> 我們創建了一個高效的分詞器來處理 Python 源代碼，但我們仍然需要一個大規模數據集來預訓練模型。在這裡，我們將我們的分詞器應用到源自 GitHub 存儲庫的 Python 代碼語料庫。然後我們將使用 <code>Trainer</code> API 和 🤗 Accelerate 來訓練模型。讓我們開始吧！',is,A,$l,os,E,gl,zl,F,ue='這實際上展示了使用本節中訓練並上傳到 Hub 的模型。你可以在<a href="https://huggingface.co/huggingface-course/codeparrot-ds?text=plt.imshow%28" rel="nofollow">這裡</a>找到。請注意，由於在文本生成過程中發生了一些隨機化，您可能會得到略有不同的結果。',Vl,Y,Nl,H,je='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>上分享出來了.',vl,S,Js="然而，對完整語料庫的訓練既耗時又費力，我們只需要與 Python 數據科學堆棧相關的數據集子集。所以，讓我們開始過濾 <code>codeparrot</code> 包含此堆棧中任何庫的所有文件的數據集。由於數據集的太大，我們希望避免下載它；因此反，我們將使用流功能來動態過濾它。為了使用前面提到的庫過濾代碼示例，我們將使用以下函數：",Ml,Gl,xl,q,Us="讓我們用兩個例子來測試一下：",pl,Xl,Fl,D,Yl,L,de="我們可以使用它來創建一個函數來流式傳輸數據集並過濾我們想要的元素：",Hl,K,Sl,P,ms="然後我們可以簡單地將此函數應用於流數據集：",rl,Bl,ql,O,Dl,ll,be="這給我們留下了大約 3% 的原始數據集，這個數據集仍然相當可觀——結果數據集有 6 GB，包含 600,000 個 Python 腳本！過濾完整數據集可能需要 2-3 小時，具體取決於您的機器和帶寬。如果您不想自己經歷這個漫長的過程，我們在 Hub 上提供過濾後的數據集供您下載：",Ll,sl,Kl,el,Pl,V,Ts,yl,Rl="讓我們看一個來自數據集的例子。我們將只顯示每個字段的前 200 個字符：",Ol,tl,ws,cl,il,ol,Wl="我們可以看到 <code>content</code> 字段包含我們希望我們的模型訓練的代碼。現在我們有了一個數據集，我們需要預處理文本，使其採用適合預訓練的格式。",ls,al,us,Jl,Ul,ml,Ql="第一步是對數據進行標記，以便我們可以將其用於訓練。由於我們的目標主要是自動完成短函數調用，因此我們可以保持上下文大小相對較小。這樣做的好處是我們可以更快地訓練模型並且它需要的內存顯著減少。如果您的應用程序擁有更多上下文很重要（例如，如果您希望模型基於具有函數定義的文件編寫單元測試），請確保增加該數量，但請記住，這需要更大的 GPU 內存佔用。現在，讓我們將上下文大小固定為 128 個標記，而不是 GPT-2 或 GPT-3 中分別使用的 1,024 或 2,048 個標記。",js,Tl,Al='大多數文檔包含超過 128 個標記，因此簡單地將輸入截斷到最大長度將消除我們數據集的很大一部分。相反，我們將使用 <code>return_overflowing_tokens</code> 標記整個輸入並將其分成幾個塊的選項，就像我們在<a href="/course/chapter6/4">第六章</a>. 我們還將使用 <code>return_length</code> 選項自動返回每個創建的塊的長度。通常最後一個塊會小於上下文大小，我們會去掉這些塊以避免填充問題；因為無論如何我們都有大量數據。',ss,N,ds='<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."/>',s,J,jt="讓我們通過查看前兩個示例來確切瞭解這是如何工作的：",Ce,bs,he,fs,Ie,Cs,dt="我們可以看 到，從這兩個示例中我們總共得到了 34 個片段。查看塊長度，我們可以看到兩個文檔末尾的塊都少於 128 個標記（分別為 117 和 41）。這些僅代表我們擁有的數據集的一小部分，因此我們可以安全地將它們扔掉。通過 <code>overflow_to_sample_mapping</code> 字段，我們還可以重建哪些塊屬於哪些輸入樣本。",_e,hs,bt='通過這個操作，我們使用了一個方便的🤗 Datasets 中的<code>Dataset.map()</code> 函數，就是不需要一對一的映射；正如我們在<a href="/course/chapter7/3">第三節</a>,我們可以創建具有比輸入批次更多或更少元素的批次。這在執行更改元素數量的數據增強或數據過濾等操作時非常有用。在我們的例子中，當將每個元素標記為指定上下文大小的塊時，我們從每個文檔中創建了許多樣本。我們只需要確保刪除現有的列，因為它們的大小存在衝突。如果我們想保留它們，我們可以適當地重複它們，並在<code>Dataset.map()</code> 調用中返回它們:',ke,Is,Ze,_s,$e,ks,ft="我們現在有 1670 萬個示例，每個示例有 128 個tokens ，總共相當於大約 21 億個tokens 。作為參考，OpenAI 的 GPT-3 和 Codex 模型分別在 300 和 1000 億個tokens 上訓練，其中 Codex 模型從 GPT-3 檢查點初始化。我們在本節中的目標不是與這些模型競爭，這些模型可以生成長而連貫的文本，而是創建一個縮小版本，為數據科學家提供快速自動完成功能。",ge,Zs,Ct="現在我們已經準備好了數據集，讓我們設置模型！",Ge,ts,Xe,$s,Be,gs,ht="我們的第一步是新初始化一個 GPT-2 模型。我們將對我們的模型使用與小型 GPT-2 模型相同的配置，因此我們加載預訓練配置，確保分詞器大小與模型詞彙量大小匹配並設置 <code>bos</code> 和 <code>eos</code> （序列的開始和結束）令牌 ID：",Re,wl,ul,ye,Gs,It="我們的模型有 1.24 億個參數，我們必須對其進行調整。在開始訓練之前，我們需要設置一個負責創建批次的數據整理器。我們可以使用 <code>DataCollatorForLanguageModeling</code> ，它是專為語言建模而設計（顧名思義）。除了堆疊和填充批次，它還負責創建語言模型標籤——在因果語言建模中，輸入也用作標籤（只是移動了一個元素），並且這個數據整理器在訓練期間即時創建它們，所以我們不需要複製 <code>input_ids</code>。",We,Xs,_t="注意 <code>DataCollatorForLanguageModeling</code> 支持掩碼語言建模 (MLM) 和因果語言建模 (CLM)。默認情況下它為 MLM 準備數據，但我們可以通過設置<code>mlm=False</code>參數切換到 CLM ：",Qe,jl,dl,ce,Bs,kt="讓我們看一個例子：",Ae,Rs,Ee,bl,fl,ie,Ws,Zt="我們可以看到示例已經堆疊在一起，並且所有張量都具有相同的形狀。",ze,oe,as,Ve,Qs,$t="現在我們已經準備好實際訓練我們的模型的一切了——畢竟這不是那麼多工作！在我們開始訓練之前，我們應該登錄 Hugging Face。如果您在筆記本上工作，則可以使用以下實用程序功能：",Ne,As,ve,Es,gt="這將顯示一個小部件，您可以在其中輸入您的 Hugging Face 登錄憑據。",xe,zs,Gt="如果您不是在notebook上工作，只需在終端中輸入以下行：",Fe,Vs,Ye,Cl,hl,Je,ns,He,Ms,Se,Ns,qe,vs,Xt="現在是關鍵的部分：讓我們看看經過訓練的模型的實際效果如何！我們可以在日誌中看到損失穩步下降，但為了讓模型進行測試，讓我們看看它在某些測試上的表現如何。為此，我們將模型包裝在文本生成中的<code>pipeline</code> ，如果有可用的，我們會將它放在 GPU 上進行快速生成：",De,Il,_l,Ue,xs,Bt="讓我們從創建散點圖的簡單任務開始：",Le,Fs,Ke,Ys,Pe,Hs,Rt="結果看起來是正確的。它也適用於 <code>pandas</code> 類型？讓我們看看我們是否使用兩個數組可以創建一個 <code>DataFrame</code> ：",Oe,Ss,lt,qs,st,Ds,Wt="很好，這是正確的答案——儘管它隨後再次插入了列 <code>x</code> 。由於生成的token數量有限，以下 <code>for</code> 循環被切斷。讓我們看看我們是否可以做一些更復雜的事情並讓模型幫助我們分組操作：",et,Ls,tt,Ks,at,Ps,Qt="不錯;這是正確的做法。最後，讓我們看看我們是否也可以將其用於 <code>scikit-learn</code> 並建立一個隨機森林模型：",nt,Os,Mt,le,pt,me,Te,se,rt,fe,yt;_=new ma({props:{fw:I[0]}}),g=new Ja({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),$=new we({props:{title:"從頭開始訓練因果語言模型",local:"從頭開始訓練因果語言模型",headingTag:"h1"}});const At=[wa,Ta],ee=[];function Et(l,M){return l[0]==="pt"?0:1}b=Et(I),G=ee[b]=At[b](I),x=new ut({props:{id:"Vpjb1lu0MDk"}}),Y=new we({props:{title:"收集數據",local:"收集數據",headingTag:"h2"}}),Gl=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}}),Xl=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}}),D=new h({props:{code:"RmFsc2UlMjBUcnVl",highlighted:'<span class="hljs-literal">False</span> <span class="hljs-literal">True</span>',wrap:!1}}),K=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}}),Bl=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}}),O=new h({props:{code:"My4yNiUyNSUyMG9mJTIwZGF0YSUyMGFmdGVyJTIwZmlsdGVyaW5nLg==",highlighted:'<span class="hljs-number">3.26</span>% of data after filtering.',wrap:!1}}),sl=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}}),el=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}}),V=new ys({props:{$$slots:{default:[ua]},$$scope:{ctx:I}}}),tl=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}}),cl=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}}),al=new we({props:{title:"準備數據集",local:"準備數據集",headingTag:"h2"}}),Jl=new ut({props:{id:"ma1TrR7gE7I"}}),bs=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}}),fs=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}}),Is=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}}),_s=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:[ja]},$$scope:{ctx:I}}}),$s=new we({props:{title:"初始化新模型",local:"初始化新模型",headingTag:"h2"}});const zt=[ba,da],te=[];function Vt(l,M){return l[0]==="pt"?0:1}wl=Vt(I),ul=te[wl]=zt[wl](I);const Nt=[Ca,fa],ae=[];function vt(l,M){return l[0]==="pt"?0:1}jl=vt(I),dl=ae[jl]=Nt[jl](I),Rs=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 xt=[Ia,ha],ne=[];function Ft(l,M){return l[0]==="pt"?0:1}bl=Ft(I),fl=ne[bl]=xt[bl](I);let W=I[0]==="tf"&&aa();as=new ys({props:{warning:!0,$$slots:{default:[_a]},$$scope:{ctx:I}}}),As=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 Yt=[Za,ka],Me=[];function Ht(l,M){return l[0]==="pt"?0:1}Cl=Ht(I),hl=Me[Cl]=Yt[Cl](I),ns=new ys({props:{$$slots:{default:[$a]},$$scope:{ctx:I}}}),Ms=new ys({props:{$$slots:{default:[Xa]},$$scope:{ctx:I}}}),Ns=new we({props:{title:"使用管道生成代碼",local:"使用管道生成代碼",headingTag:"h2"}});const St=[Ra,Ba],pe=[];function qt(l,M){return l[0]==="pt"?0:1}Il=qt(I),_l=pe[Il]=St[Il](I),Fs=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}}),Ys=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}}),Ss=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}}),qs=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}}),Ls=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}}),Ks=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}}),Os=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}}),le=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 Dt(l,M){return l[0]==="tf"?Qa:Wa}let ct=Dt(I),es=ct(I),Q=I[0]==="pt"&&na(I);return se=new Ua({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-TW/chapter7/6.mdx"}}),{c(){t=j("meta"),y=p(),n=j("p"),o=p(),U(_.$$.fragment),C=p(),U(g.$$.fragment),Z=p(),U($.$$.fragment),u=p(),G.c(),R=p(),B=j("p"),B.innerHTML=v,k=p(),X=j("p"),X.innerHTML=kl,El=p(),U(x.$$.fragment),cs=p(),nl=j("p"),nl.innerHTML=Zl,is=p(),A=j("iframe"),os=p(),E=j("iframe"),zl=p(),F=j("p"),F.innerHTML=ue,Vl=p(),U(Y.$$.fragment),Nl=p(),H=j("p"),H.innerHTML=je,vl=p(),S=j("p"),S.innerHTML=Js,Ml=p(),U(Gl.$$.fragment),xl=p(),q=j("p"),q.textContent=Us,pl=p(),U(Xl.$$.fragment),Fl=p(),U(D.$$.fragment),Yl=p(),L=j("p"),L.textContent=de,Hl=p(),U(K.$$.fragment),Sl=p(),P=j("p"),P.textContent=ms,rl=p(),U(Bl.$$.fragment),ql=p(),U(O.$$.fragment),Dl=p(),ll=j("p"),ll.textContent=be,Ll=p(),U(sl.$$.fragment),Kl=p(),U(el.$$.fragment),Pl=p(),U(V.$$.fragment),Ts=p(),yl=j("p"),yl.textContent=Rl,Ol=p(),U(tl.$$.fragment),ws=p(),U(cl.$$.fragment),il=p(),ol=j("p"),ol.innerHTML=Wl,ls=p(),U(al.$$.fragment),us=p(),U(Jl.$$.fragment),Ul=p(),ml=j("p"),ml.textContent=Ql,js=p(),Tl=j("p"),Tl.innerHTML=Al,ss=p(),N=j("div"),N.innerHTML=ds,s=p(),J=j("p"),J.textContent=jt,Ce=p(),U(bs.$$.fragment),he=p(),U(fs.$$.fragment),Ie=p(),Cs=j("p"),Cs.innerHTML=dt,_e=p(),hs=j("p"),hs.innerHTML=bt,ke=p(),U(Is.$$.fragment),Ze=p(),U(_s.$$.fragment),$e=p(),ks=j("p"),ks.textContent=ft,ge=p(),Zs=j("p"),Zs.textContent=Ct,Ge=p(),U(ts.$$.fragment),Xe=p(),U($s.$$.fragment),Be=p(),gs=j("p"),gs.innerHTML=ht,Re=p(),ul.c(),ye=p(),Gs=j("p"),Gs.innerHTML=It,We=p(),Xs=j("p"),Xs.innerHTML=_t,Qe=p(),dl.c(),ce=p(),Bs=j("p"),Bs.textContent=kt,Ae=p(),U(Rs.$$.fragment),Ee=p(),fl.c(),ie=p(),Ws=j("p"),Ws.textContent=Zt,ze=p(),W&&W.c(),oe=p(),U(as.$$.fragment),Ve=p(),Qs=j("p"),Qs.textContent=$t,Ne=p(),U(As.$$.fragment),ve=p(),Es=j("p"),Es.textContent=gt,xe=p(),zs=j("p"),zs.textContent=Gt,Fe=p(),U(Vs.$$.fragment),Ye=p(),hl.c(),Je=p(),U(ns.$$.fragment),He=p(),U(Ms.$$.fragment),Se=p(),U(Ns.$$.fragment),qe=p(),vs=j("p"),vs.innerHTML=Xt,De=p(),_l.c(),Ue=p(),xs=j("p"),xs.textContent=Bt,Le=p(),U(Fs.$$.fragment),Ke=p(),U(Ys.$$.fragment),Pe=p(),Hs=j("p"),Hs.innerHTML=Rt,Oe=p(),U(Ss.$$.fragment),lt=p(),U(qs.$$.fragment),st=p(),Ds=j("p"),Ds.innerHTML=Wt,et=p(),U(Ls.$$.fragment),tt=p(),U(Ks.$$.fragment),at=p(),Ps=j("p"),Ps.innerHTML=Qt,nt=p(),U(Os.$$.fragment),Mt=p(),U(le.$$.fragment),pt=p(),es.c(),me=p(),Q&&Q.c(),Te=p(),U(se.$$.fragment),rt=p(),fe=j("p"),this.h()},l(l){const M=ia("svelte-u9bgzb",document.head);t=d(M,"META",{name:!0,content:!0}),M.forEach(e),y=r(l),n=d(l,"P",{}),it(n).forEach(e),o=r(l),m(_.$$.fragment,l),C=r(l),m(g.$$.fragment,l),Z=r(l),m($.$$.fragment,l),u=r(l),G.l(l),R=r(l),B=d(l,"P",{"data-svelte-h":!0}),f(B)!=="svelte-145q1wm"&&(B.innerHTML=v),k=r(l),X=d(l,"P",{"data-svelte-h":!0}),f(X)!=="svelte-150g0cy"&&(X.innerHTML=kl),El=r(l),m(x.$$.fragment,l),cs=r(l),nl=d(l,"P",{"data-svelte-h":!0}),f(nl)!=="svelte-10a6fg6"&&(nl.innerHTML=Zl),is=r(l),A=d(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),it(A).forEach(e),os=r(l),E=d(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),it(E).forEach(e),zl=r(l),F=d(l,"P",{"data-svelte-h":!0}),f(F)!=="svelte-1ttf6yn"&&(F.innerHTML=ue),Vl=r(l),m(Y.$$.fragment,l),Nl=r(l),H=d(l,"P",{"data-svelte-h":!0}),f(H)!=="svelte-9g9q7s"&&(H.innerHTML=je),vl=r(l),S=d(l,"P",{"data-svelte-h":!0}),f(S)!=="svelte-11uybe4"&&(S.innerHTML=Js),Ml=r(l),m(Gl.$$.fragment,l),xl=r(l),q=d(l,"P",{"data-svelte-h":!0}),f(q)!=="svelte-ee6loh"&&(q.textContent=Us),pl=r(l),m(Xl.$$.fragment,l),Fl=r(l),m(D.$$.fragment,l),Yl=r(l),L=d(l,"P",{"data-svelte-h":!0}),f(L)!=="svelte-1vcwklb"&&(L.textContent=de),Hl=r(l),m(K.$$.fragment,l),Sl=r(l),P=d(l,"P",{"data-svelte-h":!0}),f(P)!=="svelte-2ls0p3"&&(P.textContent=ms),rl=r(l),m(Bl.$$.fragment,l),ql=r(l),m(O.$$.fragment,l),Dl=r(l),ll=d(l,"P",{"data-svelte-h":!0}),f(ll)!=="svelte-1wakzoy"&&(ll.textContent=be),Ll=r(l),m(sl.$$.fragment,l),Kl=r(l),m(el.$$.fragment,l),Pl=r(l),m(V.$$.fragment,l),Ts=r(l),yl=d(l,"P",{"data-svelte-h":!0}),f(yl)!=="svelte-1lbgl1f"&&(yl.textContent=Rl),Ol=r(l),m(tl.$$.fragment,l),ws=r(l),m(cl.$$.fragment,l),il=r(l),ol=d(l,"P",{"data-svelte-h":!0}),f(ol)!=="svelte-1sd617c"&&(ol.innerHTML=Wl),ls=r(l),m(al.$$.fragment,l),us=r(l),m(Jl.$$.fragment,l),Ul=r(l),ml=d(l,"P",{"data-svelte-h":!0}),f(ml)!=="svelte-tijx3u"&&(ml.textContent=Ql),js=r(l),Tl=d(l,"P",{"data-svelte-h":!0}),f(Tl)!=="svelte-ak9zu6"&&(Tl.innerHTML=Al),ss=r(l),N=d(l,"DIV",{class:!0,"data-svelte-h":!0}),f(N)!=="svelte-1mf8pz"&&(N.innerHTML=ds),s=r(l),J=d(l,"P",{"data-svelte-h":!0}),f(J)!=="svelte-1mnld0g"&&(J.textContent=jt),Ce=r(l),m(bs.$$.fragment,l),he=r(l),m(fs.$$.fragment,l),Ie=r(l),Cs=d(l,"P",{"data-svelte-h":!0}),f(Cs)!=="svelte-e7wmzc"&&(Cs.innerHTML=dt),_e=r(l),hs=d(l,"P",{"data-svelte-h":!0}),f(hs)!=="svelte-mmvk53"&&(hs.innerHTML=bt),ke=r(l),m(Is.$$.fragment,l),Ze=r(l),m(_s.$$.fragment,l),$e=r(l),ks=d(l,"P",{"data-svelte-h":!0}),f(ks)!=="svelte-4uq2ft"&&(ks.textContent=ft),ge=r(l),Zs=d(l,"P",{"data-svelte-h":!0}),f(Zs)!=="svelte-1lk2r1u"&&(Zs.textContent=Ct),Ge=r(l),m(ts.$$.fragment,l),Xe=r(l),m($s.$$.fragment,l),Be=r(l),gs=d(l,"P",{"data-svelte-h":!0}),f(gs)!=="svelte-zid2k"&&(gs.innerHTML=ht),Re=r(l),ul.l(l),ye=r(l),Gs=d(l,"P",{"data-svelte-h":!0}),f(Gs)!=="svelte-46qnw9"&&(Gs.innerHTML=It),We=r(l),Xs=d(l,"P",{"data-svelte-h":!0}),f(Xs)!=="svelte-1jgzakq"&&(Xs.innerHTML=_t),Qe=r(l),dl.l(l),ce=r(l),Bs=d(l,"P",{"data-svelte-h":!0}),f(Bs)!=="svelte-lc35bs"&&(Bs.textContent=kt),Ae=r(l),m(Rs.$$.fragment,l),Ee=r(l),fl.l(l),ie=r(l),Ws=d(l,"P",{"data-svelte-h":!0}),f(Ws)!=="svelte-8pe4de"&&(Ws.textContent=Zt),ze=r(l),W&&W.l(l),oe=r(l),m(as.$$.fragment,l),Ve=r(l),Qs=d(l,"P",{"data-svelte-h":!0}),f(Qs)!=="svelte-157la4h"&&(Qs.textContent=$t),Ne=r(l),m(As.$$.fragment,l),ve=r(l),Es=d(l,"P",{"data-svelte-h":!0}),f(Es)!=="svelte-1oxqs03"&&(Es.textContent=gt),xe=r(l),zs=d(l,"P",{"data-svelte-h":!0}),f(zs)!=="svelte-pruo9h"&&(zs.textContent=Gt),Fe=r(l),m(Vs.$$.fragment,l),Ye=r(l),hl.l(l),Je=r(l),m(ns.$$.fragment,l),He=r(l),m(Ms.$$.fragment,l),Se=r(l),m(Ns.$$.fragment,l),qe=r(l),vs=d(l,"P",{"data-svelte-h":!0}),f(vs)!=="svelte-1dkmacl"&&(vs.innerHTML=Xt),De=r(l),_l.l(l),Ue=r(l),xs=d(l,"P",{"data-svelte-h":!0}),f(xs)!=="svelte-12egf8k"&&(xs.textContent=Bt),Le=r(l),m(Fs.$$.fragment,l),Ke=r(l),m(Ys.$$.fragment,l),Pe=r(l),Hs=d(l,"P",{"data-svelte-h":!0}),f(Hs)!=="svelte-4mdgjy"&&(Hs.innerHTML=Rt),Oe=r(l),m(Ss.$$.fragment,l),lt=r(l),m(qs.$$.fragment,l),st=r(l),Ds=d(l,"P",{"data-svelte-h":!0}),f(Ds)!=="svelte-1px822b"&&(Ds.innerHTML=Wt),et=r(l),m(Ls.$$.fragment,l),tt=r(l),m(Ks.$$.fragment,l),at=r(l),Ps=d(l,"P",{"data-svelte-h":!0}),f(Ps)!=="svelte-mhbtoo"&&(Ps.innerHTML=Qt),nt=r(l),m(Os.$$.fragment,l),Mt=r(l),m(le.$$.fragment,l),pt=r(l),es.l(l),me=r(l),Q&&Q.l(l),Te=r(l),m(se.$$.fragment,l),rt=r(l),fe=d(l,"P",{}),it(fe).forEach(e),this.h()},h(){z(t,"name","hf:doc:metadata"),z(t,"content",Na),ea(A.src,$l="https://course-demos-codeparrot-ds.hf.space")||z(A,"src",$l),z(A,"frameborder","0"),z(A,"height","300"),z(A,"title","Gradio app"),z(A,"class","block dark:hidden container p-0 flex-grow space-iframe"),z(A,"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"),z(A,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),ea(E.src,gl="https://course-demos-codeparrot-ds-darkmode.hf.space")||z(E,"src",gl),z(E,"frameborder","0"),z(E,"height","300"),z(E,"title","Gradio app"),z(E,"class","hidden dark:block container p-0 flex-grow space-iframe"),z(E,"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"),z(E,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),z(N,"class","flex justify-center")},m(l,M){oa(document.head,t),a(l,y,M),a(l,n,M),a(l,o,M),T(_,l,M),a(l,C,M),T(g,l,M),a(l,Z,M),T($,l,M),a(l,u,M),ee[b].m(l,M),a(l,R,M),a(l,B,M),a(l,k,M),a(l,X,M),a(l,El,M),T(x,l,M),a(l,cs,M),a(l,nl,M),a(l,is,M),a(l,A,M),a(l,os,M),a(l,E,M),a(l,zl,M),a(l,F,M),a(l,Vl,M),T(Y,l,M),a(l,Nl,M),a(l,H,M),a(l,vl,M),a(l,S,M),a(l,Ml,M),T(Gl,l,M),a(l,xl,M),a(l,q,M),a(l,pl,M),T(Xl,l,M),a(l,Fl,M),T(D,l,M),a(l,Yl,M),a(l,L,M),a(l,Hl,M),T(K,l,M),a(l,Sl,M),a(l,P,M),a(l,rl,M),T(Bl,l,M),a(l,ql,M),T(O,l,M),a(l,Dl,M),a(l,ll,M),a(l,Ll,M),T(sl,l,M),a(l,Kl,M),T(el,l,M),a(l,Pl,M),T(V,l,M),a(l,Ts,M),a(l,yl,M),a(l,Ol,M),T(tl,l,M),a(l,ws,M),T(cl,l,M),a(l,il,M),a(l,ol,M),a(l,ls,M),T(al,l,M),a(l,us,M),T(Jl,l,M),a(l,Ul,M),a(l,ml,M),a(l,js,M),a(l,Tl,M),a(l,ss,M),a(l,N,M),a(l,s,M),a(l,J,M),a(l,Ce,M),T(bs,l,M),a(l,he,M),T(fs,l,M),a(l,Ie,M),a(l,Cs,M),a(l,_e,M),a(l,hs,M),a(l,ke,M),T(Is,l,M),a(l,Ze,M),T(_s,l,M),a(l,$e,M),a(l,ks,M),a(l,ge,M),a(l,Zs,M),a(l,Ge,M),T(ts,l,M),a(l,Xe,M),T($s,l,M),a(l,Be,M),a(l,gs,M),a(l,Re,M),te[wl].m(l,M),a(l,ye,M),a(l,Gs,M),a(l,We,M),a(l,Xs,M),a(l,Qe,M),ae[jl].m(l,M),a(l,ce,M),a(l,Bs,M),a(l,Ae,M),T(Rs,l,M),a(l,Ee,M),ne[bl].m(l,M),a(l,ie,M),a(l,Ws,M),a(l,ze,M),W&&W.m(l,M),a(l,oe,M),T(as,l,M),a(l,Ve,M),a(l,Qs,M),a(l,Ne,M),T(As,l,M),a(l,ve,M),a(l,Es,M),a(l,xe,M),a(l,zs,M),a(l,Fe,M),T(Vs,l,M),a(l,Ye,M),Me[Cl].m(l,M),a(l,Je,M),T(ns,l,M),a(l,He,M),T(Ms,l,M),a(l,Se,M),T(Ns,l,M),a(l,qe,M),a(l,vs,M),a(l,De,M),pe[Il].m(l,M),a(l,Ue,M),a(l,xs,M),a(l,Le,M),T(Fs,l,M),a(l,Ke,M),T(Ys,l,M),a(l,Pe,M),a(l,Hs,M),a(l,Oe,M),T(Ss,l,M),a(l,lt,M),T(qs,l,M),a(l,st,M),a(l,Ds,M),a(l,et,M),T(Ls,l,M),a(l,tt,M),T(Ks,l,M),a(l,at,M),a(l,Ps,M),a(l,nt,M),T(Os,l,M),a(l,Mt,M),T(le,l,M),a(l,pt,M),es.m(l,M),a(l,me,M),Q&&Q.m(l,M),a(l,Te,M),T(se,l,M),a(l,rt,M),a(l,fe,M),yt=!0},p(l,[M]){const Lt={};M&1&&(Lt.fw=l[0]),_.$set(Lt);let ot=b;b=Et(l),b!==ot&&(rs(),i(ee[ot],1,1,()=>{ee[ot]=null}),ps(),G=ee[b],G||(G=ee[b]=At[b](l),G.c()),c(G,1),G.m(R.parentNode,R));const Kt={};M&2&&(Kt.$$scope={dirty:M,ctx:l}),V.$set(Kt);const Pt={};M&2&&(Pt.$$scope={dirty:M,ctx:l}),ts.$set(Pt);let Jt=wl;wl=Vt(l),wl!==Jt&&(rs(),i(te[Jt],1,1,()=>{te[Jt]=null}),ps(),ul=te[wl],ul||(ul=te[wl]=zt[wl](l),ul.c()),c(ul,1),ul.m(ye.parentNode,ye));let Ut=jl;jl=vt(l),jl!==Ut&&(rs(),i(ae[Ut],1,1,()=>{ae[Ut]=null}),ps(),dl=ae[jl],dl||(dl=ae[jl]=Nt[jl](l),dl.c()),c(dl,1),dl.m(ce.parentNode,ce));let mt=bl;bl=Ft(l),bl!==mt&&(rs(),i(ne[mt],1,1,()=>{ne[mt]=null}),ps(),fl=ne[bl],fl||(fl=ne[bl]=xt[bl](l),fl.c()),c(fl,1),fl.m(ie.parentNode,ie)),l[0]==="tf"?W?M&1&&c(W,1):(W=aa(),W.c(),c(W,1),W.m(oe.parentNode,oe)):W&&(rs(),i(W,1,1,()=>{W=null}),ps());const Ot={};M&2&&(Ot.$$scope={dirty:M,ctx:l}),as.$set(Ot);let Tt=Cl;Cl=Ht(l),Cl!==Tt&&(rs(),i(Me[Tt],1,1,()=>{Me[Tt]=null}),ps(),hl=Me[Cl],hl||(hl=Me[Cl]=Yt[Cl](l),hl.c()),c(hl,1),hl.m(Je.parentNode,Je));const la={};M&2&&(la.$$scope={dirty:M,ctx:l}),ns.$set(la);const sa={};M&3&&(sa.$$scope={dirty:M,ctx:l}),Ms.$set(sa);let wt=Il;Il=qt(l),Il!==wt&&(rs(),i(pe[wt],1,1,()=>{pe[wt]=null}),ps(),_l=pe[Il],_l||(_l=pe[Il]=St[Il](l),_l.c()),c(_l,1),_l.m(Ue.parentNode,Ue)),ct!==(ct=Dt(l))&&(es.d(1),es=ct(l),es&&(es.c(),es.m(me.parentNode,me))),l[0]==="pt"?Q?M&1&&c(Q,1):(Q=na(l),Q.c(),c(Q,1),Q.m(Te.parentNode,Te)):Q&&(rs(),i(Q,1,1,()=>{Q=null}),ps())},i(l){yt||(c(_.$$.fragment,l),c(g.$$.fragment,l),c($.$$.fragment,l),c(G),c(x.$$.fragment,l),c(Y.$$.fragment,l),c(Gl.$$.fragment,l),c(Xl.$$.fragment,l),c(D.$$.fragment,l),c(K.$$.fragment,l),c(Bl.$$.fragment,l),c(O.$$.fragment,l),c(sl.$$.fragment,l),c(el.$$.fragment,l),c(V.$$.fragment,l),c(tl.$$.fragment,l),c(cl.$$.fragment,l),c(al.$$.fragment,l),c(Jl.$$.fragment,l),c(bs.$$.fragment,l),c(fs.$$.fragment,l),c(Is.$$.fragment,l),c(_s.$$.fragment,l),c(ts.$$.fragment,l),c($s.$$.fragment,l),c(ul),c(dl),c(Rs.$$.fragment,l),c(fl),c(W),c(as.$$.fragment,l),c(As.$$.fragment,l),c(Vs.$$.fragment,l),c(hl),c(ns.$$.fragment,l),c(Ms.$$.fragment,l),c(Ns.$$.fragment,l),c(_l),c(Fs.$$.fragment,l),c(Ys.$$.fragment,l),c(Ss.$$.fragment,l),c(qs.$$.fragment,l),c(Ls.$$.fragment,l),c(Ks.$$.fragment,l),c(Os.$$.fragment,l),c(le.$$.fragment,l),c(Q),c(se.$$.fragment,l),yt=!0)},o(l){i(_.$$.fragment,l),i(g.$$.fragment,l),i($.$$.fragment,l),i(G),i(x.$$.fragment,l),i(Y.$$.fragment,l),i(Gl.$$.fragment,l),i(Xl.$$.fragment,l),i(D.$$.fragment,l),i(K.$$.fragment,l),i(Bl.$$.fragment,l),i(O.$$.fragment,l),i(sl.$$.fragment,l),i(el.$$.fragment,l),i(V.$$.fragment,l),i(tl.$$.fragment,l),i(cl.$$.fragment,l),i(al.$$.fragment,l),i(Jl.$$.fragment,l),i(bs.$$.fragment,l),i(fs.$$.fragment,l),i(Is.$$.fragment,l),i(_s.$$.fragment,l),i(ts.$$.fragment,l),i($s.$$.fragment,l),i(ul),i(dl),i(Rs.$$.fragment,l),i(fl),i(W),i(as.$$.fragment,l),i(As.$$.fragment,l),i(Vs.$$.fragment,l),i(hl),i(ns.$$.fragment,l),i(Ms.$$.fragment,l),i(Ns.$$.fragment,l),i(_l),i(Fs.$$.fragment,l),i(Ys.$$.fragment,l),i(Ss.$$.fragment,l),i(qs.$$.fragment,l),i(Ls.$$.fragment,l),i(Ks.$$.fragment,l),i(Os.$$.fragment,l),i(le.$$.fragment,l),i(Q),i(se.$$.fragment,l),yt=!1},d(l){l&&(e(y),e(n),e(o),e(C),e(Z),e(u),e(R),e(B),e(k),e(X),e(El),e(cs),e(nl),e(is),e(A),e(os),e(E),e(zl),e(F),e(Vl),e(Nl),e(H),e(vl),e(S),e(Ml),e(xl),e(q),e(pl),e(Fl),e(Yl),e(L),e(Hl),e(Sl),e(P),e(rl),e(ql),e(Dl),e(ll),e(Ll),e(Kl),e(Pl),e(Ts),e(yl),e(Ol),e(ws),e(il),e(ol),e(ls),e(us),e(Ul),e(ml),e(js),e(Tl),e(ss),e(N),e(s),e(J),e(Ce),e(he),e(Ie),e(Cs),e(_e),e(hs),e(ke),e(Ze),e($e),e(ks),e(ge),e(Zs),e(Ge),e(Xe),e(Be),e(gs),e(Re),e(ye),e(Gs),e(We),e(Xs),e(Qe),e(ce),e(Bs),e(Ae),e(Ee),e(ie),e(Ws),e(ze),e(oe),e(Ve),e(Qs),e(Ne),e(ve),e(Es),e(xe),e(zs),e(Fe),e(Ye),e(Je),e(He),e(Se),e(qe),e(vs),e(De),e(Ue),e(xs),e(Le),e(Ke),e(Pe),e(Hs),e(Oe),e(lt),e(st),e(Ds),e(et),e(tt),e(at),e(Ps),e(nt),e(Mt),e(pt),e(me),e(Te),e(rt),e(fe)),e(t),w(_,l),w(g,l),w($,l),ee[b].d(l),w(x,l),w(Y,l),w(Gl,l),w(Xl,l),w(D,l),w(K,l),w(Bl,l),w(O,l),w(sl,l),w(el,l),w(V,l),w(tl,l),w(cl,l),w(al,l),w(Jl,l),w(bs,l),w(fs,l),w(Is,l),w(_s,l),w(ts,l),w($s,l),te[wl].d(l),ae[jl].d(l),w(Rs,l),ne[bl].d(l),W&&W.d(l),w(as,l),w(As,l),w(Vs,l),Me[Cl].d(l),w(ns,l),w(Ms,l),w(Ns,l),pe[Il].d(l),w(Fs,l),w(Ys,l),w(Ss,l),w(qs,l),w(Ls,l),w(Ks,l),w(Os,l),w(le,l),es.d(l),Q&&Q.d(l),w(se,l)}}}const Na='{"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 ra(()=>{const o=new URLSearchParams(window.location.search);y(0,n=o.get("fw")||"pt")}),[n]}class Ka extends ya{constructor(t){super(),ca(this,t,va,Va,pa,{})}}export{Ka as component};
