import{s as sa,f as ea,o as ta,n as ye}from"../chunks/scheduler.37c15a92.js";import{S as aa,i as na,g as u,s as p,r as o,A as Ma,h as j,f as e,c as J,j as Tt,u as m,x as b,k as gl,y as pa,a as t,v as w,t as y,b as ps,d as r,w as d,e as Kt,p as Js}from"../chunks/index.2bf4358c.js";import{T as se}from"../chunks/Tip.363c041f.js";import{Y as ot}from"../chunks/Youtube.1e50a667.js";import{C as I}from"../chunks/CodeBlock.4f5fc1ad.js";import{D as la}from"../chunks/DocNotebookDropdown.d9dfeb24.js";import{F as Ja}from"../chunks/FrameworkSwitchCourse.8d4d4ab6.js";import{H as re,E as Ua}from"../chunks/EditOnGithub.8916dc7c.js";function ra(C){let a,U;return a=new la({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(){o(a.$$.fragment)},l(n){m(a.$$.fragment,n)},m(n,c){w(a,n,c),U=!0},i(n){U||(r(a.$$.fragment,n),U=!0)},o(n){y(a.$$.fragment,n),U=!1},d(n){d(a,n)}}}function ya(C){let a,U;return a=new la({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(){o(a.$$.fragment)},l(n){m(a.$$.fragment,n)},m(n,c){w(a,n,c),U=!0},i(n){U||(r(a.$$.fragment,n),U=!0)},o(n){y(a.$$.fragment,n),U=!1},d(n){d(a,n)}}}function ca(C){let a,U="✏️ <strong>试一试！</strong>这里我们删除了所有小于设定的上下文大小的块，并不会造成大问题，因为我们使用的是比较小的上下文窗口。随着增大上下文大小（或者语料库中的文档长度都很短），被抛弃的块的比例也会增加。更有效方法是将所有 tokenize 后的样本拼接起来加入一个 batch 中，每个样本之间有一个 <code>eos_token_id</code> token 作为分隔，然后对连接后的序列进行切块处理。作为练习，修改 <code>tokenize()</code> 函数以利用这种方法。请注意，为了获取完整的 token  ID 序列你需要设置 <code>truncation=False</code> ，并删除 tokenizer 中的其他参数。";return{c(){a=u("p"),a.innerHTML=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-t8kag1"&&(a.innerHTML=U)},m(n,c){t(n,a,c)},p:ye,d(n){n&&e(a)}}}function ia(C){let a,U,n,c="有了这个配置，，我们就可以加载一个全新的 GPT-2 模型。注意，这是我们第一次不使用 <code>from_pretrained()</code> 函数，因为我们实际上是自己初始化一个全新的模型而不是从一个预训练的模型继续训练：",_,f,B,$,Q;return a=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMkMlMjBURkdQVDJMTUhlYWRNb2RlbCUyQyUyMEF1dG9Db25maWclMEElMEFjb25maWclMjAlM0QlMjBBdXRvQ29uZmlnLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJncHQyJTIyJTJDJTBBJTIwJTIwJTIwJTIwdm9jYWJfc2l6ZSUzRGxlbih0b2tlbml6ZXIpJTJDJTBBJTIwJTIwJTIwJTIwbl9jdHglM0Rjb250ZXh0X2xlbmd0aCUyQyUwQSUyMCUyMCUyMCUyMGJvc190b2tlbl9pZCUzRHRva2VuaXplci5ib3NfdG9rZW5faWQlMkMlMEElMjAlMjAlMjAlMjBlb3NfdG9rZW5faWQlM0R0b2tlbml6ZXIuZW9zX3Rva2VuX2lkJTJDJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, TFGPT2LMHeadModel, AutoConfig

config = AutoConfig.from_pretrained(
    <span class="hljs-string">&quot;gpt2&quot;</span>,
    vocab_size=<span class="hljs-built_in">len</span>(tokenizer),
    n_ctx=context_length,
    bos_token_id=tokenizer.bos_token_id,
    eos_token_id=tokenizer.eos_token_id,
)`,wrap:!1}}),f=new I({props:{code:"bW9kZWwlMjAlM0QlMjBURkdQVDJMTUhlYWRNb2RlbChjb25maWcpJTBBbW9kZWwobW9kZWwuZHVtbXlfaW5wdXRzKSUyMCUyMCUyMyUyMCVFNiU5RSU4NCVFNSVCQiVCQSVFNiVBOCVBMSVFNSU5RSU4QiUwQW1vZGVsLnN1bW1hcnkoKQ==",highlighted:`model = TFGPT2LMHeadModel(config)
model(model.dummy_inputs)  <span class="hljs-comment"># 构建模型</span>
model.summary()`,wrap:!1}}),$=new I({props:{code:"X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18lMEFMYXllciUyMCh0eXBlKSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyME91dHB1dCUyMFNoYXBlJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwUGFyYW0lMjAlMjMlMjAlMjAlMjAlMEElM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlMEF0cmFuc2Zvcm1lciUyMChURkdQVDJNYWluTGF5ZXIlMjBtdWx0aXBsZSklMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAxMjQyNDI0MzIlMjAlMEElM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlMEFUb3RhbCUyMHBhcmFtcyUzQSUyMDEyNCUyQzI0MiUyQzQzMiUwQVRyYWluYWJsZSUyMHBhcmFtcyUzQSUyMDEyNCUyQzI0MiUyQzQzMiUwQU5vbi10cmFpbmFibGUlMjBwYXJhbXMlM0ElMjAwJTBBX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18=",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(){o(a.$$.fragment),U=p(),n=u("p"),n.innerHTML=c,_=p(),o(f.$$.fragment),B=p(),o($.$$.fragment)},l(i){m(a.$$.fragment,i),U=J(i),n=j(i,"P",{"data-svelte-h":!0}),b(n)!=="svelte-j7se87"&&(n.innerHTML=c),_=J(i),m(f.$$.fragment,i),B=J(i),m($.$$.fragment,i)},m(i,h){w(a,i,h),t(i,U,h),t(i,n,h),t(i,_,h),w(f,i,h),t(i,B,h),w($,i,h),Q=!0},i(i){Q||(r(a.$$.fragment,i),r(f.$$.fragment,i),r($.$$.fragment,i),Q=!0)},o(i){y(a.$$.fragment,i),y(f.$$.fragment,i),y($.$$.fragment,i),Q=!1},d(i){i&&(e(U),e(n),e(_),e(B)),d(a,i),d(f,i),d($,i)}}}function Ta(C){let a,U,n,c="有了这个配置对象，我们就可以加载一个全新的 GPT-2 模型。注意，这是我们第一次不使用 <code>from_pretrained()</code> 函数，因为我们实际上是自己初始化一个全新的模型而不是从一个预训练的模型继续训练：",_,f,B,$,Q;return a=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMkMlMjBHUFQyTE1IZWFkTW9kZWwlMkMlMjBBdXRvQ29uZmlnJTBBJTBBY29uZmlnJTIwJTNEJTIwQXV0b0NvbmZpZy5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyZ3B0MiUyMiUyQyUwQSUyMCUyMCUyMCUyMHZvY2FiX3NpemUlM0RsZW4odG9rZW5pemVyKSUyQyUwQSUyMCUyMCUyMCUyMG5fY3R4JTNEY29udGV4dF9sZW5ndGglMkMlMEElMjAlMjAlMjAlMjBib3NfdG9rZW5faWQlM0R0b2tlbml6ZXIuYm9zX3Rva2VuX2lkJTJDJTBBJTIwJTIwJTIwJTIwZW9zX3Rva2VuX2lkJTNEdG9rZW5pemVyLmVvc190b2tlbl9pZCUyQyUwQSk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, GPT2LMHeadModel, AutoConfig

config = AutoConfig.from_pretrained(
    <span class="hljs-string">&quot;gpt2&quot;</span>,
    vocab_size=<span class="hljs-built_in">len</span>(tokenizer),
    n_ctx=context_length,
    bos_token_id=tokenizer.bos_token_id,
    eos_token_id=tokenizer.eos_token_id,
)`,wrap:!1}}),f=new I({props:{code:"bW9kZWwlMjAlM0QlMjBHUFQyTE1IZWFkTW9kZWwoY29uZmlnKSUwQW1vZGVsX3NpemUlMjAlM0QlMjBzdW0odC5udW1lbCgpJTIwZm9yJTIwdCUyMGluJTIwbW9kZWwucGFyYW1ldGVycygpKSUwQXByaW50KGYlMjJHUFQtMiUyMHNpemUlM0ElMjAlN0Jtb2RlbF9zaXplJTJGMTAwMCoqMiUzQS4xZiU3RE0lMjBwYXJhbWV0ZXJzJTIyKQ==",highlighted:`model = GPT2LMHeadModel(config)
model_size = <span class="hljs-built_in">sum</span>(t.numel() <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> model.parameters())
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;GPT-2 size: <span class="hljs-subst">{model_size/<span class="hljs-number">1000</span>**<span class="hljs-number">2</span>:<span class="hljs-number">.1</span>f}</span>M parameters&quot;</span>)`,wrap:!1}}),$=new I({props:{code:"R1BULTIlMjBzaXplJTNBJTIwMTI0LjJNJTIwcGFyYW1ldGVycw==",highlighted:'GPT-<span class="hljs-number">2</span> size: <span class="hljs-number">124.2</span>M parameters',wrap:!1}}),{c(){o(a.$$.fragment),U=p(),n=u("p"),n.innerHTML=c,_=p(),o(f.$$.fragment),B=p(),o($.$$.fragment)},l(i){m(a.$$.fragment,i),U=J(i),n=j(i,"P",{"data-svelte-h":!0}),b(n)!=="svelte-1nsrsjx"&&(n.innerHTML=c),_=J(i),m(f.$$.fragment,i),B=J(i),m($.$$.fragment,i)},m(i,h){w(a,i,h),t(i,U,h),t(i,n,h),t(i,_,h),w(f,i,h),t(i,B,h),w($,i,h),Q=!0},i(i){Q||(r(a.$$.fragment,i),r(f.$$.fragment,i),r($.$$.fragment,i),Q=!0)},o(i){y(a.$$.fragment,i),y(f.$$.fragment,i),y($.$$.fragment,i),Q=!1},d(i){i&&(e(U),e(n),e(_),e(B)),d(a,i),d(f,i),d($,i)}}}function oa(C){let a,U;return a=new I({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(){o(a.$$.fragment)},l(n){m(a.$$.fragment,n)},m(n,c){w(a,n,c),U=!0},i(n){U||(r(a.$$.fragment,n),U=!0)},o(n){y(a.$$.fragment,n),U=!1},d(n){d(a,n)}}}function ma(C){let a,U;return a=new I({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(){o(a.$$.fragment)},l(n){m(a.$$.fragment,n)},m(n,c){w(a,n,c),U=!0},i(n){U||(r(a.$$.fragment,n),U=!0)},o(n){y(a.$$.fragment,n),U=!1},d(n){d(a,n)}}}function wa(C){let a,U;return a=new I({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(){o(a.$$.fragment)},l(n){m(a.$$.fragment,n)},m(n,c){w(a,n,c),U=!0},i(n){U||(r(a.$$.fragment,n),U=!0)},o(n){y(a.$$.fragment,n),U=!1},d(n){d(a,n)}}}function da(C){let a,U;return a=new I({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(){o(a.$$.fragment)},l(n){m(a.$$.fragment,n)},m(n,c){w(a,n,c),U=!0},i(n){U||(r(a.$$.fragment,n),U=!0)},o(n){y(a.$$.fragment,n),U=!1},d(n){d(a,n)}}}function Pt(C){let a,U="现在，我们可以使用 <code>prepare_tf_dataset()</code> 方法，将上面创建的数据整理器（DataCollator）将数据集转换为 TensorFlow 数据集：",n,c,_;return c=new I({props:{code:"dGZfdHJhaW5fZGF0YXNldCUyMCUzRCUyMG1vZGVsLnByZXBhcmVfdGZfZGF0YXNldCglMEElMjAlMjAlMjAlMjB0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMGNvbGxhdGVfZm4lM0RkYXRhX2NvbGxhdG9yJTJDJTBBJTIwJTIwJTIwJTIwc2h1ZmZsZSUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjBiYXRjaF9zaXplJTNEMzIlMkMlMEEpJTBBdGZfZXZhbF9kYXRhc2V0JTIwJTNEJTIwbW9kZWwucHJlcGFyZV90Zl9kYXRhc2V0KCUwQSUyMCUyMCUyMCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnZhbGlkJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwY29sbGF0ZV9mbiUzRGRhdGFfY29sbGF0b3IlMkMlMEElMjAlMjAlMjAlMjBzaHVmZmxlJTNERmFsc2UlMkMlMEElMjAlMjAlMjAlMjBiYXRjaF9zaXplJTNEMzIlMkMlMEEp",highlighted:`tf_train_dataset = model.prepare_tf_dataset(
    tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">True</span>,
    batch_size=<span class="hljs-number">32</span>,
)
tf_eval_dataset = model.prepare_tf_dataset(
    tokenized_datasets[<span class="hljs-string">&quot;valid&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">False</span>,
    batch_size=<span class="hljs-number">32</span>,
)`,wrap:!1}}),{c(){a=u("p"),a.innerHTML=U,n=p(),o(c.$$.fragment)},l(f){a=j(f,"P",{"data-svelte-h":!0}),b(a)!=="svelte-9xwztu"&&(a.innerHTML=U),n=J(f),m(c.$$.fragment,f)},m(f,B){t(f,a,B),t(f,n,B),w(c,f,B),_=!0},i(f){_||(r(c.$$.fragment,f),_=!0)},o(f){y(c.$$.fragment,f),_=!1},d(f){f&&(e(a),e(n)),d(c,f)}}}function ua(C){let a,U="⚠️ 输入序列和目标序列对齐将在模型内部自动进行，所以数据整理器只需复制输入序列来创建目标序列。";return{c(){a=u("p"),a.textContent=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-1x635zg"&&(a.textContent=U)},m(n,c){t(n,a,c)},p:ye,d(n){n&&e(a)}}}function ja(C){let a,U="接下来我们需要做的就是配置训练超参数并调用 <code>compile()</code> 和 <code>fit()</code> 方法。然后使用带有一些预热的学习率调整策略来提高训练的稳定性：",n,c,_,f,B="现在我们只需调用 <code>model.fit()</code> ，并等待训练完成。根据你是否在完整的训练集或者训练集的子集上运行，这将分别需要 20 小时或者 2 小时，所以拿一些咖啡和一本好书来阅读！训练完成后，我们可以将模型和 tokenizer 推送到 Hub：",$,Q,i;return c=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMGNyZWF0ZV9vcHRpbWl6ZXIlMEFpbXBvcnQlMjB0ZW5zb3JmbG93JTIwYXMlMjB0ZiUwQSUwQW51bV90cmFpbl9zdGVwcyUyMCUzRCUyMGxlbih0Zl90cmFpbl9kYXRhc2V0KSUwQW9wdGltaXplciUyQyUyMHNjaGVkdWxlJTIwJTNEJTIwY3JlYXRlX29wdGltaXplciglMEElMjAlMjAlMjAlMjBpbml0X2xyJTNENWUtNSUyQyUwQSUyMCUyMCUyMCUyMG51bV93YXJtdXBfc3RlcHMlM0QxXzAwMCUyQyUwQSUyMCUyMCUyMCUyMG51bV90cmFpbl9zdGVwcyUzRG51bV90cmFpbl9zdGVwcyUyQyUwQSUyMCUyMCUyMCUyMHdlaWdodF9kZWNheV9yYXRlJTNEMC4wMSUyQyUwQSklMEFtb2RlbC5jb21waWxlKG9wdGltaXplciUzRG9wdGltaXplciklMEElMEElMjMlMjAlRTQlQkQlQkYlRTclOTQlQTglMjBmbG9hdDE2JTIwJUU2JUI3JUI3JUU1JTkwJTg4JUU3JUIyJUJFJUU1JUJBJUE2JUU4JUJGJTlCJUU4JUExJThDJUU4JUFFJUFEJUU3JUJCJTgzJTBBdGYua2VyYXMubWl4ZWRfcHJlY2lzaW9uLnNldF9nbG9iYWxfcG9saWN5KCUyMm1peGVkX2Zsb2F0MTYlMjIp",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"># 使用 float16 混合精度进行训练</span>
tf.keras.mixed_precision.set_global_policy(<span class="hljs-string">&quot;mixed_float16&quot;</span>)`,wrap:!1}}),Q=new I({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(){a=u("p"),a.innerHTML=U,n=p(),o(c.$$.fragment),_=p(),f=u("p"),f.innerHTML=B,$=p(),o(Q.$$.fragment)},l(h){a=j(h,"P",{"data-svelte-h":!0}),b(a)!=="svelte-tli4xm"&&(a.innerHTML=U),n=J(h),m(c.$$.fragment,h),_=J(h),f=j(h,"P",{"data-svelte-h":!0}),b(f)!=="svelte-e3tfk"&&(f.innerHTML=B),$=J(h),m(Q.$$.fragment,h)},m(h,g){t(h,a,g),t(h,n,g),w(c,h,g),t(h,_,g),t(h,f,g),t(h,$,g),w(Q,h,g),i=!0},i(h){i||(r(c.$$.fragment,h),r(Q.$$.fragment,h),i=!0)},o(h){y(c.$$.fragment,h),y(Q.$$.fragment,h),i=!1},d(h){h&&(e(a),e(n),e(_),e(f),e($)),d(c,h),d(Q,h)}}}function ba(C){let a,U="剩下要做的就是配置训练参数并启动 <code>Trainer</code> 。本次的训练中我们将使用余弦学习率调度，并进行一些 Warmup。训练的 batch size 是 256 （ <code>per_device_train_batch_size</code> * <code>gradient_accumulation_steps</code> ）。当单个 batch 无法放入内存时，可以使用梯度累积，并通过多次向前/向后传递逐步累积梯度。当我们在本节最后使用 🤗 Accelerate 创建训练循环时，我们将看到这一点。",n,c,_,f,B="现在我们只需启动 <code>Trainer</code> 并等待训练完成。根据你是在整个训练集还是在训练集的一个子集上运行它，这将分别需要 20 或 2 个小时，因此请喝杯咖啡或者找一本好书来阅读！",$,Q,i,h,g="训练完成后，我们可以将模型和 tokenizer 推送到 Hub：",E,V,X;return c=new I({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}}),Q=new I({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:"trainer.train()",wrap:!1}}),V=new I({props:{code:"dHJhaW5lci5wdXNoX3RvX2h1Yigp",highlighted:"trainer.push_to_hub()",wrap:!1}}),{c(){a=u("p"),a.innerHTML=U,n=p(),o(c.$$.fragment),_=p(),f=u("p"),f.innerHTML=B,$=p(),o(Q.$$.fragment),i=p(),h=u("p"),h.textContent=g,E=p(),o(V.$$.fragment)},l(k){a=j(k,"P",{"data-svelte-h":!0}),b(a)!=="svelte-6iztyo"&&(a.innerHTML=U),n=J(k),m(c.$$.fragment,k),_=J(k),f=j(k,"P",{"data-svelte-h":!0}),b(f)!=="svelte-1snttie"&&(f.innerHTML=B),$=J(k),m(Q.$$.fragment,k),i=J(k),h=j(k,"P",{"data-svelte-h":!0}),b(h)!=="svelte-pbt7ds"&&(h.textContent=g),E=J(k),m(V.$$.fragment,k)},m(k,Z){t(k,a,Z),t(k,n,Z),w(c,k,Z),t(k,_,Z),t(k,f,Z),t(k,$,Z),w(Q,k,Z),t(k,i,Z),t(k,h,Z),t(k,E,Z),w(V,k,Z),X=!0},i(k){X||(r(c.$$.fragment,k),r(Q.$$.fragment,k),r(V.$$.fragment,k),X=!0)},o(k){y(c.$$.fragment,k),y(Q.$$.fragment,k),y(V.$$.fragment,k),X=!1},d(k){k&&(e(a),e(n),e(_),e(f),e($),e(i),e(h),e(E)),d(c,k),d(Q,k),d(V,k)}}}function fa(C){let a,U="✏️ <strong>试试看！</strong> 除了 <code>TrainingArguments</code> 之外，我们只需要大约 30 行代码就可以从原始文本到训练 GPT-2。用你自己的数据集试试看，看看你能不能得到好的结果！";return{c(){a=u("p"),a.innerHTML=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-hphc86"&&(a.innerHTML=U)},m(n,c){t(n,a,c)},p:ye,d(n){n&&e(a)}}}function ha(C){let a,U='💡 如果你正在使用具有多个 GPU 的计算机，则可以尝试使用 <code>MirroredStrategy</code> 上下文来大幅加快训练速度。你需要创建一个 <code>tf.distribute.MirroredStrategy</code> 对象，并确保所有的 <code>to_tf_dataset</code> 或 <code>prepare_tf_dataset()</code> 方法以及模型创建和对 <code>fit()</code> 的调用都在其 <code>scope()</code> 上下文中运行。你可以在 <a href="https://www.tensorflow.org/guide/distributed_training#use_tfdistributestrategy_with_keras_modelfit" rel="nofollow">这里</a> 查看有关此内容的文档。';return{c(){a=u("p"),a.innerHTML=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-jf4pwl"&&(a.innerHTML=U)},m(n,c){t(n,a,c)},d(n){n&&e(a)}}}function Ia(C){let a,U="💡 如果你能使用多 GPU 的机器，尝试在那里运行代码。 <code>Trainer</code> 自动管理多台机器，这能极大地加快训练速度。";return{c(){a=u("p"),a.innerHTML=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-1783bui"&&(a.innerHTML=U)},m(n,c){t(n,a,c)},d(n){n&&e(a)}}}function Ca(C){let a;function U(_,f){return _[0]==="pt"?Ia:ha}let n=U(C),c=n(C);return{c(){c.c(),a=Kt()},l(_){c.l(_),a=Kt()},m(_,f){c.m(_,f),t(_,a,f)},p(_,f){n!==(n=U(_))&&(c.d(1),c=n(_),c&&(c.c(),c.m(a.parentNode,a)))},d(_){_&&e(a),c.d(_)}}}function _a(C){let a,U;return a=new I({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(){o(a.$$.fragment)},l(n){m(a.$$.fragment,n)},m(n,c){w(a,n,c),U=!0},i(n){U||(r(a.$$.fragment,n),U=!0)},o(n){y(a.$$.fragment,n),U=!1},d(n){d(a,n)}}}function ka(C){let a,U;return a=new I({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(){o(a.$$.fragment)},l(n){m(a.$$.fragment,n)},m(n,c){w(a,n,c),U=!0},i(n){U||(r(a.$$.fragment,n),U=!0)},o(n){y(a.$$.fragment,n),U=!1},d(n){d(a,n)}}}function Qa(C){let a,U="从这几个例子来看，模型似乎已经学习了 Python 数据科学堆栈的一些语法（当然，在将模型部署到现实世界之前，我们需要对其进行更全面的评估）。然而，有时候它需要更多的模型训练定制来达到特定情境所需的性能。例如，如果我们想动态更新 <code>batch_size</code> 或添加一个条件训练循环来跳过坏示例怎么办？一种选择是修改 <code>Trainer</code> 添加新的功能，但有时从头开始编写训练循环会更简单。这就是🤗 Accelerate 的用武之地。";return{c(){a=u("p"),a.innerHTML=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-rm1ynw"&&(a.innerHTML=U)},m(n,c){t(n,a,c)},d(n){n&&e(a)}}}function $a(C){let a,U="从上述几个示例来看，似乎模型已经学习了 Python 数据科学栈的一些语法。当然，在将模型部署到现实世界之前，我们需要更彻底地评估模型，但这仍然是一个令人印象深刻的原型。";return{c(){a=u("p"),a.textContent=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-1wdnel"&&(a.textContent=U)},m(n,c){t(n,a,c)},d(n){n&&e(a)}}}function Ot(C){let a,U,n,c="我们已经看到了如何使用 <code>Trainer</code> 训练模型，在 <code>Trainer</code> 中可以对训练过程可以通过修改一些参数进行一些定制。然而，有时我们想要完全控制训练循环，或者我们想要进行一些更自由的的更改。在这种情况下 🤗 Accelerate 是一个不错的选择，本节我们将介绍如何使用它来训练我们的模型。为了让事情变得更有趣，相比于上面的 <code>Trainer</code> 我们还将在训练循环中添加一些修改。",_,f,B,$,Q="由于我们主要关注的是为数据科学库提供合理的代码自动补充功能，因此对于更多使用这些库的训练样本赋予更高的权重是有意义的。我们可以通过使用 <code>plt</code> 、 <code>pd</code> 、 <code>sk</code> 、 <code>fit</code> 和 <code>predict</code> 等关键词来轻松地识别出这些例子，这些关键词是 <code>matplotlib.pyplot</code> 、 <code>pandas</code> 和 <code>sklearn</code> 导入后最常用重命名的名称，以及 <code>sklearn</code> 的 <code>fit/predict</code> 方法。如果这些在模型的内部是用单一的一个 <code>token</code> 表示的，我们可以通过 token 的 id 轻松地检查它们是否出现在输入序列中。然而，Tokens 有可能有空格前缀，所以我们也需要在 tokenizer 词汇表中检查这些关键词。为了验证这个策略的有效性，我们会在测试样本中添加一个应该被分割为多个 tokens 的测试 token：",i,h,g,E,V,X,k="太好了，这个方法似乎很有效！我们现在可以编写一个自定义的损失函数，它的输入有输入序列、logits 和我们刚刚选择的关键字。首先需要对齐 <code>logits</code> 和 <code>inputs</code> : 并将输入序列右移一个单位形成目标序列，因为下一个 <code>token</code> 就是当前 <code>token</code> 的预测的目标。我们可以通过从输入序列的第二个 <code>token</code> 开始设置标签，因为模型不会预测第一个 <code>token</code>。然后我们截断最后一个 <code>logit</code>，因为我们没有完整输入序列后面的标签。有了这些，我们就可以计算每个样本的损失，并计算每个样本中所有关键词的出现次数。最后，我们使用出现次数作为权重，计算所有样本的加权平均值。由于我们不想抛弃所有没有关键词的样本，我们将所有的权重都加 1：",Z,N,Zl,F,ce="在我们开始使用这个精妙的新损失函数进行训练之前，我们需要准备一些事情：",Gl,G,ie="<li>我们需要数据加载器来批量加载数据。</li> <li>我们需要设置权重衰减参数。</li> <li>有时我们在调试模型的时候可能需要临时评估，所以将评估代码包装在一个函数中。</li>",Xl,A,Te="让我们从数据加载器开始。我们只需要将数据集的格式设置为 <code>&quot;torch&quot;</code> ，然后我们就可以将它传递给一个具有适当 <code>batch size</code> 的 PyTorch 的 <code>DataLoader</code> ：",Rl,v,Wl,z,oe="接下来，我们将参数分组，以便优化器知道哪些参数需要进行额外的权重衰减。通常，所有的偏置和 LayerNorm 权重项都不需要进行权重衰减；因此我们可以这样做：",Vl,x,Us,Jl,_l="我们希望在训练过程中定期在验证集上评估模型，让我们为此编写一个函数。它只需遍历评估数据加载器，并收集所有进程中的损失值：",El,H,rs,Ul,kl='通过 <code>evaluate()</code> 函数我们定期可以获取损失值和 <a href="/course/chapter7/3">困惑度（perplexity）</a> 。接下来，我们重新加载我们的模型以确保我们再次从头开始训练，而不是从上面的 <code>Trainer</code> 继续微调：',Nl,Y,Fl,S,me="然后我们可以定义我们的优化器，使用之前的函数来分割权重衰减的参数：",Al,D,vl,q,we="现在让我们准备模型、优化器和数据加载器，然后我们可以开始训练：",zl,L,xl,K,Hl,P,de="现在我们已经将我们的 <code>train_dataloader</code> 传递给了 <code>accelerator.prepare()</code> ，我们可以使用 <code>len()</code> 来计算训练步骤的数量。请记住，我们应该在准备好 <code>dataloader</code> 后再使用 <code>len()</code> ，因为改动 <code>dataloader</code> 会改变其长度。我们使用一个从学习率衰减到 0 的经典线性学习率调度：",Yl,O,ys,rl,Ql="最后，为了将我们的模型推送到 Hub，我们需要在一个工作文件夹中创建一个 <code>Repository</code> 对象。如果你还没有登录的话，首先需要登录到 Hugging Face，我们将根据模型 ID 来确定仓库名称（你可以使用你喜欢的名字替换 <code>repo_name</code> ；它只需要包含你的用户名，可以使用 <code>get_full_repo_name()</code> 函数的查看目前的 <code>repo_name</code>）：",Sl,ll,Dl,sl,cs,yl,$l="然后我们可以将该仓库克隆到本地文件夹中。如果本地已经存在一个同名的文件夹，这个本地文件夹应该是我们正在使用的仓库的克隆在本地的版本：",ql,el,Ll,tl,ue="我们现在可以通过调用 <code>repo.push_to_hub()</code> 方法上传保存在 <code>output_dir</code> 中的所有内容。这将帮助我们在每个训练周期结束时上传中间模型。",Kl,al,Pl,nl,Ol,Ml,je="目前的损失和困惑度都是非常高的值，但这并不奇怪，因为我们还没有训练模型。到现在为止，我们已经为编写训练脚本的核心部分：训练循环已经做好了准备。在训练循环中，我们迭代遍历数据加载器并将成批量的数据传递给模型。有了模型输出的 logits，我们就可以使用自定义损失函数计算损伤。我们通过梯度累积步骤的数量来缩放损失，以避免在聚合更多步骤时产生更大的损失。在我们优化之前，我们也会剪裁梯度来更好的收敛。最后，每隔一段步数，我们用新的 <code>evaluate()</code> 函数在评估集上评估模型：",ls,pl,is,cl,ol="就是这样 - 你现在拥有自己的因果语言模型（例如 GPT-2）的自定义训练循环，你可以根据自己的需要进一步定制。",Ts,il,Tl,Bl,ss;return a=new re({props:{title:"使用🤗 Accelerate 进行训练",local:"使用🤗 Accelerate 进行训练",headingTag:"h2"}}),f=new ot({props:{id:"Hm8_PgVTFuc"}}),h=new I({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}}),E=new I({props:{code:"J0tleXdvcmQlMjBoYXMlMjBub3QlMjBzaW5nbGUlMjB0b2tlbiUzQSUyMHRlc3R0ZXN0Jw==",highlighted:'<span class="hljs-string">&#x27;Keyword has not single token: testtest&#x27;</span>',wrap:!1}}),N=new I({props:{code:"ZnJvbSUyMHRvcmNoLm5uJTIwaW1wb3J0JTIwQ3Jvc3NFbnRyb3B5TG9zcyUwQWltcG9ydCUyMHRvcmNoJTBBJTBBJTBBZGVmJTIwa2V5dG9rZW5fd2VpZ2h0ZWRfbG9zcyhpbnB1dHMlMkMlMjBsb2dpdHMlMkMlMjBrZXl0b2tlbl9pZHMlMkMlMjBhbHBoYSUzRDEuMCklM0ElMEElMjAlMjAlMjAlMjAlMjMlMjAlRTUlQjclQTYlRTclQTclQkIlMjB0b2tlbnMlMjAlM0MlMjBuJTIwJUU5JUEyJTg0JUU2JUI1JThCJTIwbiUwQSUyMCUyMCUyMCUyMHNoaWZ0X2xhYmVscyUyMCUzRCUyMGlucHV0cyU1Qi4uLiUyQyUyMDElM0ElNUQuY29udGlndW91cygpJTBBJTIwJTIwJTIwJTIwc2hpZnRfbG9naXRzJTIwJTNEJTIwbG9naXRzJTVCLi4uJTJDJTIwJTNBLTElMkMlMjAlM0ElNUQuY29udGlndW91cygpJTBBJTIwJTIwJTIwJTIwJTIzJTIwJUU4JUFFJUExJUU3JUFFJTk3JUU2JUFGJThGJUU0JUI4JTgwJUU0JUI4JUFBdG9rZW4lRTclOUElODRsb3NzJTBBJTIwJTIwJTIwJTIwbG9zc19mY3QlMjAlM0QlMjBDcm9zc0VudHJvcHlMb3NzKHJlZHVjZSUzREZhbHNlKSUwQSUyMCUyMCUyMCUyMGxvc3MlMjAlM0QlMjBsb3NzX2ZjdChzaGlmdF9sb2dpdHMudmlldygtMSUyQyUyMHNoaWZ0X2xvZ2l0cy5zaXplKC0xKSklMkMlMjBzaGlmdF9sYWJlbHMudmlldygtMSkpJTBBJTIwJTIwJTIwJTIwJTIzJTIwJUU1JUFGJUI5JUU0JUJBJThFJUU2JUFGJThGJUU0JUI4JUFBJUU2JUEwJUI3JUU2JTlDJUFDJUU5JTg3JThEJUU2JTk2JUIwJUU4JUIwJTgzJUU2JTk1JUI0JUU1JUE0JUE3JUU1JUIwJThGJUU1JUI5JUI2JUU1JUI5JUIzJUU1JTlEJTg3JTBBJTIwJTIwJTIwJTIwbG9zc19wZXJfc2FtcGxlJTIwJTNEJTIwbG9zcy52aWV3KHNoaWZ0X2xvZ2l0cy5zaXplKDApJTJDJTIwc2hpZnRfbG9naXRzLnNpemUoMSkpLm1lYW4oYXhpcyUzRDEpJTBBJTIwJTIwJTIwJTIwJTIzJTIwJUU4JUFFJUExJUU3JUFFJTk3JUU1JUI5JUI2JUU3JUJDJUE5JUU2JTk0JUJFJUU2JTlEJTgzJUU5JTg3JThEJTBBJTIwJTIwJTIwJTIwd2VpZ2h0cyUyMCUzRCUyMHRvcmNoLnN0YWNrKCU1QihpbnB1dHMlMjAlM0QlM0QlMjBrdCkuZmxvYXQoKSUyMGZvciUyMGt0JTIwaW4lMjBrZXl0b2tlbl9pZHMlNUQpLnN1bSglMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBheGlzJTNEJTVCMCUyQyUyMDIlNUQlMEElMjAlMjAlMjAlMjApJTBBJTIwJTIwJTIwJTIwd2VpZ2h0cyUyMCUzRCUyMGFscGhhJTIwKiUyMCgxLjAlMjAlMkIlMjB3ZWlnaHRzKSUwQSUyMCUyMCUyMCUyMCUyMyUyMCVFOCVBRSVBMSVFNyVBRSU5NyVFOCVBRiU4NCVFNCVCQiVCNyVFNiU5RCU4MyVFOSU4NyU4RCUwQSUyMCUyMCUyMCUyMHdlaWdodGVkX2xvc3MlMjAlM0QlMjAobG9zc19wZXJfc2FtcGxlJTIwKiUyMHdlaWdodHMpLm1lYW4oKSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMHdlaWdodGVkX2xvc3M=",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"># 左移 tokens &lt; n 预测 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"># 计算每一个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"># 对于每个样本重新调整大小并平均</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"># 计算并缩放权重</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"># 计算评价权重</span>
    weighted_loss = (loss_per_sample * weights).mean()
    <span class="hljs-keyword">return</span> weighted_loss`,wrap:!1}}),v=new I({props:{code:"ZnJvbSUyMHRvcmNoLnV0aWxzLmRhdGEuZGF0YWxvYWRlciUyMGltcG9ydCUyMERhdGFMb2FkZXIlMEElMEF0b2tlbml6ZWRfZGF0YXNldHMuc2V0X2Zvcm1hdCglMjJ0b3JjaCUyMiklMEF0cmFpbl9kYXRhbG9hZGVyJTIwJTNEJTIwRGF0YUxvYWRlcih0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCUyQyUyMGJhdGNoX3NpemUlM0QzMiUyQyUyMHNodWZmbGUlM0RUcnVlKSUwQWV2YWxfZGF0YWxvYWRlciUyMCUzRCUyMERhdGFMb2FkZXIodG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydmFsaWQlMjIlNUQlMkMlMjBiYXRjaF9zaXplJTNEMzIp",highlighted:`<span class="hljs-keyword">from</span> torch.utils.data.dataloader <span class="hljs-keyword">import</span> DataLoader

tokenized_datasets.set_format(<span class="hljs-string">&quot;torch&quot;</span>)
train_dataloader = DataLoader(tokenized_datasets[<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_datasets[<span class="hljs-string">&quot;valid&quot;</span>], batch_size=<span class="hljs-number">32</span>)`,wrap:!1}}),x=new I({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}}),H=new I({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}}),Y=new I({props:{code:"bW9kZWwlMjAlM0QlMjBHUFQyTE1IZWFkTW9kZWwoY29uZmlnKQ==",highlighted:"model = GPT2LMHeadModel(config)",wrap:!1}}),D=new I({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}}),L=new I({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}}),K=new se({props:{$$slots:{default:[Ba]},$$scope:{ctx:C}}}),O=new I({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}}),ll=new I({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}}),sl=new I({props:{code:"J3NndWdnZXIlMkZjb2RlcGFycm90LWRzLWFjY2VsZXJhdGUn",highlighted:'<span class="hljs-string">&#x27;sgugger/codeparrot-ds-accelerate&#x27;</span>',wrap:!1}}),el=new I({props:{code:"b3V0cHV0X2RpciUyMCUzRCUyMCUyMmNvZGVwYXJyb3QtZHMtYWNjZWxlcmF0ZSUyMiUwQXJlcG8lMjAlM0QlMjBSZXBvc2l0b3J5KG91dHB1dF9kaXIlMkMlMjBjbG9uZV9mcm9tJTNEcmVwb19uYW1lKQ==",highlighted:`output_dir = <span class="hljs-string">&quot;codeparrot-ds-accelerate&quot;</span>
repo = Repository(output_dir, clone_from=repo_name)`,wrap:!1}}),al=new I({props:{code:"ZXZhbHVhdGUoKQ==",highlighted:"evaluate()",wrap:!1}}),nl=new I({props:{code:"KDEwLjkzNDEyNjg1Mzk0Mjg3MSUyQyUyMDU2MDU3LjE0NDUzMTI1KQ==",highlighted:'(<span class="hljs-number">10.934126853942871</span>, <span class="hljs-number">56057.14453125</span>)',wrap:!1}}),pl=new I({props:{code:"ZnJvbSUyMHRxZG0ubm90ZWJvb2slMjBpbXBvcnQlMjB0cWRtJTBBJTBBZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTIwJTNEJTIwOCUwQWV2YWxfc3RlcHMlMjAlM0QlMjA1XzAwMCUwQSUwQW1vZGVsLnRyYWluKCklMEFjb21wbGV0ZWRfc3RlcHMlMjAlM0QlMjAwJTBBZm9yJTIwZXBvY2glMjBpbiUyMHJhbmdlKG51bV90cmFpbl9lcG9jaHMpJTNBJTBBJTIwJTIwJTIwJTIwZm9yJTIwc3RlcCUyQyUyMGJhdGNoJTIwaW4lMjB0cWRtKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGVudW1lcmF0ZSh0cmFpbl9kYXRhbG9hZGVyJTJDJTIwc3RhcnQlM0QxKSUyQyUyMHRvdGFsJTNEbnVtX3RyYWluaW5nX3N0ZXBzJTBBJTIwJTIwJTIwJTIwKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxvZ2l0cyUyMCUzRCUyMG1vZGVsKGJhdGNoJTVCJTIyaW5wdXRfaWRzJTIyJTVEKS5sb2dpdHMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwa2V5dG9rZW5fd2VpZ2h0ZWRfbG9zcyhiYXRjaCU1QiUyMmlucHV0X2lkcyUyMiU1RCUyQyUyMGxvZ2l0cyUyQyUyMGtleXRva2VuX2lkcyklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpZiUyMHN0ZXAlMjAlMjUlMjAxMDAlMjAlM0QlM0QlMjAwJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IucHJpbnQoJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIyc2FtcGxlcyUyMiUzQSUyMHN0ZXAlMjAqJTIwc2FtcGxlc19wZXJfc3RlcCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnN0ZXBzJTIyJTNBJTIwY29tcGxldGVkX3N0ZXBzJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIybG9zcyUyRnRyYWluJTIyJTNBJTIwbG9zcy5pdGVtKCklMjAqJTIwZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTdEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxvc3MlMjAlM0QlMjBsb3NzJTIwJTJGJTIwZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IuYmFja3dhcmQobG9zcyklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpZiUyMHN0ZXAlMjAlMjUlMjBncmFkaWVudF9hY2N1bXVsYXRpb25fc3RlcHMlMjAlM0QlM0QlMjAwJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IuY2xpcF9ncmFkX25vcm1fKG1vZGVsLnBhcmFtZXRlcnMoKSUyQyUyMDEuMCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuc3RlcCgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbHJfc2NoZWR1bGVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG9wdGltaXplci56ZXJvX2dyYWQoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNvbXBsZXRlZF9zdGVwcyUyMCUyQiUzRCUyMDElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpZiUyMChzdGVwJTIwJTI1JTIwKGV2YWxfc3RlcHMlMjAqJTIwZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzKSklMjAlM0QlM0QlMjAwJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZXZhbF9sb3NzJTJDJTIwcGVycGxleGl0eSUyMCUzRCUyMGV2YWx1YXRlKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBhY2NlbGVyYXRvci5wcmludCglN0IlMjJsb3NzJTJGZXZhbCUyMiUzQSUyMGV2YWxfbG9zcyUyQyUyMCUyMnBlcnBsZXhpdHklMjIlM0ElMjBwZXJwbGV4aXR5JTdEKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG1vZGVsLnRyYWluKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBhY2NlbGVyYXRvci53YWl0X2Zvcl9ldmVyeW9uZSgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdW53cmFwcGVkX21vZGVsJTIwJTNEJTIwYWNjZWxlcmF0b3IudW53cmFwX21vZGVsKG1vZGVsKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHVud3JhcHBlZF9tb2RlbC5zYXZlX3ByZXRyYWluZWQob3V0cHV0X2RpciUyQyUyMHNhdmVfZnVuY3Rpb24lM0RhY2NlbGVyYXRvci5zYXZlKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGlmJTIwYWNjZWxlcmF0b3IuaXNfbWFpbl9wcm9jZXNzJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdG9rZW5pemVyLnNhdmVfcHJldHJhaW5lZChvdXRwdXRfZGlyKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJlcG8ucHVzaF90b19odWIoJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwY29tbWl0X21lc3NhZ2UlM0RmJTIyVHJhaW5pbmclMjBpbiUyMHByb2dyZXNzJTIwc3RlcCUyMCU3QnN0ZXAlN0QlMjIlMkMlMjBibG9ja2luZyUzREZhbHNlJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKQ==",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=num_training_steps
    ):
        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;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}}),il=new se({props:{$$slots:{default:[ga]},$$scope:{ctx:C}}}),Bl=new se({props:{$$slots:{default:[Za]},$$scope:{ctx:C}}}),{c(){o(a.$$.fragment),U=p(),n=u("p"),n.innerHTML=c,_=p(),o(f.$$.fragment),B=p(),$=u("p"),$.innerHTML=Q,i=p(),o(h.$$.fragment),g=p(),o(E.$$.fragment),V=p(),X=u("p"),X.innerHTML=k,Z=p(),o(N.$$.fragment),Zl=p(),F=u("p"),F.textContent=ce,Gl=p(),G=u("ul"),G.innerHTML=ie,Xl=p(),A=u("p"),A.innerHTML=Te,Rl=p(),o(v.$$.fragment),Wl=p(),z=u("p"),z.textContent=oe,Vl=p(),o(x.$$.fragment),Us=p(),Jl=u("p"),Jl.textContent=_l,El=p(),o(H.$$.fragment),rs=p(),Ul=u("p"),Ul.innerHTML=kl,Nl=p(),o(Y.$$.fragment),Fl=p(),S=u("p"),S.textContent=me,Al=p(),o(D.$$.fragment),vl=p(),q=u("p"),q.textContent=we,zl=p(),o(L.$$.fragment),xl=p(),o(K.$$.fragment),Hl=p(),P=u("p"),P.innerHTML=de,Yl=p(),o(O.$$.fragment),ys=p(),rl=u("p"),rl.innerHTML=Ql,Sl=p(),o(ll.$$.fragment),Dl=p(),o(sl.$$.fragment),cs=p(),yl=u("p"),yl.textContent=$l,ql=p(),o(el.$$.fragment),Ll=p(),tl=u("p"),tl.innerHTML=ue,Kl=p(),o(al.$$.fragment),Pl=p(),o(nl.$$.fragment),Ol=p(),Ml=u("p"),Ml.innerHTML=je,ls=p(),o(pl.$$.fragment),is=p(),cl=u("p"),cl.textContent=ol,Ts=p(),o(il.$$.fragment),Tl=p(),o(Bl.$$.fragment)},l(s){m(a.$$.fragment,s),U=J(s),n=j(s,"P",{"data-svelte-h":!0}),b(n)!=="svelte-xjh1eu"&&(n.innerHTML=c),_=J(s),m(f.$$.fragment,s),B=J(s),$=j(s,"P",{"data-svelte-h":!0}),b($)!=="svelte-fh2nz1"&&($.innerHTML=Q),i=J(s),m(h.$$.fragment,s),g=J(s),m(E.$$.fragment,s),V=J(s),X=j(s,"P",{"data-svelte-h":!0}),b(X)!=="svelte-ptcm2l"&&(X.innerHTML=k),Z=J(s),m(N.$$.fragment,s),Zl=J(s),F=j(s,"P",{"data-svelte-h":!0}),b(F)!=="svelte-30wr61"&&(F.textContent=ce),Gl=J(s),G=j(s,"UL",{"data-svelte-h":!0}),b(G)!=="svelte-hscu3g"&&(G.innerHTML=ie),Xl=J(s),A=j(s,"P",{"data-svelte-h":!0}),b(A)!=="svelte-8e1ssw"&&(A.innerHTML=Te),Rl=J(s),m(v.$$.fragment,s),Wl=J(s),z=j(s,"P",{"data-svelte-h":!0}),b(z)!=="svelte-13o0n8c"&&(z.textContent=oe),Vl=J(s),m(x.$$.fragment,s),Us=J(s),Jl=j(s,"P",{"data-svelte-h":!0}),b(Jl)!=="svelte-19lt4yo"&&(Jl.textContent=_l),El=J(s),m(H.$$.fragment,s),rs=J(s),Ul=j(s,"P",{"data-svelte-h":!0}),b(Ul)!=="svelte-6srwzq"&&(Ul.innerHTML=kl),Nl=J(s),m(Y.$$.fragment,s),Fl=J(s),S=j(s,"P",{"data-svelte-h":!0}),b(S)!=="svelte-55938r"&&(S.textContent=me),Al=J(s),m(D.$$.fragment,s),vl=J(s),q=j(s,"P",{"data-svelte-h":!0}),b(q)!=="svelte-1jozmoj"&&(q.textContent=we),zl=J(s),m(L.$$.fragment,s),xl=J(s),m(K.$$.fragment,s),Hl=J(s),P=j(s,"P",{"data-svelte-h":!0}),b(P)!=="svelte-1eft1a0"&&(P.innerHTML=de),Yl=J(s),m(O.$$.fragment,s),ys=J(s),rl=j(s,"P",{"data-svelte-h":!0}),b(rl)!=="svelte-y3aow6"&&(rl.innerHTML=Ql),Sl=J(s),m(ll.$$.fragment,s),Dl=J(s),m(sl.$$.fragment,s),cs=J(s),yl=j(s,"P",{"data-svelte-h":!0}),b(yl)!=="svelte-1u17xr7"&&(yl.textContent=$l),ql=J(s),m(el.$$.fragment,s),Ll=J(s),tl=j(s,"P",{"data-svelte-h":!0}),b(tl)!=="svelte-1hiar3m"&&(tl.innerHTML=ue),Kl=J(s),m(al.$$.fragment,s),Pl=J(s),m(nl.$$.fragment,s),Ol=J(s),Ml=j(s,"P",{"data-svelte-h":!0}),b(Ml)!=="svelte-1q6x48q"&&(Ml.innerHTML=je),ls=J(s),m(pl.$$.fragment,s),is=J(s),cl=j(s,"P",{"data-svelte-h":!0}),b(cl)!=="svelte-al5zgg"&&(cl.textContent=ol),Ts=J(s),m(il.$$.fragment,s),Tl=J(s),m(Bl.$$.fragment,s)},m(s,T){w(a,s,T),t(s,U,T),t(s,n,T),t(s,_,T),w(f,s,T),t(s,B,T),t(s,$,T),t(s,i,T),w(h,s,T),t(s,g,T),w(E,s,T),t(s,V,T),t(s,X,T),t(s,Z,T),w(N,s,T),t(s,Zl,T),t(s,F,T),t(s,Gl,T),t(s,G,T),t(s,Xl,T),t(s,A,T),t(s,Rl,T),w(v,s,T),t(s,Wl,T),t(s,z,T),t(s,Vl,T),w(x,s,T),t(s,Us,T),t(s,Jl,T),t(s,El,T),w(H,s,T),t(s,rs,T),t(s,Ul,T),t(s,Nl,T),w(Y,s,T),t(s,Fl,T),t(s,S,T),t(s,Al,T),w(D,s,T),t(s,vl,T),t(s,q,T),t(s,zl,T),w(L,s,T),t(s,xl,T),w(K,s,T),t(s,Hl,T),t(s,P,T),t(s,Yl,T),w(O,s,T),t(s,ys,T),t(s,rl,T),t(s,Sl,T),w(ll,s,T),t(s,Dl,T),w(sl,s,T),t(s,cs,T),t(s,yl,T),t(s,ql,T),w(el,s,T),t(s,Ll,T),t(s,tl,T),t(s,Kl,T),w(al,s,T),t(s,Pl,T),w(nl,s,T),t(s,Ol,T),t(s,Ml,T),t(s,ls,T),w(pl,s,T),t(s,is,T),t(s,cl,T),t(s,Ts,T),w(il,s,T),t(s,Tl,T),w(Bl,s,T),ss=!0},i(s){ss||(r(a.$$.fragment,s),r(f.$$.fragment,s),r(h.$$.fragment,s),r(E.$$.fragment,s),r(N.$$.fragment,s),r(v.$$.fragment,s),r(x.$$.fragment,s),r(H.$$.fragment,s),r(Y.$$.fragment,s),r(D.$$.fragment,s),r(L.$$.fragment,s),r(K.$$.fragment,s),r(O.$$.fragment,s),r(ll.$$.fragment,s),r(sl.$$.fragment,s),r(el.$$.fragment,s),r(al.$$.fragment,s),r(nl.$$.fragment,s),r(pl.$$.fragment,s),r(il.$$.fragment,s),r(Bl.$$.fragment,s),ss=!0)},o(s){y(a.$$.fragment,s),y(f.$$.fragment,s),y(h.$$.fragment,s),y(E.$$.fragment,s),y(N.$$.fragment,s),y(v.$$.fragment,s),y(x.$$.fragment,s),y(H.$$.fragment,s),y(Y.$$.fragment,s),y(D.$$.fragment,s),y(L.$$.fragment,s),y(K.$$.fragment,s),y(O.$$.fragment,s),y(ll.$$.fragment,s),y(sl.$$.fragment,s),y(el.$$.fragment,s),y(al.$$.fragment,s),y(nl.$$.fragment,s),y(pl.$$.fragment,s),y(il.$$.fragment,s),y(Bl.$$.fragment,s),ss=!1},d(s){s&&(e(U),e(n),e(_),e(B),e($),e(i),e(g),e(V),e(X),e(Z),e(Zl),e(F),e(Gl),e(G),e(Xl),e(A),e(Rl),e(Wl),e(z),e(Vl),e(Us),e(Jl),e(El),e(rs),e(Ul),e(Nl),e(Fl),e(S),e(Al),e(vl),e(q),e(zl),e(xl),e(Hl),e(P),e(Yl),e(ys),e(rl),e(Sl),e(Dl),e(cs),e(yl),e(ql),e(Ll),e(tl),e(Kl),e(Pl),e(Ol),e(Ml),e(ls),e(is),e(cl),e(Ts),e(Tl)),d(a,s),d(f,s),d(h,s),d(E,s),d(N,s),d(v,s),d(x,s),d(H,s),d(Y,s),d(D,s),d(L,s),d(K,s),d(O,s),d(ll,s),d(sl,s),d(el,s),d(al,s),d(nl,s),d(pl,s),d(il,s),d(Bl,s)}}}function Ba(C){let a,U='🚨 如果你在 TPU 上训练，你需要将上述单元格开始的所有代码移到一个专门的训练函数中。更多详情请参阅 <a href="/course/chapter3">第三章</a> 。';return{c(){a=u("p"),a.innerHTML=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-vxy3ez"&&(a.innerHTML=U)},m(n,c){t(n,a,c)},p:ye,d(n){n&&e(a)}}}function ga(C){let a,U="✏️ <strong>试试看！</strong> 创建适合你的用例的自定义损失函数，或在训练循环中添加另一个自定义步骤。";return{c(){a=u("p"),a.innerHTML=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-1vr69hw"&&(a.innerHTML=U)},m(n,c){t(n,a,c)},p:ye,d(n){n&&e(a)}}}function Za(C){let a,U="✏️ <strong>试试看！</strong>  当运行长时间的训练实验时，使用 TensorBoard 或 Weights &amp; Biases 等工具记录重要指标是个好主意。向训练循环中添加适当的日志记录，这样你可以随时检查训练进度。";return{c(){a=u("p"),a.innerHTML=U},l(n){a=j(n,"P",{"data-svelte-h":!0}),b(a)!=="svelte-ry3l0i"&&(a.innerHTML=U)},m(n,c){t(n,a,c)},p:ye,d(n){n&&e(a)}}}function Ga(C){let a,U,n,c,_,f,B,$,Q,i,h,g,E='到目前为止，我们主要使用预训练模型，并通过复用预训练的权重，然后使用新的数据对它们进行微调，以适应新的应用场景。正如我们在 <a href="/course/chapter1">第一章</a> 中看到的，这通常称为 <code>迁移学习（transfer learning）</code> ，对于大多数标注数据稀缺的应用场景，它是一种将 Transformer 模型应用到大部分真实的应用场景中的一个非常成功的策略。在本章中，我们将采用不同的方法并从头开始训练一个全新的模型。如果你有大量数据而且这些数据与可用模型的预训练数据差异很大，那么这是一个很好的方法。然而，相比仅微调现有模型，预训练语言模型需要更多的计算资源。训练一个新模型可能是有意义的示例包括由音乐符号、DNA 等分子序列或编程语言组成的数据集。编程语言组成的数据集最近广泛地受到关注，这要归功于 TabNine 和 GitHub 的 Copilot 等工具的流行，它们由 OpenAI 的 Codex 模型提供支持，可以生成长代码序列。这种文本生成任务最适合使用自回归或因果语言模型（例如 GPT-2）。',V,X,k="在这一节，我们将构建一个精简版的代码生成模型：使用 Python 代码的一个数据集，来实现一行代码的补全，而不是直接生成完整的函数或类。当你使用 Python 处理数据时，你经常会接触到 Python 数据科学栈，包括 <code>matplotlib</code> ， <code>seaborn</code> ， <code>pandas</code> ，和 <code>scikit-learn</code> 这些库。当使用这些框架时，经常需要查找特定的命令，如果我们能够用模型来自动给出恰当的推荐命令就太好了！",Z,N,Zl,F,ce='在 <a href="/course/chapter6">第六章</a> 中，我们创建了一个高效的 tokenizer 来处理 Python 源代码，但我们还需要一个大规模的数据集来预训练模型。在这里，我们将使用 tokenizer 处理一个来自 GitHub 仓库的 Python 代码语料库。然后，我们将使用 Trainer API 和 🤗 Accelerate 来训练模型。让我们开始吧！',Gl,G,ie,Xl,A,Te='这里展示的是一个已经训练并上传到 Hub 的模型，它就是使用本节中的代码训练的。你可以在 <a href="https://huggingface.co/huggingface-course/codeparrot-ds?text=plt.imshow%28" rel="nofollow">这里</a> 找到它。注意，由于文本生成过程中有一些随机性，你可能会得到稍微不同的结果。',Rl,v,Wl,z,oe='我们可以从诸如 GitHub 这样的代码仓库中获取丰富的 Python 代码，通过对每个 Python 仓库进行抓取，我们就可以创建一个数据集。这就是在 <a href="https://learning.oreilly.com/library/view/natural-language-processing/9781098136789/" rel="nofollow">Transformers textbook</a> 中预训练一个大型 GPT-2 模型的方法。开发者整理了名为 <code>codeparrot</code> 的一个大约为 180GB 的 GitHub 数据集， 其中包含大约 2,000 万个的Python 文件。 开发者用这些文件构建了一个数据集，并在 <a href="https://huggingface.co/datasets/transformersbook/codeparrot" rel="nofollow">Hugging Face Hub</a> 上分享了这个数据集。',Vl,x,Us="然而，使用完整语料库的训练既耗时又费力，我们只需要找到 Python 数据科学栈相关的数据集子集。所以，让我们从 <code>codeparrot</code> 数据集中筛选出包含这个栈中所有相关库的所有文件。由于数据集的太大，我们希望避免直接把全部的数据集下载下来；因此，我们将使用流式传输的方法来动态过滤它。为了使用上述的库来筛选代码样本，我们将使用以下函数：",Jl,_l,El,H,rs="让我们用两个例子来测试一下：",Ul,kl,Nl,Y,Fl,S,me="我们可以使用这个函数来创建一个新的函数，该函数将流式传输数据集并过滤我们想要的元素：",Al,D,vl,q,we="然后我们可以直接使用这里函数流式处理数据集：",zl,L,xl,K,Hl,P,de="完成这个操作后，我们过滤后的数据集只有原始数据集的大约 3％，但这仍然是相当可观的大小——最终的数据集是 6GB，由 600,000 个 Python 脚本组成！",Yl,O,ys="过滤完整的数据集可能需要 2-3 小时，这取决于你的机器性能和带宽。如果你不想亲自经历这个漫长的过程，我们在 Hub 上提供了过滤后的数据集供你下载：",rl,Ql,Sl,ll,Dl,sl,cs="让我们看一个来自数据集的例子。我们将只显示每个字段的前 200 个字符：",yl,$l,ql,el,Ll,tl,ue="我们可以看到， <code>content</code> 字段包含了我们希望模型训练的代码。有了这个数据集之后，我们需要对文本进行一些处理，以便它们适合于预训练。",Kl,al,Pl,nl,Ol,Ml,je="首先，我们需要将数据进行分词处理，这样才能进行训练。由于我们的主要目标是自动补全短的函数调用，因此我们可以将上下文大小设置得相对较小。这样做的好处是我们可以更快地训练模型，而且需要的内存也大大减少。如果你的应用需要更多的上下文（比如，你希望模型根据包含函数定义的文件编写单元测试），那么应该增大该数字，但是也要记住这会增加 GPU 显存的占用。现在，我们将上下文大小固定为 128 个 tokens 而不是在 GPT-2 或 GPT-3 中使用的 1,024 或 2,048 个 tokens",ls,pl,is='大多数文档都包含超过 128 个 tokens 因此简单地将输入截断到最大长度会删除我们数据集的很一大部分。因此，我们将使用 <code>return_overflowing_tokens</code> 选项将整个输入进行分词处理，并将其分割为几个块，正如我们在 <a href="/course/chapter6/4">第六章</a> 中所做的那样。我们还将使用 <code>return_length</code> 选项自动返回创建的每个块的长度。通常，最后一个块的大小会小于上下文大小，我们将去掉最后一块以避免填充问题；因为我们已经有足够的数据，所以不需要它们。',cl,ol,Ts='<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."/>',il,Tl,Bl="让我们通过查看前两个示例来具体了解结果怎么样：",ss,s,T,os,fe,ms,mt="我们可以看到，这两个例子总共得到了 34 个块。查看块长度，我们可以看到两个文档末端的块少于 128 个 tokens （分别为 117 和 41）。不过这些只占我们所拥有的总块数的一小部分，因此我们可以放心地丢掉它们。通过 <code>overflow_to_sample_mapping</code> 字段，我们还可以分辨出哪些块属于哪个样本。",he,ws,wt='在这个操作中，我们使用了🤗 Datasets 中的 <code>Dataset.map()</code> 函数的一个便捷的特性，即它并不需要一对一地设置分块后和分块前的映射关系；正如我们在 <a href="/course/chapter7/3">第三节</a> 中看到的，我们可以自由地将一个样本拆分或者删除部分样本来创建比输入的 <code>batch_size</code> 更多或更少元素的 batch。 <code>Dataset.map()</code> 函数会自动帮我们关联映射关系，当进行像数据增强或数据过滤这样改变元素数量的操作时非常有用。在我们的情况下，当将每个样本分词并分割成指定上下文大小的块时，我们从每个样本中创建了许多样本。我们需要删除原本的列，因为它们的大小和我们分割后的大小不一样。如果我们想保留它们，我们可以复制它们来填充，并在 <code>Dataset.map()</code> 调用中返回它们。',Ie,ds,Ce,us,_e,js,dt="我们现在有 1670 万个样本，每个样本有 128 个 tokens 总共相当于大约 21 亿个 tokens。作为参考，OpenAI 的 GPT-3 和 Codex 模型分别在 300 和 1000 亿个 tokens 上进行了训练，其中 Codex 模型从 GPT-3 checkpoint 初始化。本节的目标不是与这些能生成长且连贯文本的模型竞争，而是创建一个能为数据科学家提供快速自动代码补全功能的精简版本。",ke,bs,ut="既然我们已经准备好了数据集，那就来设置模型吧！",Qe,ts,$e,fs,Be,hs,jt="我们的第一步是初始化一个全新地 GPT-2 模型。我们可以通过加载预训练配置来初始化一个与 GPT-2 small 相同的配置的模型，并确保 tokenizer 大小与模型的词汇表大小匹配，以及设置 <code>bos</code> 和 <code>eos</code> （序列的开始和结束） token IDs：",ge,ml,wl,ee,Is,bt="我们的新模型有 124M 个参数需要训练。在开始训练之前，我们需要设置一个数据整理器（DataCollator），它将负责创建 Batch。我们可以使用 <code>DataCollatorForLanguageModeling</code> ，顾名思义，它专门用于语言建模。除了堆叠和填充创建 Batch 之外，它还负责创建语言模型的待预测的标签 —— 在因果语言建模中，输入就是待预测的标签（只是偏移一个元素），而这个数据整理器（DataCollator）在训练过程中实时将输入偏移一个元素来创建它们，因此我们不需要复制 <code>input_ids</code> 。",Ze,Cs,ft="注意， <code>DataCollatorForLanguageModeling</code> 同时支持掩码语言建模 （MLM） 和因果语言建模 （CLM）。默认情况下它安装 MLM 需要的格式准备数据，但我们可以通过设置 <code>mlm=False</code> 参数切换到 CLM。",Ge,dl,ul,te,_s,ht="让我们看一个例子：",Xe,ks,Re,jl,bl,ae,Qs,It="我们可以看到示例的数据已经处理好了，并且所有 tensor 都具有相同的形状。",We,ne,as,Ve,$s,Ct="现在我们已经准备好了所有东西，可以开始训练我们的模型了——好像也不是那么困难！在我们开始训练之前，我们应该登录到 Hugging Face。如果你正在使用 Notebook 运行代码，你可以使用下面的实用函数进行登录：",Ee,Bs,Ne,gs,_t="这将显示一个小部件，你可以在其中输入你的 Hugging Face 登录凭据。",Fe,Zs,kt="如果你不是在 Notebook 上工作，只需在终端中输入以下行：",Ae,Gs,ve,fl,hl,Me,ns,ze,Ms,xe,Xs,He,Rs,Qt="现在是见证奇迹的时刻：我们来看看训练好的模型到底表现如何！我们可以在日志中看到损失持续下降，但要测试模型的效果，我们就看看它对一些提示的反应如何。为此，我们将模型包装在一个文本生成的 <code>pipeline</code> 中，并如果有 GPU 可用，我们将把它放在 GPU 上加快生成速度：",Ye,Il,Cl,pe,Ws,$t="让我们从简单的创建散点图任务开始：",Se,Vs,De,Es,qe,Ns,Bt="结果看起来是正确的。那么对于 <code>pandas</code> 操作也可以吗？让我们看看是否能从两个数组创建一个 <code>DataFrame</code> ：",Le,Fs,Ke,As,Pe,vs,gt="很好，这是正确的答案——尽管它又把 <code>x</code> 重复插入了一次。而且由于生成的 token 数量有限，所以下面的 <code>for</code> 循环被切断了。让我们看看我们是否能做些更复杂的事情，让模型帮助我们使用 <code>groupby</code> 操作：",Oe,zs,lt,xs,st,Hs,Zt="不错；是正确的。最后，让我们看看是否能引导模型使用 <code>scikit-learn</code> 并建立一个随机森林模型：",et,Ys,tt,Ss,at,Je,Ue,Ds,nt,be,Mt;_=new Ja({props:{fw:C[0]}}),B=new re({props:{title:"从头开始训练因果语言模型",local:"从头开始训练因果语言模型",headingTag:"h1"}});const Gt=[ya,ra],qs=[];function Xt(l,M){return l[0]==="pt"?0:1}Q=Xt(C),i=qs[Q]=Gt[Q](C),N=new ot({props:{id:"Vpjb1lu0MDk"}}),v=new re({props:{title:"收集数据",local:"收集数据",headingTag:"h2"}}),_l=new I({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}}),kl=new I({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}}),Y=new I({props:{code:"RmFsc2UlMjBUcnVl",highlighted:'<span class="hljs-literal">False</span> <span class="hljs-literal">True</span>',wrap:!1}}),D=new I({props:{code:"ZnJvbSUyMGNvbGxlY3Rpb25zJTIwaW1wb3J0JTIwZGVmYXVsdGRpY3QlMEFmcm9tJTIwdHFkbSUyMGltcG9ydCUyMHRxZG0lMEFmcm9tJTIwZGF0YXNldHMlMjBpbXBvcnQlMjBEYXRhc2V0JTBBJTBBJTBBZGVmJTIwZmlsdGVyX3N0cmVhbWluZ19kYXRhc2V0KGRhdGFzZXQlMkMlMjBmaWx0ZXJzKSUzQSUwQSUyMCUyMCUyMCUyMGZpbHRlcmVkX2RpY3QlMjAlM0QlMjBkZWZhdWx0ZGljdChsaXN0KSUwQSUyMCUyMCUyMCUyMHRvdGFsJTIwJTNEJTIwMCUwQSUyMCUyMCUyMCUyMGZvciUyMHNhbXBsZSUyMGluJTIwdHFkbShpdGVyKGRhdGFzZXQpKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRvdGFsJTIwJTJCJTNEJTIwMSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGlmJTIwYW55X2tleXdvcmRfaW5fc3RyaW5nKHNhbXBsZSU1QiUyMmNvbnRlbnQlMjIlNUQlMkMlMjBmaWx0ZXJzKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZvciUyMGslMkMlMjB2JTIwaW4lMjBzYW1wbGUuaXRlbXMoKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZpbHRlcmVkX2RpY3QlNUJrJTVELmFwcGVuZCh2KSUwQSUyMCUyMCUyMCUyMHByaW50KGYlMjIlN0JsZW4oZmlsdGVyZWRfZGljdCU1Qidjb250ZW50JyU1RCklMkZ0b3RhbCUzQS4yJTI1JTdEJTIwb2YlMjBkYXRhJTIwYWZ0ZXIlMjBmaWx0ZXJpbmcuJTIyKSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMERhdGFzZXQuZnJvbV9kaWN0KGZpbHRlcmVkX2RpY3Qp",highlighted:`<span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> defaultdict
<span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm
<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset


<span class="hljs-keyword">def</span> <span class="hljs-title function_">filter_streaming_dataset</span>(<span class="hljs-params">dataset, filters</span>):
    filtered_dict = defaultdict(<span class="hljs-built_in">list</span>)
    total = <span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> sample <span class="hljs-keyword">in</span> tqdm(<span class="hljs-built_in">iter</span>(dataset)):
        total += <span class="hljs-number">1</span>
        <span class="hljs-keyword">if</span> any_keyword_in_string(sample[<span class="hljs-string">&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}}),L=new I({props:{code:"JTIzJTIwJUU2JTg5JUE3JUU4JUExJThDJUU4JUJGJTk5JUU0JUI4JUFBJUU0JUJCJUEzJUU3JUEwJTgxJUU1JTlEJTk3JUU5JTlDJTgwJUU4JUE2JTgxJUU5JTlEJTlFJUU1JUI4JUI4JUU5JTk1JUJGJUU3JTlBJTg0JUU2JTk3JUI2JUU5JTk3JUI0JTJDJUU1JTlCJUEwJUU2JUFEJUE0JUU0JUJEJUEwJUU1JThGJUFGJUU0JUJCJUE1JUU4JUI3JUIzJUU4JUJGJTg3JUU1JUFFJTgzJTJDJUU3JUJCJUE3JUU3JUJCJUFEJUU2JTg5JUE3JUU4JUExJThDJUU0JUI4JThCJUU0JUI4JTgwJUU0JUI4JUFBISUwQWZyb20lMjBkYXRhc2V0cyUyMGltcG9ydCUyMGxvYWRfZGF0YXNldCUwQSUwQXNwbGl0JTIwJTNEJTIwJTIydHJhaW4lMjIlMjAlMjAlMjMlMjAlMjJ2YWxpZCUyMiUwQWZpbHRlcnMlMjAlM0QlMjAlNUIlMjJwYW5kYXMlMjIlMkMlMjAlMjJza2xlYXJuJTIyJTJDJTIwJTIybWF0cGxvdGxpYiUyMiUyQyUyMCUyMnNlYWJvcm4lMjIlNUQlMEElMEFkYXRhJTIwJTNEJTIwbG9hZF9kYXRhc2V0KGYlMjJ0cmFuc2Zvcm1lcnNib29rJTJGY29kZXBhcnJvdC0lN0JzcGxpdCU3RCUyMiUyQyUyMHNwbGl0JTNEc3BsaXQlMkMlMjBzdHJlYW1pbmclM0RUcnVlKSUwQWZpbHRlcmVkX2RhdGElMjAlM0QlMjBmaWx0ZXJfc3RyZWFtaW5nX2RhdGFzZXQoZGF0YSUyQyUyMGZpbHRlcnMp",highlighted:`<span class="hljs-comment"># 执行这个代码块需要非常长的时间,因此你可以跳过它,继续执行下一个!</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}}),K=new I({props:{code:"My4yNiUyNSUyMG9mJTIwZGF0YSUyMGFmdGVyJTIwZmlsdGVyaW5nLg==",highlighted:'<span class="hljs-number">3.26</span>% of data after filtering.',wrap:!1}}),Ql=new I({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTJDJTIwRGF0YXNldERpY3QlMEElMEFkc190cmFpbiUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJodWdnaW5nZmFjZS1jb3Vyc2UlMkZjb2RlcGFycm90LWRzLXRyYWluJTIyJTJDJTIwc3BsaXQlM0QlMjJ0cmFpbiUyMiklMEFkc192YWxpZCUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJodWdnaW5nZmFjZS1jb3Vyc2UlMkZjb2RlcGFycm90LWRzLXZhbGlkJTIyJTJDJTIwc3BsaXQlM0QlMjJ2YWxpZGF0aW9uJTIyKSUwQSUwQXJhd19kYXRhc2V0cyUyMCUzRCUyMERhdGFzZXREaWN0KCUwQSUyMCUyMCUyMCUyMCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnRyYWluJTIyJTNBJTIwZHNfdHJhaW4lMkMlMjAlMjAlMjMlMjAuc2h1ZmZsZSgpLnNlbGVjdChyYW5nZSg1MDAwMCkpJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIydmFsaWQlMjIlM0ElMjBkc192YWxpZCUyQyUyMCUyMCUyMyUyMC5zaHVmZmxlKCkuc2VsZWN0KHJhbmdlKDUwMCkpJTBBJTIwJTIwJTIwJTIwJTdEJTBBKSUwQSUwQXJhd19kYXRhc2V0cw==",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;validation&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}}),ll=new I({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}}),$l=new I({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}}),el=new I({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 re({props:{title:"准备数据集",local:"准备数据集",headingTag:"h2"}}),nl=new ot({props:{id:"ma1TrR7gE7I"}}),s=new I({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}}),os=new I({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}}),ds=new I({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}}),us=new I({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 se({props:{$$slots:{default:[ca]},$$scope:{ctx:C}}}),fs=new re({props:{title:"初始化一个新模型",local:"初始化一个新模型",headingTag:"h2"}});const Rt=[Ta,ia],Ls=[];function Wt(l,M){return l[0]==="pt"?0:1}ml=Wt(C),wl=Ls[ml]=Rt[ml](C);const Vt=[ma,oa],Ks=[];function Et(l,M){return l[0]==="pt"?0:1}dl=Et(C),ul=Ks[dl]=Vt[dl](C),ks=new I({props:{code:"b3V0JTIwJTNEJTIwZGF0YV9jb2xsYXRvciglNUJ0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QmklNUQlMjBmb3IlMjBpJTIwaW4lMjByYW5nZSg1KSU1RCklMEFmb3IlMjBrZXklMjBpbiUyMG91dCUzQSUwQSUyMCUyMCUyMCUyMHByaW50KGYlMjIlN0JrZXklN0QlMjBzaGFwZSUzQSUyMCU3Qm91dCU1QmtleSU1RC5zaGFwZSU3RCUyMik=",highlighted:`out = data_collator([tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>)])
<span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> out:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;<span class="hljs-subst">{key}</span> shape: <span class="hljs-subst">{out[key].shape}</span>&quot;</span>)`,wrap:!1}});const Nt=[da,wa],Ps=[];function Ft(l,M){return l[0]==="pt"?0:1}jl=Ft(C),bl=Ps[jl]=Nt[jl](C);let R=C[0]==="tf"&&Pt();as=new se({props:{warning:!0,$$slots:{default:[ua]},$$scope:{ctx:C}}}),Bs=new I({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`,wrap:!1}}),Gs=new I({props:{code:"aHVnZ2luZ2ZhY2UtY2xpJTIwbG9naW4=",highlighted:"huggingface-cli login",wrap:!1}});const At=[ba,ja],Os=[];function vt(l,M){return l[0]==="pt"?0:1}fl=vt(C),hl=Os[fl]=At[fl](C),ns=new se({props:{$$slots:{default:[fa]},$$scope:{ctx:C}}}),Ms=new se({props:{$$slots:{default:[Ca]},$$scope:{ctx:C}}}),Xs=new re({props:{title:"使用 pipeline 进行代码生成",local:"使用管道生成代码",headingTag:"h2"}});const zt=[ka,_a],le=[];function xt(l,M){return l[0]==="pt"?0:1}Il=xt(C),Cl=le[Il]=zt[Il](C),Vs=new I({props:{code:"dHh0JTIwJTNEJTIwJTIyJTIyJTIyJTVDJTBBJTIzJTIwJUU1JTg4JTlCJUU1JUJCJUJBJUU0JUI4JTgwJUU0JUJBJTlCJUU2JTk1JUIwJUU2JThEJUFFJTBBeCUyMCUzRCUyMG5wLnJhbmRvbS5yYW5kbigxMDApJTBBeSUyMCUzRCUyMG5wLnJhbmRvbS5yYW5kbigxMDApJTBBJTBBJTIzJTIwJUU0JUJEJUJGJUU3JTk0JUE4JTIweCUyQ3klMjAlRTUlODglOUIlRTUlQkIlQkElRTYlOTUlQTMlRTclODIlQjklRTUlOUIlQkUlMEElMjIlMjIlMjIlMEFwcmludChwaXBlKHR4dCUyQyUyMG51bV9yZXR1cm5fc2VxdWVuY2VzJTNEMSklNUIwJTVEJTVCJTIyZ2VuZXJhdGVkX3RleHQlMjIlNUQp",highlighted:`txt = <span class="hljs-string">&quot;&quot;&quot;\\
# 创建一些数据
x = np.random.randn(100)
y = np.random.randn(100)

# 使用 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}}),Es=new I({props:{code:"JTIzJTIwJUU1JTg4JTlCJUU1JUJCJUJBJUU0JUI4JTgwJUU0JUJBJTlCJUU2JTk1JUIwJUU2JThEJUFFJTBBeCUyMCUzRCUyMG5wLnJhbmRvbS5yYW5kbigxMDApJTBBeSUyMCUzRCUyMG5wLnJhbmRvbS5yYW5kbigxMDApJTBBJTBBJTIzJTIwJUU0JUJEJUJGJUU3JTk0JUE4JTIweCUyQ3klMjAlRTUlODglOUIlRTUlQkIlQkElRTYlOTUlQTMlRTclODIlQjklRTUlOUIlQkUlMEFwbHQuc2NhdHRlcih4JTJDJTIweSklMEElMEElMjMlMjAlRTUlODglOUIlRTUlQkIlQkElRTYlOTUlQTMlRTclODIlQjk=",highlighted:`<span class="hljs-comment"># 创建一些数据</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"># 使用 x,y 创建散点图</span>
plt.scatter(x, y)

<span class="hljs-comment"># 创建散点</span>`,wrap:!1}}),Fs=new I({props:{code:"dHh0JTIwJTNEJTIwJTIyJTIyJTIyJTVDJTBBJTIzJTIwJUU1JTg4JTlCJUU1JUJCJUJBJUU0JUI4JTgwJUU0JUJBJTlCJUU2JTk1JUIwJUU2JThEJUFFJTBBeCUyMCUzRCUyMG5wLnJhbmRvbS5yYW5kbigxMDApJTBBeSUyMCUzRCUyMG5wLnJhbmRvbS5yYW5kbigxMDApJTBBJTBBJTIzJTIwJUU0JUJCJThFJTIweCUyMCVFNSU5MiU4QyUyMHklMjAlRTUlODglOUIlRTUlQkIlQkElMjBkYXRhZnJhbWUlMEElMjIlMjIlMjIlMEFwcmludChwaXBlKHR4dCUyQyUyMG51bV9yZXR1cm5fc2VxdWVuY2VzJTNEMSklNUIwJTVEJTVCJTIyZ2VuZXJhdGVkX3RleHQlMjIlNUQp",highlighted:`txt = <span class="hljs-string">&quot;&quot;&quot;\\
# 创建一些数据
x = np.random.randn(100)
y = np.random.randn(100)

# 从 x 和 y 创建 dataframe
&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}}),As=new I({props:{code:"JTIzJTIwJUU1JTg4JTlCJUU1JUJCJUJBJUU0JUI4JTgwJUU0JUJBJTlCJUU2JTk1JUIwJUU2JThEJUFFJTBBeCUyMCUzRCUyMG5wLnJhbmRvbS5yYW5kbigxMDApJTBBeSUyMCUzRCUyMG5wLnJhbmRvbS5yYW5kbigxMDApJTBBJTBBJTIzJTIwJUU0JUJCJThFJTIweCUyMCVFNSU5MiU4QyUyMHklMjAlRTUlODglOUIlRTUlQkIlQkElMjBkYXRhZnJhbWUlMEFkZiUyMCUzRCUyMHBkLkRhdGFGcmFtZSglN0IneCclM0ElMjB4JTJDJTIwJ3knJTNBJTIweSU3RCklMEFkZi5pbnNlcnQoMCUyQyd4JyUyQyUyMHgpJTBBZm9y",highlighted:`<span class="hljs-comment"># 创建一些数据</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"># 从 x 和 y 创建 dataframe</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}}),zs=new I({props:{code:"dHh0JTIwJTNEJTIwJTIyJTIyJTIyJTVDJTBBJTIzJTIwJUU2JTlDJTg5JUU4JTgxJThDJUU0JUI4JTlBJTJDJUU2JTk0JUI2JUU1JTg1JUE1JUU1JTkyJThDJUU1JTkwJThEJUU1JUFEJTk3JUU3JTlBJTg0JTIwZGF0YWZyYW1lJTBBZGYlMjAlM0QlMjBwZC5EYXRhRnJhbWUoJTdCJ3Byb2Zlc3Npb24nJTNBJTIweCUyQyUyMCdpbmNvbWUnJTNBeSUyQyUyMCduYW1lJyUzQSUyMHolN0QpJTBBJTBBJTIzJTIwJUU4JUFFJUExJUU3JUFFJTk3JUU2JUFGJThGJUU0JUI4JUFBJUU4JTgxJThDJUU0JUI4JTlBJUU3JTlBJTg0JUU1JUI5JUIzJUU1JTlEJTg3JUU2JTk0JUI2JUU1JTg1JUE1JTBBJTIyJTIyJTIyJTBBcHJpbnQocGlwZSh0eHQlMkMlMjBudW1fcmV0dXJuX3NlcXVlbmNlcyUzRDEpJTVCMCU1RCU1QiUyMmdlbmVyYXRlZF90ZXh0JTIyJTVEKQ==",highlighted:`txt = <span class="hljs-string">&quot;&quot;&quot;\\
# 有职业,收入和名字的 dataframe
df = pd.DataFrame({&#x27;profession&#x27;: x, &#x27;income&#x27;:y, &#x27;name&#x27;: z})

# 计算每个职业的平均收入
&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}}),xs=new I({props:{code:"JTIzJTIwJUU2JTlDJTg5JUU4JTgxJThDJUU0JUI4JTlBJTJDJUU2JTk0JUI2JUU1JTg1JUE1JUU1JTkyJThDJUU1JTkwJThEJUU1JUFEJTk3JUU3JTlBJTg0JTIwZGF0YWZyYW1lJTBBZGYlMjAlM0QlMjBwZC5EYXRhRnJhbWUoJTdCJ3Byb2Zlc3Npb24nJTNBJTIweCUyQyUyMCdpbmNvbWUnJTNBeSUyQyUyMCduYW1lJyUzQSUyMHolN0QpJTBBJTBBJTIzJTIwJUU4JUFFJUExJUU3JUFFJTk3JUU2JUFGJThGJUU0JUI4JUFBJUU4JTgxJThDJUU0JUI4JTlBJUU3JTlBJTg0JUU1JUI5JUIzJUU1JTlEJTg3JUU2JTk0JUI2JUU1JTg1JUE1JTBBcHJvZmVzc2lvbiUyMCUzRCUyMGRmLmdyb3VwYnkoJTVCJ3Byb2Zlc3Npb24nJTVEKS5tZWFuKCklMEElMEElMjMlMjAlRTglQUUlQTElRTclQUUlOTc=",highlighted:`<span class="hljs-comment"># 有职业,收入和名字的 dataframe</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"># 计算每个职业的平均收入</span>
profession = df.groupby([<span class="hljs-string">&#x27;profession&#x27;</span>]).mean()

<span class="hljs-comment"># 计算</span>`,wrap:!1}}),Ys=new I({props:{code:"dHh0JTIwJTNEJTIwJTIyJTIyJTIyJTBBJTIzJTIwJUU0JUJCJThFJTIwc2Npa2l0LWxlYXJuJTIwJUU1JUFGJUJDJUU1JTg1JUE1JUU5JTlBJThGJUU2JTlDJUJBJUU2JUEzJUFFJUU2JTlFJTk3JUU1JTlCJTlFJUU1JUJEJTkyJUU1JTk5JUE4JTBBZnJvbSUyMHNrbGVhcm4uZW5zZW1ibGUlMjBpbXBvcnQlMjBSYW5kb21Gb3Jlc3RSZWdyZXNzb3IlMEElMEElMjMlMjAlRTclOTQlQTglMjBYJTJDJTIweSUyMCVFNiU4QiU5RiVFNSU5MCU4OCVFNSVCOCVBNiVFNiU5QyU4OSUyMDMwMCUyMCVFNCVCOCVBQSVFNCVCQyVCMCVFNyVBRSU5NyVFNSU5OSVBOCVFNyU5QSU4NCVFOSU5QSU4RiVFNiU5QyVCQSVFNiVBMyVBRSVFNiU5RSU5NyVFNiVBOCVBMSVFNSU5RSU4QiUzQSUwQSUyMiUyMiUyMiUwQXByaW50KHBpcGUodHh0JTJDJTIwbnVtX3JldHVybl9zZXF1ZW5jZXMlM0QxKSU1QjAlNUQlNUIlMjJnZW5lcmF0ZWRfdGV4dCUyMiU1RCk=",highlighted:`txt = <span class="hljs-string">&quot;&quot;&quot;
# 从 scikit-learn 导入随机森林回归器
from sklearn.ensemble import RandomForestRegressor

# 用 X, y 拟合带有 300 个估算器的随机森林模型:
&quot;&quot;&quot;</span>
<span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;generated_text&quot;</span>])`,wrap:!1}}),Ss=new I({props:{code:"JTIzJTIwJUU0JUJCJThFJTIwc2Npa2l0LWxlYXJuJTIwJUU1JUFGJUJDJUU1JTg1JUE1JUU5JTlBJThGJUU2JTlDJUJBJUU2JUEzJUFFJUU2JTlFJTk3JUU1JTlCJTlFJUU1JUJEJTkyJUU1JTk5JUE4JTBBZnJvbSUyMHNrbGVhcm4uZW5zZW1ibGUlMjBpbXBvcnQlMjBSYW5kb21Gb3Jlc3RSZWdyZXNzb3IlMEElMEElMjMlMjAlRTclOTQlQTglMjBYJTJDJTIweSUyMCVFNiU4QiU5RiVFNSU5MCU4OCVFNSVCOCVBNiVFNiU5QyU4OSUyMDMwMCUyMCVFNCVCOCVBQSVFNCVCQyVCMCVFNyVBRSU5NyVFNSU5OSVBOCVFNyU5QSU4NCVFOSU5QSU4RiVFNiU5QyVCQSVFNiVBMyVBRSVFNiU5RSU5NyVFNiVBOCVBMSVFNSU5RSU4QiUzQSUwQXJmJTIwJTNEJTIwUmFuZG9tRm9yZXN0UmVncmVzc29yKG5fZXN0aW1hdG9ycyUzRDMwMCUyQyUyMHJhbmRvbV9zdGF0ZSUzRHJhbmRvbV9zdGF0ZSUyQyUyMG1heF9kZXB0aCUzRDMpJTBBcmYuZml0KFglMkMlMjB5KSUwQXJm",highlighted:`<span class="hljs-comment"># 从 scikit-learn 导入随机森林回归器</span>
<span class="hljs-keyword">from</span> sklearn.ensemble <span class="hljs-keyword">import</span> RandomForestRegressor

<span class="hljs-comment"># 用 X, y 拟合带有 300 个估算器的随机森林模型:</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 Ht(l,M){return l[0]==="tf"?$a:Qa}let pt=Ht(C),es=pt(C),W=C[0]==="pt"&&Ot(C);return Ds=new Ua({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter7/6.mdx"}}),{c(){a=u("meta"),U=p(),n=u("p"),c=p(),o(_.$$.fragment),f=p(),o(B.$$.fragment),$=p(),i.c(),h=p(),g=u("p"),g.innerHTML=E,V=p(),X=u("p"),X.innerHTML=k,Z=p(),o(N.$$.fragment),Zl=p(),F=u("p"),F.innerHTML=ce,Gl=p(),G=u("iframe"),Xl=p(),A=u("p"),A.innerHTML=Te,Rl=p(),o(v.$$.fragment),Wl=p(),z=u("p"),z.innerHTML=oe,Vl=p(),x=u("p"),x.innerHTML=Us,Jl=p(),o(_l.$$.fragment),El=p(),H=u("p"),H.textContent=rs,Ul=p(),o(kl.$$.fragment),Nl=p(),o(Y.$$.fragment),Fl=p(),S=u("p"),S.textContent=me,Al=p(),o(D.$$.fragment),vl=p(),q=u("p"),q.textContent=we,zl=p(),o(L.$$.fragment),xl=p(),o(K.$$.fragment),Hl=p(),P=u("p"),P.textContent=de,Yl=p(),O=u("p"),O.textContent=ys,rl=p(),o(Ql.$$.fragment),Sl=p(),o(ll.$$.fragment),Dl=p(),sl=u("p"),sl.textContent=cs,yl=p(),o($l.$$.fragment),ql=p(),o(el.$$.fragment),Ll=p(),tl=u("p"),tl.innerHTML=ue,Kl=p(),o(al.$$.fragment),Pl=p(),o(nl.$$.fragment),Ol=p(),Ml=u("p"),Ml.textContent=je,ls=p(),pl=u("p"),pl.innerHTML=is,cl=p(),ol=u("div"),ol.innerHTML=Ts,il=p(),Tl=u("p"),Tl.textContent=Bl,ss=p(),o(s.$$.fragment),T=p(),o(os.$$.fragment),fe=p(),ms=u("p"),ms.innerHTML=mt,he=p(),ws=u("p"),ws.innerHTML=wt,Ie=p(),o(ds.$$.fragment),Ce=p(),o(us.$$.fragment),_e=p(),js=u("p"),js.textContent=dt,ke=p(),bs=u("p"),bs.textContent=ut,Qe=p(),o(ts.$$.fragment),$e=p(),o(fs.$$.fragment),Be=p(),hs=u("p"),hs.innerHTML=jt,ge=p(),wl.c(),ee=p(),Is=u("p"),Is.innerHTML=bt,Ze=p(),Cs=u("p"),Cs.innerHTML=ft,Ge=p(),ul.c(),te=p(),_s=u("p"),_s.textContent=ht,Xe=p(),o(ks.$$.fragment),Re=p(),bl.c(),ae=p(),Qs=u("p"),Qs.textContent=It,We=p(),R&&R.c(),ne=p(),o(as.$$.fragment),Ve=p(),$s=u("p"),$s.textContent=Ct,Ee=p(),o(Bs.$$.fragment),Ne=p(),gs=u("p"),gs.textContent=_t,Fe=p(),Zs=u("p"),Zs.textContent=kt,Ae=p(),o(Gs.$$.fragment),ve=p(),hl.c(),Me=p(),o(ns.$$.fragment),ze=p(),o(Ms.$$.fragment),xe=p(),o(Xs.$$.fragment),He=p(),Rs=u("p"),Rs.innerHTML=Qt,Ye=p(),Cl.c(),pe=p(),Ws=u("p"),Ws.textContent=$t,Se=p(),o(Vs.$$.fragment),De=p(),o(Es.$$.fragment),qe=p(),Ns=u("p"),Ns.innerHTML=Bt,Le=p(),o(Fs.$$.fragment),Ke=p(),o(As.$$.fragment),Pe=p(),vs=u("p"),vs.innerHTML=gt,Oe=p(),o(zs.$$.fragment),lt=p(),o(xs.$$.fragment),st=p(),Hs=u("p"),Hs.innerHTML=Zt,et=p(),o(Ys.$$.fragment),tt=p(),o(Ss.$$.fragment),at=p(),es.c(),Je=p(),W&&W.c(),Ue=p(),o(Ds.$$.fragment),nt=p(),be=u("p"),this.h()},l(l){const M=Ma("svelte-u9bgzb",document.head);a=j(M,"META",{name:!0,content:!0}),M.forEach(e),U=J(l),n=j(l,"P",{}),Tt(n).forEach(e),c=J(l),m(_.$$.fragment,l),f=J(l),m(B.$$.fragment,l),$=J(l),i.l(l),h=J(l),g=j(l,"P",{"data-svelte-h":!0}),b(g)!=="svelte-1t2m835"&&(g.innerHTML=E),V=J(l),X=j(l,"P",{"data-svelte-h":!0}),b(X)!=="svelte-z2rcsh"&&(X.innerHTML=k),Z=J(l),m(N.$$.fragment,l),Zl=J(l),F=j(l,"P",{"data-svelte-h":!0}),b(F)!=="svelte-ginij8"&&(F.innerHTML=ce),Gl=J(l),G=j(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),Tt(G).forEach(e),Xl=J(l),A=j(l,"P",{"data-svelte-h":!0}),b(A)!=="svelte-1k1xdgt"&&(A.innerHTML=Te),Rl=J(l),m(v.$$.fragment,l),Wl=J(l),z=j(l,"P",{"data-svelte-h":!0}),b(z)!=="svelte-13yypc1"&&(z.innerHTML=oe),Vl=J(l),x=j(l,"P",{"data-svelte-h":!0}),b(x)!=="svelte-1eicdj2"&&(x.innerHTML=Us),Jl=J(l),m(_l.$$.fragment,l),El=J(l),H=j(l,"P",{"data-svelte-h":!0}),b(H)!=="svelte-188avx"&&(H.textContent=rs),Ul=J(l),m(kl.$$.fragment,l),Nl=J(l),m(Y.$$.fragment,l),Fl=J(l),S=j(l,"P",{"data-svelte-h":!0}),b(S)!=="svelte-1eff2hf"&&(S.textContent=me),Al=J(l),m(D.$$.fragment,l),vl=J(l),q=j(l,"P",{"data-svelte-h":!0}),b(q)!=="svelte-1th7mdr"&&(q.textContent=we),zl=J(l),m(L.$$.fragment,l),xl=J(l),m(K.$$.fragment,l),Hl=J(l),P=j(l,"P",{"data-svelte-h":!0}),b(P)!=="svelte-xkdug1"&&(P.textContent=de),Yl=J(l),O=j(l,"P",{"data-svelte-h":!0}),b(O)!=="svelte-13urty5"&&(O.textContent=ys),rl=J(l),m(Ql.$$.fragment,l),Sl=J(l),m(ll.$$.fragment,l),Dl=J(l),sl=j(l,"P",{"data-svelte-h":!0}),b(sl)!=="svelte-zsplm1"&&(sl.textContent=cs),yl=J(l),m($l.$$.fragment,l),ql=J(l),m(el.$$.fragment,l),Ll=J(l),tl=j(l,"P",{"data-svelte-h":!0}),b(tl)!=="svelte-r7e49a"&&(tl.innerHTML=ue),Kl=J(l),m(al.$$.fragment,l),Pl=J(l),m(nl.$$.fragment,l),Ol=J(l),Ml=j(l,"P",{"data-svelte-h":!0}),b(Ml)!=="svelte-14jihz1"&&(Ml.textContent=je),ls=J(l),pl=j(l,"P",{"data-svelte-h":!0}),b(pl)!=="svelte-13spu2c"&&(pl.innerHTML=is),cl=J(l),ol=j(l,"DIV",{class:!0,"data-svelte-h":!0}),b(ol)!=="svelte-1mf8pz"&&(ol.innerHTML=Ts),il=J(l),Tl=j(l,"P",{"data-svelte-h":!0}),b(Tl)!=="svelte-1jx1efh"&&(Tl.textContent=Bl),ss=J(l),m(s.$$.fragment,l),T=J(l),m(os.$$.fragment,l),fe=J(l),ms=j(l,"P",{"data-svelte-h":!0}),b(ms)!=="svelte-1v8sp6o"&&(ms.innerHTML=mt),he=J(l),ws=j(l,"P",{"data-svelte-h":!0}),b(ws)!=="svelte-1lj93sp"&&(ws.innerHTML=wt),Ie=J(l),m(ds.$$.fragment,l),Ce=J(l),m(us.$$.fragment,l),_e=J(l),js=j(l,"P",{"data-svelte-h":!0}),b(js)!=="svelte-1vpqfku"&&(js.textContent=dt),ke=J(l),bs=j(l,"P",{"data-svelte-h":!0}),b(bs)!=="svelte-1bmkjtp"&&(bs.textContent=ut),Qe=J(l),m(ts.$$.fragment,l),$e=J(l),m(fs.$$.fragment,l),Be=J(l),hs=j(l,"P",{"data-svelte-h":!0}),b(hs)!=="svelte-3zroq5"&&(hs.innerHTML=jt),ge=J(l),wl.l(l),ee=J(l),Is=j(l,"P",{"data-svelte-h":!0}),b(Is)!=="svelte-15c2pk7"&&(Is.innerHTML=bt),Ze=J(l),Cs=j(l,"P",{"data-svelte-h":!0}),b(Cs)!=="svelte-sbssvb"&&(Cs.innerHTML=ft),Ge=J(l),ul.l(l),te=J(l),_s=j(l,"P",{"data-svelte-h":!0}),b(_s)!=="svelte-ccdb1w"&&(_s.textContent=ht),Xe=J(l),m(ks.$$.fragment,l),Re=J(l),bl.l(l),ae=J(l),Qs=j(l,"P",{"data-svelte-h":!0}),b(Qs)!=="svelte-18al98k"&&(Qs.textContent=It),We=J(l),R&&R.l(l),ne=J(l),m(as.$$.fragment,l),Ve=J(l),$s=j(l,"P",{"data-svelte-h":!0}),b($s)!=="svelte-14vmza0"&&($s.textContent=Ct),Ee=J(l),m(Bs.$$.fragment,l),Ne=J(l),gs=j(l,"P",{"data-svelte-h":!0}),b(gs)!=="svelte-ui4fcw"&&(gs.textContent=_t),Fe=J(l),Zs=j(l,"P",{"data-svelte-h":!0}),b(Zs)!=="svelte-v3d1p4"&&(Zs.textContent=kt),Ae=J(l),m(Gs.$$.fragment,l),ve=J(l),hl.l(l),Me=J(l),m(ns.$$.fragment,l),ze=J(l),m(Ms.$$.fragment,l),xe=J(l),m(Xs.$$.fragment,l),He=J(l),Rs=j(l,"P",{"data-svelte-h":!0}),b(Rs)!=="svelte-6igul9"&&(Rs.innerHTML=Qt),Ye=J(l),Cl.l(l),pe=J(l),Ws=j(l,"P",{"data-svelte-h":!0}),b(Ws)!=="svelte-p85nsj"&&(Ws.textContent=$t),Se=J(l),m(Vs.$$.fragment,l),De=J(l),m(Es.$$.fragment,l),qe=J(l),Ns=j(l,"P",{"data-svelte-h":!0}),b(Ns)!=="svelte-4055bd"&&(Ns.innerHTML=Bt),Le=J(l),m(Fs.$$.fragment,l),Ke=J(l),m(As.$$.fragment,l),Pe=J(l),vs=j(l,"P",{"data-svelte-h":!0}),b(vs)!=="svelte-11bk8i6"&&(vs.innerHTML=gt),Oe=J(l),m(zs.$$.fragment,l),lt=J(l),m(xs.$$.fragment,l),st=J(l),Hs=j(l,"P",{"data-svelte-h":!0}),b(Hs)!=="svelte-15u88hv"&&(Hs.innerHTML=Zt),et=J(l),m(Ys.$$.fragment,l),tt=J(l),m(Ss.$$.fragment,l),at=J(l),es.l(l),Je=J(l),W&&W.l(l),Ue=J(l),m(Ds.$$.fragment,l),nt=J(l),be=j(l,"P",{}),Tt(be).forEach(e),this.h()},h(){gl(a,"name","hf:doc:metadata"),gl(a,"content",Xa),ea(G.src,ie="https://course-demos-codeparrot-ds.hf.space")||gl(G,"src",ie),gl(G,"frameborder","0"),gl(G,"height","300"),gl(G,"title","Gradio app"),gl(G,"class","block dark:hidden container p-0 flex-grow space-iframe"),gl(G,"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"),gl(G,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),gl(ol,"class","flex justify-center")},m(l,M){pa(document.head,a),t(l,U,M),t(l,n,M),t(l,c,M),w(_,l,M),t(l,f,M),w(B,l,M),t(l,$,M),qs[Q].m(l,M),t(l,h,M),t(l,g,M),t(l,V,M),t(l,X,M),t(l,Z,M),w(N,l,M),t(l,Zl,M),t(l,F,M),t(l,Gl,M),t(l,G,M),t(l,Xl,M),t(l,A,M),t(l,Rl,M),w(v,l,M),t(l,Wl,M),t(l,z,M),t(l,Vl,M),t(l,x,M),t(l,Jl,M),w(_l,l,M),t(l,El,M),t(l,H,M),t(l,Ul,M),w(kl,l,M),t(l,Nl,M),w(Y,l,M),t(l,Fl,M),t(l,S,M),t(l,Al,M),w(D,l,M),t(l,vl,M),t(l,q,M),t(l,zl,M),w(L,l,M),t(l,xl,M),w(K,l,M),t(l,Hl,M),t(l,P,M),t(l,Yl,M),t(l,O,M),t(l,rl,M),w(Ql,l,M),t(l,Sl,M),w(ll,l,M),t(l,Dl,M),t(l,sl,M),t(l,yl,M),w($l,l,M),t(l,ql,M),w(el,l,M),t(l,Ll,M),t(l,tl,M),t(l,Kl,M),w(al,l,M),t(l,Pl,M),w(nl,l,M),t(l,Ol,M),t(l,Ml,M),t(l,ls,M),t(l,pl,M),t(l,cl,M),t(l,ol,M),t(l,il,M),t(l,Tl,M),t(l,ss,M),w(s,l,M),t(l,T,M),w(os,l,M),t(l,fe,M),t(l,ms,M),t(l,he,M),t(l,ws,M),t(l,Ie,M),w(ds,l,M),t(l,Ce,M),w(us,l,M),t(l,_e,M),t(l,js,M),t(l,ke,M),t(l,bs,M),t(l,Qe,M),w(ts,l,M),t(l,$e,M),w(fs,l,M),t(l,Be,M),t(l,hs,M),t(l,ge,M),Ls[ml].m(l,M),t(l,ee,M),t(l,Is,M),t(l,Ze,M),t(l,Cs,M),t(l,Ge,M),Ks[dl].m(l,M),t(l,te,M),t(l,_s,M),t(l,Xe,M),w(ks,l,M),t(l,Re,M),Ps[jl].m(l,M),t(l,ae,M),t(l,Qs,M),t(l,We,M),R&&R.m(l,M),t(l,ne,M),w(as,l,M),t(l,Ve,M),t(l,$s,M),t(l,Ee,M),w(Bs,l,M),t(l,Ne,M),t(l,gs,M),t(l,Fe,M),t(l,Zs,M),t(l,Ae,M),w(Gs,l,M),t(l,ve,M),Os[fl].m(l,M),t(l,Me,M),w(ns,l,M),t(l,ze,M),w(Ms,l,M),t(l,xe,M),w(Xs,l,M),t(l,He,M),t(l,Rs,M),t(l,Ye,M),le[Il].m(l,M),t(l,pe,M),t(l,Ws,M),t(l,Se,M),w(Vs,l,M),t(l,De,M),w(Es,l,M),t(l,qe,M),t(l,Ns,M),t(l,Le,M),w(Fs,l,M),t(l,Ke,M),w(As,l,M),t(l,Pe,M),t(l,vs,M),t(l,Oe,M),w(zs,l,M),t(l,lt,M),w(xs,l,M),t(l,st,M),t(l,Hs,M),t(l,et,M),w(Ys,l,M),t(l,tt,M),w(Ss,l,M),t(l,at,M),es.m(l,M),t(l,Je,M),W&&W.m(l,M),t(l,Ue,M),w(Ds,l,M),t(l,nt,M),t(l,be,M),Mt=!0},p(l,[M]){const Yt={};M&1&&(Yt.fw=l[0]),_.$set(Yt);let Jt=Q;Q=Xt(l),Q!==Jt&&(Js(),y(qs[Jt],1,1,()=>{qs[Jt]=null}),ps(),i=qs[Q],i||(i=qs[Q]=Gt[Q](l),i.c()),r(i,1),i.m(h.parentNode,h));const St={};M&2&&(St.$$scope={dirty:M,ctx:l}),ts.$set(St);let Ut=ml;ml=Wt(l),ml!==Ut&&(Js(),y(Ls[Ut],1,1,()=>{Ls[Ut]=null}),ps(),wl=Ls[ml],wl||(wl=Ls[ml]=Rt[ml](l),wl.c()),r(wl,1),wl.m(ee.parentNode,ee));let rt=dl;dl=Et(l),dl!==rt&&(Js(),y(Ks[rt],1,1,()=>{Ks[rt]=null}),ps(),ul=Ks[dl],ul||(ul=Ks[dl]=Vt[dl](l),ul.c()),r(ul,1),ul.m(te.parentNode,te));let yt=jl;jl=Ft(l),jl!==yt&&(Js(),y(Ps[yt],1,1,()=>{Ps[yt]=null}),ps(),bl=Ps[jl],bl||(bl=Ps[jl]=Nt[jl](l),bl.c()),r(bl,1),bl.m(ae.parentNode,ae)),l[0]==="tf"?R?M&1&&r(R,1):(R=Pt(),R.c(),r(R,1),R.m(ne.parentNode,ne)):R&&(Js(),y(R,1,1,()=>{R=null}),ps());const Dt={};M&2&&(Dt.$$scope={dirty:M,ctx:l}),as.$set(Dt);let ct=fl;fl=vt(l),fl!==ct&&(Js(),y(Os[ct],1,1,()=>{Os[ct]=null}),ps(),hl=Os[fl],hl||(hl=Os[fl]=At[fl](l),hl.c()),r(hl,1),hl.m(Me.parentNode,Me));const qt={};M&2&&(qt.$$scope={dirty:M,ctx:l}),ns.$set(qt);const Lt={};M&3&&(Lt.$$scope={dirty:M,ctx:l}),Ms.$set(Lt);let it=Il;Il=xt(l),Il!==it&&(Js(),y(le[it],1,1,()=>{le[it]=null}),ps(),Cl=le[Il],Cl||(Cl=le[Il]=zt[Il](l),Cl.c()),r(Cl,1),Cl.m(pe.parentNode,pe)),pt!==(pt=Ht(l))&&(es.d(1),es=pt(l),es&&(es.c(),es.m(Je.parentNode,Je))),l[0]==="pt"?W?M&1&&r(W,1):(W=Ot(l),W.c(),r(W,1),W.m(Ue.parentNode,Ue)):W&&(Js(),y(W,1,1,()=>{W=null}),ps())},i(l){Mt||(r(_.$$.fragment,l),r(B.$$.fragment,l),r(i),r(N.$$.fragment,l),r(v.$$.fragment,l),r(_l.$$.fragment,l),r(kl.$$.fragment,l),r(Y.$$.fragment,l),r(D.$$.fragment,l),r(L.$$.fragment,l),r(K.$$.fragment,l),r(Ql.$$.fragment,l),r(ll.$$.fragment,l),r($l.$$.fragment,l),r(el.$$.fragment,l),r(al.$$.fragment,l),r(nl.$$.fragment,l),r(s.$$.fragment,l),r(os.$$.fragment,l),r(ds.$$.fragment,l),r(us.$$.fragment,l),r(ts.$$.fragment,l),r(fs.$$.fragment,l),r(wl),r(ul),r(ks.$$.fragment,l),r(bl),r(R),r(as.$$.fragment,l),r(Bs.$$.fragment,l),r(Gs.$$.fragment,l),r(hl),r(ns.$$.fragment,l),r(Ms.$$.fragment,l),r(Xs.$$.fragment,l),r(Cl),r(Vs.$$.fragment,l),r(Es.$$.fragment,l),r(Fs.$$.fragment,l),r(As.$$.fragment,l),r(zs.$$.fragment,l),r(xs.$$.fragment,l),r(Ys.$$.fragment,l),r(Ss.$$.fragment,l),r(W),r(Ds.$$.fragment,l),Mt=!0)},o(l){y(_.$$.fragment,l),y(B.$$.fragment,l),y(i),y(N.$$.fragment,l),y(v.$$.fragment,l),y(_l.$$.fragment,l),y(kl.$$.fragment,l),y(Y.$$.fragment,l),y(D.$$.fragment,l),y(L.$$.fragment,l),y(K.$$.fragment,l),y(Ql.$$.fragment,l),y(ll.$$.fragment,l),y($l.$$.fragment,l),y(el.$$.fragment,l),y(al.$$.fragment,l),y(nl.$$.fragment,l),y(s.$$.fragment,l),y(os.$$.fragment,l),y(ds.$$.fragment,l),y(us.$$.fragment,l),y(ts.$$.fragment,l),y(fs.$$.fragment,l),y(wl),y(ul),y(ks.$$.fragment,l),y(bl),y(R),y(as.$$.fragment,l),y(Bs.$$.fragment,l),y(Gs.$$.fragment,l),y(hl),y(ns.$$.fragment,l),y(Ms.$$.fragment,l),y(Xs.$$.fragment,l),y(Cl),y(Vs.$$.fragment,l),y(Es.$$.fragment,l),y(Fs.$$.fragment,l),y(As.$$.fragment,l),y(zs.$$.fragment,l),y(xs.$$.fragment,l),y(Ys.$$.fragment,l),y(Ss.$$.fragment,l),y(W),y(Ds.$$.fragment,l),Mt=!1},d(l){l&&(e(U),e(n),e(c),e(f),e($),e(h),e(g),e(V),e(X),e(Z),e(Zl),e(F),e(Gl),e(G),e(Xl),e(A),e(Rl),e(Wl),e(z),e(Vl),e(x),e(Jl),e(El),e(H),e(Ul),e(Nl),e(Fl),e(S),e(Al),e(vl),e(q),e(zl),e(xl),e(Hl),e(P),e(Yl),e(O),e(rl),e(Sl),e(Dl),e(sl),e(yl),e(ql),e(Ll),e(tl),e(Kl),e(Pl),e(Ol),e(Ml),e(ls),e(pl),e(cl),e(ol),e(il),e(Tl),e(ss),e(T),e(fe),e(ms),e(he),e(ws),e(Ie),e(Ce),e(_e),e(js),e(ke),e(bs),e(Qe),e($e),e(Be),e(hs),e(ge),e(ee),e(Is),e(Ze),e(Cs),e(Ge),e(te),e(_s),e(Xe),e(Re),e(ae),e(Qs),e(We),e(ne),e(Ve),e($s),e(Ee),e(Ne),e(gs),e(Fe),e(Zs),e(Ae),e(ve),e(Me),e(ze),e(xe),e(He),e(Rs),e(Ye),e(pe),e(Ws),e(Se),e(De),e(qe),e(Ns),e(Le),e(Ke),e(Pe),e(vs),e(Oe),e(lt),e(st),e(Hs),e(et),e(tt),e(at),e(Je),e(Ue),e(nt),e(be)),e(a),d(_,l),d(B,l),qs[Q].d(l),d(N,l),d(v,l),d(_l,l),d(kl,l),d(Y,l),d(D,l),d(L,l),d(K,l),d(Ql,l),d(ll,l),d($l,l),d(el,l),d(al,l),d(nl,l),d(s,l),d(os,l),d(ds,l),d(us,l),d(ts,l),d(fs,l),Ls[ml].d(l),Ks[dl].d(l),d(ks,l),Ps[jl].d(l),R&&R.d(l),d(as,l),d(Bs,l),d(Gs,l),Os[fl].d(l),d(ns,l),d(Ms,l),d(Xs,l),le[Il].d(l),d(Vs,l),d(Es,l),d(Fs,l),d(As,l),d(zs,l),d(xs,l),d(Ys,l),d(Ss,l),es.d(l),W&&W.d(l),d(Ds,l)}}}const Xa='{"title":"从头开始训练因果语言模型","local":"从头开始训练因果语言模型","sections":[{"title":"收集数据","local":"收集数据","sections":[],"depth":2},{"title":"准备数据集","local":"准备数据集","sections":[],"depth":2},{"title":"初始化一个新模型","local":"初始化一个新模型","sections":[],"depth":2},{"title":"使用 pipeline 进行代码生成","local":"使用管道生成代码","sections":[],"depth":2},{"title":"使用🤗 Accelerate 进行训练","local":"使用🤗 Accelerate 进行训练","sections":[],"depth":2}],"depth":1}';function Ra(C,a,U){let n="pt";return ta(()=>{const c=new URLSearchParams(window.location.search);U(0,n=c.get("fw")||"pt")}),[n]}class xa extends aa{constructor(a){super(),na(this,a,Ra,Ga,sa,{})}}export{xa as component};
