import{s as TM,f as pM,o as iM,n as Al}from"../chunks/scheduler.37c15a92.js";import{S as wM,i as UM,g as d,s as M,r as y,A as yM,h as m,f as s,c as J,j as va,u as r,x as j,k as bl,y as rM,a as t,v as o,t as i,b as _l,d as p,w as c,p as kl}from"../chunks/index.2bf4358c.js";import{T as xl}from"../chunks/Tip.363c041f.js";import{Y as Wa}from"../chunks/Youtube.1e50a667.js";import{C as h}from"../chunks/CodeBlock.4e987730.js";import{D as JM}from"../chunks/DocNotebookDropdown.efc1fb7c.js";import{F as oM}from"../chunks/FrameworkSwitchCourse.8d4d4ab6.js";import{H as Sl,E as cM}from"../chunks/getInferenceSnippets.1837c472.js";function dM(f){let a,U;return a=new JM({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/section3_tf.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter7/section3_tf.ipynb"}]}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function mM(f){let a,U;return a=new JM({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/section3_pt.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter7/section3_pt.ipynb"}]}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function IM(f){let a,U='🙋 如果你对“掩码语言建模”和“预训练模型”这两个术语感到陌生，请回顾 <a href="/course/chapter1">第一章</a> ，我们在其中解释了所有这些核心概念，并附有视频！';return{c(){a=d("p"),a.innerHTML=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-1btttrs"&&(a.innerHTML=U)},m(n,w){t(n,a,w)},p:Al,d(n){n&&s(a)}}}function jM(f){let a,U="让我们继续，我们可以使用 <code>TFAutoModelForMaskedLM</code> 类下载 DistilBERT：",n,w,B,I,R="然后，我们可以通过调用 <code>summary()</code> 方法查看模型有多少参数：",$,C,V,k,Z;return w=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRGQXV0b01vZGVsRm9yTWFza2VkTE0lMEElMEFtb2RlbF9jaGVja3BvaW50JTIwJTNEJTIwJTIyZGlzdGlsYmVydC1iYXNlLXVuY2FzZWQlMjIlMEFtb2RlbCUyMCUzRCUyMFRGQXV0b01vZGVsRm9yTWFza2VkTE0uZnJvbV9wcmV0cmFpbmVkKG1vZGVsX2NoZWNrcG9pbnQp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForMaskedLM

model_checkpoint = <span class="hljs-string">&quot;distilbert-base-uncased&quot;</span>
model = TFAutoModelForMaskedLM.from_pretrained(model_checkpoint)`,wrap:!1}}),C=new h({props:{code:"bW9kZWwobW9kZWwuZHVtbXlfaW5wdXRzKSUyMCUyMCUyMyUyMCVFNiU5RSU4NCVFNSVCQiVCQSVFNiVBOCVBMSVFNSU5RSU4QiUwQW1vZGVsLnN1bW1hcnkoKQ==",highlighted:`model(model.dummy_inputs)  <span class="hljs-comment"># 构建模型</span>
model.summary()`,wrap:!1}}),k=new h({props:{code:"TW9kZWwlM0ElMjAlMjJ0Zl9kaXN0aWxfYmVydF9mb3JfbWFza2VkX2xtJTIyJTBBX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18lMEElMjBMYXllciUyMCh0eXBlKSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyM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Qzk4NSUyQzUzMCUwQVRyYWluYWJsZSUyMHBhcmFtcyUzQSUyMDY2JTJDOTg1JTJDNTMwJTBBTm9uLXRyYWluYWJsZSUyMHBhcmFtcyUzQSUyMDAlMEFfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fXw==",highlighted:`Model: <span class="hljs-string">&quot;tf_distil_bert_for_masked_lm&quot;</span>
_________________________________________________________________
 Layer (<span class="hljs-built_in">type</span>)                Output Shape              Param <span class="hljs-comment">#   </span>
=================================================================
 distilbert (TFDistilBertMai  multiple                 <span class="hljs-number">66362880</span>  
 nLayer)                                                         
                                                                 
 vocab_transform (Dense)     multiple                  <span class="hljs-number">590592</span>    
                                                                 
 vocab_layer_norm (LayerNorm  multiple                 <span class="hljs-number">1536</span>      
 alization)                                                      
                                                                 
 vocab_projector (TFDistilBe  multiple                 <span class="hljs-number">23866170</span>  
 rtLMHead)                                                       
                                                                 
=================================================================
Total params: <span class="hljs-number">66</span>,<span class="hljs-number">985</span>,<span class="hljs-number">530</span>
Trainable params: <span class="hljs-number">66</span>,<span class="hljs-number">985</span>,<span class="hljs-number">530</span>
Non-trainable params: <span class="hljs-number">0</span>
_________________________________________________________________`,wrap:!1}}),{c(){a=d("p"),a.innerHTML=U,n=M(),y(w.$$.fragment),B=M(),I=d("p"),I.innerHTML=R,$=M(),y(C.$$.fragment),V=M(),y(k.$$.fragment)},l(u){a=m(u,"P",{"data-svelte-h":!0}),j(a)!=="svelte-d6d97j"&&(a.innerHTML=U),n=J(u),r(w.$$.fragment,u),B=J(u),I=m(u,"P",{"data-svelte-h":!0}),j(I)!=="svelte-m0n4e3"&&(I.innerHTML=R),$=J(u),r(C.$$.fragment,u),V=J(u),r(k.$$.fragment,u)},m(u,_){t(u,a,_),t(u,n,_),o(w,u,_),t(u,B,_),t(u,I,_),t(u,$,_),o(C,u,_),t(u,V,_),o(k,u,_),Z=!0},i(u){Z||(p(w.$$.fragment,u),p(C.$$.fragment,u),p(k.$$.fragment,u),Z=!0)},o(u){i(w.$$.fragment,u),i(C.$$.fragment,u),i(k.$$.fragment,u),Z=!1},d(u){u&&(s(a),s(n),s(B),s(I),s($),s(V)),c(w,u),c(C,u),c(k,u)}}}function uM(f){let a,U="让我们继续，我们可以使用 <code>AutoModelForMaskedLM</code> 类下载 DistilBERT：",n,w,B,I,R="然后，我们可以通过调用 <code>num_parameters()</code> 方法查看模型有多少参数：",$,C,V,k,Z;return w=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvck1hc2tlZExNJTBBJTBBbW9kZWxfY2hlY2twb2ludCUyMCUzRCUyMCUyMmRpc3RpbGJlcnQtYmFzZS11bmNhc2VkJTIyJTBBbW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JNYXNrZWRMTS5mcm9tX3ByZXRyYWluZWQobW9kZWxfY2hlY2twb2ludCk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForMaskedLM

model_checkpoint = <span class="hljs-string">&quot;distilbert-base-uncased&quot;</span>
model = AutoModelForMaskedLM.from_pretrained(model_checkpoint)`,wrap:!1}}),C=new h({props:{code:"ZGlzdGlsYmVydF9udW1fcGFyYW1ldGVycyUyMCUzRCUyMG1vZGVsLm51bV9wYXJhbWV0ZXJzKCklMjAlMkYlMjAxXzAwMF8wMDAlMEFwcmludChmJTIyJyUzRSUzRSUzRSUyMERpc3RpbEJFUlQlMjBudW1iZXIlMjBvZiUyMHBhcmFtZXRlcnMlM0ElMjAlN0Jyb3VuZChkaXN0aWxiZXJ0X251bV9wYXJhbWV0ZXJzKSU3RE0nJTIyKSUwQXByaW50KGYlMjInJTNFJTNFJTNFJTIwQkVSVCUyMG51bWJlciUyMG9mJTIwcGFyYW1ldGVycyUzQSUyMDExME0nJTIyKQ==",highlighted:`distilbert_num_parameters = model.num_parameters() / <span class="hljs-number">1_000_000</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&#x27;&gt;&gt;&gt; DistilBERT number of parameters: <span class="hljs-subst">{<span class="hljs-built_in">round</span>(distilbert_num_parameters)}</span>M&#x27;&quot;</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&#x27;&gt;&gt;&gt; BERT number of parameters: 110M&#x27;&quot;</span>)`,wrap:!1}}),k=new h({props:{code:"JyUzRSUzRSUzRSUyMERpc3RpbEJFUlQlMjBudW1iZXIlMjBvZiUyMHBhcmFtZXRlcnMlM0ElMjA2N00nJTBBJyUzRSUzRSUzRSUyMEJFUlQlMjBudW1iZXIlMjBvZiUyMHBhcmFtZXRlcnMlM0ElMjAxMTBNJw==",highlighted:`<span class="hljs-string">&#x27;&gt;&gt;&gt; DistilBERT number of parameters: 67M&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; BERT number of parameters: 110M&#x27;</span>`,wrap:!1}}),{c(){a=d("p"),a.innerHTML=U,n=M(),y(w.$$.fragment),B=M(),I=d("p"),I.innerHTML=R,$=M(),y(C.$$.fragment),V=M(),y(k.$$.fragment)},l(u){a=m(u,"P",{"data-svelte-h":!0}),j(a)!=="svelte-192xbl5"&&(a.innerHTML=U),n=J(u),r(w.$$.fragment,u),B=J(u),I=m(u,"P",{"data-svelte-h":!0}),j(I)!=="svelte-d41bcm"&&(I.innerHTML=R),$=J(u),r(C.$$.fragment,u),V=J(u),r(k.$$.fragment,u)},m(u,_){t(u,a,_),t(u,n,_),o(w,u,_),t(u,B,_),t(u,I,_),t(u,$,_),o(C,u,_),t(u,V,_),o(k,u,_),Z=!0},i(u){Z||(p(w.$$.fragment,u),p(C.$$.fragment,u),p(k.$$.fragment,u),Z=!0)},o(u){i(w.$$.fragment,u),i(C.$$.fragment,u),i(k.$$.fragment,u),Z=!1},d(u){u&&(s(a),s(n),s(B),s(I),s($),s(V)),c(w,u),c(C,u),c(k,u)}}}function hM(f){let a,U;return a=new h({props:{code:"aW1wb3J0JTIw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cmdzb3J0KC1tYXNrX3Rva2VuX2xvZ2l0cyklNUIlM0E1JTVELnRvbGlzdCgpJTBBJTBBZm9yJTIwdG9rZW4lMjBpbiUyMHRvcF81X3Rva2VucyUzQSUwQSUyMCUyMCUyMCUyMHByaW50KGYlMjIlM0UlM0UlM0UlMjAlN0J0ZXh0LnJlcGxhY2UodG9rZW5pemVyLm1hc2tfdG9rZW4lMkMlMjB0b2tlbml6ZXIuZGVjb2RlKCU1QnRva2VuJTVEKSklN0QlMjIp",highlighted:`<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf

inputs = tokenizer(text, return_tensors=<span class="hljs-string">&quot;np&quot;</span>)
token_logits = model(**inputs).logits
<span class="hljs-comment"># 找到 [MASK] 的位置并提取其 logits</span>
mask_token_index = np.argwhere(inputs[<span class="hljs-string">&quot;input_ids&quot;</span>] == tokenizer.mask_token_id)[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>]
mask_token_logits = token_logits[<span class="hljs-number">0</span>, mask_token_index, :]
<span class="hljs-comment"># 选择具有最高 logits 的 [MASK] 候选词</span>
<span class="hljs-comment"># 通过在 argsort 前对数组取负,来得到最大的 logits</span>
top_5_tokens = np.argsort(-mask_token_logits)[:<span class="hljs-number">5</span>].tolist()

<span class="hljs-keyword">for</span> token <span class="hljs-keyword">in</span> top_5_tokens:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&gt;&gt;&gt; <span class="hljs-subst">{text.replace(tokenizer.mask_token, tokenizer.decode([token]))}</span>&quot;</span>)`,wrap:!1}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function bM(f){let a,U;return a=new h({props:{code:"aW1wb3J0JTIwdG9yY2glMEElMEFpbnB1dHMlMjAlM0QlMjB0b2tlbml6ZXIodGV4dCUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIycHQlMjIpJTBBdG9rZW5fbG9naXRzJTIwJTNEJTIwbW9kZWwoKippbnB1dHMpLmxvZ2l0cyUwQSUyMyUyMCVFNiU4OSVCRSVFNSU4OCVCMCUyMCU1Qk1BU0slNUQlMjAlRTclOUElODQlRTQlQkQlOEQlRTclQkQlQUUlRTUlQjklQjYlRTYlOEYlOTAlRTUlOEYlOTYlRTUlODUlQjYlMjBsb2dpdHMlMEFtYXNrX3Rva2VuX2luZGV4JTIwJTNEJTIwdG9yY2gud2hlcmUoaW5wdXRzJTVCJTIyaW5wdXRfaWRzJTIyJTVEJTIwJTNEJTNEJTIwdG9rZW5pemVyLm1hc2tfdG9rZW5faWQpJTVCMSU1RCUwQW1hc2tfdG9rZW5fbG9naXRzJTIwJTNEJTIwdG9rZW5fbG9naXRzJTVCMCUyQyUyMG1hc2tfdG9rZW5faW5kZXglMkMlMjAlM0ElNUQlMEElMjMlMjAlRTklODAlODklRTYlOEIlQTklRTUlODUlQjclRTYlOUMlODklRTYlOUMlODAlRTklQUIlOTglMjBsb2dpdHMlMjAlRTclOUElODQlMjAlNUJNQVNLJTVEJTIwJUU1JTgwJTk5JUU5JTgwJTg5JUU4JUFGJThEJTBBdG9wXzVfdG9rZW5zJTIwJTNEJTIwdG9yY2gudG9wayhtYXNrX3Rva2VuX2xvZ2l0cyUyQyUyMDUlMkMlMjBkaW0lM0QxKS5pbmRpY2VzJTVCMCU1RC50b2xpc3QoKSUwQSUwQWZvciUyMHRva2VuJTIwaW4lMjB0b3BfNV90b2tlbnMlM0ElMEElMjAlMjAlMjAlMjBwcmludChmJTIyJyUzRSUzRSUzRSUyMCU3QnRleHQucmVwbGFjZSh0b2tlbml6ZXIubWFza190b2tlbiUyQyUyMHRva2VuaXplci5kZWNvZGUoJTVCdG9rZW4lNUQpKSU3RCclMjIp",highlighted:`<span class="hljs-keyword">import</span> torch

inputs = tokenizer(text, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)
token_logits = model(**inputs).logits
<span class="hljs-comment"># 找到 [MASK] 的位置并提取其 logits</span>
mask_token_index = torch.where(inputs[<span class="hljs-string">&quot;input_ids&quot;</span>] == tokenizer.mask_token_id)[<span class="hljs-number">1</span>]
mask_token_logits = token_logits[<span class="hljs-number">0</span>, mask_token_index, :]
<span class="hljs-comment"># 选择具有最高 logits 的 [MASK] 候选词</span>
top_5_tokens = torch.topk(mask_token_logits, <span class="hljs-number">5</span>, dim=<span class="hljs-number">1</span>).indices[<span class="hljs-number">0</span>].tolist()

<span class="hljs-keyword">for</span> token <span class="hljs-keyword">in</span> top_5_tokens:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&#x27;&gt;&gt;&gt; <span class="hljs-subst">{text.replace(tokenizer.mask_token, tokenizer.decode([token]))}</span>&#x27;&quot;</span>)`,wrap:!1}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function fM(f){let a,U="✏️ <strong>试一试！</strong> 创建一个 <code>unsupervised</code> 部分的随机样本，并验证其标签既不是 <code>0</code> 也不是 <code>1</code> 。或者，你也可以检查 <code>train</code> 和 <code>test</code> 部分的标签确实是 <code>0</code> 或 <code>1</code> —— 每个 NLP 实践者在开始新项目时都应该对数据标注进行的有用的、合理的检查！";return{c(){a=d("p"),a.innerHTML=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-xmutc3"&&(a.innerHTML=U)},m(n,w){t(n,a,w)},p:Al,d(n){n&&s(a)}}}function CM(f){let a,U='✏️ <strong>试试看！</strong> 一些 Transformer 模型，例如 <a href="https://huggingface.co/google/bigbird-roberta-base" rel="nofollow">BigBird</a> 和 <a href="hf.co/allenai/longformer-base-4096">Longformer</a> 具有比 BERT 和其他早期 Transformer 模型更长的上下文长度。选择一个 <code>checkpoint</code> 来实例化 <code>tokenizer</code> 并验证 <code>model_max_length</code> 是否与模型卡上标注的大小一致。';return{c(){a=d("p"),a.innerHTML=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-p2ymzx"&&(a.innerHTML=U)},m(n,w){t(n,a,w)},p:Al,d(n){n&&s(a)}}}function GM(f){let a,U="注意，在实际应用场景中，使用小的块可能会有丢失长句子之间的语义信息从而对最终模型的性能产生不利的影响，所以如果显存条件允许的话，你应该选择一个与你将要使用模型的相匹配的大小。";return{c(){a=d("p"),a.textContent=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-zmdzzq"&&(a.textContent=U)},m(n,w){t(n,a,w)},p:Al,d(n){n&&s(a)}}}function BM(f){let a,U="✏️ <strong>试一试！</strong> 多运行上面的代码片段几次，亲眼看看随机遮蔽的效果！也可以用 <code>tokenizer.convert_ids_to_tokens()</code> 替换 <code>tokenizer.decode()</code> 方法，看看只把一个给定单词的单个 token 遮蔽，而保持这个单词其他 tokens 不变的效果。";return{c(){a=d("p"),a.innerHTML=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-1sbubvt"&&(a.innerHTML=U)},m(n,w){t(n,a,w)},p:Al,d(n){n&&s(a)}}}function aM(f){let a,U="随机掩码的一个缺点是，当使用 <code>Trainer</code> 时，每次计算出来的评估结果会有些许不同，即使我们会对训练集和测试集使用相同的数据整理器。稍后，我们在学习使用 🤗 Accelerate 进行微调时， 就会看到如何利用灵活的自定义评估循环的来冻结随机性。";return{c(){a=d("p"),a.innerHTML=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-brwgnr"&&(a.innerHTML=U)},m(n,w){t(n,a,w)},d(n){n&&s(a)}}}function gM(f){let a,U;return a=new h({props:{code:"aW1wb3J0JTIwY29sbGVjdGlvbnMlMEFpbXBvcnQlMjBudW1weSUyMGFzJTIwbnAlMEElMEFmcm9tJTIwdHJhbnNmb3JtZXJzLmRhdG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CUyMGZvciUyMHdvcmRfaWQlMjBpbiUyMG5wLndoZXJlKG1hc2spJTVCMCU1RCUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHdvcmRfaWQlMjAlM0QlMjB3b3JkX2lkLml0ZW0oKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZvciUyMGlkeCUyMGluJTIwbWFwcGluZyU1QndvcmRfaWQlNUQlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBuZXdfbGFiZWxzJTVCaWR4JTVEJTIwJTNEJTIwbGFiZWxzJTVCaWR4JTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaW5wdXRfaWRzJTVCaWR4JTVEJTIwJTNEJTIwdG9rZW5pemVyLm1hc2tfdG9rZW5faWQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlJTVCJTIybGFiZWxzJTIyJTVEJTIwJTNEJTIwbmV3X2xhYmVscyUwQSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMHRmX2RlZmF1bHRfZGF0YV9jb2xsYXRvcihmZWF0dXJlcyk=",highlighted:`<span class="hljs-keyword">import</span> collections
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-keyword">from</span> transformers.data.data_collator <span class="hljs-keyword">import</span> tf_default_data_collator

wwm_probability = <span class="hljs-number">0.2</span>


<span class="hljs-keyword">def</span> <span class="hljs-title function_">whole_word_masking_data_collator</span>(<span class="hljs-params">features</span>):
    <span class="hljs-keyword">for</span> feature <span class="hljs-keyword">in</span> features:
        word_ids = feature.pop(<span class="hljs-string">&quot;word_ids&quot;</span>)

        <span class="hljs-comment"># 创建一个单词与对应 token 索引之间的映射</span>
        mapping = collections.defaultdict(<span class="hljs-built_in">list</span>)
        current_word_index = -<span class="hljs-number">1</span>
        current_word = <span class="hljs-literal">None</span>
        <span class="hljs-keyword">for</span> idx, word_id <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(word_ids):
            <span class="hljs-keyword">if</span> word_id <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
                <span class="hljs-keyword">if</span> word_id != current_word:
                    current_word = word_id
                    current_word_index += <span class="hljs-number">1</span>
                mapping[current_word_index].append(idx)

        <span class="hljs-comment"># 随机遮蔽单词</span>
        mask = np.random.binomial(<span class="hljs-number">1</span>, wwm_probability, (<span class="hljs-built_in">len</span>(mapping),))
        input_ids = feature[<span class="hljs-string">&quot;input_ids&quot;</span>]
        labels = feature[<span class="hljs-string">&quot;labels&quot;</span>]
        new_labels = [-<span class="hljs-number">100</span>] * <span class="hljs-built_in">len</span>(labels)
        <span class="hljs-keyword">for</span> word_id <span class="hljs-keyword">in</span> np.where(mask)[<span class="hljs-number">0</span>]:
            word_id = word_id.item()
            <span class="hljs-keyword">for</span> idx <span class="hljs-keyword">in</span> mapping[word_id]:
                new_labels[idx] = labels[idx]
                input_ids[idx] = tokenizer.mask_token_id
        feature[<span class="hljs-string">&quot;labels&quot;</span>] = new_labels

    <span class="hljs-keyword">return</span> tf_default_data_collator(features)`,wrap:!1}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function _M(f){let a,U;return a=new h({props:{code:"aW1wb3J0JTIwY29sbGVjdGlvbnMlMEFpbXBvcnQlMjBudW1weSUyMGFzJTIwbnAlMEElMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwZGVmYXVsdF9kYXRhX2NvbGxhdG9yJTBBJTBBd3dtX3Byb2JhYmlsaXR5JTIwJTNEJTIwMC4yJTBBJTBBJTBBZGVmJTI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CUyMHdvcmRfaWQuaXRlbSgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZm9yJTIwaWR4JTIwaW4lMjBtYXBwaW5nJTVCd29yZF9pZCU1RCUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG5ld19sYWJlbHMlNUJpZHglNUQlMjAlM0QlMjBsYWJlbHMlNUJpZHglNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpbnB1dF9pZHMlNUJpZHglNUQlMjAlM0QlMjB0b2tlbml6ZXIubWFza190b2tlbl9pZCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmUlNUIlMjJsYWJlbHMlMjIlNUQlMjAlM0QlMjBuZXdfbGFiZWxzJTBBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwZGVmYXVsdF9kYXRhX2NvbGxhdG9yKGZlYXR1cmVzKQ==",highlighted:`<span class="hljs-keyword">import</span> collections
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> default_data_collator

wwm_probability = <span class="hljs-number">0.2</span>


<span class="hljs-keyword">def</span> <span class="hljs-title function_">whole_word_masking_data_collator</span>(<span class="hljs-params">features</span>):
    <span class="hljs-keyword">for</span> feature <span class="hljs-keyword">in</span> features:
        word_ids = feature.pop(<span class="hljs-string">&quot;word_ids&quot;</span>)

        <span class="hljs-comment"># 创建一个单词与对应 token 索引之间的映射</span>
        mapping = collections.defaultdict(<span class="hljs-built_in">list</span>)
        current_word_index = -<span class="hljs-number">1</span>
        current_word = <span class="hljs-literal">None</span>
        <span class="hljs-keyword">for</span> idx, word_id <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(word_ids):
            <span class="hljs-keyword">if</span> word_id <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
                <span class="hljs-keyword">if</span> word_id != current_word:
                    current_word = word_id
                    current_word_index += <span class="hljs-number">1</span>
                mapping[current_word_index].append(idx)

        <span class="hljs-comment"># 随机遮蔽单词</span>
        mask = np.random.binomial(<span class="hljs-number">1</span>, wwm_probability, (<span class="hljs-built_in">len</span>(mapping),))
        input_ids = feature[<span class="hljs-string">&quot;input_ids&quot;</span>]
        labels = feature[<span class="hljs-string">&quot;labels&quot;</span>]
        new_labels = [-<span class="hljs-number">100</span>] * <span class="hljs-built_in">len</span>(labels)
        <span class="hljs-keyword">for</span> word_id <span class="hljs-keyword">in</span> np.where(mask)[<span class="hljs-number">0</span>]:
            word_id = word_id.item()
            <span class="hljs-keyword">for</span> idx <span class="hljs-keyword">in</span> mapping[word_id]:
                new_labels[idx] = labels[idx]
                input_ids[idx] = tokenizer.mask_token_id
        feature[<span class="hljs-string">&quot;labels&quot;</span>] = new_labels

    <span class="hljs-keyword">return</span> default_data_collator(features)`,wrap:!1}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function kM(f){let a,U="✏️ <strong>试试看！</strong> 多次运行上面的代码片段，亲眼看看随机遮蔽的效果！也可以将 <code>tokenizer.decode()</code> 方法替换为 <code>tokenizer.convert_ids_to_tokens()</code> ，可以观察到给定单词的所有 tokens 总是被一起遮蔽。";return{c(){a=d("p"),a.innerHTML=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-1nibq2c"&&(a.innerHTML=U)},m(n,w){t(n,a,w)},p:Al,d(n){n&&s(a)}}}function ZM(f){let a,U="登陆后，我们可以指定 <code>Trainer</code> 的参数：",n,w,B,I,R="在这里，我们调整了一些默认选项，包括 <code>logging_steps</code> ，以确保我们可以跟踪每个 epoch 的训练损失。我们还使用了 <code>fp16=True</code> 来实现混合精度训练，从而进一步提高训练速度。默认情况下， <code>Trainer</code> 将删除模型的 <code>forward()</code> 方法中未使用的列。这意味着，如果你使用全词屏蔽（whole word masking）数据整理器，你还需要设置 <code>remove_unused_columns=False</code> ，以确保我们不会在训练期间丢失 <code>word_ids</code> 列。",$,C,V='请注意，你可以使用 <code>hub_model_id</code> 参数指定你想推送到的仓库的名称（如果你想把它推送到一个组织，就必须使用这个参数）。例如，当我们将模型推送到 <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code> 组织</a> 时，就在 <code>TrainingArguments</code> 中添加了 <code>hub_model_id=&quot;huggingface-course/distilbert-finetuned-imdb&quot;</code> 。默认情况下，使用的仓库将保存在你的账户中并以你设置的输出目录命名，因此在我们的示例中，它将是 <code>&quot;lewtun/distilbert-finetuned-imdb&quot;</code> 。',k,Z,u="现在，我们拥有了初始化 <code>Trainer</code> 所需的所有要素。这里我们只使用了标准的 <code>data_collator</code> ，但你可以尝试使用全词屏蔽作为数据整理器的一个练习，并对比一下不同屏蔽方式的结果有什么不同：",_,Q,X,G,W="我们现在准备运行 <code>trainer.train()</code> —— 但在此之前让我们简要地看一下 <code>困惑度（perplexity）</code> ，这是评估语言模型性能常用的指标。",S;return w=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluaW5nQXJndW1lbnRzJTBBJTBBYmF0Y2hfc2l6ZSUyMCUzRCUyMDY0JTBBJTIzJTIwJUU1JTlDJUE4JUU2JUFGJThGJUU0JUI4JUFBJTIwZXBvY2glMjAlRTglQkUlOTMlRTUlODclQkElRTglQUUlQUQlRTclQkIlODMlRTclOUElODQlMjBsb3NzJTBBbG9nZ2luZ19zdGVwcyUyMCUzRCUyMGxlbihkb3duc2FtcGxlZF9kYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQpJTIwJTJGJTJGJTIwYmF0Y2hfc2l6ZSUwQW1vZGVsX25hbWUlMjAlM0QlMjBtb2RlbF9jaGVja3BvaW50LnNwbGl0KCUyMiUyRiUyMiklNUItMSU1RCUwQSUwQXRyYWluaW5nX2FyZ3MlMjAlM0QlMjBUcmFpbmluZ0FyZ3VtZW50cyglMEElMjAlMjAlMjAlMjBvdXRwdXRfZGlyJTNEZiUyMiU3Qm1vZGVsX25hbWUlN0QtZmluZXR1bmVkLWltZGIlMjIlMkMlMEElMjAlMjAlMjAlMjBvdmVyd3JpdGVfb3V0cHV0X2RpciUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjBldmFsdWF0aW9uX3N0cmF0ZWd5JTNEJTIyZXBvY2glMjIlMkMlMEElMjAlMjAlMjAlMjBsZWFybmluZ19yYXRlJTNEMmUtNSUyQyUwQSUyMCUyMCUyMCUyMHdlaWdodF9kZWNheSUzRDAuMDElMkMlMEElMjAlMjAlMjAlMjBwZXJfZGV2aWNlX3RyYWluX2JhdGNoX3NpemUlM0RiYXRjaF9zaXplJTJDJTBBJTIwJTIwJTIwJTIwcGVyX2RldmljZV9ldmFsX2JhdGNoX3NpemUlM0RiYXRjaF9zaXplJTJDJTBBJTIwJTIwJTIwJTIwcHVzaF90b19odWIlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwZnAxNiUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjBsb2dnaW5nX3N0ZXBzJTNEbG9nZ2luZ19zdGVwcyUyQyUwQSk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments

batch_size = <span class="hljs-number">64</span>
<span class="hljs-comment"># 在每个 epoch 输出训练的 loss</span>
logging_steps = <span class="hljs-built_in">len</span>(downsampled_dataset[<span class="hljs-string">&quot;train&quot;</span>]) // batch_size
model_name = model_checkpoint.split(<span class="hljs-string">&quot;/&quot;</span>)[-<span class="hljs-number">1</span>]

training_args = TrainingArguments(
    output_dir=<span class="hljs-string">f&quot;<span class="hljs-subst">{model_name}</span>-finetuned-imdb&quot;</span>,
    overwrite_output_dir=<span class="hljs-literal">True</span>,
    evaluation_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
    learning_rate=<span class="hljs-number">2e-5</span>,
    weight_decay=<span class="hljs-number">0.01</span>,
    per_device_train_batch_size=batch_size,
    per_device_eval_batch_size=batch_size,
    push_to_hub=<span class="hljs-literal">True</span>,
    fp16=<span class="hljs-literal">True</span>,
    logging_steps=logging_steps,
)`,wrap:!1}}),Q=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluZXIlMEElMEF0cmFpbmVyJTIwJTNEJTIwVHJhaW5lciglMEElMjAlMjAlMjAlMjBtb2RlbCUzRG1vZGVsJTJDJTBBJTIwJTIwJTIwJTIwYXJncyUzRHRyYWluaW5nX2FyZ3MlMkMlMEElMjAlMjAlMjAlMjB0cmFpbl9kYXRhc2V0JTNEZG93bnNhbXBsZWRfZGF0YXNldCU1QiUyMnRyYWluJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwZXZhbF9kYXRhc2V0JTNEZG93bnNhbXBsZWRfZGF0YXNldCU1QiUyMnRlc3QlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBkYXRhX2NvbGxhdG9yJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMHRva2VuaXplciUzRHRva2VuaXplciUyQyUwQSk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=downsampled_dataset[<span class="hljs-string">&quot;train&quot;</span>],
    eval_dataset=downsampled_dataset[<span class="hljs-string">&quot;test&quot;</span>],
    data_collator=data_collator,
    tokenizer=tokenizer,
)`,wrap:!1}}),{c(){a=d("p"),a.innerHTML=U,n=M(),y(w.$$.fragment),B=M(),I=d("p"),I.innerHTML=R,$=M(),C=d("p"),C.innerHTML=V,k=M(),Z=d("p"),Z.innerHTML=u,_=M(),y(Q.$$.fragment),X=M(),G=d("p"),G.innerHTML=W},l(g){a=m(g,"P",{"data-svelte-h":!0}),j(a)!=="svelte-sgiu7t"&&(a.innerHTML=U),n=J(g),r(w.$$.fragment,g),B=J(g),I=m(g,"P",{"data-svelte-h":!0}),j(I)!=="svelte-x735wp"&&(I.innerHTML=R),$=J(g),C=m(g,"P",{"data-svelte-h":!0}),j(C)!=="svelte-13x7huo"&&(C.innerHTML=V),k=J(g),Z=m(g,"P",{"data-svelte-h":!0}),j(Z)!=="svelte-1dk9c28"&&(Z.innerHTML=u),_=J(g),r(Q.$$.fragment,g),X=J(g),G=m(g,"P",{"data-svelte-h":!0}),j(G)!=="svelte-18ib3w"&&(G.innerHTML=W)},m(g,N){t(g,a,N),t(g,n,N),o(w,g,N),t(g,B,N),t(g,I,N),t(g,$,N),t(g,C,N),t(g,k,N),t(g,Z,N),t(g,_,N),o(Q,g,N),t(g,X,N),t(g,G,N),S=!0},i(g){S||(p(w.$$.fragment,g),p(Q.$$.fragment,g),S=!0)},o(g){i(w.$$.fragment,g),i(Q.$$.fragment,g),S=!1},d(g){g&&(s(a),s(n),s(B),s(I),s($),s(C),s(k),s(Z),s(_),s(X),s(G)),c(w,g),c(Q,g)}}}function RM(f){let a,U="登录后，我们可以创建我们的 <code>tf.data</code> 数据集。我们将使用 <code>prepare_tf_dataset()</code> 方法，该方法会根据所使用的模型自动推断哪些列应该存入数据集。如果你想准确控制要使用的列，可以改用 <code>Dataset.to_tf_dataset()</code> 方法。为了简单起见，我们在这里只使用标准数据整理器，但你也可以尝试全词屏蔽（whole word masking）数据整理器，并作为一个练习对比一下效果：",n,w,B,I,R="接下来我们使用🤗 Transformers 库的 <code>create_optimizer()</code> 函数来设置超参数并编译模型，该函数提供了一个带有线性学习率衰减的 <code>AdamW</code> 优化器。在 <code>compile()</code> 的参数中我们没有指定损失函数，这代表使用模型内置的默认损失函数，然后我们将训练精度设置为<code>mixed_ﬂoat16</code> 。注意，如果你使用的是 Colab GPU 或者其他不支持 float16 加速的 GPU，你可能应该注释掉这一行。",$,C,V='另外，我们还设置了一个 <code>PushToHubCallback</code> ，它将在每个 epoch 后将模型保存到 Hub。你可以使用 <code>hub_model_id</code> 参数指定你想推送到的仓库的名称（如果你想把它推送到一个组织，你必须使用这个参数）。例如，要将模型推送到 <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code> 组织</a> ，就需要添加 <code>hub_model_id=&quot;huggingface-course/distilbert-finetuned-imdb&quot;</code> 。在默认的情况下，模型的仓库将保存在你的账户中，并以你设置的输出目录命名，所以在我们的示例中，该仓库的名称是 <code>lewtun/distilbert-finetuned-imdb</code> 。',k,Z,u,_,Q="我们现在已经准备好运行 <code>model.fit()</code> 了 —— 但在此之前，让我们先简单了解一下 <code>困惑度（perplexity）</code> ，它是一种常用的评估语言模型性能的指标。",X;return w=new h({props:{code:"dGZfdHJhaW5fZGF0YXNldCUyMCUzRCUyMG1vZGVsLnByZXBhcmVfdGZfZGF0YXNldCglMEElMjAlMjAlMjAlMjBkb3duc2FtcGxlZF9kYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBjb2xsYXRlX2ZuJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMHNodWZmbGUlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hfc2l6ZSUzRDMyJTJDJTBBKSUwQSUwQXRmX2V2YWxfZGF0YXNldCUyMCUzRCUyMG1vZGVsLnByZXBhcmVfdGZfZGF0YXNldCglMEElMjAlMjAlMjAlMjBkb3duc2FtcGxlZF9kYXRhc2V0JTVCJTIydGVzdCUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMGNvbGxhdGVfZm4lM0RkYXRhX2NvbGxhdG9yJTJDJTBBJTIwJTIwJTIwJTIwc2h1ZmZsZSUzREZhbHNlJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hfc2l6ZSUzRDMyJTJDJTBBKQ==",highlighted:`tf_train_dataset = model.prepare_tf_dataset(
    downsampled_dataset[<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(
    downsampled_dataset[<span class="hljs-string">&quot;test&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">False</span>,
    batch_size=<span class="hljs-number">32</span>,
)`,wrap:!1}}),Z=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMGNyZWF0ZV9vcHRpbWl6ZXIlMEFmcm9tJTIwdHJhbnNmb3JtZXJzLmtlcmFzX2NhbGxiYWNrcyUyMGltcG9ydCUyMFB1c2hUb0h1YkNhbGxiYWNrJTBBaW1wb3J0JTIwdGVuc29yZmxvdyUyMGFzJTIwdGYlMEElMEFudW1fdHJhaW5fc3RlcHMlMjAlM0QlMjBsZW4odGZfdHJhaW5fZGF0YXNldCklMEFvcHRpbWl6ZXIlMkMlMjBzY2hlZHVsZSUyMCUzRCUyMGNyZWF0ZV9vcHRpbWl6ZXIoJTBBJTIwJTIwJTIwJTIwaW5pdF9sciUzRDJlLTUlMkMlMEElMjAlMjAlMjAlMjBudW1fd2FybXVwX3N0ZXBzJTNEMV8wMDAlMkMlMEElMjAlMjAlMjAlMjBudW1fdHJhaW5fc3RlcHMlM0RudW1fdHJhaW5fc3RlcHMlMkMlMEElMjAlMjAlMjAlMjB3ZWlnaHRfZGVjYXlfcmF0ZSUzRDAuMDElMkMlMEEpJTBBbW9kZWwuY29tcGlsZShvcHRpbWl6ZXIlM0RvcHRpbWl6ZXIpJTBBJTBBJTIzJTIwJUU0JUJEJUJGJUU3JTk0JUE4JTIwZmxvYXQxNiUyMCVFNyVCMiVCRSVFNSVCQSVBNiVFOCVCRiU5QiVFOCVBMSU4QyVFNiVCNyVCNyVFNSU5MCU4OCVFNyVCMiVCRSVFNSVCQSVBNiVFOCVBRSVBRCVFNyVCQiU4MyUwQXRmLmtlcmFzLm1peGVkX3ByZWNpc2lvbi5zZXRfZ2xvYmFsX3BvbGljeSglMjJtaXhlZF9mbG9hdDE2JTIyKSUwQSUwQW1vZGVsX25hbWUlMjAlM0QlMjBtb2RlbF9jaGVja3BvaW50LnNwbGl0KCUyMiUyRiUyMiklNUItMSU1RCUwQWNhbGxiYWNrJTIwJTNEJTIwUHVzaFRvSHViQ2FsbGJhY2soJTBBJTIwJTIwJTIwJTIwb3V0cHV0X2RpciUzRGYlMjIlN0Jtb2RlbF9uYW1lJTdELWZpbmV0dW5lZC1pbWRiJTIyJTJDJTIwdG9rZW5pemVyJTNEdG9rZW5pemVyJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> create_optimizer
<span class="hljs-keyword">from</span> transformers.keras_callbacks <span class="hljs-keyword">import</span> PushToHubCallback
<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">2e-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>)

model_name = model_checkpoint.split(<span class="hljs-string">&quot;/&quot;</span>)[-<span class="hljs-number">1</span>]
callback = PushToHubCallback(
    output_dir=<span class="hljs-string">f&quot;<span class="hljs-subst">{model_name}</span>-finetuned-imdb&quot;</span>, tokenizer=tokenizer
)`,wrap:!1}}),{c(){a=d("p"),a.innerHTML=U,n=M(),y(w.$$.fragment),B=M(),I=d("p"),I.innerHTML=R,$=M(),C=d("p"),C.innerHTML=V,k=M(),y(Z.$$.fragment),u=M(),_=d("p"),_.innerHTML=Q},l(G){a=m(G,"P",{"data-svelte-h":!0}),j(a)!=="svelte-n2qvqe"&&(a.innerHTML=U),n=J(G),r(w.$$.fragment,G),B=J(G),I=m(G,"P",{"data-svelte-h":!0}),j(I)!=="svelte-dhzu0"&&(I.innerHTML=R),$=J(G),C=m(G,"P",{"data-svelte-h":!0}),j(C)!=="svelte-18unet4"&&(C.innerHTML=V),k=J(G),r(Z.$$.fragment,G),u=J(G),_=m(G,"P",{"data-svelte-h":!0}),j(_)!=="svelte-11mkgbk"&&(_.innerHTML=Q)},m(G,W){t(G,a,W),t(G,n,W),o(w,G,W),t(G,B,W),t(G,I,W),t(G,$,W),t(G,C,W),t(G,k,W),o(Z,G,W),t(G,u,W),t(G,_,W),X=!0},i(G){X||(p(w.$$.fragment,G),p(Z.$$.fragment,G),X=!0)},o(G){i(w.$$.fragment,G),i(Z.$$.fragment,G),X=!1},d(G){G&&(s(a),s(n),s(B),s(I),s($),s(C),s(k),s(u),s(_)),c(w,G),c(Z,G)}}}function VM(f){let a,U="如果测试集主要由语法正确的句子组成，那么衡量语言模型质量的一种方式就是计算它给测试集中所有句子的下一个词的概率。高概率表示模型对未见过的例子不感到“惊讶”或“困惑”，这表明它已经学习了语言的基本语法模式。困惑度有很多种数学定义，我们将使用的定义是交叉熵损失的指数。具体方法是使用 <code>model.evaluate()</code> 方法计算测试集上的交叉熵损失，取结果的指数来计算预训练模型的困惑度。",n,w,B;return w=new h({props:{code:"aW1wb3J0JTIwbWF0aCUwQSUwQWV2YWxfbG9zcyUyMCUzRCUyMG1vZGVsLmV2YWx1YXRlKHRmX2V2YWxfZGF0YXNldCklMEFwcmludChmJTIyUGVycGxleGl0eSUzQSUyMCU3Qm1hdGguZXhwKGV2YWxfbG9zcyklM0EuMmYlN0QlMjIp",highlighted:`<span class="hljs-keyword">import</span> math

eval_loss = model.evaluate(tf_eval_dataset)
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Perplexity: <span class="hljs-subst">{math.exp(eval_loss):<span class="hljs-number">.2</span>f}</span>&quot;</span>)`,wrap:!1}}),{c(){a=d("p"),a.innerHTML=U,n=M(),y(w.$$.fragment)},l(I){a=m(I,"P",{"data-svelte-h":!0}),j(a)!=="svelte-xdrkl8"&&(a.innerHTML=U),n=J(I),r(w.$$.fragment,I)},m(I,R){t(I,a,R),t(I,n,R),o(w,I,R),B=!0},i(I){B||(p(w.$$.fragment,I),B=!0)},o(I){i(w.$$.fragment,I),B=!1},d(I){I&&(s(a),s(n)),c(w,I)}}}function NM(f){let a,U="如果测试集主要由语法正确的句子组成，那么衡量语言模型质量的一种方式就是计算它给测试集中所有句子的下一个词的概率。高概率表示模型对未见过的例子不感到“惊讶”或“困惑”，这表明它已经学习了语言的基本语法模式。困惑度有很多种数学定义，我们将使用的定义是交叉熵损失的指数。具体方法是使用 <code>Trainer.evaluate()</code>方法计算测试集上的交叉熵损失，取结果的指数来计算预训练模型的困惑度。",n,w,B;return w=new h({props:{code:"aW1wb3J0JTIwbWF0aCUwQSUwQWV2YWxfcmVzdWx0cyUyMCUzRCUyMHRyYWluZXIuZXZhbHVhdGUoKSUwQXByaW50KGYlMjIlM0UlM0UlM0UlMjBQZXJwbGV4aXR5JTNBJTIwJTdCbWF0aC5leHAoZXZhbF9yZXN1bHRzJTVCJ2V2YWxfbG9zcyclNUQpJTNBLjJmJTdEJTIyKQ==",highlighted:`<span class="hljs-keyword">import</span> math

eval_results = trainer.evaluate()
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&gt;&gt;&gt; Perplexity: <span class="hljs-subst">{math.exp(eval_results[<span class="hljs-string">&#x27;eval_loss&#x27;</span>]):<span class="hljs-number">.2</span>f}</span>&quot;</span>)`,wrap:!1}}),{c(){a=d("p"),a.innerHTML=U,n=M(),y(w.$$.fragment)},l(I){a=m(I,"P",{"data-svelte-h":!0}),j(a)!=="svelte-urx3qw"&&(a.innerHTML=U),n=J(I),r(w.$$.fragment,I)},m(I,R){t(I,a,R),t(I,n,R),o(w,I,R),B=!0},i(I){B||(p(w.$$.fragment,I),B=!0)},o(I){i(w.$$.fragment,I),B=!1},d(I){I&&(s(a),s(n)),c(w,I)}}}function $M(f){let a,U;return a=new h({props:{code:"bW9kZWwuZml0KHRmX3RyYWluX2RhdGFzZXQlMkMlMjB2YWxpZGF0aW9uX2RhdGElM0R0Zl9ldmFsX2RhdGFzZXQlMkMlMjBjYWxsYmFja3MlM0QlNUJjYWxsYmFjayU1RCk=",highlighted:"model.fit(tf_train_dataset, validation_data=tf_eval_dataset, callbacks=[callback])",wrap:!1}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function WM(f){let a,U;return a=new h({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:"trainer.train()",wrap:!1}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function QM(f){let a,U;return a=new h({props:{code:"ZXZhbF9sb3NzJTIwJTNEJTIwbW9kZWwuZXZhbHVhdGUodGZfZXZhbF9kYXRhc2V0KSUwQXByaW50KGYlMjJQZXJwbGV4aXR5JTNBJTIwJTdCbWF0aC5leHAoZXZhbF9sb3NzKSUzQS4yZiU3RCUyMik=",highlighted:`eval_loss = model.evaluate(tf_eval_dataset)
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Perplexity: <span class="hljs-subst">{math.exp(eval_loss):<span class="hljs-number">.2</span>f}</span>&quot;</span>)`,wrap:!1}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function XM(f){let a,U;return a=new h({props:{code:"ZXZhbF9yZXN1bHRzJTIwJTNEJTIwdHJhaW5lci5ldmFsdWF0ZSgpJTBBcHJpbnQoZiUyMiUzRSUzRSUzRSUyMFBlcnBsZXhpdHklM0ElMjAlN0JtYXRoLmV4cChldmFsX3Jlc3VsdHMlNUInZXZhbF9sb3NzJyU1RCklM0EuMmYlN0QlMjIp",highlighted:`eval_results = trainer.evaluate()
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&gt;&gt;&gt; Perplexity: <span class="hljs-subst">{math.exp(eval_results[<span class="hljs-string">&#x27;eval_loss&#x27;</span>]):<span class="hljs-number">.2</span>f}</span>&quot;</span>)`,wrap:!1}}),{c(){y(a.$$.fragment)},l(n){r(a.$$.fragment,n)},m(n,w){o(a,n,w),U=!0},i(n){U||(p(a.$$.fragment,n),U=!0)},o(n){i(a.$$.fragment,n),U=!1},d(n){c(a,n)}}}function nM(f){let a,U="一旦训练完成，我们可以将带有训练信息的模型卡片推送到 Hub（checkpoint 在训练过程中就已经保存了）：",n,w,B;return w=new h({props:{code:"dHJhaW5lci5wdXNoX3RvX2h1Yigp",highlighted:"trainer.push_to_hub()",wrap:!1}}),{c(){a=d("p"),a.textContent=U,n=M(),y(w.$$.fragment)},l(I){a=m(I,"P",{"data-svelte-h":!0}),j(a)!=="svelte-1qj9opz"&&(a.textContent=U),n=J(I),r(w.$$.fragment,I)},m(I,R){t(I,a,R),t(I,n,R),o(w,I,R),B=!0},i(I){B||(p(w.$$.fragment,I),B=!0)},o(I){i(w.$$.fragment,I),B=!1},d(I){I&&(s(a),s(n)),c(w,I)}}}function EM(f){let a,U="✏️ <strong>轮到你了！</strong> 将数据整理器改为全词屏蔽的数据整理器后运行上面的训练。你能得到更好的结果吗？";return{c(){a=d("p"),a.innerHTML=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-ee0dhu"&&(a.innerHTML=U)},m(n,w){t(n,a,w)},p:Al,d(n){n&&s(a)}}}function MM(f){let a,U="在我们的使用案例中，我们不需要对训练循环做任何特殊的处理，但在某些情况下，你可能需要实现一些自定义逻辑。对于这些应用，你可以使用 🤗 Accelerate —— 让我们看一看！",n,w,B,I,R=`从上面的 <code>Trainer</code> 训练流程中我们就能发现，微调一个掩码的语言模型与 <a href="https://chat.openai.com/course/chapter3" rel="nofollow">第三章</a> 中的文本分类非常相似。事实上，唯一的不同之处是使用了一个特殊的数据整理器，我们已经在本节的前面讨论过这个问题了！
然而，我们注意到 <code>DataCollatorForLanguageModeling</code> 在每次评估时也会进行随机遮罩，因此我们在每次训练运行中都会看到困惑度得分有些波动。消除这种随机性的一种方法是在整个测试集上 <code>仅进行一次</code> 遮罩，然后在评估过程中使用🤗 Transformers 中的默认数据整理器来收集 batch。为实现这个过程，让我们实现一个简单的函数，类似于我们第一次使用 <code>DataCollatorForLanguageModeling</code> 时进行遮罩的方式：`,$,C,V,k,Z="接下来，我们将上述函数应用到测试集，并删除未进行遮罩的列，这样就实现了使用遮罩过的数据替换原始输入的数据。你可以通过将上述 <code>data_collator</code> 替换为支持全词遮罩的数据整理器并且删除下面的第一行（全词遮罩的数据整理器需要 <code>word_ids</code> 列来定位同一个单词中的不同 <code>token</code>）来实现全词遮罩",u,_,Q,X,G="然后我们可以像往常一样设置 DataLoader，但我们将使用🤗 Transformers 中的 <code>default_data_collator</code> 来设置 DataLoader：",W,S,g,N,Ls="从这里开始，我们将遵循🤗 Accelerate 的标准步骤。第一个任务是重新加载预训练模型：",Zl,A,vl,K,x="然后我们需要指定优化器；我们将使用标准的 <code>AdamW</code> ：",Yl,P,O,fl,Hl="有了这些对象，我们现在可以用 <code>Accelerator</code> 对象包装所有的组件，以进行训练：",ll,el,q,sl,Cl="现在我们的模型、优化器和 DataLoader 都配置好了，我们可以按照以下方式设置学习率调度器：",Rl,v,Ll,tl,il="在开始训练之前，我们还需要做的最后一件事就是在 Hugging Face Hub 上创建一个模型仓库！我们可以使用🤗 Hub 库的 <code>get_full_repo_name</code>，生成我们仓库的全名：",Dl,al,nl,Gl,Vl,E,D="然后，我们可以使用🤗 Hub 的 <code>Repository</code> 类创建并克隆仓库：",wl,Y,ql,Ml,Bl="完成后，只需写出完整的训练和评估循环即可：",Nl,H,Kl,Jl,Tl,pl,gl="很棒，我们已经能够评估每个 epoch 的困惑度，并确保运行的结果可以复现！",Pl;return w=new Sl({props:{title:"使用 🤗 Accelerate 微调 DistilBERT",local:"使用 🤗 Accelerate 微调 DistilBERT",headingTag:"h2"}}),C=new h({props:{code:"ZGVmJTIwaW5zZXJ0X3JhbmRvbV9tYXNrKGJhdGNoKSUzQSUwQSUyMCUyMCUyMCUyMGZlYXR1cmVzJTIwJTNEJTIwJTVCZGljdCh6aXAoYmF0Y2glMkMlMjB0KSklMjBmb3IlMjB0JTIwaW4lMjB6aXAoKmJhdGNoLnZhbHVlcygpKSU1RCUwQSUyMCUyMCUyMCUyMG1hc2tlZF9pbnB1dHMlMjAlM0QlMjBkYXRhX2NvbGxhdG9yKGZlYXR1cmVzKSUwQSUyMCUyMCUyMCUyMCUyMyUyMCVFNCVCOCVCQSVFNiU5NSVCMCVFNiU4RCVBRSVFOSU5QiU4NiVFNCVCOCVBRCVFNyU5QSU4NCVFNiVBRiU4RiVFNCVCOCU4MCVFNSU4OCU5NyVFNSU4OCU5QiVFNSVCQiVCQSVFNCVCOCU4MCVFNCVCOCVBQSVFNiU5NiVCMCVFNyU5QSU4NCUyMm1hc2tlZCUyMiVFNSU4OCU5NyUwQSUyMCUyMCUyMCUyMHJldHVybiUyMCU3QiUyMm1hc2tlZF8lMjIlMjAlMkIlMjBrJTNBJTIwdi5udW1weSgpJTIwZm9yJTIwayUyQyUyMHYlMjBpbiUyMG1hc2tlZF9pbnB1dHMuaXRlbXMoKSU3RA==",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">insert_random_mask</span>(<span class="hljs-params">batch</span>):
    features = [<span class="hljs-built_in">dict</span>(<span class="hljs-built_in">zip</span>(batch, t)) <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(*batch.values())]
    masked_inputs = data_collator(features)
    <span class="hljs-comment"># 为数据集中的每一列创建一个新的&quot;masked&quot;列</span>
    <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;masked_&quot;</span> + k: v.numpy() <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> masked_inputs.items()}`,wrap:!1}}),_=new h({props:{code:"ZG93bnNhbXBsZWRfZGF0YXNldCUyMCUzRCUyMGRvd25zYW1wbGVkX2RhdGFzZXQucmVtb3ZlX2NvbHVtbnMoJTVCJTIyd29yZF9pZHMlMjIlNUQpJTBBZXZhbF9kYXRhc2V0JTIwJTNEJTIwZG93bnNhbXBsZWRfZGF0YXNldCU1QiUyMnRlc3QlMjIlNUQubWFwKCUwQSUyMCUyMCUyMCUyMGluc2VydF9yYW5kb21fbWFzayUyQyUwQSUyMCUyMCUyMCUyMGJhdGNoZWQlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwcmVtb3ZlX2NvbHVtbnMlM0Rkb3duc2FtcGxlZF9kYXRhc2V0JTVCJTIydGVzdCUyMiU1RC5jb2x1bW5fbmFtZXMlMkMlMEEpJTBBZXZhbF9kYXRhc2V0JTIwJTNEJTIwZXZhbF9kYXRhc2V0LnJlbmFtZV9jb2x1bW5zKCUwQSUyMCUyMCUyMCUyMCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMm1hc2tlZF9pbnB1dF9pZHMlMjIlM0ElMjAlMjJpbnB1dF9pZHMlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJtYXNrZWRfYXR0ZW50aW9uX21hc2slMjIlM0ElMjAlMjJhdHRlbnRpb25fbWFzayUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMm1hc2tlZF9sYWJlbHMlMjIlM0ElMjAlMjJsYWJlbHMlMjIlMkMlMEElMjAlMjAlMjAlMjAlN0QlMEEp",highlighted:`downsampled_dataset = downsampled_dataset.remove_columns([<span class="hljs-string">&quot;word_ids&quot;</span>])
eval_dataset = downsampled_dataset[<span class="hljs-string">&quot;test&quot;</span>].<span class="hljs-built_in">map</span>(
    insert_random_mask,
    batched=<span class="hljs-literal">True</span>,
    remove_columns=downsampled_dataset[<span class="hljs-string">&quot;test&quot;</span>].column_names,
)
eval_dataset = eval_dataset.rename_columns(
    {
        <span class="hljs-string">&quot;masked_input_ids&quot;</span>: <span class="hljs-string">&quot;input_ids&quot;</span>,
        <span class="hljs-string">&quot;masked_attention_mask&quot;</span>: <span class="hljs-string">&quot;attention_mask&quot;</span>,
        <span class="hljs-string">&quot;masked_labels&quot;</span>: <span class="hljs-string">&quot;labels&quot;</span>,
    }
)`,wrap:!1}}),S=new h({props:{code:"ZnJvbSUyMHRvcmNoLnV0aWxzLmRhdGElMjBpbXBvcnQlMjBEYXRhTG9hZGVyJTBBZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMGRlZmF1bHRfZGF0YV9jb2xsYXRvciUwQSUwQWJhdGNoX3NpemUlMjAlM0QlMjA2NCUwQXRyYWluX2RhdGFsb2FkZXIlMjAlM0QlMjBEYXRhTG9hZGVyKCUwQSUyMCUyMCUyMCUyMGRvd25zYW1wbGVkX2RhdGFzZXQlNUIlMjJ0cmFpbiUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMHNodWZmbGUlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hfc2l6ZSUzRGJhdGNoX3NpemUlMkMlMEElMjAlMjAlMjAlMjBjb2xsYXRlX2ZuJTNEZGF0YV9jb2xsYXRvciUyQyUwQSklMEFldmFsX2RhdGFsb2FkZXIlMjAlM0QlMjBEYXRhTG9hZGVyKCUwQSUyMCUyMCUyMCUyMGV2YWxfZGF0YXNldCUyQyUyMGJhdGNoX3NpemUlM0RiYXRjaF9zaXplJTJDJTIwY29sbGF0ZV9mbiUzRGRlZmF1bHRfZGF0YV9jb2xsYXRvciUwQSk=",highlighted:`<span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> default_data_collator

batch_size = <span class="hljs-number">64</span>
train_dataloader = DataLoader(
    downsampled_dataset[<span class="hljs-string">&quot;train&quot;</span>],
    shuffle=<span class="hljs-literal">True</span>,
    batch_size=batch_size,
    collate_fn=data_collator,
)
eval_dataloader = DataLoader(
    eval_dataset, batch_size=batch_size, collate_fn=default_data_collator
)`,wrap:!1}}),A=new h({props:{code:"bW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JNYXNrZWRMTS5mcm9tX3ByZXRyYWluZWQobW9kZWxfY2hlY2twb2ludCk=",highlighted:'model = <span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">AutoModelForMaskedLM</span>.</span></span>from<span class="hljs-constructor">_pretrained(<span class="hljs-params">model_checkpoint</span>)</span>',wrap:!1}}),P=new h({props:{code:"ZnJvbSUyMHRvcmNoLm9wdGltJTIwaW1wb3J0JTIwQWRhbVclMEElMEFvcHRpbWl6ZXIlMjAlM0QlMjBBZGFtVyhtb2RlbC5wYXJhbWV0ZXJzKCklMkMlMjBsciUzRDVlLTUp",highlighted:`<span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW

optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">5e-5</span>)`,wrap:!1}}),el=new h({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUwQWFjY2VsZXJhdG9yJTIwJTNEJTIwQWNjZWxlcmF0b3IoKSUwQW1vZGVsJTJDJTIwb3B0aW1pemVyJTJDJTIwdHJhaW5fZGF0YWxvYWRlciUyQyUyMGV2YWxfZGF0YWxvYWRlciUyMCUzRCUyMGFjY2VsZXJhdG9yLnByZXBhcmUoJTBBJTIwJTIwJTIwJTIwbW9kZWwlMkMlMjBvcHRpbWl6ZXIlMkMlMjB0cmFpbl9kYXRhbG9hZGVyJTJDJTIwZXZhbF9kYXRhbG9hZGVyJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator

accelerator = Accelerator()
model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare(
    model, optimizer, train_dataloader, eval_dataloader
)`,wrap:!1}}),v=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMGdldF9zY2hlZHVsZXIlMEElMEFudW1fdHJhaW5fZXBvY2hzJTIwJTNEJTIwMyUwQW51bV91cGRhdGVfc3RlcHNfcGVyX2Vwb2NoJTIwJTNEJTIwbGVuKHRyYWluX2RhdGFsb2FkZXIpJTBBbnVtX3RyYWluaW5nX3N0ZXBzJTIwJTNEJTIwbnVtX3RyYWluX2Vwb2NocyUyMColMjBudW1fdXBkYXRlX3N0ZXBzX3Blcl9lcG9jaCUwQSUwQWxyX3NjaGVkdWxlciUyMCUzRCUyMGdldF9zY2hlZHVsZXIoJTBBJTIwJTIwJTIwJTIwJTIybGluZWFyJTIyJTJDJTBBJTIwJTIwJTIwJTIwb3B0aW1pemVyJTNEb3B0aW1pemVyJTJDJTBBJTIwJTIwJTIwJTIwbnVtX3dhcm11cF9zdGVwcyUzRDAlMkMlMEElMjAlMjAlMjAlMjBudW1fdHJhaW5pbmdfc3RlcHMlM0RudW1fdHJhaW5pbmdfc3RlcHMlMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler

num_train_epochs = <span class="hljs-number">3</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(
    <span class="hljs-string">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">0</span>,
    num_training_steps=num_training_steps,
)`,wrap:!1}}),al=new h({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMGdldF9mdWxsX3JlcG9fbmFtZSUwQSUwQW1vZGVsX25hbWUlMjAlM0QlMjAlMjJkaXN0aWxiZXJ0LWJhc2UtdW5jYXNlZC1maW5ldHVuZWQtaW1kYi1hY2NlbGVyYXRlJTIyJTBBcmVwb19uYW1lJTIwJTNEJTIwZ2V0X2Z1bGxfcmVwb19uYW1lKG1vZGVsX25hbWUpJTBBcmVwb19uYW1l",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> get_full_repo_name

model_name = <span class="hljs-string">&quot;distilbert-base-uncased-finetuned-imdb-accelerate&quot;</span>
repo_name = get_full_repo_name(model_name)
repo_name`,wrap:!1}}),Gl=new h({props:{code:"J2xld3R1biUyRmRpc3RpbGJlcnQtYmFzZS11bmNhc2VkLWZpbmV0dW5lZC1pbWRiLWFjY2VsZXJhdGUn",highlighted:'<span class="hljs-string">&#x27;lewtun/distilbert-base-uncased-finetuned-imdb-accelerate&#x27;</span>',wrap:!1}}),Y=new h({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMFJlcG9zaXRvcnklMEElMEFvdXRwdXRfZGlyJTIwJTNEJTIwbW9kZWxfbmFtZSUwQXJlcG8lMjAlM0QlMjBSZXBvc2l0b3J5KG91dHB1dF9kaXIlMkMlMjBjbG9uZV9mcm9tJTNEcmVwb19uYW1lKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository

output_dir = model_name
repo = Repository(output_dir, clone_from=repo_name)`,wrap:!1}}),H=new h({props:{code:"ZnJvbSUyMHRxZG0uYXV0byUyMGltcG9ydCUyMHRxZG0lMEFpbXBvcnQlMjB0b3JjaCUwQWltcG9ydCUyMG1hdGglMEElMEFwcm9ncmVzc19iYXIlMjAlM0QlMjB0cWRtKHJhbmdlKG51bV90cmFpbmluZ19zdGVwcykpJTBBJTBBZm9yJTIwZXBvY2glMjBpbiUyMHJhbmdlKG51bV90cmFpbl9lcG9jaHMpJTNBJ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jBzYXZlX2Z1bmN0aW9uJTNEYWNjZWxlcmF0b3Iuc2F2ZSklMEElMjAlMjAlMjAlMjBpZiUyMGFjY2VsZXJhdG9yLmlzX21haW5fcHJvY2VzcyUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRva2VuaXplci5zYXZlX3ByZXRyYWluZWQob3V0cHV0X2RpciklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXBvLnB1c2hfdG9faHViKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNvbW1pdF9tZXNzYWdlJTNEZiUyMlRyYWluaW5nJTIwaW4lMjBwcm9ncmVzcyUyMGVwb2NoJTIwJTdCZXBvY2glN0QlMjIlMkMlMjBibG9ja2luZyUzREZhbHNlJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKQ==",highlighted:`<span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> math

progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps))

<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-comment"># 训练</span>
    model.train()
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
        outputs = model(**batch)
        loss = outputs.loss
        accelerator.backward(loss)

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(<span class="hljs-number">1</span>)

    <span class="hljs-comment"># 评估</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)

        loss = outputs.loss
        losses.append(accelerator.gather(loss.repeat(batch_size)))

    losses = torch.cat(losses)
    losses = losses[: <span class="hljs-built_in">len</span>(eval_dataset)]
    <span class="hljs-keyword">try</span>:
        perplexity = math.exp(torch.mean(losses))
    <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-built_in">print</span>(<span class="hljs-string">f&quot;&gt;&gt;&gt; Epoch <span class="hljs-subst">{epoch}</span>: Perplexity: <span class="hljs-subst">{perplexity}</span>&quot;</span>)

    <span class="hljs-comment"># 保存并上传</span>
    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 epoch <span class="hljs-subst">{epoch}</span>&quot;</span>, blocking=<span class="hljs-literal">False</span>
        )`,wrap:!1}}),Jl=new h({props:{code:"RXBvY2glMjAwJTNBJTIwUGVycGxleGl0eSUzQSUyMDExLjM5NzU0NTMwNzkwMDQ3MiUwQUVwb2NoJTIwMSUzQSUyMFBlcnBsZXhpdHklM0ElMjAxMC45MDQ5MDkzMzA5ODMwOTIlMEFFcG9jaCUyMDIlM0ElMjBQZXJwbGV4aXR5JTNBJTIwMTAuNzI5NTAzNTA1MzQwNDA5",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>Epoch <span class="hljs-number">0</span>: Perplexity: <span class="hljs-number">11.397545307900472</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>Epoch <span class="hljs-number">1</span>: Perplexity: <span class="hljs-number">10.904909330983092</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>Epoch <span class="hljs-number">2</span>: Perplexity: <span class="hljs-number">10.729503505340409</span>`,wrap:!1}}),{c(){a=d("p"),a.textContent=U,n=M(),y(w.$$.fragment),B=M(),I=d("p"),I.innerHTML=R,$=M(),y(C.$$.fragment),V=M(),k=d("p"),k.innerHTML=Z,u=M(),y(_.$$.fragment),Q=M(),X=d("p"),X.innerHTML=G,W=M(),y(S.$$.fragment),g=M(),N=d("p"),N.textContent=Ls,Zl=M(),y(A.$$.fragment),vl=M(),K=d("p"),K.innerHTML=x,Yl=M(),y(P.$$.fragment),O=M(),fl=d("p"),fl.innerHTML=Hl,ll=M(),y(el.$$.fragment),q=M(),sl=d("p"),sl.textContent=Cl,Rl=M(),y(v.$$.fragment),Ll=M(),tl=d("p"),tl.innerHTML=il,Dl=M(),y(al.$$.fragment),nl=M(),y(Gl.$$.fragment),Vl=M(),E=d("p"),E.innerHTML=D,wl=M(),y(Y.$$.fragment),ql=M(),Ml=d("p"),Ml.textContent=Bl,Nl=M(),y(H.$$.fragment),Kl=M(),y(Jl.$$.fragment),Tl=M(),pl=d("p"),pl.textContent=gl},l(T){a=m(T,"P",{"data-svelte-h":!0}),j(a)!=="svelte-azwi3u"&&(a.textContent=U),n=J(T),r(w.$$.fragment,T),B=J(T),I=m(T,"P",{"data-svelte-h":!0}),j(I)!=="svelte-treyod"&&(I.innerHTML=R),$=J(T),r(C.$$.fragment,T),V=J(T),k=m(T,"P",{"data-svelte-h":!0}),j(k)!=="svelte-10uf4i"&&(k.innerHTML=Z),u=J(T),r(_.$$.fragment,T),Q=J(T),X=m(T,"P",{"data-svelte-h":!0}),j(X)!=="svelte-1cnh84o"&&(X.innerHTML=G),W=J(T),r(S.$$.fragment,T),g=J(T),N=m(T,"P",{"data-svelte-h":!0}),j(N)!=="svelte-6famzt"&&(N.textContent=Ls),Zl=J(T),r(A.$$.fragment,T),vl=J(T),K=m(T,"P",{"data-svelte-h":!0}),j(K)!=="svelte-1y3zs5u"&&(K.innerHTML=x),Yl=J(T),r(P.$$.fragment,T),O=J(T),fl=m(T,"P",{"data-svelte-h":!0}),j(fl)!=="svelte-rwcyg5"&&(fl.innerHTML=Hl),ll=J(T),r(el.$$.fragment,T),q=J(T),sl=m(T,"P",{"data-svelte-h":!0}),j(sl)!=="svelte-7v1073"&&(sl.textContent=Cl),Rl=J(T),r(v.$$.fragment,T),Ll=J(T),tl=m(T,"P",{"data-svelte-h":!0}),j(tl)!=="svelte-1igg54x"&&(tl.innerHTML=il),Dl=J(T),r(al.$$.fragment,T),nl=J(T),r(Gl.$$.fragment,T),Vl=J(T),E=m(T,"P",{"data-svelte-h":!0}),j(E)!=="svelte-zxg9xa"&&(E.innerHTML=D),wl=J(T),r(Y.$$.fragment,T),ql=J(T),Ml=m(T,"P",{"data-svelte-h":!0}),j(Ml)!=="svelte-1jcxtd4"&&(Ml.textContent=Bl),Nl=J(T),r(H.$$.fragment,T),Kl=J(T),r(Jl.$$.fragment,T),Tl=J(T),pl=m(T,"P",{"data-svelte-h":!0}),j(pl)!=="svelte-w09g07"&&(pl.textContent=gl)},m(T,b){t(T,a,b),t(T,n,b),o(w,T,b),t(T,B,b),t(T,I,b),t(T,$,b),o(C,T,b),t(T,V,b),t(T,k,b),t(T,u,b),o(_,T,b),t(T,Q,b),t(T,X,b),t(T,W,b),o(S,T,b),t(T,g,b),t(T,N,b),t(T,Zl,b),o(A,T,b),t(T,vl,b),t(T,K,b),t(T,Yl,b),o(P,T,b),t(T,O,b),t(T,fl,b),t(T,ll,b),o(el,T,b),t(T,q,b),t(T,sl,b),t(T,Rl,b),o(v,T,b),t(T,Ll,b),t(T,tl,b),t(T,Dl,b),o(al,T,b),t(T,nl,b),o(Gl,T,b),t(T,Vl,b),t(T,E,b),t(T,wl,b),o(Y,T,b),t(T,ql,b),t(T,Ml,b),t(T,Nl,b),o(H,T,b),t(T,Kl,b),o(Jl,T,b),t(T,Tl,b),t(T,pl,b),Pl=!0},i(T){Pl||(p(w.$$.fragment,T),p(C.$$.fragment,T),p(_.$$.fragment,T),p(S.$$.fragment,T),p(A.$$.fragment,T),p(P.$$.fragment,T),p(el.$$.fragment,T),p(v.$$.fragment,T),p(al.$$.fragment,T),p(Gl.$$.fragment,T),p(Y.$$.fragment,T),p(H.$$.fragment,T),p(Jl.$$.fragment,T),Pl=!0)},o(T){i(w.$$.fragment,T),i(C.$$.fragment,T),i(_.$$.fragment,T),i(S.$$.fragment,T),i(A.$$.fragment,T),i(P.$$.fragment,T),i(el.$$.fragment,T),i(v.$$.fragment,T),i(al.$$.fragment,T),i(Gl.$$.fragment,T),i(Y.$$.fragment,T),i(H.$$.fragment,T),i(Jl.$$.fragment,T),Pl=!1},d(T){T&&(s(a),s(n),s(B),s(I),s($),s(V),s(k),s(u),s(Q),s(X),s(W),s(g),s(N),s(Zl),s(vl),s(K),s(Yl),s(O),s(fl),s(ll),s(q),s(sl),s(Rl),s(Ll),s(tl),s(Dl),s(nl),s(Vl),s(E),s(wl),s(ql),s(Ml),s(Nl),s(Kl),s(Tl),s(pl)),c(w,T),c(C,T),c(_,T),c(S,T),c(A,T),c(P,T),c(el,T),c(v,T),c(al,T),c(Gl,T),c(Y,T),c(H,T),c(Jl,T)}}}function FM(f){let a,U='✏️ <strong>试试看！</strong> 为了量化领域适应的好处，分别使用预训练和微调的 DistilBERT checkpoint 以及数据集自带的 IMDb 标签来微调一个分类器，并对比一下这个两个 checkpoint 的差异。如果你需要复习文本分类的知识，请查看 <a href="/course/chapter3">第三章</a> 。';return{c(){a=d("p"),a.innerHTML=U},l(n){a=m(n,"P",{"data-svelte-h":!0}),j(a)!=="svelte-f3aveu"&&(a.innerHTML=U)},m(n,w){t(n,a,w)},p:Al,d(n){n&&s(a)}}}function zM(f){let a,U,n,w,B,I,R,$,C,V,k,Z,u="对于许多涉及 Transformer 模型的 NLP 任务，你可以简单地从 Hugging Face Hub 中获取一个预训练的模型，然后直接在你的数据上对其进行微调，以完成手头的任务。只要用于预训练的语料库与用于微调的语料库没有太大区别，迁移学习通常会产生很好的结果。",_,Q,X="但是，在某些情况下，你可能需要先在你的数据上微调语言模型，然后再训练特定于任务的 head。例如，如果你的数据集包含法律合同或科学文章，像 BERT 这样的普通 Transformer 模型通常会将你语料库中的特定领域词视为稀有 tokens ，导致性能可能不尽如人意。通过在特定领域内数据上微调语言模型，你可以提高许多下游任务的性能，这意味着你通常只需执行一次此步骤！",G,W,S='这种在特定领域内数据上微调预训练语言模型的过程通常称为 <code>领域适应（domain adaptation）</code> 。它于 2018 年由 <a href="https://arxiv.org/abs/1801.06146" rel="nofollow">ULMFiT</a> 推广， NLP 的首批神经架构之一 （基于 LSTM）。下图显示了使用 ULMFiT这是使迁移学习真正适用于 进行领域自适应的示例；在本节中，我们将做类似的事情，但我们将使用 Transformer 而不是 LSTM！',g,N,Ls='<img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/ulmfit.svg" alt="ULMFiT."/> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/ulmfit-dark.svg" alt="ULMFiT."/>',Zl,A,vl='在本节结束时，你将在 Hub 上拥有一个 <a href="https://huggingface.co/huggingface-course/distilbert-base-uncased-finetuned-imdb?text=This+is+a+great+%5BMASK%5D." rel="nofollow">掩码语言模型(masked language model)</a> ，该模型可以自动补全句子，如下所示：',K,x,Yl,P,O,fl="让我们开始吧！",Hl,ll,el,q,sl,Cl,Rl,v,Ll='首先，让我们为掩码语言建模选择一个合适的预训练模型。如以下屏幕截图所示，你可以通过在 <a href="https://huggingface.co/models?pipeline_tag=fill-mask&amp;sort=downloads" rel="nofollow">Hugging Face Hub</a> 上选择“Fill-Mask”过滤器：',tl,il,Dl='<img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/mlm-models.png" alt="Hub models." width="80%"/>',al,nl,Gl='尽管 BERT 和 RoBERTa 系列模型的下载量最大，但我们将使用名为 <a href="https://huggingface.co/distilbert-base-uncased" rel="nofollow">DistilBERT</a> 的模型。它可以更快地训练，而且对下游性能几乎没有损失。它使用了一种称为 <a href="https://en.wikipedia.org/wiki/Knowledge_distillation" rel="nofollow"><code>知识蒸馏（knowledge distillation）</code></a> 的特殊技术进行训练，其中使用像 BERT 这样的大型“教师模型”来指导参数少得多的“学生模型”的训练。在本节中对知识蒸馏进行详细解释会使我们偏离本节主题太远，但如果你有兴趣，可以阅读 <a href="https://www.oreilly.com/library/view/natural-language-processing/9781098136789/" rel="nofollow"><code>使用 Transformers 进行自然语言处理（Natural Language Processing with Transformers）</code></a> （俗称 Transformers 教科书）中知识蒸馏的相关内容。',Vl,E,D,wl,Y,ql="DistilBERT 大约有 6700 万个参数，大约只有 BERT base 模型的二分之一，这大致意味着训练的速度可以提高两倍 —— 非常棒！现在让我们看看对于下面的一小部分文本，这个模型最有可能预测什么：",Ml,Bl,Nl,H,Kl='作为人类，我们可以想象 <code>[MASK]</code> token 有很多可能性，例如 “day”、“ride” 或者 “painting”。对于预训练模型，预测取决于模型所训练的语料库，因为它会学习获取数据中存在的语料统计分布。与 BERT 一样，DistilBERT 在 <a href="https://huggingface.co/datasets/wikipedia" rel="nofollow">English Wikipedia</a> 和 <a href="https://huggingface.co/datasets/bookcorpus" rel="nofollow">BookCorpus</a> 数据集上进行预训练，所以我们猜想模型对 <code>[MASK]</code> 的预测能够反映这些领域。为了预测 <code>[MASK]</code> ，我们需要 DistilBERT 的 tokenizer 来处理输入，所以让我们也从 Hub 下载它：',Jl,Tl,pl,gl,Pl="有了 tokenizer 和模型，我们现在可以将我们的示例文本传递给模型，提取 logits，并打印出前 5 个候选词：",T,b,Ul,Es,Ol,qs,le,Ya="我们可以从输出中看到，模型的预测的是日常术语，考虑到模型训练的语料数据主要来源于维基百科，这并不奇怪。现在让我们看看如何将这个领域改变成稍微更加独特——高度两极分化的电影评论！",Ks,ee,Ps,se,Ha='为了展示领域适应性，我们将使用来自IMDB的 <a href="https://huggingface.co/datasets/imdb" rel="nofollow">大型电影评论数据集(Large Movie Review Dataset)</a>，这是一个电影评论语料库，通常用于对情感分析模型进行基准测试。通过在这个语料库上对 DistilBERT 进行微调，我们期望语言模型会从其预训练的维基百科的事实性数据，适应到更主观的电影评论的领域。',Os,te,La="首先，我们可以使用🤗 Datasets 中的 <code>load_dataset()</code> 函数从 Hugging Face 中获取数据：",lt,ae,et,ne,st,Me,Da="我们可以看到 <code>train</code> 和 <code>test</code> 分别包含了 25,000 条评论，还有一个没有的标签的 <code>unsupervised（无监督）</code> 部分包含 50,000 条评论。接下来让我们从里面取一些样本，来了解一下我们正在处理的文本的特点。正如我们在本课程的前几章中所做的那样，我们将把 <code>Dataset.shuffle()</code> 函数链接到 <code>Dataset.select()</code> 函数创建随机样本：",tt,Je,at,Te,nt,pe,qa="是的，这些肯定是电影评论，如果你年龄足够大，你甚至可能会理解上述评论中关于拥有 VHS （一种古老的盒式摄像机格式）版本的评论😜！虽然语言模型不需要预先标注好的标签，但我们已经可以看到数据集其实包含了标签， <code>0</code> 代表负面评论， <code>1</code> 代表正面评论。",Mt,$l,Jt,ie,Ka='现在我们已经快速浏览了一下数据，接下来我们要深入准备这些数据以供进行掩码语言建模。如我们所见，与我们在 <a href="https://chat.openai.com/course/chapter3" rel="nofollow">第三章</a> 看到的序列分类任务相比，这里需要采取一些额外的步骤。让我们开始吧！',Tt,we,pt,Ue,it,ye,Pa="对于自回归和掩码语言建模，常见的预处理步骤是将所有的文本拼接起来，然后再将整个语料库切割为相同大小的块。这与我们之前的做法有很大的不同，我们之前只是对单个的示例进行 tokenize。为什么要将所有的示例连接在一起呢？原因是如果单个示例太长，可能会被截断，这会导致我们失去可能对语言建模任务有用的信息！",wt,re,Oa='因此，我们首先会像往常一样对语料库进行 tokenize 处理，但是不在 tokenizer 中设置 <code>truncation=True</code> 选项。如果我们有可以使用快速 tokenizer（如 <a href="/course/chapter6/3">第六章</a> 中所述），除此之外，我们还需要获取单词的 <code>ID</code>，因为后面我们需要用到它们来进行全词掩码。最后我们将把这个过程封装在一个简单的函数中，并删除 <code>text</code> 和 <code>label</code> 列，因为我们不再需要它们。',Ut,oe,yt,ce,rt,de,ln="由于 DistilBERT 是一个类似 BERT 的模型，我们可以看到编码后的文本包含了我们在之前章节中看到的 <code>input_ids</code> 和 <code>attention_mask</code> ，以及我们添加的 <code>word_ids</code> 。",ot,me,en="现在我们已经对电影评论进行了 <code>tokenize</code>，下一步是将它们全部组合在一起并将结果分割成块。但是，这些块应该有多大呢？这最终将取决于你可以使用的显存大小，但一个好的起点是查看模型的最大上下文大小。这可以在 tokenizer 的 <code>model_max_length</code> 属性中找到：",ct,Ie,dt,je,mt,ue,sn="该值来自于与 checkpoint 相关联的 <code>tokenizer_config.json</code> 文件；在我们的例子中，我们可以看到上下文大小是 512 个 <code>tokens</code> 与 BERT 模型一样。",It,Wl,jt,he,tn="因此，为了可以在像 Google Colab 那样的 GPU 上运行我们的实验，我们会选择一个稍小一点、可以放入内存中的分块大小：",ut,be,ht,Ql,bt,fe,an="现在来到了最有趣的部分。为了展示如何把这些示例连接在一，我们从分词后的训练集中取出几个评论，并打印出每个评论的 token 数量：",ft,Ce,Ct,Ge,Gt,Be,nn="然后，我们可以用一个简单的字典推导式将所有这些示例连接在一起，如下所示：",Bt,ge,gt,_e,_t,ke,Mn="很棒，总长度计算出来了 —— 现在，让我们将连接的评论拆分为大小为 <code>chunk_size</code> 的块。为此，我们迭代了 <code>concatenated_examples</code> 中的特征，并使用列表推导式为每个特征分块。结果是一个字典，键是特征的名称，值是对应值经过分块的列表：",kt,Ze,Zt,Re,Rt,Ve,Jn="正如你在这个例子中看到的，最后一个块通常会小于所设置的分块的大小。有两种常见的策略来处理这个问题：",Vt,Ne,Tn="<li>如果最后一个块小于 <code>chunk_size</code> ，就丢弃。</li> <li>填充最后一个块，直到其长度等于 <code>chunk_size</code> 。</li>",Nt,$e,pn="我们将在这里采用第一种方法，最后让我们将上述所有逻辑包装在一个函数中，以便我们可以将其应用于我们的已分词数据集上：",$t,We,Wt,Qe,wn="注意，在 <code>group_texts()</code> 的最后一步，我们创建了一个新的 <code>labels</code> 列，它是通过复制 <code>input_ids</code> 列形成的。这是因为在掩码语言模型的目标是预测输入中随机遮住(Masked)的 token，我们保存了让我们的语言模型从中学习 <code>[Mask]</code> 的答案。",Qt,Xe,Un="现在，让我们使用我们强大的 <code>Dataset.map()</code> 函数将 <code>group_texts()</code> 应用到我们的已分词数据集上：",Xt,Ee,Et,Fe,Ft,ze,yn="通过对文本进行分块，我们得到了比原来的训练集和测试集的 25000 个例子多得多的评论数据。这是因为我们现在有了涉及跨越原始语料库中多个例子的连续标记的例子。你可以通过在其中一个块中查找特殊的 <code>[SEP]</code> 和 <code>[CLS]</code> tokens 来清晰地看到这一点：",zt,xe,xt,Se,St,Ae,rn="在这个例子中，你可以看到两个重叠的电影评论，一个关于高中电影，另一个关于无家可归的问题。让我们也检查一下掩码语言模型待预测的标签是什么样的：",At,ve,vt,Ye,Yt,He,on="正如我们上面的 <code>group_texts()</code> 函数所预期的那样，这看起来与解码的 <code>input_ids</code> 完全相同 —— 但是要怎么样才能让我们的的模型可以学习到一些东西呢？我们缺少一个关键的步骤：在输入中随机插入 <code>[MASK]</code> token！让我们看看如何在微调期间使用特殊的数据整理器来实时地完成这个步骤。",Ht,Le,Lt,De,cn='微调掩码语言模型几乎与微调序列分类模型相同，就像我们在 <a href="/course/chapter3">第三章</a> 所做的那样。唯一的区别是我们需要一个特殊的数据整理器，它可以随机屏蔽每批文本中的一些 tokens。幸运的是，🤗 Transformers 为这项任务准备了专用的 <code>DataCollatorForLanguageModeling</code> 。我们只需要将 tokenizer 和一个 <code>mlm_probability</code> 参数（掩盖 tokens 的比例）传递给它。在这里我们将 <code>mlm_probability</code> 参数设置为 15%，这是 <code>BERT</code> 默认的数量，也是文献中最常见的选择。',Dt,qe,qt,Ke,dn="为了了解随机掩码数据整理器的工作原理，让我们把一些例子输入到数据整理器。由于数据整理器期望接收一个字典列表，其中每个字典中存储一段连续文本的块，所以我们首先遍历数据集取出来一些样本数据，然后将样本数据输入到整理器。在输入到数据整理器之间，我们删除了 <code>word_ids</code> 这个键，因为它不需要这个键。",Kt,Pe,Pt,Oe,Ot,ls,mn="很棒，成功了！我们可以看到， <code>[MASK]</code> tokens 已随机插入我们文本中的不同位置。这些将是我们的模型在训练期间必须预测的 tokens  —— 数据整理器的美妙之处在于，它会在每个 batch 中随机插入 <code>[MASK]</code> ！",la,Xl,ea,Fs,es,In="在为掩码语言建模训练模型时，不仅仅可以遮蔽单个 <code>token</code>，还可以一次遮蔽整个单词的所有 <code>token</code>，这种方法被称为全词屏蔽（whole word masking）。如果我们想使用全词屏蔽（whole word masking），我们就需要自己构建一个数据整理器。数据整理器的核心是一个函数，它接受一个样本列表并将它们转换为一个 <code>batch</code>，所以现在让我们这样做吧！我们将使用先前计算的<code>word ID</code>，构建一个单词索引和相应 <code>token</code> 之间的映射，然后随机决定遮蔽哪些单词，并使用这种方法对输入进行遮蔽。请注意，除了与掩码对应的标签外，所有其他的标签均应该设置为 <code>-100</code> 。",sa,yl,rl,zs,ss,jn="接下来，我们可以在之前的样本上试试它：",ta,ts,aa,as,na,El,Ma,ns,un='现在我们有了两个数据整理器，剩下的微调步骤与其他任务类似都是标准的。如果你在 Google Colab 上运行并且没有幸运地分配到神秘的 P100 GPU😭，那么训练可能会需要一些时间，所以我们首先将训练集的大小减小到几千个例子。不用担心，我们仍然可以得到一个相当不错的语言模型！在 🤗 Datasets 中快速筛选数据集的方法是使用我们在 <a href="/course/chapter5">第五章</a> 中看到的 <code>Dataset.train_test_split()</code> 函数：',Ja,Ms,Ta,Js,pa,Ts,hn="运行上述代码会自动创建新的 <code>train</code> 和 <code>test</code> 数据集，训练集大小为 10,000 个示例，验证的大小是训练集的 10％ —— 如果你有一个强大的 GPU，可以自行增加这个比例！我们接下来要做的事情是登录 Hugging Face Hub。如果你在 Notebook 中运行这段代码，你可以通过以下的工具函数进行登录：",ia,ps,wa,is,bn="它将显示一个小部件，在其中你可以输入你的账号和密码进行登陆。或者，你也可以在你最喜欢的终端中输入指令：",Ua,ws,ya,Us,fn="然后在那里登录。",ra,ol,cl,xs,ys,oa,rs,ca,os,Cn="语言建模与文本分类或问答等其他任务有所不同，在其他任务中，我们会得到一个带标签的语料库进行训练，而语言建模则没有任何明确的标签。那么我们如何确定什么是好的语言模型呢？就像手机中的自动更正功能一样，一个好的语言模型会较高概率输出一个语法正确的句子，较低概率输出无意义的句子。为了给你一个更直观感受，你可以在网上找到一整套“自动更正失败”的例子。其中，人们手机中的模型产生了一些相当有趣（并且常常不妥当）的自动生成的结果！",da,dl,ml,Ss,cs,ma,ds,Gn="较低的困惑度分数意味着更好的语言模型，我们可以看到，我们的初始模型的困惑度相当地高。让我们看看我们是否可以通过微调来降低它！为此，我们首先运行训练循环：",Ia,Il,jl,As,ms,Bn="然后像之前那样计算测试集上的结果困惑度：",ja,ul,hl,vs,Is,ua,js,gn="太棒了——困惑度显著降低，这告诉我们模型已经学习到了电影评论领域的一些知识！",ha,Ys,Fl,ba,Hs,us,fa,hs,_n="你可以使用 Hub 上的模型部件或者在本地使用🤗 Transformers 的 <code>pipeline</code> 加载微调模型预测文本。让我们使用后者通过 <code>fill-mask</code> pipeline 下载我们的模型：",Ca,bs,Ga,fs,kn="然后我们可以将文本“This is a great [MASK]”提供给 pipeline，看看前 5 个预测是什么：",Ba,Cs,ga,Gs,_a,Bs,Zn="Nice！—— 我们的模型显然已经调整了它的权重来预测与电影更密切相关的词！",ka,gs,Za,_s,Rn='这标志着我们第一次训练语言模型的实验到现在就结束了。在 <a href="https://chat.openai.com/course/en/chapter7/6" rel="nofollow">第 6 节</a> 中，你将学习如何从头开始训练一个自动回归模型，比如 GPT-2；如果你想看看如何预训练你自己的 Transformer 模型，就赶快去那里看看吧！',Ra,zl,Va,ks,Na,Ds,$a;B=new oM({props:{fw:f[0]}}),R=new Sl({props:{title:"微调掩码语言模型（masked language model）",local:"微调掩码语言模型（masked language model）",headingTag:"h1"}});const Vn=[mM,dM],Zs=[];function Nn(l,e){return l[0]==="pt"?0:1}C=Nn(f),V=Zs[C]=Vn[C](f),ll=new Wa({props:{id:"mqElG5QJWUg"}}),q=new xl({props:{$$slots:{default:[IM]},$$scope:{ctx:f}}}),Cl=new Sl({props:{title:"选择用于掩码语言建模的预训练模型",local:"选择用于掩码语言建模的预训练模型",headingTag:"h2"}});const $n=[uM,jM],Rs=[];function Wn(l,e){return l[0]==="pt"?0:1}E=Wn(f),D=Rs[E]=$n[E](f),Bl=new h({props:{code:"dGV4dCUyMCUzRCUyMCUyMlRoaXMlMjBpcyUyMGElMjBncmVhdCUyMCU1Qk1BU0slNUQuJTIy",highlighted:'text = <span class="hljs-string">&quot;This is a great [MASK].&quot;</span>',wrap:!1}}),Tl=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZChtb2RlbF9jaGVja3BvaW50KQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)`,wrap:!1}});const Qn=[bM,hM],Vs=[];function Xn(l,e){return l[0]==="pt"?0:1}b=Xn(f),Ul=Vs[b]=Qn[b](f),Ol=new h({props:{code:"JyUzRSUzRSUzRSUyMFRoaXMlMjBpcyUyMGElMjBncmVhdCUyMGRlYWwuJyUwQSclM0UlM0UlM0UlMjBUaGlzJTIwaXMlMjBhJTIwZ3JlYXQlMjBzdWNjZXNzLiclMEEnJTNFJTNFJTNFJTIwVGhpcyUyMGlzJTIwYSUyMGdyZWF0JTIwYWR2ZW50dXJlLiclMEEnJTNFJTNFJTNFJTIwVGhpcyUyMGlzJTIwYSUyMGdyZWF0JTIwaWRlYS4nJTBBJyUzRSUzRSUzRSUyMFRoaXMlMjBpcyUyMGElMjBncmVhdCUyMGZlYXQuJw==",highlighted:`<span class="hljs-string">&#x27;&gt;&gt;&gt; This is a great deal.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; This is a great success.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; This is a great adventure.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; This is a great idea.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; This is a great feat.&#x27;</span>`,wrap:!1}}),ee=new Sl({props:{title:"数据集",local:"数据集",headingTag:"h2"}}),ae=new h({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBJTBBaW1kYl9kYXRhc2V0JTIwJTNEJTIwbG9hZF9kYXRhc2V0KCUyMmltZGIlMjIpJTBBaW1kYl9kYXRhc2V0",highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

imdb_dataset = load_dataset(<span class="hljs-string">&quot;imdb&quot;</span>)
imdb_dataset`,wrap:!1}}),ne=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ3RleHQnJTJDJTIwJ2xhYmVsJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwMjUwMDAlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTIwJTIwJTIwJTIwdGVzdCUzQSUyMERhdGFzZXQoJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZmVhdHVyZXMlM0ElMjAlNUIndGV4dCclMkMlMjAnbGFiZWwnJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX3Jvd3MlM0ElMjAyNTAwMCUwQSUyMCUyMCUyMCUyMCU3RCklMEElMjAlMjAlMjAlMjB1bnN1cGVydmlzZWQlM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ3RleHQnJTJDJTIwJ2xhYmVsJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwNTAwMDAlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTdEKQ==",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;text&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>],
        num_rows: <span class="hljs-number">25000</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;text&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>],
        num_rows: <span class="hljs-number">25000</span>
    })
    unsupervised: Dataset({
        features: [<span class="hljs-string">&#x27;text&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>],
        num_rows: <span class="hljs-number">50000</span>
    })
})`,wrap:!1}}),Je=new h({props:{code:"c2FtcGxlJTIwJTNEJTIwaW1kYl9kYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQuc2h1ZmZsZShzZWVkJTNENDIpLnNlbGVjdChyYW5nZSgzKSklMEElMEFmb3IlMjByb3clMjBpbiUyMHNhbXBsZSUzQSUwQSUyMCUyMCUyMCUyMHByaW50KGYlMjIlNUNuJyUzRSUzRSUzRSUyMFJldmlldyUzQSUyMCU3QnJvdyU1Qid0ZXh0JyU1RCU3RCclMjIpJTBBJTIwJTIwJTIwJTIwcHJpbnQoZiUyMiclM0UlM0UlM0UlMjBMYWJlbCUzQSUyMCU3QnJvdyU1QidsYWJlbCclNUQlN0QnJTIyKQ==",highlighted:`sample = imdb_dataset[<span class="hljs-string">&quot;train&quot;</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">3</span>))

<span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> sample:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\\n&#x27;&gt;&gt;&gt; Review: <span class="hljs-subst">{row[<span class="hljs-string">&#x27;text&#x27;</span>]}</span>&#x27;&quot;</span>)
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&#x27;&gt;&gt;&gt; Label: <span class="hljs-subst">{row[<span class="hljs-string">&#x27;label&#x27;</span>]}</span>&#x27;&quot;</span>)`,wrap:!1}}),Te=new h({props:{code:" ",highlighted:`
<span class="hljs-string">&#x27;&gt;&gt;&gt; Review: This is your typical Priyadarshan movie--a bunch of loony characters out on some silly mission. His signature climax has the entire cast of the film coming together and fighting each other in some crazy moshpit over hidden money. Whether it is a winning lottery ticket in Malamaal Weekly, black money in Hera Pheri, &quot;kodokoo&quot; in Phir Hera Pheri, etc., etc., the director is becoming ridiculously predictable. Don\\&#x27;t get me wrong; as clichéd and preposterous his movies may be, I usually end up enjoying the comedy. However, in most his previous movies there has actually been some good humor, (Hungama and Hera Pheri being noteworthy ones). Now, the hilarity of his films is fading as he is using the same formula over and over again.&lt;br /&gt;&lt;br /&gt;Songs are good. Tanushree Datta looks awesome. Rajpal Yadav is irritating, and Tusshar is not a whole lot better. Kunal Khemu is OK, and Sharman Joshi is the best.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Label: 0&#x27;</span>

<span class="hljs-string">&#x27;&gt;&gt;&gt; Review: Okay, the story makes no sense, the characters lack any dimensionally, the best dialogue is ad-libs about the low quality of movie, the cinematography is dismal, and only editing saves a bit of the muddle, but Sam&quot; Peckinpah directed the film. Somehow, his direction is not enough. For those who appreciate Peckinpah and his great work, this movie is a disappointment. Even a great cast cannot redeem the time the viewer wastes with this minimal effort.&lt;br /&gt;&lt;br /&gt;The proper response to the movie is the contempt that the director San Peckinpah, James Caan, Robert Duvall, Burt Young, Bo Hopkins, Arthur Hill, and even Gig Young bring to their work. Watch the great Peckinpah films. Skip this mess.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Label: 0&#x27;</span>

<span class="hljs-string">&#x27;&gt;&gt;&gt; Review: I saw this movie at the theaters when I was about 6 or 7 years old. I loved it then, and have recently come to own a VHS version. &lt;br /&gt;&lt;br /&gt;My 4 and 6 year old children love this movie and have been asking again and again to watch it. &lt;br /&gt;&lt;br /&gt;I have enjoyed watching it again too. Though I have to admit it is not as good on a little TV.&lt;br /&gt;&lt;br /&gt;I do not have older children so I do not know what they would think of it. &lt;br /&gt;&lt;br /&gt;The songs are very cute. My daughter keeps singing them over and over.&lt;br /&gt;&lt;br /&gt;Hope this helps.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Label: 1&#x27;</span>`,wrap:!1}}),$l=new xl({props:{$$slots:{default:[fM]},$$scope:{ctx:f}}}),we=new Sl({props:{title:"预处理数据",local:"预处理数据",headingTag:"h2"}}),Ue=new Wa({props:{id:"8PmhEIXhBvI"}}),oe=new h({props:{code:"ZGVmJTIwdG9rZW5pemVfZnVuY3Rpb24oZXhhbXBsZXMpJTNBJTBBJTIwJTIwJTIwJTIwcmVzdWx0JTIwJTNEJTIwdG9rZW5pemVyKGV4YW1wbGVzJTVCJTIydGV4dCUyMiU1RCklMEElMjAlMjAlMjAlMjBpZiUyMHRva2VuaXplci5pc19mYXN0JTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmVzdWx0JTVCJTIyd29yZF9pZHMlMjIlNUQlMjAlM0QlMjAlNUJyZXN1bHQud29yZF9pZHMoaSklMjBmb3IlMjBpJTIwaW4lMjByYW5nZShsZW4ocmVzdWx0JTVCJTIyaW5wdXRfaWRzJTIyJTVEKSklNUQlMEElMjAlMjAlMjAlMjByZXR1cm4lMjByZXN1bHQlMEElMEElMEElMjMlMjAlRTQlQkQlQkYlRTclOTQlQTglMjBiYXRjaGVkJTNEVHJ1ZSUyMCVFNiU5RCVBNSVFNiVCRiU4MCVFNiVCNCVCQiVFNSVCRiVBQiVFOSU4MCU5RiVFNSVBNCU5QSVFNyVCQSVCRiVFNyVBOCU4QiElMEF0b2tlbml6ZWRfZGF0YXNldHMlMjAlM0QlMjBpbWRiX2RhdGFzZXQubWFwKCUwQSUyMCUyMCUyMCUyMHRva2VuaXplX2Z1bmN0aW9uJTJDJTIwYmF0Y2hlZCUzRFRydWUlMkMlMjByZW1vdmVfY29sdW1ucyUzRCU1QiUyMnRleHQlMjIlMkMlMjAlMjJsYWJlbCUyMiU1RCUwQSklMEF0b2tlbml6ZWRfZGF0YXNldHM=",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">examples</span>):
    result = tokenizer(examples[<span class="hljs-string">&quot;text&quot;</span>])
    <span class="hljs-keyword">if</span> tokenizer.is_fast:
        result[<span class="hljs-string">&quot;word_ids&quot;</span>] = [result.word_ids(i) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(result[<span class="hljs-string">&quot;input_ids&quot;</span>]))]
    <span class="hljs-keyword">return</span> result


<span class="hljs-comment"># 使用 batched=True 来激活快速多线程!</span>
tokenized_datasets = imdb_dataset.<span class="hljs-built_in">map</span>(
    tokenize_function, batched=<span class="hljs-literal">True</span>, remove_columns=[<span class="hljs-string">&quot;text&quot;</span>, <span class="hljs-string">&quot;label&quot;</span>]
)
tokenized_datasets`,wrap:!1}}),ce=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2F0dGVudGlvbl9tYXNrJyUyQyUyMCdpbnB1dF9pZHMnJTJDJTIwJ3dvcmRfaWRzJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwMjUwMDAlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTIwJTIwJTIwJTIwdGVzdCUzQSUyMERhdGFzZXQoJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZmVhdHVyZXMlM0ElMjAlNUInYXR0ZW50aW9uX21hc2snJTJDJTIwJ2lucHV0X2lkcyclMkMlMjAnd29yZF9pZHMnJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX3Jvd3MlM0ElMjAyNTAwMCUwQSUyMCUyMCUyMCUyMCU3RCklMEElMjAlMjAlMjAlMjB1bnN1cGVydmlzZWQlM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2F0dGVudGlvbl9tYXNrJyUyQyUyMCdpbnB1dF9pZHMnJTJDJTIwJ3dvcmRfaWRzJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwNTAwMDAlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTdEKQ==",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;word_ids&#x27;</span>],
        num_rows: <span class="hljs-number">25000</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;word_ids&#x27;</span>],
        num_rows: <span class="hljs-number">25000</span>
    })
    unsupervised: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;word_ids&#x27;</span>],
        num_rows: <span class="hljs-number">50000</span>
    })
})`,wrap:!1}}),Ie=new h({props:{code:"dG9rZW5pemVyLm1vZGVsX21heF9sZW5ndGg=",highlighted:"tokenizer.model_max_length",wrap:!1}}),je=new h({props:{code:"NTEy",highlighted:'<span class="hljs-number">512</span>',wrap:!1}}),Wl=new xl({props:{$$slots:{default:[CM]},$$scope:{ctx:f}}}),be=new h({props:{code:"Y2h1bmtfc2l6ZSUyMCUzRCUyMDEyOA==",highlighted:'chunk_size = <span class="hljs-number">128</span>',wrap:!1}}),Ql=new xl({props:{warning:!0,$$slots:{default:[GM]},$$scope:{ctx:f}}}),Ce=new h({props:{code:"JTIzJTIwJUU1JTg4JTg3JUU3JTg5JTg3JUU0JUJDJTlBJUU0JUI4JUJBJUU2JUFGJThGJUU0JUI4JUFBJUU3JTg5JUI5JUU1JUJFJTgxJUU3JTk0JTlGJUU2JTg4JTkwJUU0JUI4JTgwJUU0JUI4JUFBJUU1JTg4JTk3JUU4JUExJUE4JUU3JTlBJTg0JUU1JTg4JTk3JUU4JUExJUE4JTBBdG9rZW5pemVkX3NhbXBsZXMlMjAlM0QlMjB0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QiUzQTMlNUQlMEElMEFmb3IlMjBpZHglMkMlMjBzYW1wbGUlMjBpbiUyMGVudW1lcmF0ZSh0b2tlbml6ZWRfc2FtcGxlcyU1QiUyMmlucHV0X2lkcyUyMiU1RCklM0ElMEElMjAlMjAlMjAlMjBwcmludChmJTIyJyUzRSUzRSUzRSUyMFJldmlldyUyMCU3QmlkeCU3RCUyMGxlbmd0aCUzQSUyMCU3QmxlbihzYW1wbGUpJTdEJyUyMik=",highlighted:`<span class="hljs-comment"># 切片会为每个特征生成一个列表的列表</span>
tokenized_samples = tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>][:<span class="hljs-number">3</span>]

<span class="hljs-keyword">for</span> idx, sample <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(tokenized_samples[<span class="hljs-string">&quot;input_ids&quot;</span>]):
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&#x27;&gt;&gt;&gt; Review <span class="hljs-subst">{idx}</span> length: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(sample)}</span>&#x27;&quot;</span>)`,wrap:!1}}),Ge=new h({props:{code:"JyUzRSUzRSUzRSUyMFJldmlldyUyMDAlMjBsZW5ndGglM0ElMjAyMDAnJTBBJyUzRSUzRSUzRSUyMFJldmlldyUyMDElMjBsZW5ndGglM0ElMjA1NTknJTBBJyUzRSUzRSUzRSUyMFJldmlldyUyMDIlMjBsZW5ndGglM0ElMjAxOTIn",highlighted:`<span class="hljs-string">&#x27;&gt;&gt;&gt; Review 0 length: 200&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Review 1 length: 559&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Review 2 length: 192&#x27;</span>`,wrap:!1}}),ge=new h({props:{code:"Y29uY2F0ZW5hdGVkX2V4YW1wbGVzJTIwJTNEJTIwJTdCJTBBJTIwJTIwJTIwJTIwayUzQSUyMHN1bSh0b2tlbml6ZWRfc2FtcGxlcyU1QmslNUQlMkMlMjAlNUIlNUQpJTIwZm9yJTIwayUyMGluJTIwdG9rZW5pemVkX3NhbXBsZXMua2V5cygpJTBBJTdEJTBBdG90YWxfbGVuZ3RoJTIwJTNEJTIwbGVuKGNvbmNhdGVuYXRlZF9leGFtcGxlcyU1QiUyMmlucHV0X2lkcyUyMiU1RCklMEFwcmludChmJTIyJyUzRSUzRSUzRSUyMENvbmNhdGVuYXRlZCUyMHJldmlld3MlMjBsZW5ndGglM0ElMjAlN0J0b3RhbF9sZW5ndGglN0QnJTIyKQ==",highlighted:`concatenated_examples = {
    k: <span class="hljs-built_in">sum</span>(tokenized_samples[k], []) <span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> tokenized_samples.keys()
}
total_length = <span class="hljs-built_in">len</span>(concatenated_examples[<span class="hljs-string">&quot;input_ids&quot;</span>])
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&#x27;&gt;&gt;&gt; Concatenated reviews length: <span class="hljs-subst">{total_length}</span>&#x27;&quot;</span>)`,wrap:!1}}),_e=new h({props:{code:"JyUzRSUzRSUzRSUyMENvbmNhdGVuYXRlZCUyMHJldmlld3MlMjBsZW5ndGglM0ElMjA5NTEn",highlighted:'<span class="hljs-string">&#x27;&gt;&gt;&gt; Concatenated reviews length: 951&#x27;</span>',wrap:!1}}),Ze=new h({props:{code:"Y2h1bmtzJTIwJTNEJTIwJTdCJTBBJTIwJTIwJTIwJTIwayUzQSUyMCU1QnQlNUJpJTIwJTNBJTIwaSUyMCUyQiUyMGNodW5rX3NpemUlNUQlMjBmb3IlMjBpJTIwaW4lMjByYW5nZSgwJTJDJTIwdG90YWxfbGVuZ3RoJTJDJTIwY2h1bmtfc2l6ZSklNUQlMEElMjAlMjAlMjAlMjBmb3IlMjBrJTJDJTIwdCUyMGluJTIwY29uY2F0ZW5hdGVkX2V4YW1wbGVzLml0ZW1zKCklMEElN0QlMEElMEFmb3IlMjBjaHVuayUyMGluJTIwY2h1bmtzJTVCJTIyaW5wdXRfaWRzJTIyJTVEJTNBJTBBJTIwJTIwJTIwJTIwcHJpbnQoZiUyMiclM0UlM0UlM0UlMjBDaHVuayUyMGxlbmd0aCUzQSUyMCU3QmxlbihjaHVuayklN0QnJTIyKQ==",highlighted:`chunks = {
    k: [t[i : i + chunk_size] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, total_length, chunk_size)]
    <span class="hljs-keyword">for</span> k, t <span class="hljs-keyword">in</span> concatenated_examples.items()
}

<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> chunks[<span class="hljs-string">&quot;input_ids&quot;</span>]:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&#x27;&gt;&gt;&gt; Chunk length: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(chunk)}</span>&#x27;&quot;</span>)`,wrap:!1}}),Re=new h({props:{code:"JyUzRSUzRSUzRSUyMENodW5rJTIwbGVuZ3RoJTNBJTIwMTI4JyUwQSclM0UlM0UlM0UlMjBDaHVuayUyMGxlbmd0aCUzQSUyMDEyOCclMEEnJTNFJTNFJTNFJTIwQ2h1bmslMjBsZW5ndGglM0ElMjAxMjgnJTBBJyUzRSUzRSUzRSUyMENodW5rJTIwbGVuZ3RoJTNBJTIwMTI4JyUwQSclM0UlM0UlM0UlMjBDaHVuayUyMGxlbmd0aCUzQSUyMDEyOCclMEEnJTNFJTNFJTNFJTIwQ2h1bmslMjBsZW5ndGglM0ElMjAxMjgnJTBBJyUzRSUzRSUzRSUyMENodW5rJTIwbGVuZ3RoJTNBJTIwMTI4JyUwQSclM0UlM0UlM0UlMjBDaHVuayUyMGxlbmd0aCUzQSUyMDU1Jw==",highlighted:`<span class="hljs-string">&#x27;&gt;&gt;&gt; Chunk length: 128&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Chunk length: 128&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Chunk length: 128&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Chunk length: 128&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Chunk length: 128&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Chunk length: 128&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Chunk length: 128&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; Chunk length: 55&#x27;</span>`,wrap:!1}}),We=new h({props:{code:"ZGVmJTIwZ3JvdXBfdGV4dHMoZXhhbXBsZXMpJTNBJTBBJTIwJTIwJTIwJTIwJTIzJTIwJUU2JThCJUJDJUU2JThFJUE1JUU2JTg5JTgwJUU2JTlDJTg5JUU3JTlBJTg0JUU2JTk2JTg3JUU2JTlDJUFDJTBBJTIwJTIwJTIwJTIwY29uY2F0ZW5hdGVkX2V4YW1wbGVzJTIwJTNEJTIwJTdCayUzQSUyMHN1bShleGFtcGxlcyU1QmslNUQlMkMlMjAlNUIlNUQpJTIwZm9yJTIwayUyMGluJTIwZXhhbXBsZXMua2V5cygpJTdEJTBBJTIwJTIwJTIwJTIwJTIzJTIwJUU4JUFFJUExJUU3JUFFJTk3JUU2JThCJUJDJUU2JThFJUE1JUU2JTk2JTg3JUU2JTlDJUFDJUU3JTlBJTg0JUU5JTk1JUJGJUU1JUJBJUE2JTBBJTIwJTIwJTIwJTIwdG90YWxf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MjByZXN1bHQlNUIlMjJsYWJlbHMlMjIlNUQlMjAlM0QlMjByZXN1bHQlNUIlMjJpbnB1dF9pZHMlMjIlNUQuY29weSgpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwcmVzdWx0",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">group_texts</span>(<span class="hljs-params">examples</span>):
    <span class="hljs-comment"># 拼接所有的文本</span>
    concatenated_examples = {k: <span class="hljs-built_in">sum</span>(examples[k], []) <span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> examples.keys()}
    <span class="hljs-comment"># 计算拼接文本的长度</span>
    total_length = <span class="hljs-built_in">len</span>(concatenated_examples[<span class="hljs-built_in">list</span>(examples.keys())[<span class="hljs-number">0</span>]])
    <span class="hljs-comment"># 如果最后一个块小于 chunk_size,我们将其丢弃</span>
    total_length = (total_length // chunk_size) * chunk_size
    <span class="hljs-comment"># 按最大长度分块</span>
    result = {
        k: [t[i : i + chunk_size] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, total_length, chunk_size)]
        <span class="hljs-keyword">for</span> k, t <span class="hljs-keyword">in</span> concatenated_examples.items()
    }
    <span class="hljs-comment"># 创建一个新的 labels 列</span>
    result[<span class="hljs-string">&quot;labels&quot;</span>] = result[<span class="hljs-string">&quot;input_ids&quot;</span>].copy()
    <span class="hljs-keyword">return</span> result`,wrap:!1}}),Ee=new h({props:{code:"bG1fZGF0YXNldHMlMjAlM0QlMjB0b2tlbml6ZWRfZGF0YXNldHMubWFwKGdyb3VwX3RleHRzJTJDJTIwYmF0Y2hlZCUzRFRydWUpJTBBbG1fZGF0YXNldHM=",highlighted:`lm_datasets = tokenized_datasets.<span class="hljs-built_in">map</span>(group_texts, batched=<span class="hljs-literal">True</span>)
lm_datasets`,wrap:!1}}),Fe=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2F0dGVudGlvbl9tYXNrJyUyQyUyMCdpbnB1dF9pZHMnJTJDJTIwJ2xhYmVscyclMkMlMjAnd29yZF9pZHMnJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX3Jvd3MlM0ElMjA2MTI4OSUwQSUyMCUyMCUyMCUyMCU3RCklMEElMjAlMjAlMjAlMjB0ZXN0JTNBJTIwRGF0YXNldCglN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlcyUzQSUyMCU1QidhdHRlbnRpb25fbWFzayclMkMlMjAnaW5wdXRfaWRzJyUyQyUyMCdsYWJlbHMnJTJDJTIwJ3dvcmRfaWRzJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwNTk5MDUlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTIwJTIwJTIwJTIwdW5zdXBlcnZpc2VkJTNBJTIwRGF0YXNldCglN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlcyUzQSUyMCU1QidhdHRlbnRpb25fbWFzayclMkMlMjAnaW5wdXRfaWRzJyUyQyUyMCdsYWJlbHMnJTJDJTIwJ3dvcmRfaWRzJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwMTIyOTYzJTBBJTIwJTIwJTIwJTIwJTdEKSUwQSU3RCk=",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;labels&#x27;</span>, <span class="hljs-string">&#x27;word_ids&#x27;</span>],
        num_rows: <span class="hljs-number">61289</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;labels&#x27;</span>, <span class="hljs-string">&#x27;word_ids&#x27;</span>],
        num_rows: <span class="hljs-number">59905</span>
    })
    unsupervised: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;labels&#x27;</span>, <span class="hljs-string">&#x27;word_ids&#x27;</span>],
        num_rows: <span class="hljs-number">122963</span>
    })
})`,wrap:!1}}),xe=new h({props:{code:"dG9rZW5pemVyLmRlY29kZShsbV9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVEJTVCMSU1RCU1QiUyMmlucHV0X2lkcyUyMiU1RCk=",highlighted:'tokenizer.decode(lm_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">1</span>][<span class="hljs-string">&quot;input_ids&quot;</span>])',wrap:!1}}),Se=new h({props:{code:"JTIyLi4uLiUyMGF0Li4uLi4uLi4uLiUyMGhpZ2guJTIwYSUyMGNsYXNzaWMlMjBsaW5lJTIwJTNBJTIwaW5zcGVjdG9yJTIwJTNBJTIwaSdtJTIwaGVyZSUyMHRvJTIwc2FjayUyMG9uZSUyMG9mJTIweW91ciUyMHRlYWNoZXJzLiUyMHN0dWRlbnQlMjAlM0ElMjB3ZWxjb21lJTIwdG8lMjBicm9td2VsbCUyMGhpZ2guJTIwaSUyMGV4cGVjdCUyMHRoYXQlMjBtYW55JTIwYWR1bHRzJTIwb2YlMjBteSUyMGFnZSUyMHRoaW5rJTIwdGhhdCUyMGJyb213ZWxsJTIwaGlnaCUyMGlzJTIwZmFyJTIwZmV0Y2hlZC4lMjB3aGF0JTIwYSUyMHBpdHklMjB0aGF0JTIwaXQlMjBpc24ndCElMjAlNUJTRVAlNUQlMjAlNUJDTFMlNUQlMjBob21lbGVzc25lc3MlMjAoJTIwb3IlMjBob3VzZWxlc3NuZXNzJTIwYXMlMjBnZW9yZ2UlMjBjYXJsaW4lMjBzdGF0ZWQlMjApJTIwaGFzJTIwYmVlbiUyMGFuJTIwaXNzdWUlMjBmb3IlMjB5ZWFycyUyMGJ1dCUyMG5ldmVyJTIwYSUyMHBsYW4lMjB0byUyMGhlbHAlMjB0aG9zZSUyMG9uJTIwdGhlJTIwc3RyZWV0JTIwdGhhdCUyMHdlcmUlMjBvbmNlJTIwY29uc2lkZXJlZCUyMGh1bWFuJTIwd2hvJTIwZGlkJTIwZXZlcnl0aGluZyUyMGZyb20lMjBnb2luZyUyMHRvJTIwc2Nob29sJTJDJTIwd29yayUyQyUyMG9yJTIwdm90ZSUyMGZvciUyMHRoZSUyMG1hdHRlci4lMjBtb3N0JTIwcGVvcGxlJTIwdGhpbmslMjBvZiUyMHRoZSUyMGhvbWVsZXNzJTIy",highlighted:'<span class="hljs-string">&quot;.... at.......... high. a classic line : inspector : i&#x27;m here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn&#x27;t! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless&quot;</span>',wrap:!1}}),ve=new h({props:{code:"dG9rZW5pemVyLmRlY29kZShsbV9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVEJTVCMSU1RCU1QiUyMmxhYmVscyUyMiU1RCk=",highlighted:'tokenizer.decode(lm_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">1</span>][<span class="hljs-string">&quot;labels&quot;</span>])',wrap:!1}}),Ye=new h({props:{code:"JTIyLi4uLiUyMGF0Li4uLi4uLi4uLiUyMGhpZ2guJTIwYSUyMGNsYXNzaWMlMjBsaW5lJTIwJTNBJTIwaW5zcGVjdG9yJTIwJTNBJTIwaSdtJTIwaGVyZSUyMHRvJTIwc2FjayUyMG9uZSUyMG9mJTIweW91ciUyMHRlYWNoZXJzLiUyMHN0dWRlbnQlMjAlM0ElMjB3ZWxjb21lJTIwdG8lMjBicm9td2VsbCUyMGhpZ2guJTIwaSUyMGV4cGVjdCUyMHRoYXQlMjBtYW55JTIwYWR1bHRzJTIwb2YlMjBteSUyMGFnZSUyMHRoaW5rJTIwdGhhdCUyMGJyb213ZWxsJTIwaGlnaCUyMGlzJTIwZmFyJTIwZmV0Y2hlZC4lMjB3aGF0JTIwYSUyMHBpdHklMjB0aGF0JTIwaXQlMjBpc24ndCElMjAlNUJTRVAlNUQlMjAlNUJDTFMlNUQlMjBob21lbGVzc25lc3MlMjAoJTIwb3IlMjBob3VzZWxlc3NuZXNzJTIwYXMlMjBnZW9yZ2UlMjBjYXJsaW4lMjBzdGF0ZWQlMjApJTIwaGFzJTIwYmVlbiUyMGFuJTIwaXNzdWUlMjBmb3IlMjB5ZWFycyUyMGJ1dCUyMG5ldmVyJTIwYSUyMHBsYW4lMjB0byUyMGhlbHAlMjB0aG9zZSUyMG9uJTIwdGhlJTIwc3RyZWV0JTIwdGhhdCUyMHdlcmUlMjBvbmNlJTIwY29uc2lkZXJlZCUyMGh1bWFuJTIwd2hvJTIwZGlkJTIwZXZlcnl0aGluZyUyMGZyb20lMjBnb2luZyUyMHRvJTIwc2Nob29sJTJDJTIwd29yayUyQyUyMG9yJTIwdm90ZSUyMGZvciUyMHRoZSUyMG1hdHRlci4lMjBtb3N0JTIwcGVvcGxlJTIwdGhpbmslMjBvZiUyMHRoZSUyMGhvbWVsZXNzJTIy",highlighted:'<span class="hljs-string">&quot;.... at.......... high. a classic line : inspector : i&#x27;m here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn&#x27;t! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless&quot;</span>',wrap:!1}}),Le=new Sl({props:{title:"使用 Trainer API 微调 DistilBERT",local:"使用 Trainer API 微调 DistilBERT",headingTag:"h2"}}),qe=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvckZvckxhbmd1YWdlTW9kZWxpbmclMEElMEFkYXRhX2NvbGxhdG9yJTIwJTNEJTIwRGF0YUNvbGxhdG9yRm9yTGFuZ3VhZ2VNb2RlbGluZyh0b2tlbml6ZXIlM0R0b2tlbml6ZXIlMkMlMjBtbG1fcHJvYmFiaWxpdHklM0QwLjE1KQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForLanguageModeling

data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=<span class="hljs-number">0.15</span>)`,wrap:!1}}),Pe=new h({props:{code:"c2FtcGxlcyUyMCUzRCUyMCU1QmxtX2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUJpJTVEJTIwZm9yJTIwaSUyMGluJTIwcmFuZ2UoMiklNUQlMEFmb3IlMjBzYW1wbGUlMjBpbiUyMHNhbXBsZXMlM0ElMEElMjAlMjAlMjAlMjBfJTIwJTNEJTIwc2FtcGxlLnBvcCglMjJ3b3JkX2lkcyUyMiklMEElMEFmb3IlMjBjaHVuayUyMGluJTIwZGF0YV9jb2xsYXRvcihzYW1wbGVzKSU1QiUyMmlucHV0X2lkcyUyMiU1RCUzQSUwQSUyMCUyMCUyMCUyMHByaW50KGYlMjIlNUNuJyUzRSUzRSUzRSUyMCU3QnRva2VuaXplci5kZWNvZGUoY2h1bmspJTdEJyUyMik=",highlighted:`samples = [lm_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">2</span>)]
<span class="hljs-keyword">for</span> sample <span class="hljs-keyword">in</span> samples:
    _ = sample.pop(<span class="hljs-string">&quot;word_ids&quot;</span>)

<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> data_collator(samples)[<span class="hljs-string">&quot;input_ids&quot;</span>]:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\\n&#x27;&gt;&gt;&gt; <span class="hljs-subst">{tokenizer.decode(chunk)}</span>&#x27;&quot;</span>)`,wrap:!1}}),Oe=new h({props:{code:"JyUzRSUzRSUzRSUyMCU1QkNMUyU1RCUyMGJyb213ZWxsJTIwJTVCTUFTSyU1RCUyMGlzJTIwYSUyMGNhcnRvb24lMjBjb21lZHkuJTIwaXQlMjByYW4lMjBhdCUyMHRoZSUyMHNhbWUlMjAlNUJNQVNLJTVEJTIwYXMlMjBzb21lJTIwb3RoZXIlMjAlNUJNQVNLJTVEJTIwYWJvdXQlMjBzY2hvb2wlMjBsaWZlJTJDJTIwJTVCTUFTSyU1RCUyMGFzJTIwJTIyJTIwdGVhY2hlcnMlMjAlMjIuJTIwJTVCTUFTSyU1RCUyMCU1Qk1BU0slNUQlMjAlNUJNQVNLJTVEJTIwaW4lMjB0aGUlMjB0ZWFjaGluZyUyMCU1Qk1BU0slNUQlMjBsZWFkJTIwJTVCTUFTSyU1RCUyMHRvJTIwYmVsaWV2ZSUyMHRoYXQlMjBicm9td2VsbCUyMGhpZ2glNUMnJTVCTUFTSyU1RCUyMHNhdGlyZSUyMGlzJTIwbXVjaCUyMGNsb3NlciUyMHRvJTIwcmVhbGl0eSUyMHRoYW4lMjBpcyUyMCUyMiUyMHRlYWNoZXJzJTIwJTIyLiUyMHRoZSUyMHNjcmFtYmxlJTIwJTVCTUFTSyU1RCUyMCU1Qk1BU0slNUQlMjBmaW5hbmNpYWxseSUyQyUyMHRoZSUyMCU1Qk1BU0slNURmdWwlMjBzdHVkZW50cyUyMHdob2duJTIwJTVCTUFTSyU1RCUyMHJpZ2h0JTIwdGhyb3VnaCUyMCU1Qk1BU0slNUQlMjBwYXRoZXRpYyUyMHRlYWNoZXJzJTVDJ3BvbXAlMkMlMjB0aGUlMjBwZXR0aW5lc3MlMjBvZiUyMHRoZSUyMHdob2xlJTIwc2l0dWF0aW9uJTJDJTIw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ODdpbiUyMHN0YXRlZCUyMCklRTUlODUlQUMlMjBiZWVuJTIwYW4lMjBpc3N1ZSUyMGZvciUyMHllYXJzJTIwYnV0JTIwbmV2ZXIlMjAlNUJNQVNLJTVEJTIwcGxhbiUyMHRvJTIwaGVscCUyMHRob3NlJTIwb24lMjB0aGUlMjBzdHJlZXQlMjB0aGF0JTIwd2VyZSUyMG9uY2UlMjBjb25zaWRlcmVkJTIwaHVtYW4lMjAlNUJNQVNLJTVEJTIwZGlkJTIwZXZlcnl0aGluZyUyMGZyb20lMjBnb2luZyUyMHRvJTIwc2Nob29sJTJDJTIwJTVCTUFTSyU1RCUyQyUyMCU1Qk1BU0slNUQlMjB2b3RlJTIwZm9yJTIwdGhlJTIwbWF0dGVyLiUyMG1vc3QlMjBwZW9wbGUlMjB0aGluayUyMCU1Qk1BU0slNUQlMjB0aGUlMjBob21lbGVzcyc=",highlighted:`<span class="hljs-string">&#x27;&gt;&gt;&gt; [CLS] bromwell [MASK] is a cartoon comedy. it ran at the same [MASK] as some other [MASK] about school life, [MASK] as &quot; teachers &quot;. [MASK] [MASK] [MASK] in the teaching [MASK] lead [MASK] to believe that bromwell high\\&#x27;[MASK] satire is much closer to reality than is &quot; teachers &quot;. the scramble [MASK] [MASK] financially, the [MASK]ful students whogn [MASK] right through [MASK] pathetic teachers\\&#x27;pomp, the pettiness of the whole situation, distinction remind me of the schools i knew and their students. when i saw [MASK] episode in [MASK] a student repeatedly tried to burn down the school, [MASK] immediately recalled. [MASK]...&#x27;</span>

<span class="hljs-string">&#x27;&gt;&gt;&gt; .... at.. [MASK]... [MASK]... high. a classic line plucked inspector : i\\&#x27;[MASK] here to [MASK] one of your [MASK]. student : welcome to bromwell [MASK]. i expect that many adults of my age think that [MASK]mwell [MASK] is [MASK] fetched. what a pity that it isn\\&#x27;t! [SEP] [CLS] [MASK]ness ( or [MASK]lessness as george 宇in stated )公 been an issue for years but never [MASK] plan to help those on the street that were once considered human [MASK] did everything from going to school, [MASK], [MASK] vote for the matter. most people think [MASK] the homeless&#x27;</span>`,wrap:!1}}),Xl=new xl({props:{$$slots:{default:[BM]},$$scope:{ctx:f}}});let L=f[0]==="pt"&&aM();const En=[_M,gM],Ns=[];function Fn(l,e){return l[0]==="pt"?0:1}yl=Fn(f),rl=Ns[yl]=En[yl](f),ts=new h({props:{code:"c2FtcGxlcyUyMCUzRCUyMCU1QmxtX2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUJpJTVEJTIwZm9yJTIwaSUyMGluJTIwcmFuZ2UoMiklNUQlMEFiYXRjaCUyMCUzRCUyMHdob2xlX3dvcmRfbWFza2luZ19kYXRhX2NvbGxhdG9yKHNhbXBsZXMpJTBBJTBBZm9yJTIwY2h1bmslMjBpbiUyMGJhdGNoJTVCJTIyaW5wdXRfaWRzJTIyJTVEJTNBJTBBJTIwJTIwJTIwJTIwcHJpbnQoZiUyMiU1Q24nJTNFJTNFJTNFJTIwJTdCdG9rZW5pemVyLmRlY29kZShjaHVuayklN0QnJTIyKQ==",highlighted:`samples = [lm_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">2</span>)]
batch = whole_word_masking_data_collator(samples)

<span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> batch[<span class="hljs-string">&quot;input_ids&quot;</span>]:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\\n&#x27;&gt;&gt;&gt; <span class="hljs-subst">{tokenizer.decode(chunk)}</span>&#x27;&quot;</span>)`,wrap:!1}}),as=new h({props:{code:"JyUzRSUzRSUzRSUyMCU1QkNMUyU1RCUyMGJyb213ZWxsJTIwaGlnaCUyMGlzJTIwYSUyMGNhcnRvb24lMjBjb21lZHklMjAlNUJNQVNLJTVEJTIwaXQlMjByYW4lMjBhdCUyMHRoZSUyMHNhbWUlMjB0aW1lJTIwYXMlMjBzb21lJTIwb3RoZXIlMjBwcm9nc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MGElMjBjbGFzc2ljJTIwbGluZSUyMCUzQSUyMGluc3BlY3RvciUyMCUzQSUyMGklNUMnbSUyMGhlcmUlMjB0byUyMHNhY2slMjBvbmUlMjBvZiUyMHlvdXIlMjB0ZWFjaGVycy4lMjBzdHVkZW50JTIwJTNBJTIwd2VsY29tZSUyMHRvJTIwYnJvbXdlbGwlMjBoaWdoLiUyMGklMjBleHBlY3QlMjB0aGF0JTIwbWFueSUyMGFkdWx0cyUyMG9mJTIwbXklMjBhZ2UlMjB0aGluayUyMHRoYXQlMjBicm9td2VsbCUyMGhpZ2glMjBpcyUyMGZhciUyMGZldGNoZWQuJTIwd2hhdCUyMGElMjBwaXR5JTIwdGhhdCUyMGl0JTIwaXNuJTVDJ3QhJTIwJTVCU0VQJTVEJTIwJTVCQ0xTJTVEJTIwaG9tZWxlc3NuZXNzJTIwKCUyMG9yJTIwaG91c2VsZXNzbmVzcyUyMGFzJTIwZ2VvcmdlJTIwY2FybGluJTIwc3RhdGVkJTIwKSUyMGhhcyUyMGJlZW4lMjBhbiUyMGlzc3VlJTIwZm9yJTIweWVhcnMlMjBidXQlMjBuZXZlciUyMGElMjBwbGFuJTIwdG8lMjBoZWxwJTIwdGhvc2UlMjBvbiUyMHRoZSUyMHN0cmVldCUyMHRoYXQlMjB3ZXJlJTIwb25jZSUyMGNvbnNpZGVyZWQlMjBodW1hbiUyMHdobyUyMGRpZCUyMGV2ZXJ5dGhpbmclMjBmcm9tJTIwZ29pbmclMjB0byUyMHNjaG9vbCUyQyUyMHdvcmslMkMlMjBvciUyMHZvdGUlMjBmb3IlMjB0aGUlMjBtYXR0ZXIuJTIwbW9zdCUyMHBlb3BsZSUyMHRoaW5rJTIwb2YlMjB0aGUlMjBob21lbGVzcyc=",highlighted:`<span class="hljs-string">&#x27;&gt;&gt;&gt; [CLS] bromwell high is a cartoon comedy [MASK] it ran at the same time as some other programs about school life, such as &quot; teachers &quot;. my 35 years in the teaching profession lead me to believe that bromwell high\\&#x27;s satire is much closer to reality than is &quot; teachers &quot;. the scramble to survive financially, the insightful students who can see right through their pathetic teachers\\&#x27;pomp, the pettiness of the whole situation, all remind me of the schools i knew and their students. when i saw the episode in which a student repeatedly tried to burn down the school, i immediately recalled.....&#x27;</span>

<span class="hljs-string">&#x27;&gt;&gt;&gt; .... [MASK] [MASK] [MASK] [MASK]....... high. a classic line : inspector : i\\&#x27;m here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn\\&#x27;t! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless&#x27;</span>`,wrap:!1}}),El=new xl({props:{$$slots:{default:[kM]},$$scope:{ctx:f}}}),Ms=new h({props:{code:"dHJhaW5fc2l6ZSUyMCUzRCUyMDEwXzAwMCUwQXRlc3Rfc2l6ZSUyMCUzRCUyMGludCgwLjElMjAqJTIwdHJhaW5fc2l6ZSklMEElMEFkb3duc2FtcGxlZF9kYXRhc2V0JTIwJTNEJTIwbG1fZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RC50cmFpbl90ZXN0X3NwbGl0KCUwQSUyMCUyMCUyMCUyMHRyYWluX3NpemUlM0R0cmFpbl9zaXplJTJDJTIwdGVzdF9zaXplJTNEdGVzdF9zaXplJTJDJTIwc2VlZCUzRDQyJTBBKSUwQWRvd25zYW1wbGVkX2RhdGFzZXQ=",highlighted:`train_size = <span class="hljs-number">10_000</span>
test_size = <span class="hljs-built_in">int</span>(<span class="hljs-number">0.1</span> * train_size)

downsampled_dataset = lm_datasets[<span class="hljs-string">&quot;train&quot;</span>].train_test_split(
    train_size=train_size, test_size=test_size, seed=<span class="hljs-number">42</span>
)
downsampled_dataset`,wrap:!1}}),Js=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2F0dGVudGlvbl9tYXNrJyUyQyUyMCdpbnB1dF9pZHMnJTJDJTIwJ2xhYmVscyclMkMlMjAnd29yZF9pZHMnJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX3Jvd3MlM0ElMjAxMDAwMCUwQSUyMCUyMCUyMCUyMCU3RCklMEElMjAlMjAlMjAlMjB0ZXN0JTNBJTIwRGF0YXNldCglN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlcyUzQSUyMCU1QidhdHRlbnRpb25fbWFzayclMkMlMjAnaW5wdXRfaWRzJyUyQyUyMCdsYWJlbHMnJTJDJTIwJ3dvcmRfaWRzJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwMTAwMCUwQSUyMCUyMCUyMCUyMCU3RCklMEElN0Qp",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;labels&#x27;</span>, <span class="hljs-string">&#x27;word_ids&#x27;</span>],
        num_rows: <span class="hljs-number">10000</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;labels&#x27;</span>, <span class="hljs-string">&#x27;word_ids&#x27;</span>],
        num_rows: <span class="hljs-number">1000</span>
    })
})`,wrap:!1}}),ps=new h({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`,wrap:!1}}),ws=new h({props:{code:"aHVnZ2luZ2ZhY2UtY2xpJTIwbG9naW4=",highlighted:'huggingface-<span class="hljs-keyword">cli</span> login',wrap:!1}});const zn=[RM,ZM],$s=[];function xn(l,e){return l[0]==="tf"?0:1}ol=xn(f),cl=$s[ol]=zn[ol](f),ys=new Sl({props:{title:"语言模型的困惑度（perplexity）",local:"语言模型的困惑度(perplexity)",headingTag:"h3"}}),rs=new Wa({props:{id:"NURcDHhYe98"}});const Sn=[NM,VM],Ws=[];function An(l,e){return l[0]==="pt"?0:1}dl=An(f),ml=Ws[dl]=Sn[dl](f),cs=new h({props:{code:"UGVycGxleGl0eSUzQSUyMDIxLjc1",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>Perplexity: <span class="hljs-number">21.75</span>',wrap:!1}});const vn=[WM,$M],Qs=[];function Yn(l,e){return l[0]==="pt"?0:1}Il=Yn(f),jl=Qs[Il]=vn[Il](f);const Hn=[XM,QM],Xs=[];function Ln(l,e){return l[0]==="pt"?0:1}ul=Ln(f),hl=Xs[ul]=Hn[ul](f),Is=new h({props:{code:"UGVycGxleGl0eSUzQSUyMDExLjMy",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>Perplexity: <span class="hljs-number">11.32</span>',wrap:!1}});let F=f[0]==="pt"&&nM();Fl=new xl({props:{$$slots:{default:[EM]},$$scope:{ctx:f}}});let z=f[0]==="pt"&&MM();return us=new Sl({props:{title:"使用我们微调的模型",local:"使用我们微调的模型",headingTag:"h2"}}),bs=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBbWFza19maWxsZXIlMjAlM0QlMjBwaXBlbGluZSglMEElMjAlMjAlMjAlMjAlMjJmaWxsLW1hc2slMjIlMkMlMjBtb2RlbCUzRCUyMmh1Z2dpbmdmYWNlLWNvdXJzZSUyRmRpc3RpbGJlcnQtYmFzZS11bmNhc2VkLWZpbmV0dW5lZC1pbWRiJTIyJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

mask_filler = pipeline(
    <span class="hljs-string">&quot;fill-mask&quot;</span>, model=<span class="hljs-string">&quot;huggingface-course/distilbert-base-uncased-finetuned-imdb&quot;</span>
)`,wrap:!1}}),Cs=new h({props:{code:"cHJlZHMlMjAlM0QlMjBtYXNrX2ZpbGxlcih0ZXh0KSUwQSUwQWZvciUyMHByZWQlMjBpbiUyMHByZWRzJTNBJTBBJTIwJTIwJTIwJTIwcHJpbnQoZiUyMiUzRSUzRSUzRSUyMCU3QnByZWQlNUInc2VxdWVuY2UnJTVEJTdEJTIyKQ==",highlighted:`preds = mask_filler(text)

<span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> preds:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;&gt;&gt;&gt; <span class="hljs-subst">{pred[<span class="hljs-string">&#x27;sequence&#x27;</span>]}</span>&quot;</span>)`,wrap:!1}}),Gs=new h({props:{code:"JyUzRSUzRSUzRSUyMHRoaXMlMjBpcyUyMGElMjBncmVhdCUyMG1vdmllLiclMEEnJTNFJTNFJTNFJTIwdGhpcyUyMGlzJTIwYSUyMGdyZWF0JTIwZmlsbS4nJTBBJyUzRSUzRSUzRSUyMHRoaXMlMjBpcyUyMGElMjBncmVhdCUyMHN0b3J5LiclMEEnJTNFJTNFJTNFJTIwdGhpcyUyMGlzJTIwYSUyMGdyZWF0JTIwbW92aWVzLiclMEEnJTNFJTNFJTNFJTIwdGhpcyUyMGlzJTIwYSUyMGdyZWF0JTIwY2hhcmFjdGVyLic=",highlighted:`<span class="hljs-string">&#x27;&gt;&gt;&gt; this is a great movie.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; this is a great film.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; this is a great story.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; this is a great movies.&#x27;</span>
<span class="hljs-string">&#x27;&gt;&gt;&gt; this is a great character.&#x27;</span>`,wrap:!1}}),gs=new Wa({props:{id:"0Oxphw4Q9fo"}}),zl=new xl({props:{$$slots:{default:[FM]},$$scope:{ctx:f}}}),ks=new cM({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter7/3.mdx"}}),{c(){a=d("meta"),U=M(),n=d("p"),w=M(),y(B.$$.fragment),I=M(),y(R.$$.fragment),$=M(),V.c(),k=M(),Z=d("p"),Z.textContent=u,_=M(),Q=d("p"),Q.textContent=X,G=M(),W=d("p"),W.innerHTML=S,g=M(),N=d("div"),N.innerHTML=Ls,Zl=M(),A=d("p"),A.innerHTML=vl,K=M(),x=d("iframe"),P=M(),O=d("p"),O.textContent=fl,Hl=M(),y(ll.$$.fragment),el=M(),y(q.$$.fragment),sl=M(),y(Cl.$$.fragment),Rl=M(),v=d("p"),v.innerHTML=Ll,tl=M(),il=d("div"),il.innerHTML=Dl,al=M(),nl=d("p"),nl.innerHTML=Gl,Vl=M(),D.c(),wl=M(),Y=d("p"),Y.textContent=ql,Ml=M(),y(Bl.$$.fragment),Nl=M(),H=d("p"),H.innerHTML=Kl,Jl=M(),y(Tl.$$.fragment),pl=M(),gl=d("p"),gl.textContent=Pl,T=M(),Ul.c(),Es=M(),y(Ol.$$.fragment),qs=M(),le=d("p"),le.textContent=Ya,Ks=M(),y(ee.$$.fragment),Ps=M(),se=d("p"),se.innerHTML=Ha,Os=M(),te=d("p"),te.innerHTML=La,lt=M(),y(ae.$$.fragment),et=M(),y(ne.$$.fragment),st=M(),Me=d("p"),Me.innerHTML=Da,tt=M(),y(Je.$$.fragment),at=M(),y(Te.$$.fragment),nt=M(),pe=d("p"),pe.innerHTML=qa,Mt=M(),y($l.$$.fragment),Jt=M(),ie=d("p"),ie.innerHTML=Ka,Tt=M(),y(we.$$.fragment),pt=M(),y(Ue.$$.fragment),it=M(),ye=d("p"),ye.textContent=Pa,wt=M(),re=d("p"),re.innerHTML=Oa,Ut=M(),y(oe.$$.fragment),yt=M(),y(ce.$$.fragment),rt=M(),de=d("p"),de.innerHTML=ln,ot=M(),me=d("p"),me.innerHTML=en,ct=M(),y(Ie.$$.fragment),dt=M(),y(je.$$.fragment),mt=M(),ue=d("p"),ue.innerHTML=sn,It=M(),y(Wl.$$.fragment),jt=M(),he=d("p"),he.textContent=tn,ut=M(),y(be.$$.fragment),ht=M(),y(Ql.$$.fragment),bt=M(),fe=d("p"),fe.textContent=an,ft=M(),y(Ce.$$.fragment),Ct=M(),y(Ge.$$.fragment),Gt=M(),Be=d("p"),Be.textContent=nn,Bt=M(),y(ge.$$.fragment),gt=M(),y(_e.$$.fragment),_t=M(),ke=d("p"),ke.innerHTML=Mn,kt=M(),y(Ze.$$.fragment),Zt=M(),y(Re.$$.fragment),Rt=M(),Ve=d("p"),Ve.textContent=Jn,Vt=M(),Ne=d("ul"),Ne.innerHTML=Tn,Nt=M(),$e=d("p"),$e.textContent=pn,$t=M(),y(We.$$.fragment),Wt=M(),Qe=d("p"),Qe.innerHTML=wn,Qt=M(),Xe=d("p"),Xe.innerHTML=Un,Xt=M(),y(Ee.$$.fragment),Et=M(),y(Fe.$$.fragment),Ft=M(),ze=d("p"),ze.innerHTML=yn,zt=M(),y(xe.$$.fragment),xt=M(),y(Se.$$.fragment),St=M(),Ae=d("p"),Ae.textContent=rn,At=M(),y(ve.$$.fragment),vt=M(),y(Ye.$$.fragment),Yt=M(),He=d("p"),He.innerHTML=on,Ht=M(),y(Le.$$.fragment),Lt=M(),De=d("p"),De.innerHTML=cn,Dt=M(),y(qe.$$.fragment),qt=M(),Ke=d("p"),Ke.innerHTML=dn,Kt=M(),y(Pe.$$.fragment),Pt=M(),y(Oe.$$.fragment),Ot=M(),ls=d("p"),ls.innerHTML=mn,la=M(),y(Xl.$$.fragment),ea=M(),L&&L.c(),Fs=M(),es=d("p"),es.innerHTML=In,sa=M(),rl.c(),zs=M(),ss=d("p"),ss.textContent=jn,ta=M(),y(ts.$$.fragment),aa=M(),y(as.$$.fragment),na=M(),y(El.$$.fragment),Ma=M(),ns=d("p"),ns.innerHTML=un,Ja=M(),y(Ms.$$.fragment),Ta=M(),y(Js.$$.fragment),pa=M(),Ts=d("p"),Ts.innerHTML=hn,ia=M(),y(ps.$$.fragment),wa=M(),is=d("p"),is.textContent=bn,Ua=M(),y(ws.$$.fragment),ya=M(),Us=d("p"),Us.textContent=fn,ra=M(),cl.c(),xs=M(),y(ys.$$.fragment),oa=M(),y(rs.$$.fragment),ca=M(),os=d("p"),os.textContent=Cn,da=M(),ml.c(),Ss=M(),y(cs.$$.fragment),ma=M(),ds=d("p"),ds.textContent=Gn,Ia=M(),jl.c(),As=M(),ms=d("p"),ms.textContent=Bn,ja=M(),hl.c(),vs=M(),y(Is.$$.fragment),ua=M(),js=d("p"),js.textContent=gn,ha=M(),F&&F.c(),Ys=M(),y(Fl.$$.fragment),ba=M(),z&&z.c(),Hs=M(),y(us.$$.fragment),fa=M(),hs=d("p"),hs.innerHTML=_n,Ca=M(),y(bs.$$.fragment),Ga=M(),fs=d("p"),fs.textContent=kn,Ba=M(),y(Cs.$$.fragment),ga=M(),y(Gs.$$.fragment),_a=M(),Bs=d("p"),Bs.textContent=Zn,ka=M(),y(gs.$$.fragment),Za=M(),_s=d("p"),_s.innerHTML=Rn,Ra=M(),y(zl.$$.fragment),Va=M(),y(ks.$$.fragment),Na=M(),Ds=d("p"),this.h()},l(l){const e=yM("svelte-u9bgzb",document.head);a=m(e,"META",{name:!0,content:!0}),e.forEach(s),U=J(l),n=m(l,"P",{}),va(n).forEach(s),w=J(l),r(B.$$.fragment,l),I=J(l),r(R.$$.fragment,l),$=J(l),V.l(l),k=J(l),Z=m(l,"P",{"data-svelte-h":!0}),j(Z)!=="svelte-12h72jb"&&(Z.textContent=u),_=J(l),Q=m(l,"P",{"data-svelte-h":!0}),j(Q)!=="svelte-jyzeuh"&&(Q.textContent=X),G=J(l),W=m(l,"P",{"data-svelte-h":!0}),j(W)!=="svelte-lt33zq"&&(W.innerHTML=S),g=J(l),N=m(l,"DIV",{class:!0,"data-svelte-h":!0}),j(N)!=="svelte-1lvgpuv"&&(N.innerHTML=Ls),Zl=J(l),A=m(l,"P",{"data-svelte-h":!0}),j(A)!=="svelte-nljpeg"&&(A.innerHTML=vl),K=J(l),x=m(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),va(x).forEach(s),P=J(l),O=m(l,"P",{"data-svelte-h":!0}),j(O)!=="svelte-izpy37"&&(O.textContent=fl),Hl=J(l),r(ll.$$.fragment,l),el=J(l),r(q.$$.fragment,l),sl=J(l),r(Cl.$$.fragment,l),Rl=J(l),v=m(l,"P",{"data-svelte-h":!0}),j(v)!=="svelte-1990a1u"&&(v.innerHTML=Ll),tl=J(l),il=m(l,"DIV",{class:!0,"data-svelte-h":!0}),j(il)!=="svelte-zc90xi"&&(il.innerHTML=Dl),al=J(l),nl=m(l,"P",{"data-svelte-h":!0}),j(nl)!=="svelte-1dmevbk"&&(nl.innerHTML=Gl),Vl=J(l),D.l(l),wl=J(l),Y=m(l,"P",{"data-svelte-h":!0}),j(Y)!=="svelte-1hi4inx"&&(Y.textContent=ql),Ml=J(l),r(Bl.$$.fragment,l),Nl=J(l),H=m(l,"P",{"data-svelte-h":!0}),j(H)!=="svelte-5ndf81"&&(H.innerHTML=Kl),Jl=J(l),r(Tl.$$.fragment,l),pl=J(l),gl=m(l,"P",{"data-svelte-h":!0}),j(gl)!=="svelte-1t9w6ur"&&(gl.textContent=Pl),T=J(l),Ul.l(l),Es=J(l),r(Ol.$$.fragment,l),qs=J(l),le=m(l,"P",{"data-svelte-h":!0}),j(le)!=="svelte-1mehzlg"&&(le.textContent=Ya),Ks=J(l),r(ee.$$.fragment,l),Ps=J(l),se=m(l,"P",{"data-svelte-h":!0}),j(se)!=="svelte-4i4vx1"&&(se.innerHTML=Ha),Os=J(l),te=m(l,"P",{"data-svelte-h":!0}),j(te)!=="svelte-qsvm8j"&&(te.innerHTML=La),lt=J(l),r(ae.$$.fragment,l),et=J(l),r(ne.$$.fragment,l),st=J(l),Me=m(l,"P",{"data-svelte-h":!0}),j(Me)!=="svelte-am4ara"&&(Me.innerHTML=Da),tt=J(l),r(Je.$$.fragment,l),at=J(l),r(Te.$$.fragment,l),nt=J(l),pe=m(l,"P",{"data-svelte-h":!0}),j(pe)!=="svelte-1b2rkwx"&&(pe.innerHTML=qa),Mt=J(l),r($l.$$.fragment,l),Jt=J(l),ie=m(l,"P",{"data-svelte-h":!0}),j(ie)!=="svelte-18qg0wm"&&(ie.innerHTML=Ka),Tt=J(l),r(we.$$.fragment,l),pt=J(l),r(Ue.$$.fragment,l),it=J(l),ye=m(l,"P",{"data-svelte-h":!0}),j(ye)!=="svelte-jxtb4e"&&(ye.textContent=Pa),wt=J(l),re=m(l,"P",{"data-svelte-h":!0}),j(re)!=="svelte-19k2l08"&&(re.innerHTML=Oa),Ut=J(l),r(oe.$$.fragment,l),yt=J(l),r(ce.$$.fragment,l),rt=J(l),de=m(l,"P",{"data-svelte-h":!0}),j(de)!=="svelte-1vdhek4"&&(de.innerHTML=ln),ot=J(l),me=m(l,"P",{"data-svelte-h":!0}),j(me)!=="svelte-j9fq9b"&&(me.innerHTML=en),ct=J(l),r(Ie.$$.fragment,l),dt=J(l),r(je.$$.fragment,l),mt=J(l),ue=m(l,"P",{"data-svelte-h":!0}),j(ue)!=="svelte-g2tbu7"&&(ue.innerHTML=sn),It=J(l),r(Wl.$$.fragment,l),jt=J(l),he=m(l,"P",{"data-svelte-h":!0}),j(he)!=="svelte-1k24b7d"&&(he.textContent=tn),ut=J(l),r(be.$$.fragment,l),ht=J(l),r(Ql.$$.fragment,l),bt=J(l),fe=m(l,"P",{"data-svelte-h":!0}),j(fe)!=="svelte-1e9e90y"&&(fe.textContent=an),ft=J(l),r(Ce.$$.fragment,l),Ct=J(l),r(Ge.$$.fragment,l),Gt=J(l),Be=m(l,"P",{"data-svelte-h":!0}),j(Be)!=="svelte-t3f6y9"&&(Be.textContent=nn),Bt=J(l),r(ge.$$.fragment,l),gt=J(l),r(_e.$$.fragment,l),_t=J(l),ke=m(l,"P",{"data-svelte-h":!0}),j(ke)!=="svelte-ydr74z"&&(ke.innerHTML=Mn),kt=J(l),r(Ze.$$.fragment,l),Zt=J(l),r(Re.$$.fragment,l),Rt=J(l),Ve=m(l,"P",{"data-svelte-h":!0}),j(Ve)!=="svelte-1b3aqs0"&&(Ve.textContent=Jn),Vt=J(l),Ne=m(l,"UL",{"data-svelte-h":!0}),j(Ne)!=="svelte-1mt01b7"&&(Ne.innerHTML=Tn),Nt=J(l),$e=m(l,"P",{"data-svelte-h":!0}),j($e)!=="svelte-1gemtoi"&&($e.textContent=pn),$t=J(l),r(We.$$.fragment,l),Wt=J(l),Qe=m(l,"P",{"data-svelte-h":!0}),j(Qe)!=="svelte-6obbsz"&&(Qe.innerHTML=wn),Qt=J(l),Xe=m(l,"P",{"data-svelte-h":!0}),j(Xe)!=="svelte-c716ik"&&(Xe.innerHTML=Un),Xt=J(l),r(Ee.$$.fragment,l),Et=J(l),r(Fe.$$.fragment,l),Ft=J(l),ze=m(l,"P",{"data-svelte-h":!0}),j(ze)!=="svelte-j2hkw4"&&(ze.innerHTML=yn),zt=J(l),r(xe.$$.fragment,l),xt=J(l),r(Se.$$.fragment,l),St=J(l),Ae=m(l,"P",{"data-svelte-h":!0}),j(Ae)!=="svelte-zok8qe"&&(Ae.textContent=rn),At=J(l),r(ve.$$.fragment,l),vt=J(l),r(Ye.$$.fragment,l),Yt=J(l),He=m(l,"P",{"data-svelte-h":!0}),j(He)!=="svelte-cing7m"&&(He.innerHTML=on),Ht=J(l),r(Le.$$.fragment,l),Lt=J(l),De=m(l,"P",{"data-svelte-h":!0}),j(De)!=="svelte-o05rfl"&&(De.innerHTML=cn),Dt=J(l),r(qe.$$.fragment,l),qt=J(l),Ke=m(l,"P",{"data-svelte-h":!0}),j(Ke)!=="svelte-ti9ohy"&&(Ke.innerHTML=dn),Kt=J(l),r(Pe.$$.fragment,l),Pt=J(l),r(Oe.$$.fragment,l),Ot=J(l),ls=m(l,"P",{"data-svelte-h":!0}),j(ls)!=="svelte-kus9dt"&&(ls.innerHTML=mn),la=J(l),r(Xl.$$.fragment,l),ea=J(l),L&&L.l(l),Fs=J(l),es=m(l,"P",{"data-svelte-h":!0}),j(es)!=="svelte-1wl6e7j"&&(es.innerHTML=In),sa=J(l),rl.l(l),zs=J(l),ss=m(l,"P",{"data-svelte-h":!0}),j(ss)!=="svelte-xpwpko"&&(ss.textContent=jn),ta=J(l),r(ts.$$.fragment,l),aa=J(l),r(as.$$.fragment,l),na=J(l),r(El.$$.fragment,l),Ma=J(l),ns=m(l,"P",{"data-svelte-h":!0}),j(ns)!=="svelte-m9pwgl"&&(ns.innerHTML=un),Ja=J(l),r(Ms.$$.fragment,l),Ta=J(l),r(Js.$$.fragment,l),pa=J(l),Ts=m(l,"P",{"data-svelte-h":!0}),j(Ts)!=="svelte-1j3o8bw"&&(Ts.innerHTML=hn),ia=J(l),r(ps.$$.fragment,l),wa=J(l),is=m(l,"P",{"data-svelte-h":!0}),j(is)!=="svelte-knfuag"&&(is.textContent=bn),Ua=J(l),r(ws.$$.fragment,l),ya=J(l),Us=m(l,"P",{"data-svelte-h":!0}),j(Us)!=="svelte-1sc37gp"&&(Us.textContent=fn),ra=J(l),cl.l(l),xs=J(l),r(ys.$$.fragment,l),oa=J(l),r(rs.$$.fragment,l),ca=J(l),os=m(l,"P",{"data-svelte-h":!0}),j(os)!=="svelte-1h2zmrt"&&(os.textContent=Cn),da=J(l),ml.l(l),Ss=J(l),r(cs.$$.fragment,l),ma=J(l),ds=m(l,"P",{"data-svelte-h":!0}),j(ds)!=="svelte-cn0ezb"&&(ds.textContent=Gn),Ia=J(l),jl.l(l),As=J(l),ms=m(l,"P",{"data-svelte-h":!0}),j(ms)!=="svelte-pk0jij"&&(ms.textContent=Bn),ja=J(l),hl.l(l),vs=J(l),r(Is.$$.fragment,l),ua=J(l),js=m(l,"P",{"data-svelte-h":!0}),j(js)!=="svelte-ocxfg1"&&(js.textContent=gn),ha=J(l),F&&F.l(l),Ys=J(l),r(Fl.$$.fragment,l),ba=J(l),z&&z.l(l),Hs=J(l),r(us.$$.fragment,l),fa=J(l),hs=m(l,"P",{"data-svelte-h":!0}),j(hs)!=="svelte-1qpjkjr"&&(hs.innerHTML=_n),Ca=J(l),r(bs.$$.fragment,l),Ga=J(l),fs=m(l,"P",{"data-svelte-h":!0}),j(fs)!=="svelte-o6miyd"&&(fs.textContent=kn),Ba=J(l),r(Cs.$$.fragment,l),ga=J(l),r(Gs.$$.fragment,l),_a=J(l),Bs=m(l,"P",{"data-svelte-h":!0}),j(Bs)!=="svelte-10rs4cq"&&(Bs.textContent=Zn),ka=J(l),r(gs.$$.fragment,l),Za=J(l),_s=m(l,"P",{"data-svelte-h":!0}),j(_s)!=="svelte-ymfy4o"&&(_s.innerHTML=Rn),Ra=J(l),r(zl.$$.fragment,l),Va=J(l),r(ks.$$.fragment,l),Na=J(l),Ds=m(l,"P",{}),va(Ds).forEach(s),this.h()},h(){bl(a,"name","hf:doc:metadata"),bl(a,"content",xM),bl(N,"class","flex justify-center"),pM(x.src,Yl="https://course-demos-distilbert-base-uncased-finetuned-imdb.hf.space")||bl(x,"src",Yl),bl(x,"frameborder","0"),bl(x,"height","300"),bl(x,"title","Gradio app"),bl(x,"class","block dark:hidden container p-0 flex-grow space-iframe"),bl(x,"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"),bl(x,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),bl(il,"class","flex justify-center")},m(l,e){rM(document.head,a),t(l,U,e),t(l,n,e),t(l,w,e),o(B,l,e),t(l,I,e),o(R,l,e),t(l,$,e),Zs[C].m(l,e),t(l,k,e),t(l,Z,e),t(l,_,e),t(l,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,l,e),t(l,Ga,e),t(l,fs,e),t(l,Ba,e),o(Cs,l,e),t(l,ga,e),o(Gs,l,e),t(l,_a,e),t(l,Bs,e),t(l,ka,e),o(gs,l,e),t(l,Za,e),t(l,_s,e),t(l,Ra,e),o(zl,l,e),t(l,Va,e),o(ks,l,e),t(l,Na,e),t(l,Ds,e),$a=!0},p(l,[e]){const Dn={};e&1&&(Dn.fw=l[0]),B.$set(Dn);let Qa=C;C=Nn(l),C!==Qa&&(kl(),i(Zs[Qa],1,1,()=>{Zs[Qa]=null}),_l(),V=Zs[C],V||(V=Zs[C]=Vn[C](l),V.c()),p(V,1),V.m(k.parentNode,k));const qn={};e&2&&(qn.$$scope={dirty:e,ctx:l}),q.$set(qn);let Xa=E;E=Wn(l),E!==Xa&&(kl(),i(Rs[Xa],1,1,()=>{Rs[Xa]=null}),_l(),D=Rs[E],D||(D=Rs[E]=$n[E](l),D.c()),p(D,1),D.m(wl.parentNode,wl));let Ea=b;b=Xn(l),b!==Ea&&(kl(),i(Vs[Ea],1,1,()=>{Vs[Ea]=null}),_l(),Ul=Vs[b],Ul||(Ul=Vs[b]=Qn[b](l),Ul.c()),p(Ul,1),Ul.m(Es.parentNode,Es));const Kn={};e&2&&(Kn.$$scope={dirty:e,ctx:l}),$l.$set(Kn);const Pn={};e&2&&(Pn.$$scope={dirty:e,ctx:l}),Wl.$set(Pn);const On={};e&2&&(On.$$scope={dirty:e,ctx:l}),Ql.$set(On);const lM={};e&2&&(lM.$$scope={dirty:e,ctx:l}),Xl.$set(lM),l[0]==="pt"?L||(L=aM(),L.c(),L.m(Fs.parentNode,Fs)):L&&(L.d(1),L=null);let Fa=yl;yl=Fn(l),yl!==Fa&&(kl(),i(Ns[Fa],1,1,()=>{Ns[Fa]=null}),_l(),rl=Ns[yl],rl||(rl=Ns[yl]=En[yl](l),rl.c()),p(rl,1),rl.m(zs.parentNode,zs));const eM={};e&2&&(eM.$$scope={dirty:e,ctx:l}),El.$set(eM);let za=ol;ol=xn(l),ol!==za&&(kl(),i($s[za],1,1,()=>{$s[za]=null}),_l(),cl=$s[ol],cl||(cl=$s[ol]=zn[ol](l),cl.c()),p(cl,1),cl.m(xs.parentNode,xs));let xa=dl;dl=An(l),dl!==xa&&(kl(),i(Ws[xa],1,1,()=>{Ws[xa]=null}),_l(),ml=Ws[dl],ml||(ml=Ws[dl]=Sn[dl](l),ml.c()),p(ml,1),ml.m(Ss.parentNode,Ss));let Sa=Il;Il=Yn(l),Il!==Sa&&(kl(),i(Qs[Sa],1,1,()=>{Qs[Sa]=null}),_l(),jl=Qs[Il],jl||(jl=Qs[Il]=vn[Il](l),jl.c()),p(jl,1),jl.m(As.parentNode,As));let Aa=ul;ul=Ln(l),ul!==Aa&&(kl(),i(Xs[Aa],1,1,()=>{Xs[Aa]=null}),_l(),hl=Xs[ul],hl||(hl=Xs[ul]=Hn[ul](l),hl.c()),p(hl,1),hl.m(vs.parentNode,vs)),l[0]==="pt"?F?e&1&&p(F,1):(F=nM(),F.c(),p(F,1),F.m(Ys.parentNode,Ys)):F&&(kl(),i(F,1,1,()=>{F=null}),_l());const sM={};e&2&&(sM.$$scope={dirty:e,ctx:l}),Fl.$set(sM),l[0]==="pt"?z?e&1&&p(z,1):(z=MM(),z.c(),p(z,1),z.m(Hs.parentNode,Hs)):z&&(kl(),i(z,1,1,()=>{z=null}),_l());const tM={};e&2&&(tM.$$scope={dirty:e,ctx:l}),zl.$set(tM)},i(l){$a||(p(B.$$.fragment,l),p(R.$$.fragment,l),p(V),p(ll.$$.fragment,l),p(q.$$.fragment,l),p(Cl.$$.fragment,l),p(D),p(Bl.$$.fragment,l),p(Tl.$$.fragment,l),p(Ul),p(Ol.$$.fragment,l),p(ee.$$.fragment,l),p(ae.$$.fragment,l),p(ne.$$.fragment,l),p(Je.$$.fragment,l),p(Te.$$.fragment,l),p($l.$$.fragment,l),p(we.$$.fragment,l),p(Ue.$$.fragment,l),p(oe.$$.fragment,l),p(ce.$$.fragment,l),p(Ie.$$.fragment,l),p(je.$$.fragment,l),p(Wl.$$.fragment,l),p(be.$$.fragment,l),p(Ql.$$.fragment,l),p(Ce.$$.fragment,l),p(Ge.$$.fragment,l),p(ge.$$.fragment,l),p(_e.$$.fragment,l),p(Ze.$$.fragment,l),p(Re.$$.fragment,l),p(We.$$.fragment,l),p(Ee.$$.fragment,l),p(Fe.$$.fragment,l),p(xe.$$.fragment,l),p(Se.$$.fragment,l),p(ve.$$.fragment,l),p(Ye.$$.fragment,l),p(Le.$$.fragment,l),p(qe.$$.fragment,l),p(Pe.$$.fragment,l),p(Oe.$$.fragment,l),p(Xl.$$.fragment,l),p(rl),p(ts.$$.fragment,l),p(as.$$.fragment,l),p(El.$$.fragment,l),p(Ms.$$.fragment,l),p(Js.$$.fragment,l),p(ps.$$.fragment,l),p(ws.$$.fragment,l),p(cl),p(ys.$$.fragment,l),p(rs.$$.fragment,l),p(ml),p(cs.$$.fragment,l),p(jl),p(hl),p(Is.$$.fragment,l),p(F),p(Fl.$$.fragment,l),p(z),p(us.$$.fragment,l),p(bs.$$.fragment,l),p(Cs.$$.fragment,l),p(Gs.$$.fragment,l),p(gs.$$.fragment,l),p(zl.$$.fragment,l),p(ks.$$.fragment,l),$a=!0)},o(l){i(B.$$.fragment,l),i(R.$$.fragment,l),i(V),i(ll.$$.fragment,l),i(q.$$.fragment,l),i(Cl.$$.fragment,l),i(D),i(Bl.$$.fragment,l),i(Tl.$$.fragment,l),i(Ul),i(Ol.$$.fragment,l),i(ee.$$.fragment,l),i(ae.$$.fragment,l),i(ne.$$.fragment,l),i(Je.$$.fragment,l),i(Te.$$.fragment,l),i($l.$$.fragment,l),i(we.$$.fragment,l),i(Ue.$$.fragment,l),i(oe.$$.fragment,l),i(ce.$$.fragment,l),i(Ie.$$.fragment,l),i(je.$$.fragment,l),i(Wl.$$.fragment,l),i(be.$$.fragment,l),i(Ql.$$.fragment,l),i(Ce.$$.fragment,l),i(Ge.$$.fragment,l),i(ge.$$.fragment,l),i(_e.$$.fragment,l),i(Ze.$$.fragment,l),i(Re.$$.fragment,l),i(We.$$.fragment,l),i(Ee.$$.fragment,l),i(Fe.$$.fragment,l),i(xe.$$.fragment,l),i(Se.$$.fragment,l),i(ve.$$.fragment,l),i(Ye.$$.fragment,l),i(Le.$$.fragment,l),i(qe.$$.fragment,l),i(Pe.$$.fragment,l),i(Oe.$$.fragment,l),i(Xl.$$.fragment,l),i(rl),i(ts.$$.fragment,l),i(as.$$.fragment,l),i(El.$$.fragment,l),i(Ms.$$.fragment,l),i(Js.$$.fragment,l),i(ps.$$.fragment,l),i(ws.$$.fragment,l),i(cl),i(ys.$$.fragment,l),i(rs.$$.fragment,l),i(ml),i(cs.$$.fragment,l),i(jl),i(hl),i(Is.$$.fragment,l),i(F),i(Fl.$$.fragment,l),i(z),i(us.$$.fragment,l),i(bs.$$.fragment,l),i(Cs.$$.fragment,l),i(Gs.$$.fragment,l),i(gs.$$.fragment,l),i(zl.$$.fragment,l),i(ks.$$.fragment,l),$a=!1},d(l){l&&(s(U),s(n),s(w),s(I),s($),s(k),s(Z),s(_),s(Q),s(G),s(W),s(g),s(N),s(Zl),s(A),s(K),s(x),s(P),s(O),s(Hl),s(el),s(sl),s(Rl),s(v),s(tl),s(il),s(al),s(nl),s(Vl),s(wl),s(Y),s(Ml),s(Nl),s(H),s(Jl),s(pl),s(gl),s(T),s(Es),s(qs),s(le),s(Ks),s(Ps),s(se),s(Os),s(te),s(lt),s(et),s(st),s(Me),s(tt),s(at),s(nt),s(pe),s(Mt),s(Jt),s(ie),s(Tt),s(pt),s(it),s(ye),s(wt),s(re),s(Ut),s(yt),s(rt),s(de),s(ot),s(me),s(ct),s(dt),s(mt),s(ue),s(It),s(jt),s(he),s(ut),s(ht),s(bt),s(fe),s(ft),s(Ct),s(Gt),s(Be),s(Bt),s(gt),s(_t),s(ke),s(kt),s(Zt),s(Rt),s(Ve),s(Vt),s(Ne),s(Nt),s($e),s($t),s(Wt),s(Qe),s(Qt),s(Xe),s(Xt),s(Et),s(Ft),s(ze),s(zt),s(xt),s(St),s(Ae),s(At),s(vt),s(Yt),s(He),s(Ht),s(Lt),s(De),s(Dt),s(qt),s(Ke),s(Kt),s(Pt),s(Ot),s(ls),s(la),s(ea),s(Fs),s(es),s(sa),s(zs),s(ss),s(ta),s(aa),s(na),s(Ma),s(ns),s(Ja),s(Ta),s(pa),s(Ts),s(ia),s(wa),s(is),s(Ua),s(ya),s(Us),s(ra),s(xs),s(oa),s(ca),s(os),s(da),s(Ss),s(ma),s(ds),s(Ia),s(As),s(ms),s(ja),s(vs),s(ua),s(js),s(ha),s(Ys),s(ba),s(Hs),s(fa),s(hs),s(Ca),s(Ga),s(fs),s(Ba),s(ga),s(_a),s(Bs),s(ka),s(Za),s(_s),s(Ra),s(Va),s(Na),s(Ds)),s(a),c(B,l),c(R,l),Zs[C].d(l),c(ll,l),c(q,l),c(Cl,l),Rs[E].d(l),c(Bl,l),c(Tl,l),Vs[b].d(l),c(Ol,l),c(ee,l),c(ae,l),c(ne,l),c(Je,l),c(Te,l),c($l,l),c(we,l),c(Ue,l),c(oe,l),c(ce,l),c(Ie,l),c(je,l),c(Wl,l),c(be,l),c(Ql,l),c(Ce,l),c(Ge,l),c(ge,l),c(_e,l),c(Ze,l),c(Re,l),c(We,l),c(Ee,l),c(Fe,l),c(xe,l),c(Se,l),c(ve,l),c(Ye,l),c(Le,l),c(qe,l),c(Pe,l),c(Oe,l),c(Xl,l),L&&L.d(l),Ns[yl].d(l),c(ts,l),c(as,l),c(El,l),c(Ms,l),c(Js,l),c(ps,l),c(ws,l),$s[ol].d(l),c(ys,l),c(rs,l),Ws[dl].d(l),c(cs,l),Qs[Il].d(l),Xs[ul].d(l),c(Is,l),F&&F.d(l),c(Fl,l),z&&z.d(l),c(us,l),c(bs,l),c(Cs,l),c(Gs,l),c(gs,l),c(zl,l),c(ks,l)}}}const xM='{"title":"微调掩码语言模型（masked language model）","local":"微调掩码语言模型（masked language model）","sections":[{"title":"选择用于掩码语言建模的预训练模型","local":"选择用于掩码语言建模的预训练模型","sections":[],"depth":2},{"title":"数据集","local":"数据集","sections":[],"depth":2},{"title":"预处理数据","local":"预处理数据","sections":[],"depth":2},{"title":"使用 Trainer API 微调 DistilBERT","local":"使用 Trainer API 微调 DistilBERT","sections":[{"title":"语言模型的困惑度（perplexity）","local":"语言模型的困惑度(perplexity)","sections":[],"depth":3}],"depth":2},{"title":"使用 🤗 Accelerate 微调 DistilBERT","local":"使用 🤗 Accelerate 微调 DistilBERT","sections":[],"depth":2},{"title":"使用我们微调的模型","local":"使用我们微调的模型","sections":[],"depth":2}],"depth":1}';function SM(f,a,U){let n="pt";return iM(()=>{const w=new URLSearchParams(window.location.search);U(0,n=w.get("fw")||"pt")}),[n]}class PM extends wM{constructor(a){super(),UM(this,a,SM,zM,TM,{})}}export{PM as component};
