import{s as ra,f as ca,o as Ja,n as Ve}from"../chunks/scheduler.37c15a92.js";import{S as ya,i as Ta,g as T,s as a,r as j,m as wn,A as oa,h as o,f as s,c as p,j as It,u as m,x as U,n as dn,k as Ce,y as Wl,a as t,v as w,t as J,b as ns,d as c,w as d,p as as}from"../chunks/index.2bf4358c.js";import{T as ps}from"../chunks/Tip.363c041f.js";import{Y as aa}from"../chunks/Youtube.1e50a667.js";import{C}from"../chunks/CodeBlock.4e987730.js";import{D as ia}from"../chunks/DocNotebookDropdown.efc1fb7c.js";import{F as Ua}from"../chunks/FrameworkSwitchCourse.8d4d4ab6.js";import{H as yl,E as ja}from"../chunks/getInferenceSnippets.8229e22a.js";function ma(k){let M,u;return M=new ia({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/section2_tf.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter7/section2_tf.ipynb"}]}}),{c(){j(M.$$.fragment)},l(i){m(M.$$.fragment,i)},m(i,f){w(M,i,f),u=!0},i(i){u||(c(M.$$.fragment,i),u=!0)},o(i){J(M.$$.fragment,i),u=!1},d(i){d(M,i)}}}function wa(k){let M,u;return M=new ia({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/section2_pt.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter7/section2_pt.ipynb"}]}}),{c(){j(M.$$.fragment)},l(i){m(M.$$.fragment,i)},m(i,f){w(M,i,f),u=!0},i(i){u||(c(M.$$.fragment,i),u=!0)},o(i){J(M.$$.fragment,i),u=!1},d(i){d(M,i)}}}function da(k){let M,u='💡 只要你的数据集由分词文本和对应的标签组成，就能够将这里描述的数据处理过程应用到自己的数据集中。如果需要复习如何在 <code>Dataset</code> 中加载自定义数据集，请复习 <a href="/course/chapter5">第五章</a> 。';return{c(){M=T("p"),M.innerHTML=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-d627kc"&&(M.innerHTML=u)},m(i,f){t(i,M,f)},p:Ve,d(i){i&&s(M)}}}function ba(k){let M,u="✏️ <strong>轮到你了！</strong> 检查同一个句子的词性标注 （POS）或分块（chunking）列，查看输出的结果。";return{c(){M=T("p"),M.innerHTML=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-13f8y4m"&&(M.innerHTML=u)},m(i,f){t(i,M,f)},p:Ve,d(i){i&&s(M)}}}function ua(k){let M,u="✏️ <strong>轮到你了！</strong> 有些研究人员更喜欢只为每个单只分配一个标签，对该单词其他部分的 token 分配 <code>-100</code> 标签。这是为了避免那些分解成许多子词的长单词对损失作出过多的贡献。请按照这个思路，改变之前的函数，使标签与 inputs ID 对齐。";return{c(){M=T("p"),M.innerHTML=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-j440e3"&&(M.innerHTML=u)},m(i,f){t(i,M,f)},p:Ve,d(i){i&&s(M)}}}function fa(k){let M,u,i,f="使用 <code>Keras</code> 的代码将与之前非常相似；唯一的变化是数据如何整理成 <code>batch</code> 以及计算评估的分数。",_;return M=new yl({props:{title:"使用 Keras 微调模型",local:"使用 Keras 微调模型",headingTag:"h2"}}),{c(){j(M.$$.fragment),u=a(),i=T("p"),i.innerHTML=f},l(h){m(M.$$.fragment,h),u=p(h),i=o(h,"P",{"data-svelte-h":!0}),U(i)!=="svelte-61noe8"&&(i.innerHTML=f)},m(h,x){w(M,h,x),t(h,u,x),t(h,i,x),_=!0},i(h){_||(c(M.$$.fragment,h),_=!0)},o(h){J(M.$$.fragment,h),_=!1},d(h){h&&(s(u),s(i)),d(M,h)}}}function ha(k){let M,u,i,f="使用 <code>Trainer</code> 的代码会和以前一样，唯一的变化是数据如何整理成 <code>batch</code> 以及计算评估的分数。",_;return M=new yl({props:{title:"使用 Trainer API 微调模型",local:"使用 Trainer API 微调模型",headingTag:"h2"}}),{c(){j(M.$$.fragment),u=a(),i=T("p"),i.innerHTML=f},l(h){m(M.$$.fragment,h),u=p(h),i=o(h,"P",{"data-svelte-h":!0}),U(i)!=="svelte-1g43t55"&&(i.innerHTML=f)},m(h,x){w(M,h,x),t(h,u,x),t(h,i,x),_=!0},i(h){_||(c(M.$$.fragment,h),_=!0)},o(h){J(M.$$.fragment,h),_=!1},d(h){h&&(s(u),s(i)),d(M,h)}}}function Ca(k){let M,u;return M=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvckZvclRva2VuQ2xhc3NpZmljYXRpb24lMEElMEFkYXRhX2NvbGxhdG9yJTIwJTNEJTIwRGF0YUNvbGxhdG9yRm9yVG9rZW5DbGFzc2lmaWNhdGlvbiglMEElMjAlMjAlMjAlMjB0b2tlbml6ZXIlM0R0b2tlbml6ZXIlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMnRmJTIyJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForTokenClassification

data_collator = DataCollatorForTokenClassification(
    tokenizer=tokenizer, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>
)`,wrap:!1}}),{c(){j(M.$$.fragment)},l(i){m(M.$$.fragment,i)},m(i,f){w(M,i,f),u=!0},i(i){u||(c(M.$$.fragment,i),u=!0)},o(i){J(M.$$.fragment,i),u=!1},d(i){d(M,i)}}}function Ia(k){let M,u;return M=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvckZvclRva2VuQ2xhc3NpZmljYXRpb24lMEElMEFkYXRhX2NvbGxhdG9yJTIwJTNEJTIwRGF0YUNvbGxhdG9yRm9yVG9rZW5DbGFzc2lmaWNhdGlvbih0b2tlbml6ZXIlM0R0b2tlbml6ZXIp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForTokenClassification

data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer)`,wrap:!1}}),{c(){j(M.$$.fragment)},l(i){m(M.$$.fragment,i)},m(i,f){w(M,i,f),u=!0},i(i){u||(c(M.$$.fragment,i),u=!0)},o(i){J(M.$$.fragment,i),u=!1},d(i){d(M,i)}}}function $a(k){let M,u="我们的数据整理器已准备就绪！现在让我们使用 <code>to_tf_dataset()</code> 方法创建一个 <code>tf.data.Dataset</code> 。除此之外，你还可以使用 <code>model.prepare_tf_dataset()</code> 来使用更少的模板代码来创建 <code>Dataset</code>——你将在本章的其他小节中看到这样的操作。",i,f,_,h,x="下一站：模型本身。",Z;return f=new C({props:{code:"dGZfdHJhaW5fZGF0YXNldCUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVELnRvX3RmX2RhdGFzZXQoJTBBJTIwJTIwJTIwJTIwY29sdW1ucyUzRCU1QiUyMmF0dGVudGlvbl9tYXNrJTIyJTJDJTIwJTIyaW5wdXRfaWRzJTIyJTJDJTIwJTIybGFiZWxzJTIyJTJDJTIwJTIydG9rZW5fdHlwZV9pZHMlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBjb2xsYXRlX2ZuJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMHNodWZmbGUlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hfc2l6ZSUzRDE2JTJDJTBBKSUwQSUwQXRmX2V2YWxfZGF0YXNldCUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnZhbGlkYXRpb24lMjIlNUQudG9fdGZfZGF0YXNldCglMEElMjAlMjAlMjAlMjBjb2x1bW5zJTNEJTVCJTIyYXR0ZW50aW9uX21hc2slMjIlMkMlMjAlMjJpbnB1dF9pZHMlMjIlMkMlMjAlMjJsYWJlbHMlMjIlMkMlMjAlMjJ0b2tlbl90eXBlX2lkcyUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMGNvbGxhdGVfZm4lM0RkYXRhX2NvbGxhdG9yJTJDJTBBJTIwJTIwJTIwJTIwc2h1ZmZsZSUzREZhbHNlJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hfc2l6ZSUzRDE2JTJDJTBBKQ==",highlighted:`tf_train_dataset = tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>].to_tf_dataset(
    columns=[<span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>, <span class="hljs-string">&quot;token_type_ids&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">True</span>,
    batch_size=<span class="hljs-number">16</span>,
)

tf_eval_dataset = tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>].to_tf_dataset(
    columns=[<span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>, <span class="hljs-string">&quot;token_type_ids&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">False</span>,
    batch_size=<span class="hljs-number">16</span>,
)`,wrap:!1}}),{c(){M=T("p"),M.innerHTML=u,i=a(),j(f.$$.fragment),_=a(),h=T("p"),h.textContent=x},l(b){M=o(b,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1b2bdhk"&&(M.innerHTML=u),i=p(b),m(f.$$.fragment,b),_=p(b),h=o(b,"P",{"data-svelte-h":!0}),U(h)!=="svelte-d0y39"&&(h.textContent=x)},m(b,g){t(b,M,g),t(b,i,g),w(f,b,g),t(b,_,g),t(b,h,g),Z=!0},i(b){Z||(c(f.$$.fragment,b),Z=!0)},o(b){J(f.$$.fragment,b),Z=!1},d(b){b&&(s(M),s(i),s(_),s(h)),d(f,b)}}}function ka(k){let M,u="正如我们所看到的，已经使用 <code>-100</code> 将第二组标签填充到了与第一组标签相同的长度。";return{c(){M=T("p"),M.innerHTML=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1stm0yh"&&(M.innerHTML=u)},m(i,f){t(i,M,f)},i:Ve,o:Ve,d(i){i&&s(M)}}}function pa(k){let M,u,i,f="由于我们正在研究 Token 分类问题，因此我们将使用 <code>TFAutoModelForTokenClassification</code> 类。实例化此模型时要记得传递我们标签的数量，最简单方法是将该数字传递给 <code>num_labels</code> 参数，但是如果我们想要创建一个就像我们在本节开头看到的那样的推理小部件，那么就需要用最标准的方法正确设置标签的对应关系。",_,h,x="最标准的方法是用两个字典 <code>id2label</code> 和 <code>label2id</code> 来设置它们，这两个字典包含从 ID 到标签的映射以及反向的映射：",Z,b,g,B,A="现在我们只需将这两个字典传递给 <code>TFAutoModelForTokenClassification.from_pretrained()</code> 方法，它们就会被保存在模型的配置中，然后被正确地保存和上传到 Hub：",G,$,W,E,es='就像我们在 <a href="/course/chapter3">第三章</a> 中实例化 <code>TFAutoModelForTokenClassification</code> 类一样 创建模型会发出一个警告，提示一些权重未被使用（来自预训练模型头部的权重）和一些其他权重被随机初始化（来自新 Token 分类头部的权重），别担心，我们马上就要训练这些权重。首先让我们确认一下我们的模型是否具有正确的标签数量：',Rl,X,vl,D,Q,N,El,q,il,K,ve="现在，我们已准备好训练模型了！不过，我们在这之前还要做两件事：登录到 Hugging Face 并设置我们的训练超参数。如果你在 notebook 上工作，有一个便捷函数可以帮助你做到这一点：",rl,ll,Gl,Y,ss="这将显示一个小部件，你可以在其中输入你的 Hugging Face 账号和密码。",Ql,V,Xl="如果你不是在 notebook 上工作，只需在终端中输入以下代码：",F,Vl,Zl,R,Ie="登录后，我们可以编译我们模型所需要的所有配置。🤗 Transformers 提供了一个便捷的 <code>create_optimizer()</code> 函数，与 <code>TensorFlow</code> 内置的 <code>Adam</code> 优化器相比，它可以提供一个带有恰当的权重衰减和学习率衰减机制的 <code>AdamW</code> 优化器，不过这两个机制都将提高模型的性能：",nl,H,Nl,z,Tl="请注意，我们没有给 <code>compile()</code> 提供 <code>loss</code> 参数。这是因为模型实际上可以使用默认的方式自动计算损失 —— 如果你在编译时没有提供损失的计算方法或在输入中提供真实的标签值（就像我们在数据集中所做的那样），那么模型将使用内部默认的 <code>loss</code> 计算方法进行训练，具体的计算方式取决于你选择的任务和模型类型。",ol,Ul,jl="接下来，我们定义一个 <code>PushToHubCallback</code> 回调函数，以便在训练期间将模型上传到 Hub，并在拟合模型时添加该回调函数：",al,el,Yl,S,cl='你可以使用 <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/bert-finetuned-ner&quot;</code> 。默认情况下，这个仓库将保存在你的账户之内，并以你设置的输出目录命名，例如 <code>&quot;cool_huggingface_user/bert-finetuned-ner&quot;</code> 。',pl,sl,Al,v,$e="请注意，在训练过程中每次保存模型时（这里是每个 epooch），它都会在后台上传到 Hub。这样，如有需要，你将能够在另一台机器上继续你的训练。",Ml,tl,Fl="到此阶段，你可以在模型 Hub 上使用推理小部件来测试你的模型并与你的朋友分享。你已经成功地在一个 tokens 分类任务上微调了一个模型——恭喜你！但我们的模型真的好用吗？我们应该找出一些指标来对模型进评估。",L;return M=new yl({props:{title:"初始化模型",local:"初始化模型",headingTag:"h3"}}),b=new C({props:{code:"aWQybGFiZWwlMjAlM0QlMjAlN0JzdHIoaSklM0ElMjBsYWJlbCUyMGZvciUyMGklMkMlMjBsYWJlbCUyMGluJTIwZW51bWVyYXRlKGxhYmVsX25hbWVzKSU3RCUwQWxhYmVsMmlkJTIwJTNEJTIwJTdCdiUzQSUyMGslMjBmb3IlMjBrJTJDJTIwdiUyMGluJTIwaWQybGFiZWwuaXRlbXMoKSU3RA==",highlighted:`id2label = {<span class="hljs-built_in">str</span>(i): label <span class="hljs-keyword">for</span> i, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(label_names)}
label2id = {v: k <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> id2label.items()}`,wrap:!1}}),$=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRGQXV0b01vZGVsRm9yVG9rZW5DbGFzc2lmaWNhdGlvbiUwQSUwQW1vZGVsJTIwJTNEJTIwVEZBdXRvTW9kZWxGb3JUb2tlbkNsYXNzaWZpY2F0aW9uLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjBtb2RlbF9jaGVja3BvaW50JTJDJTBBJTIwJTIwJTIwJTIwaWQybGFiZWwlM0RpZDJsYWJlbCUyQyUwQSUyMCUyMCUyMCUyMGxhYmVsMmlkJTNEbGFiZWwyaWQlMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForTokenClassification

model = TFAutoModelForTokenClassification.from_pretrained(
    model_checkpoint,
    id2label=id2label,
    label2id=label2id,
)`,wrap:!1}}),X=new C({props:{code:"bW9kZWwuY29uZmlnLm51bV9sYWJlbHM=",highlighted:"model.config.num_labels",wrap:!1}}),D=new C({props:{code:"OQ==",highlighted:'<span class="hljs-number">9</span>',wrap:!1}}),N=new ps({props:{warning:!0,$$slots:{default:[_a]},$$scope:{ctx:k}}}),q=new yl({props:{title:"微调模型",local:"微调模型",headingTag:"h3"}}),ll=new C({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`,wrap:!1}}),Vl=new C({props:{code:"aHVnZ2luZ2ZhY2UtY2xpJTIwbG9naW4=",highlighted:"huggingface-cli login",wrap:!1}}),H=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMGNyZWF0ZV9vcHRpbWl6ZXIlMEFpbXBvcnQlMjB0ZW5zb3JmbG93JTIwYXMlMjB0ZiUwQSUwQSUyMyUyMCVFNSU5QyVBOCVFNiVCNyVCNyVFNSU5MCU4OCVFNyVCMiVCRSVFNSVCQSVBNiUyMGZsb2F0MTYlMjAlRTQlQjglQUQlRTglQkYlOUIlRTglQTElOEMlRTglQUUlQUQlRTclQkIlODMlMEF0Zi5rZXJhcy5taXhlZF9wcmVjaXNpb24uc2V0X2dsb2JhbF9wb2xpY3koJTIybWl4ZWRfZmxvYXQxNiUyMiklMEElMEElMjMlMjAlRTglQUUlQUQlRTclQkIlODMlRTYlQUQlQTUlRTYlOTUlQjAlRTYlOTglQUYlRTYlOTUlQjAlRTYlOEQlQUUlRTklOUIlODYlRTQlQjglQUQlRTclOUElODQlRTYlQTAlQjclRTYlOUMlQUMlRTYlOTUlQjAlRTklODclOEYlMkMlRTklOTklQTQlRTQlQkIlQTUlMjBiYXRjaCUyMCVFNSVBNCVBNyVFNSVCMCU4RiUyQyVFNyU4NCVCNiVFNSU5MCU4RSVFNCVCOSU5OCVFNCVCQiVBNSVFNiU4MCVCQiVFNyU5QSU4NCUyMGVwb2NoJTIwJUU2JTk1JUIwJUUzJTgwJTgyJTBBJTIzJTIwJUU2JUIzJUE4JUU2JTg0JThGJUU4JUJGJTk5JUU5JTg3JThDJUU3JTlBJTg0JTIwdGZfdHJhaW5fZGF0YXNldCUyMCVFNiU5OCVBRiUyMGJhdGNoJTIwJUU1JUJEJUEyJUU1JUJDJThGJUU3JTlBJTg0JTIwdGYuZGF0YS5EYXRhc2V0JTJDJTBBJTIzJTIwJUU4JTgwJThDJUU0JUI4JThEJUU2JTk4JUFGJUU1JThFJTlGJUU1JUE3JThCJUU3JTlBJTg0JTIwSHVnZ2luZyUyMEZhY2UlMjBEYXRhc2V0JTIwJTJDJUU2JTg5JTgwJUU0JUJCJUE1JUU0JUJEJUJGJUU3JTk0JUE4JTIwbGVuKCklMjAlRTglQUUlQTElRTclQUUlOTclRTUlQUUlODMlRTclOUElODQlRTklOTUlQkYlRTUlQkElQTYlRTUlQjclQjIlRTclQkIlOEYlRTYlOTglQUYlMjBudW1fc2FtcGxlcyUyMCUyRiUyRiUyMGJhdGNoX3NpemUlRTMlODAlODIlMEFudW1fZXBvY2hzJTIwJTNEJTIwMyUwQW51bV90cmFpbl9zdGVwcyUyMCUzRCUyMGxlbih0Zl90cmFpbl9kYXRhc2V0KSUyMColMjBudW1fZXBvY2hzJTBBJTBBb3B0aW1pemVyJTJDJTIwc2NoZWR1bGUlMjAlM0QlMjBjcmVhdGVfb3B0aW1pemVyKCUwQSUyMCUyMCUyMCUyMGluaXRfbHIlM0QyZS01JTJDJTBBJTIwJTIwJTIwJTIwbnVtX3dhcm11cF9zdGVwcyUzRDAlMkMlMEElMjAlMjAlMjAlMjBudW1fdHJhaW5fc3RlcHMlM0RudW1fdHJhaW5fc3RlcHMlMkMlMEElMjAlMjAlMjAlMjB3ZWlnaHRfZGVjYXlfcmF0ZSUzRDAuMDElMkMlMEEpJTBBbW9kZWwuY29tcGlsZShvcHRpbWl6ZXIlM0RvcHRpbWl6ZXIp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> create_optimizer
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf

<span class="hljs-comment"># 在混合精度 float16 中进行训练</span>
tf.keras.mixed_precision.set_global_policy(<span class="hljs-string">&quot;mixed_float16&quot;</span>)

<span class="hljs-comment"># 训练步数是数据集中的样本数量,除以 batch 大小,然后乘以总的 epoch 数。</span>
<span class="hljs-comment"># 注意这里的 tf_train_dataset 是 batch 形式的 tf.data.Dataset,</span>
<span class="hljs-comment"># 而不是原始的 Hugging Face Dataset ,所以使用 len() 计算它的长度已经是 num_samples // batch_size。</span>
num_epochs = <span class="hljs-number">3</span>
num_train_steps = <span class="hljs-built_in">len</span>(tf_train_dataset) * num_epochs

optimizer, schedule = create_optimizer(
    init_lr=<span class="hljs-number">2e-5</span>,
    num_warmup_steps=<span class="hljs-number">0</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)`,wrap:!1}}),el=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycy5rZXJhc19jYWxsYmFja3MlMjBpbXBvcnQlMjBQdXNoVG9IdWJDYWxsYmFjayUwQSUwQWNhbGxiYWNrJTIwJTNEJTIwUHVzaFRvSHViQ2FsbGJhY2sob3V0cHV0X2RpciUzRCUyMmJlcnQtZmluZXR1bmVkLW5lciUyMiUyQyUyMHRva2VuaXplciUzRHRva2VuaXplciklMEElMEFtb2RlbC5maXQoJTBBJTIwJTIwJTIwJTIwdGZfdHJhaW5fZGF0YXNldCUyQyUwQSUyMCUyMCUyMCUyMHZhbGlkYXRpb25fZGF0YSUzRHRmX2V2YWxfZGF0YXNldCUyQyUwQSUyMCUyMCUyMCUyMGNhbGxiYWNrcyUzRCU1QmNhbGxiYWNrJTVEJTJDJTBBJTIwJTIwJTIwJTIwZXBvY2hzJTNEbnVtX2Vwb2NocyUyQyUwQSk=",highlighted:`<span class="hljs-keyword">from</span> transformers.keras_callbacks <span class="hljs-keyword">import</span> PushToHubCallback

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

model.fit(
    tf_train_dataset,
    validation_data=tf_eval_dataset,
    callbacks=[callback],
    epochs=num_epochs,
)`,wrap:!1}}),sl=new ps({props:{$$slots:{default:[ga]},$$scope:{ctx:k}}}),{c(){j(M.$$.fragment),u=a(),i=T("p"),i.innerHTML=f,_=a(),h=T("p"),h.innerHTML=x,Z=a(),j(b.$$.fragment),g=a(),B=T("p"),B.innerHTML=A,G=a(),j($.$$.fragment),W=a(),E=T("p"),E.innerHTML=es,Rl=a(),j(X.$$.fragment),vl=a(),j(D.$$.fragment),Q=a(),j(N.$$.fragment),El=a(),j(q.$$.fragment),il=a(),K=T("p"),K.textContent=ve,rl=a(),j(ll.$$.fragment),Gl=a(),Y=T("p"),Y.textContent=ss,Ql=a(),V=T("p"),V.textContent=Xl,F=a(),j(Vl.$$.fragment),Zl=a(),R=T("p"),R.innerHTML=Ie,nl=a(),j(H.$$.fragment),Nl=a(),z=T("p"),z.innerHTML=Tl,ol=a(),Ul=T("p"),Ul.innerHTML=jl,al=a(),j(el.$$.fragment),Yl=a(),S=T("p"),S.innerHTML=cl,pl=a(),j(sl.$$.fragment),Al=a(),v=T("p"),v.textContent=$e,Ml=a(),tl=T("p"),tl.textContent=Fl},l(r){m(M.$$.fragment,r),u=p(r),i=o(r,"P",{"data-svelte-h":!0}),U(i)!=="svelte-n9ouf6"&&(i.innerHTML=f),_=p(r),h=o(r,"P",{"data-svelte-h":!0}),U(h)!=="svelte-yvgquq"&&(h.innerHTML=x),Z=p(r),m(b.$$.fragment,r),g=p(r),B=o(r,"P",{"data-svelte-h":!0}),U(B)!=="svelte-s2j55p"&&(B.innerHTML=A),G=p(r),m($.$$.fragment,r),W=p(r),E=o(r,"P",{"data-svelte-h":!0}),U(E)!=="svelte-1xetjnr"&&(E.innerHTML=es),Rl=p(r),m(X.$$.fragment,r),vl=p(r),m(D.$$.fragment,r),Q=p(r),m(N.$$.fragment,r),El=p(r),m(q.$$.fragment,r),il=p(r),K=o(r,"P",{"data-svelte-h":!0}),U(K)!=="svelte-hllk6x"&&(K.textContent=ve),rl=p(r),m(ll.$$.fragment,r),Gl=p(r),Y=o(r,"P",{"data-svelte-h":!0}),U(Y)!=="svelte-1n7l0od"&&(Y.textContent=ss),Ql=p(r),V=o(r,"P",{"data-svelte-h":!0}),U(V)!=="svelte-hcdc88"&&(V.textContent=Xl),F=p(r),m(Vl.$$.fragment,r),Zl=p(r),R=o(r,"P",{"data-svelte-h":!0}),U(R)!=="svelte-p52opu"&&(R.innerHTML=Ie),nl=p(r),m(H.$$.fragment,r),Nl=p(r),z=o(r,"P",{"data-svelte-h":!0}),U(z)!=="svelte-1q8ooz4"&&(z.innerHTML=Tl),ol=p(r),Ul=o(r,"P",{"data-svelte-h":!0}),U(Ul)!=="svelte-i2wzac"&&(Ul.innerHTML=jl),al=p(r),m(el.$$.fragment,r),Yl=p(r),S=o(r,"P",{"data-svelte-h":!0}),U(S)!=="svelte-1uv0ajc"&&(S.innerHTML=cl),pl=p(r),m(sl.$$.fragment,r),Al=p(r),v=o(r,"P",{"data-svelte-h":!0}),U(v)!=="svelte-1bv2kv1"&&(v.textContent=$e),Ml=p(r),tl=o(r,"P",{"data-svelte-h":!0}),U(tl)!=="svelte-1dg2htp"&&(tl.textContent=Fl)},m(r,I){w(M,r,I),t(r,u,I),t(r,i,I),t(r,_,I),t(r,h,I),t(r,Z,I),w(b,r,I),t(r,g,I),t(r,B,I),t(r,G,I),w($,r,I),t(r,W,I),t(r,E,I),t(r,Rl,I),w(X,r,I),t(r,vl,I),w(D,r,I),t(r,Q,I),w(N,r,I),t(r,El,I),w(q,r,I),t(r,il,I),t(r,K,I),t(r,rl,I),w(ll,r,I),t(r,Gl,I),t(r,Y,I),t(r,Ql,I),t(r,V,I),t(r,F,I),w(Vl,r,I),t(r,Zl,I),t(r,R,I),t(r,nl,I),w(H,r,I),t(r,Nl,I),t(r,z,I),t(r,ol,I),t(r,Ul,I),t(r,al,I),w(el,r,I),t(r,Yl,I),t(r,S,I),t(r,pl,I),w(sl,r,I),t(r,Al,I),t(r,v,I),t(r,Ml,I),t(r,tl,I),L=!0},i(r){L||(c(M.$$.fragment,r),c(b.$$.fragment,r),c($.$$.fragment,r),c(X.$$.fragment,r),c(D.$$.fragment,r),c(N.$$.fragment,r),c(q.$$.fragment,r),c(ll.$$.fragment,r),c(Vl.$$.fragment,r),c(H.$$.fragment,r),c(el.$$.fragment,r),c(sl.$$.fragment,r),L=!0)},o(r){J(M.$$.fragment,r),J(b.$$.fragment,r),J($.$$.fragment,r),J(X.$$.fragment,r),J(D.$$.fragment,r),J(N.$$.fragment,r),J(q.$$.fragment,r),J(ll.$$.fragment,r),J(Vl.$$.fragment,r),J(H.$$.fragment,r),J(el.$$.fragment,r),J(sl.$$.fragment,r),L=!1},d(r){r&&(s(u),s(i),s(_),s(h),s(Z),s(g),s(B),s(G),s(W),s(E),s(Rl),s(vl),s(Q),s(El),s(il),s(K),s(rl),s(Gl),s(Y),s(Ql),s(V),s(F),s(Zl),s(R),s(nl),s(Nl),s(z),s(ol),s(Ul),s(al),s(Yl),s(S),s(pl),s(Al),s(v),s(Ml),s(tl)),d(M,r),d(b,r),d($,r),d(X,r),d(D,r),d(N,r),d(q,r),d(ll,r),d(Vl,r),d(H,r),d(el,r),d(sl,r)}}}function _a(k){let M,u="⚠️ 如果你的模型的标签数量错误，那么在后面调用 <code>model.fit()</code> 时，你会得到一个晦涩的错误。这可能会令人烦恼，所以确保你做了这个检查，确认你的标签数量是正确的。";return{c(){M=T("p"),M.innerHTML=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-17fhtwo"&&(M.innerHTML=u)},m(i,f){t(i,M,f)},p:Ve,d(i){i&&s(M)}}}function ga(k){let M,u="💡 如果设置了模型保存的路径，并且在那里已经存在了一个非空的同名文件夹，那么该目录应该是 Hub 仓库克隆在本地的版本，如果不是，则会在调用 model.ﬁt() 方法时收到一个错误，并需要设置一个新的路径。";return{c(){M=T("p"),M.textContent=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1tb8y9b"&&(M.textContent=u)},m(i,f){t(i,M,f)},p:Ve,d(i){i&&s(M)}}}function xa(k){let M,u='用于评估 Token 分类预测的经典框架是 <a href="https://github.com/chakki-works/seqeval" rel="nofollow"><em>seqeval</em></a> 。要使用此指标，我们首先需要安装 seqeval 库：',i,f,_,h,x='然后我们可以通过 <code>evaluate.load()</code> 函数加载它，就像我们在 <a href="/course/chapter3">第三章</a> 中所做的那样：',Z;return f=new C({props:{code:"IXBpcCUyMGluc3RhbGwlMjBzZXFldmFs",highlighted:"!pip install seqeval",wrap:!1}}),{c(){M=T("p"),M.innerHTML=u,i=a(),j(f.$$.fragment),_=a(),h=T("p"),h.innerHTML=x},l(b){M=o(b,"P",{"data-svelte-h":!0}),U(M)!=="svelte-us56qu"&&(M.innerHTML=u),i=p(b),m(f.$$.fragment,b),_=p(b),h=o(b,"P",{"data-svelte-h":!0}),U(h)!=="svelte-1qq971n"&&(h.innerHTML=x)},m(b,g){t(b,M,g),t(b,i,g),w(f,b,g),t(b,_,g),t(b,h,g),Z=!0},i(b){Z||(c(f.$$.fragment,b),Z=!0)},o(b){J(f.$$.fragment,b),Z=!1},d(b){b&&(s(M),s(i),s(_),s(h)),d(f,b)}}}function Za(k){let M,u="要让 <code>Trainer</code> 在每个周期计算一个指标，我们需要定义一个 <code>compute_metrics()</code> 函数，该函数的输入是预测值和标签的数组，并返回带有指标名称和评估结果的字典。",i,f,_='用于评估 Token 分类预测的经典框架是 <a href="https://github.com/chakki-works/seqeval" rel="nofollow"><em>seqeval</em></a> 。要使用此指标，我们首先需要安装 seqeval 库：',h,x,Z,b,g='然后我们可以通过 <code>evaluate.load()</code> 函数加载它，就像我们在 <a href="/course/chapter3">第三章</a> 中所做的那样：',B;return x=new C({props:{code:"IXBpcCUyMGluc3RhbGwlMjBzZXFldmFs",highlighted:"!pip install seqeval",wrap:!1}}),{c(){M=T("p"),M.innerHTML=u,i=a(),f=T("p"),f.innerHTML=_,h=a(),j(x.$$.fragment),Z=a(),b=T("p"),b.innerHTML=g},l(A){M=o(A,"P",{"data-svelte-h":!0}),U(M)!=="svelte-firjku"&&(M.innerHTML=u),i=p(A),f=o(A,"P",{"data-svelte-h":!0}),U(f)!=="svelte-us56qu"&&(f.innerHTML=_),h=p(A),m(x.$$.fragment,A),Z=p(A),b=o(A,"P",{"data-svelte-h":!0}),U(b)!=="svelte-1qq971n"&&(b.innerHTML=g)},m(A,G){t(A,M,G),t(A,i,G),t(A,f,G),t(A,h,G),w(x,A,G),t(A,Z,G),t(A,b,G),B=!0},i(A){B||(c(x.$$.fragment,A),B=!0)},o(A){J(x.$$.fragment,A),B=!1},d(A){A&&(s(M),s(i),s(f),s(h),s(Z),s(b)),d(x,A)}}}function Aa(k){let M,u="它返回了大量的信息！我们获得每个单独实体以及整体的准确率、召回率和 F1 分数。现在，让我们看看如果我们尝试使用真实的模型预测来计算分数。",i,f,_="在使用基于TensorFlow 的框架时，我们通常不会把预测拼接在一起，因为这样会导致序列长度不统一。这意味着我们不能直接使用 <code>model.predict()</code> 方法 —— 但这并不能阻止我们。我们可以逐批获取一些预测并在在此的过程中将它们拼接成一个大的列表，删除表示 <code>masking/padding</code> 的 <code>-100</code> tokens 然后在合并后的大列表上计算度量值：",h,x,Z,b,g,B,A="与我们的模型相比，你的模型的表现如何？如果你看到类似的数字，那么你的训练就成功了！",G;return x=new C({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBYWxsX3ByZWRpY3Rpb25zJTIwJTNEJTIwJTVCJTVEJTBBYWxsX2xhYmVscyUyMCUzRCUyMCU1QiU1RCUwQWZvciUyMGJhdGNoJTIwaW4lMjB0Zl9ldmFsX2RhdGFzZXQlM0ElMEElMjAlMjAlMjAlMjBsb2dpdHMlMjAlM0QlMjBtb2RlbC5wcmVkaWN0X29uX2JhdGNoKGJhdGNoKSU1QiUyMmxvZ2l0cyUyMiU1RCUwQSUyMCUyMCUyMCUyMGxhYmVscyUyMCUzRCUyMGJhdGNoJTVCJTIybGFiZWxzJTIyJTVEJTBBJTIwJTIwJTIwJTIwcHJlZGljdGlvbnMlMjAlM0QlMjBucC5hcmdtYXgobG9naXRzJTJDJTIwYXhpcyUzRC0xKSUwQSUyMCUyMCUyMCUyMGZvciUyMHByZWRpY3Rpb24lMkMlMjBsYWJlbCUyMGluJTIwemlwKHByZWRpY3Rpb25zJTJDJTIwbGFiZWxzKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZvciUyMHByZWRpY3RlZF9pZHglMkMlMjBsYWJlbF9pZHglMjBpbiUyMHppcChwcmVkaWN0aW9uJTJDJTIwbGFiZWwpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaWYlMjBsYWJlbF9pZHglMjAlM0QlM0QlMjAtMTAwJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwY29udGludWUlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBhbGxfcHJlZGljdGlvbnMuYXBwZW5kKGxhYmVsX25hbWVzJTVCcHJlZGljdGVkX2lkeCU1RCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBhbGxfbGFiZWxzLmFwcGVuZChsYWJlbF9uYW1lcyU1QmxhYmVsX2lkeCU1RCklMEFtZXRyaWMuY29tcHV0ZShwcmVkaWN0aW9ucyUzRCU1QmFsbF9wcmVkaWN0aW9ucyU1RCUyQyUyMHJlZmVyZW5jZXMlM0QlNUJhbGxfbGFiZWxzJTVEKQ==",highlighted:`<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

all_predictions = []
all_labels = []
<span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> tf_eval_dataset:
    logits = model.predict_on_batch(batch)[<span class="hljs-string">&quot;logits&quot;</span>]
    labels = batch[<span class="hljs-string">&quot;labels&quot;</span>]
    predictions = np.argmax(logits, axis=-<span class="hljs-number">1</span>)
    <span class="hljs-keyword">for</span> prediction, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(predictions, labels):
        <span class="hljs-keyword">for</span> predicted_idx, label_idx <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(prediction, label):
            <span class="hljs-keyword">if</span> label_idx == -<span class="hljs-number">100</span>:
                <span class="hljs-keyword">continue</span>
            all_predictions.append(label_names[predicted_idx])
            all_labels.append(label_names[label_idx])
metric.compute(predictions=[all_predictions], references=[all_labels])`,wrap:!1}}),b=new C({props:{code:"JTdCJ0xPQyclM0ElMjAlN0IncHJlY2lzaW9uJyUzQSUyMDAuOTElMkMlMjAncmVjYWxsJyUzQSUyMDAuOTIlMkMlMjAnZjEnJTNBJTIwMC45MSUyQyUyMCdudW1iZXInJTNBJTIwMTY2OCU3RCUyQyUwQSUyMCdNSVNDJyUzQSUyMCU3QidwcmVjaXNpb24nJTNBJTIwMC43MCUyQyUyMCdyZWNhbGwnJTNBJTIwMC43OSUyQyUyMCdmMSclM0ElMjAwLjc0JTJDJTIwJ251bWJlciclM0ElMjA3MDIlN0QlMkMlMEElMjAnT1JHJyUzQSUyMCU3QidwcmVjaXNpb24nJTNBJTIwMC44NSUyQyUyMCdyZWNhbGwnJTNBJTIwMC45MCUyQyUyMCdmMSclM0ElMjAwLjg4JTJDJTIwJ251bWJlciclM0ElMjAxNjYxJTdEJTJDJTBBJTIwJ1BFUiclM0ElMjAlN0IncHJlY2lzaW9uJyUzQSUyMDAuOTUlMkMlMjAncmVjYWxsJyUzQSUyMDAuOTUlMkMlMjAnZjEnJTNBJTIwMC45NSUyQyUyMCdudW1iZXInJTNBJTIwMTYxNyU3RCUyQyUwQSUyMCdvdmVyYWxsX3ByZWNpc2lvbiclM0ElMjAwLjg3JTJDJTBBJTIwJ292ZXJhbGxfcmVjYWxsJyUzQSUyMDAuOTElMkMlMEElMjAnb3ZlcmFsbF9mMSclM0ElMjAwLjg5JTJDJTBBJTIwJ292ZXJhbGxfYWNjdXJhY3knJTNBJTIwMC45NyU3RA==",highlighted:`{<span class="hljs-string">&#x27;LOC&#x27;</span>: {<span class="hljs-string">&#x27;precision&#x27;</span>: <span class="hljs-number">0.91</span>, <span class="hljs-string">&#x27;recall&#x27;</span>: <span class="hljs-number">0.92</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">0.91</span>, <span class="hljs-string">&#x27;number&#x27;</span>: <span class="hljs-number">1668</span>},
 <span class="hljs-string">&#x27;MISC&#x27;</span>: {<span class="hljs-string">&#x27;precision&#x27;</span>: <span class="hljs-number">0.70</span>, <span class="hljs-string">&#x27;recall&#x27;</span>: <span class="hljs-number">0.79</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">0.74</span>, <span class="hljs-string">&#x27;number&#x27;</span>: <span class="hljs-number">702</span>},
 <span class="hljs-string">&#x27;ORG&#x27;</span>: {<span class="hljs-string">&#x27;precision&#x27;</span>: <span class="hljs-number">0.85</span>, <span class="hljs-string">&#x27;recall&#x27;</span>: <span class="hljs-number">0.90</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">0.88</span>, <span class="hljs-string">&#x27;number&#x27;</span>: <span class="hljs-number">1661</span>},
 <span class="hljs-string">&#x27;PER&#x27;</span>: {<span class="hljs-string">&#x27;precision&#x27;</span>: <span class="hljs-number">0.95</span>, <span class="hljs-string">&#x27;recall&#x27;</span>: <span class="hljs-number">0.95</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">0.95</span>, <span class="hljs-string">&#x27;number&#x27;</span>: <span class="hljs-number">1617</span>},
 <span class="hljs-string">&#x27;overall_precision&#x27;</span>: <span class="hljs-number">0.87</span>,
 <span class="hljs-string">&#x27;overall_recall&#x27;</span>: <span class="hljs-number">0.91</span>,
 <span class="hljs-string">&#x27;overall_f1&#x27;</span>: <span class="hljs-number">0.89</span>,
 <span class="hljs-string">&#x27;overall_accuracy&#x27;</span>: <span class="hljs-number">0.97</span>}`,wrap:!1}}),{c(){M=T("p"),M.textContent=u,i=a(),f=T("p"),f.innerHTML=_,h=a(),j(x.$$.fragment),Z=a(),j(b.$$.fragment),g=a(),B=T("p"),B.textContent=A},l($){M=o($,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1tgxz9r"&&(M.textContent=u),i=p($),f=o($,"P",{"data-svelte-h":!0}),U(f)!=="svelte-vq488"&&(f.innerHTML=_),h=p($),m(x.$$.fragment,$),Z=p($),m(b.$$.fragment,$),g=p($),B=o($,"P",{"data-svelte-h":!0}),U(B)!=="svelte-1by56bc"&&(B.textContent=A)},m($,W){t($,M,W),t($,i,W),t($,f,W),t($,h,W),w(x,$,W),t($,Z,W),w(b,$,W),t($,g,W),t($,B,W),G=!0},i($){G||(c(x.$$.fragment,$),c(b.$$.fragment,$),G=!0)},o($){J(x.$$.fragment,$),J(b.$$.fragment,$),G=!1},d($){$&&(s(M),s(i),s(f),s(h),s(Z),s(g),s(B)),d(x,$),d(b,$)}}}function Ba(k){let M,u="结果返回很多信息！包括每个单独实体及整体的准确率、召回率和 F1 分数。在这里我们将只保留总分，但是你可以自由地调整 <code>compute_metrics()</code> 函数返回的所需要指标。这个函数中我们首先会先取预测 <code>logits</code> 的 <code>argmax</code>，并将其转换为预测值。 <code>compute_metrics()</code> 函数首先取 <code>logits</code> 的 <code>argmax</code>，将它们转换为预测值（通常情况下，logits 和概率的顺序是相同，所以我们不需要使用 softmax）。然后我们需要将标签和预测值都从整数转换为字符串。我们删除所有标签为 <code>-100</code> 的值，最后将结果传递给 <code>metric.compute()</code> 方法：",i,f,_,h,x="现在已经完成了，我们下面就可以开始定义我们的 <code>Trainer</code> 了。接下来我们只需要一个 <code>model</code> 并对其微调！",Z;return f=new C({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBJTBBZGVmJTIwY29tcHV0ZV9tZXRyaWNzKGV2YWxfcHJlZHMpJTNBJTBBJTIwJTIwJTIwJTIwbG9naXRzJTJDJTIwbGFiZWxzJTIwJTNEJTIwZXZhbF9wcmVkcyUwQSUyMCUyMCUyMCUyMHByZWRpY3Rpb25zJTIwJTNEJTIwbnAuYXJnbWF4KGxvZ2l0cyUyQyUyMGF4aXMlM0QtMSklMEElMEElMjAlMjAlMjAlMjAlMjMlMjAlRTUlODglQTAlRTklOTklQTQlRTUlQkYlQkQlRTclOTUlQTUlRTclOUElODQlRTclQjQlQTIlRTUlQkMlOTUoJUU3JTg5JUI5JUU2JUFFJThBJTIwdG9rZW5zJTIwKSVFNSVCOSVCNiVFOCVCRCVBQyVFNiU4RCVBMiVFNCVCOCVCQSVFNiVBMCU4NyVFNyVBRCVCRSUwQSUyMCUyMCUyMCUyMHRydWVfbGFiZWxzJTIwJTNEJTIwJTVCJTVCbGFiZWxfbmFtZXMlNUJsJTVEJTIwZm9yJTIwbCUyMGluJTIwbGFiZWwlMjBpZiUyMGwlMjAhJTNEJTIwLTEwMCU1RCUyMGZvciUyMGxhYmVsJTIwaW4lMjBsYWJlbHMlNUQlMEElMjAlMjAlMjAlMjB0cnVlX3ByZWRpY3Rpb25zJTIwJTNEJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTVCbGFiZWxfbmFtZXMlNUJwJTVEJTIwZm9yJTIwKHAlMkMlMjBsKSUyMGluJTIwemlwKHByZWRpY3Rpb24lMkMlMjBsYWJlbCklMjBpZiUyMGwlMjAhJTNEJTIwLTEwMCU1RCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZvciUyMHByZWRpY3Rpb24lMkMlMjBsYWJlbCUyMGluJTIwemlwKHByZWRpY3Rpb25zJTJDJTIwbGFiZWxzKSUwQSUyMCUyMCUyMCUyMCU1RCUwQSUyMCUyMCUyMCUyMGFsbF9tZXRyaWNzJTIwJTNEJTIwbWV0cmljLmNvbXB1dGUocHJlZGljdGlvbnMlM0R0cnVlX3ByZWRpY3Rpb25zJTJDJTIwcmVmZXJlbmNlcyUzRHRydWVfbGFiZWxzKSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnByZWNpc2lvbiUyMiUzQSUyMGFsbF9tZXRyaWNzJTVCJTIyb3ZlcmFsbF9wcmVjaXNpb24lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJyZWNhbGwlMjIlM0ElMjBhbGxfbWV0cmljcyU1QiUyMm92ZXJhbGxfcmVjYWxsJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIyZjElMjIlM0ElMjBhbGxfbWV0cmljcyU1QiUyMm92ZXJhbGxfZjElMjIlNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJhY2N1cmFjeSUyMiUzQSUyMGFsbF9tZXRyaWNzJTVCJTIyb3ZlcmFsbF9hY2N1cmFjeSUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMCU3RA==",highlighted:`<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np


<span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_preds</span>):
    logits, labels = eval_preds
    predictions = np.argmax(logits, axis=-<span class="hljs-number">1</span>)

    <span class="hljs-comment"># 删除忽略的索引(特殊 tokens )并转换为标签</span>
    true_labels = [[label_names[l] <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> label <span class="hljs-keyword">if</span> l != -<span class="hljs-number">100</span>] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> labels]
    true_predictions = [
        [label_names[p] <span class="hljs-keyword">for</span> (p, l) <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(prediction, label) <span class="hljs-keyword">if</span> l != -<span class="hljs-number">100</span>]
        <span class="hljs-keyword">for</span> prediction, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(predictions, labels)
    ]
    all_metrics = metric.compute(predictions=true_predictions, references=true_labels)
    <span class="hljs-keyword">return</span> {
        <span class="hljs-string">&quot;precision&quot;</span>: all_metrics[<span class="hljs-string">&quot;overall_precision&quot;</span>],
        <span class="hljs-string">&quot;recall&quot;</span>: all_metrics[<span class="hljs-string">&quot;overall_recall&quot;</span>],
        <span class="hljs-string">&quot;f1&quot;</span>: all_metrics[<span class="hljs-string">&quot;overall_f1&quot;</span>],
        <span class="hljs-string">&quot;accuracy&quot;</span>: all_metrics[<span class="hljs-string">&quot;overall_accuracy&quot;</span>],
    }`,wrap:!1}}),{c(){M=T("p"),M.innerHTML=u,i=a(),j(f.$$.fragment),_=a(),h=T("p"),h.innerHTML=x},l(b){M=o(b,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1sy48jj"&&(M.innerHTML=u),i=p(b),m(f.$$.fragment,b),_=p(b),h=o(b,"P",{"data-svelte-h":!0}),U(h)!=="svelte-1f2ntp9"&&(h.innerHTML=x)},m(b,g){t(b,M,g),t(b,i,g),w(f,b,g),t(b,_,g),t(b,h,g),Z=!0},i(b){Z||(c(f.$$.fragment,b),Z=!0)},o(b){J(f.$$.fragment,b),Z=!1},d(b){b&&(s(M),s(i),s(_),s(h)),d(f,b)}}}function Ma(k){let M,u,i,f="由于我们正在研究 Token 分类问题，因此我们将使用 <code>AutoModelForTokenClassification</code> 类。定义此模型时要记得传递我们标签的数量，最简单方法是将该数字传递给 <code>num_labels</code> 参数，但是如果我们想要一个就像我们在本节开头看到的那样的推理小部件，就需要用最标准的方法正确设置标签的对应关系。",_,h,x="最标准的方式是用两个字典 <code>id2label</code> 和 <code>label2id</code> 来设置标签，这两个字典包含从 ID 到标签的映射以及反向的映射：",Z,b,g,B,A="现在我们只需将它们传递给 <code>AutoModelForTokenClassification.from_pretrained()</code> 方法，它们就会被保存在模型的配置中，然后被正确地保存和上传到 Hub：",G,$,W,E,es='就像我们在 <a href="/course/chapter3">第三章</a> 中定义 <code>AutoModelForSequenceClassification</code> 类一样 创建模型会发出一个警告，提示一些权重未被使用（来自预训练头部的权重）和一些其他权重被随机初始化（来自新 Token 分类头部的权重），不过不用担心，我们马上就要训练这些权重。在这之前，让我们先确认一下我们的模型是否具有正确的标签数量：',Rl,X,vl,D,Q,N,El,q,il,K,ve="我们现在准备好训练我们的模型了！在定义 <code>Trainer</code> 之前，我们只需要做最后两件事：登录 Hugging Face 并设置我们的训练参数。如果你在 notebook 上工作，有一个方便的小工具可以帮助你：",rl,ll,Gl,Y,ss="登录后，我们就可以设置我们的 <code>TrainingArguments</code> ：",Ql,V,Xl,F,Vl='你已经对大多数内容有所了解了：我们设置了一些超参数（如学习率、训练的轮数和权重衰减），并设定 <code>push_to_hub=True</code> ，表示我们希望在每个训练轮次结束时保存并评估模型，然后将结果上传到模型中心。注意，你可以通过 <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/bert-finetuned-ner&quot;</code> 。默认情况下，使用的仓库将保存在你的账户之内，并以你设置的输出目录命名，所以在我们的例子中，仓库的地址是 <code>&quot;sgugger/bert-finetuned-ner&quot;</code> 。',Zl,R,Ie,nl,H="最后，我们将所有内容传递给 <code>Trainer</code> 并启动训练：",Nl,z,Tl,ol,Ul="请注意，在训练过程中每次保存模型时（这里是每个 epooch），它都会在后台上传到 Hub。这样，如有必要，你将能够在另一台机器上继续你的训练。",jl,al,el="训练完成后，我们使用 <code>push_to_hub()</code> 上传模型的最新版本",Yl,S,cl,pl,sl="如果你想检查一下是否上传成功，这个命令会返回刚刚执行的提交的 URL：",Al,v,$e,Ml,tl="同时 <code>Trainer</code> 还创建并上传了一张包含所有评估结果的模型卡。到此阶段，你可以在模型 Hub 上使用推理小部件来测试你的模型并与你的朋友分享。你已经成功地在一个 tokens 分类任务上微调了一个模型——恭喜你！",Fl,L,r="如果你想更深入地了解训练循环，我们现在将向你展示如何使用 🤗 Accelerate 做同样的事情。",I,ml,Ee,wl,Ms='现在我们来看看完整的训练循环，这样你就可以轻松地定制你需要的部分。它与我们在 <a href="/course/chapter3/4">第三章</a> 中所做的内容很相似，但对评估部分有一些改动。',Hl,ke,Xe,dl,ut="首先我们需要为我们的数据集构建 <code>DataLoader</code> 。我们将 <code>data_collator</code> 传递给 <code>collate_fn</code> 参数并随机打乱训练集，但不打乱验证集：",Ne,bl,Ye,ul,is="接下来我们重新实例化我们的模型，以确保我们不是继续之前的微调，而是重新开始从 BERT 微调预训练模型：",zl,_e,Fe,fl,rs="然后我们需要一个优化器。我们将使用经典 <code>AdamW</code> ，它类似于 <code>Adam</code> ，但在权重衰减的方式上进行了改进：",Sl,Ll,Dl,ge,cs="当我们拥有了所有这些对象之后，我们就可以将它们发传递给 <code>accelerator.prepare()</code> 方法：",ql,Ol,Pl,Kl,le,xe,Js="现在我们已经将我们的 <code>train_dataloader</code> 传递给了 <code>accelerator.prepare()</code> 方法，我们还可以使用 <code>len()</code> 来计算训练步骤的数量。请记住，我们应该在准备好 <code>dataloader</code> 后再使用 <code>len()</code> ，因为改动 <code>dataloader</code> 会改变训练长度的数量。这里我们将使用一个从学习率衰减到 0 的经典线性学习率调度：",ee,se,te,ne,Ze="最后，为了将我们的模型推送到 Hub，我们需要在一个工作文件夹中创建一个 <code>Repository</code> 对象。如果你还没有登录的话，首先需要登录到 Hugging Face，然后根据模型 ID 来确定仓库名称（你可以将 <code>repo_name</code> 替换为你喜欢的名字；只需要包含你的用户名即可，你可以使用 <code>get_full_repo_name()</code> 函数的查看目前的 <code>repo_name</code> ）：",ys,ae,pe,Ae,He,hl,ft="然后我们可以将该仓库克隆到本地文件夹中。如果本地已经存在同名的文件夹，这个本地文件夹必须是我们正在使用的仓库克隆在本地的版本：",ze,Cl,Se,Il,Ts="我们现在可以通过调用 <code>repo.push_to_hub()</code> 方法上传保存在 <code>output_dir</code> 中的所有内容。它帮助我们在每个训练周期结束时上传中间模型。",Me,Be,Le,$l,os="我们现在准备编写完整的训练循环。为了简化其评估部分，我们定义了一个 <code>postprocess()</code> 函数，该函数会接收模型的预测和真实的标签，并将它们转换为字符串列表，也就是我们的 <code>metric</code> （评估函数）对象需要的输入格式。",ie,We,De,Jl,Us="然后我们可以编写训练循环。在定义一个进度条来跟踪训练的进度后，循环分为三个部分：",re,ce,Re="<li>训练本身，这是经典的迭代过程，即在 <code>train_dataloader</code> 上进行迭代，在模型上前向传播训练数据，然后反向传递 <code>loss</code> 和优化参数</li> <li>评估，在获取模型在一个 <code>batch</code> 上的输出之后，这里有一个需要注意的地方：由于两个进程可能已将输入和标签填充到不同的形状，我们需要使用 <code>accelerator.pad_across_processes()</code> 使预测和真实的标签在调用 <code>gather()</code> 方法之前具有相同的形状。如果我们不这样做，评估循环将会出错或无限期卡住。最后我们将结果发送到 <code>metric.add_batch()</code> 方法中，并在评估循环结束时调用 <code>metric.compute()</code> 方法。</li> <li>保存和上传，首先保存模型和 <code>tokenizer</code> 然后调用 <code>repo.push_to_hub()</code> 方法。注意，我们使用参数 <code>blocking=False</code> 来告诉 🤗 <code>Hub</code> 库在一个异步进程中推送。这样，在训练时，这个指令在后台将模型和 <code>tokenizer</code> 推送到 <code>hub</code>。</li>",qe,kl,ht="以下是完整的训练循环代码：",Oe,_l,js,Je,Ge="如果这是你第一次看到使用 🤗 Accelerate 保存模型，让我们花点时间来了解一下这个过程中的三行代码：",Pe,gl,ms,ye,Qe=`第一行是不言自明的：它告诉所有的进程先等待，直到所有的进程都处于这个阶段再继续（阻塞）。这是为了确保在保存之前，我们在每个进程中都有相同的模型。
第二行代码用于获取 <code>unwrapped_model</code> ，它就是我们定义的基本模型。 <code>accelerator.prepare()</code> 方法会为了在分布式训练中工作而对模型进行了一些修改，所以它不再有 <code>save_pretraining()</code> 方法；使用 <code>accelerator.unwrap_model()</code> 方法可以撤销对模型的更改。
在第三行代码中，我们调用 <code>save_pretraining()</code> ，并指定 <code>accelerator.save()</code> 作为 <code>save_function</code> 而不是默认的 <code>torch.save()</code> 。`,Ke,xl,ws='完成这些操作后，你应该拥有一个与 <code>Trainer</code> 训练出的模型结果相当类似的模型。你可以在 <a href="https://huggingface.co/huggingface-course/bert-finetuned-ner-accelerate" rel="nofollow">huggingface-course/bert-finetuned-ner-accelerate</a> 查看我们使用这些代码训练的模型。如果你想在训练循环中测试任何调整，你可以直接通过编辑上面显示的代码来实现它们！',Te;return M=new yl({props:{title:"定义模型",local:"定义模型",headingTag:"h3"}}),b=new C({props:{code:"aWQybGFiZWwlMjAlM0QlMjAlN0JzdHIoaSklM0ElMjBsYWJlbCUyMGZvciUyMGklMkMlMjBsYWJlbCUyMGluJTIwZW51bWVyYXRlKGxhYmVsX25hbWVzKSU3RCUwQWxhYmVsMmlkJTIwJTNEJTIwJTdCdiUzQSUyMGslMjBmb3IlMjBrJTJDJTIwdiUyMGluJTIwaWQybGFiZWwuaXRlbXMoKSU3RA==",highlighted:`id2label = {<span class="hljs-built_in">str</span>(i): label <span class="hljs-keyword">for</span> i, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(label_names)}
label2id = {v: k <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> id2label.items()}`,wrap:!1}}),$=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvclRva2VuQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvclRva2VuQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMG1vZGVsX2NoZWNrcG9pbnQlMkMlMEElMjAlMjAlMjAlMjBpZDJsYWJlbCUzRGlkMmxhYmVsJTJDJTBBJTIwJTIwJTIwJTIwbGFiZWwyaWQlM0RsYWJlbDJpZCUyQyUwQSk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForTokenClassification

model = AutoModelForTokenClassification.from_pretrained(
    model_checkpoint,
    id2label=id2label,
    label2id=label2id,
)`,wrap:!1}}),X=new C({props:{code:"bW9kZWwuY29uZmlnLm51bV9sYWJlbHM=",highlighted:"model.config.num_labels",wrap:!1}}),D=new C({props:{code:"OQ==",highlighted:'<span class="hljs-number">9</span>',wrap:!1}}),N=new ps({props:{warning:!0,$$slots:{default:[Wa]},$$scope:{ctx:k}}}),q=new yl({props:{title:"微调模型",local:"微调模型",headingTag:"h3"}}),ll=new C({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`,wrap:!1}}),V=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluaW5nQXJndW1lbnRzJTBBJTBBYXJncyUyMCUzRCUyMFRyYWluaW5nQXJndW1lbnRzKCUwQSUyMCUyMCUyMCUyMCUyMmJlcnQtZmluZXR1bmVkLW5lciUyMiUyQyUwQSUyMCUyMCUyMCUyMGV2YWx1YXRpb25fc3RyYXRlZ3klM0QlMjJlcG9jaCUyMiUyQyUwQSUyMCUyMCUyMCUyMHNhdmVfc3RyYXRlZ3klM0QlMjJlcG9jaCUyMiUyQyUwQSUyMCUyMCUyMCUyMGxlYXJuaW5nX3JhdGUlM0QyZS01JTJDJTBBJTIwJTIwJTIwJTIwbnVtX3RyYWluX2Vwb2NocyUzRDMlMkMlMEElMjAlMjAlMjAlMjB3ZWlnaHRfZGVjYXklM0QwLjAxJTJDJTBBJTIwJTIwJTIwJTIwcHVzaF90b19odWIlM0RUcnVlJTJDJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments

args = TrainingArguments(
    <span class="hljs-string">&quot;bert-finetuned-ner&quot;</span>,
    evaluation_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
    save_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
    learning_rate=<span class="hljs-number">2e-5</span>,
    num_train_epochs=<span class="hljs-number">3</span>,
    weight_decay=<span class="hljs-number">0.01</span>,
    push_to_hub=<span class="hljs-literal">True</span>,
)`,wrap:!1}}),R=new ps({props:{$$slots:{default:[Ra]},$$scope:{ctx:k}}}),z=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluZXIlMEElMEF0cmFpbmVyJTIwJTNEJTIwVHJhaW5lciglMEElMjAlMjAlMjAlMjBtb2RlbCUzRG1vZGVsJTJDJTBBJTIwJTIwJTIwJTIwYXJncyUzRGFyZ3MlMkMlMEElMjAlMjAlMjAlMjB0cmFpbl9kYXRhc2V0JTNEdG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBldmFsX2RhdGFzZXQlM0R0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ2YWxpZGF0aW9uJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwZGF0YV9jb2xsYXRvciUzRGRhdGFfY29sbGF0b3IlMkMlMEElMjAlMjAlMjAlMjBjb21wdXRlX21ldHJpY3MlM0Rjb21wdXRlX21ldHJpY3MlMkMlMEElMjAlMjAlMjAlMjB0b2tlbml6ZXIlM0R0b2tlbml6ZXIlMkMlMEEpJTBBdHJhaW5lci50cmFpbigp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer

trainer = Trainer(
    model=model,
    args=args,
    train_dataset=tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>],
    eval_dataset=tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>],
    data_collator=data_collator,
    compute_metrics=compute_metrics,
    tokenizer=tokenizer,
)
trainer.train()`,wrap:!1}}),S=new C({props:{code:"dHJhaW5lci5wdXNoX3RvX2h1Yihjb21taXRfbWVzc2FnZSUzRCUyMlRyYWluaW5nJTIwY29tcGxldGUlMjIp",highlighted:'trainer.push_to_hub(commit_message=<span class="hljs-string">&quot;Training complete&quot;</span>)',wrap:!1}}),v=new C({props:{code:"J2h0dHBzJTNBJTJGJTJGaHVnZ2luZ2ZhY2UuY28lMkZzZ3VnZ2VyJTJGYmVydC1maW5ldHVuZWQtbmVyJTJGY29tbWl0JTJGMjZhYjIxZTViMTU2OGY5YWZlY2NkYWVkMmQ4NzE1ZjU3MWQ3ODZlZCc=",highlighted:'<span class="hljs-string">&#x27;https://huggingface.co/sgugger/bert-finetuned-ner/commit/26ab21e5b1568f9afeccdaed2d8715f571d786ed&#x27;</span>',wrap:!1}}),ml=new yl({props:{title:"自定义训练循环",local:"自定义训练循环",headingTag:"h2"}}),ke=new yl({props:{title:"做好训练前的准备",local:"做好训练前的准备",headingTag:"h3"}}),bl=new C({props:{code:"ZnJvbSUyMHRvcmNoLnV0aWxzLmRhdGElMjBpbXBvcnQlMjBEYXRhTG9hZGVyJTBBJTBBdHJhaW5fZGF0YWxvYWRlciUyMCUzRCUyMERhdGFMb2FkZXIoJTBBJTIwJTIwJTIwJTIwdG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBzaHVmZmxlJTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMGNvbGxhdGVfZm4lM0RkYXRhX2NvbGxhdG9yJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hfc2l6ZSUzRDglMkMlMEEpJTBBZXZhbF9kYXRhbG9hZGVyJTIwJTNEJTIwRGF0YUxvYWRlciglMEElMjAlMjAlMjAlMjB0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ2YWxpZGF0aW9uJTIyJTVEJTJDJTIwY29sbGF0ZV9mbiUzRGRhdGFfY29sbGF0b3IlMkMlMjBiYXRjaF9zaXplJTNEOCUwQSk=",highlighted:`<span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader

train_dataloader = DataLoader(
    tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>],
    shuffle=<span class="hljs-literal">True</span>,
    collate_fn=data_collator,
    batch_size=<span class="hljs-number">8</span>,
)
eval_dataloader = DataLoader(
    tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>], collate_fn=data_collator, batch_size=<span class="hljs-number">8</span>
)`,wrap:!1}}),_e=new C({props:{code:"bW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JUb2tlbkNsYXNzaWZpY2F0aW9uLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjBtb2RlbF9jaGVja3BvaW50JTJDJTBBJTIwJTIwJTIwJTIwaWQybGFiZWwlM0RpZDJsYWJlbCUyQyUwQSUyMCUyMCUyMCUyMGxhYmVsMmlkJTNEbGFiZWwyaWQlMkMlMEEp",highlighted:`model = AutoModelForTokenClassification.from_pretrained(
    model_checkpoint,
    id2label=id2label,
    label2id=label2id,
)`,wrap:!1}}),Ll=new C({props:{code:"ZnJvbSUyMHRvcmNoLm9wdGltJTIwaW1wb3J0JTIwQWRhbVclMEElMEFvcHRpbWl6ZXIlMjAlM0QlMjBBZGFtVyhtb2RlbC5wYXJhbWV0ZXJzKCklMkMlMjBsciUzRDJlLTUp",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">2e-5</span>)`,wrap:!1}}),Ol=new C({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}}),Kl=new ps({props:{$$slots:{default:[Ga]},$$scope:{ctx:k}}}),se=new C({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}}),ae=new C({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMFJlcG9zaXRvcnklMkMlMjBnZXRfZnVsbF9yZXBvX25hbWUlMEElMEFtb2RlbF9uYW1lJTIwJTNEJTIwJTIyYmVydC1maW5ldHVuZWQtbmVyLWFjY2VsZXJhdGUlMjIlMEFyZXBvX25hbWUlMjAlM0QlMjBnZXRfZnVsbF9yZXBvX25hbWUobW9kZWxfbmFtZSklMEFyZXBvX25hbWU=",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository, get_full_repo_name

model_name = <span class="hljs-string">&quot;bert-finetuned-ner-accelerate&quot;</span>
repo_name = get_full_repo_name(model_name)
repo_name`,wrap:!1}}),Ae=new C({props:{code:"J3NndWdnZXIlMkZiZXJ0LWZpbmV0dW5lZC1uZXItYWNjZWxlcmF0ZSc=",highlighted:'<span class="hljs-string">&#x27;sgugger/bert-finetuned-ner-accelerate&#x27;</span>',wrap:!1}}),Cl=new C({props:{code:"b3V0cHV0X2RpciUyMCUzRCUyMCUyMmJlcnQtZmluZXR1bmVkLW5lci1hY2NlbGVyYXRlJTIyJTBBcmVwbyUyMCUzRCUyMFJlcG9zaXRvcnkob3V0cHV0X2RpciUyQyUyMGNsb25lX2Zyb20lM0RyZXBvX25hbWUp",highlighted:`output_dir = <span class="hljs-string">&quot;bert-finetuned-ner-accelerate&quot;</span>
repo = Repository(output_dir, clone_from=repo_name)`,wrap:!1}}),Be=new yl({props:{title:"训练循环",local:"训练循环",headingTag:"h3"}}),We=new C({props:{code:"ZGVmJTIwcG9zdHByb2Nlc3MocHJlZGljdGlvbnMlMkMlMjBsYWJlbHMpJTNBJTBBJTIwJTIwJTIwJTIwcHJlZGljdGlvbnMlMjAlM0QlMjBwcmVkaWN0aW9ucy5kZXRhY2goKS5jcHUoKS5jbG9uZSgpLm51bXB5KCklMEElMjAlMjAlMjAlMjBsYWJlbHMlMjAlM0QlMjBsYWJlbHMuZGV0YWNoKCkuY3B1KCkuY2xvbmUoKS5udW1weSgpJTBBJTBBJTIwJTIwJTIwJTIwJTIzJTIwJUU1JTg4JUEwJUU5JTk5JUE0JUU1JUJGJUJEJUU3JTk1JUE1JUU3JTlBJTg0JUU3JUI0JUEyJUU1JUJDJTk1KCVFNyU4OSVCOSVFNiVBRSU4QSUyMHRva2VucyUyMCklRTUlQjklQjYlRTglQkQlQUMlRTYlOEQlQTIlRTQlQjglQkElRTYlQTAlODclRTclQUQlQkUlMEElMjAlMjAlMjAlMjB0cnVlX2xhYmVscyUyMCUzRCUyMCU1QiU1QmxhYmVsX25hbWVzJTVCbCU1RCUyMGZvciUyMGwlMjBpbiUyMGxhYmVsJTIwaWYlMjBsJTIwISUzRCUyMC0xMDAlNUQlMjBmb3IlMjBsYWJlbCUyMGluJTIwbGFiZWxzJTVEJTBBJTIwJTIwJTIwJTIwdHJ1ZV9wcmVkaWN0aW9ucyUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU1QmxhYmVsX25hbWVzJTVCcCU1RCUyMGZvciUyMChwJTJDJTIwbCklMjBpbiUyMHppcChwcmVkaWN0aW9uJTJDJTIwbGFiZWwpJTIwaWYlMjBsJTIwISUzRCUyMC0xMDAlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmb3IlMjBwcmVkaWN0aW9uJTJDJTIwbGFiZWwlMjBpbiUyMHppcChwcmVkaWN0aW9ucyUyQyUyMGxhYmVscyklMEElMjAlMjAlMjAlMjAlNUQlMEElMjAlMjAlMjAlMjByZXR1cm4lMjB0cnVlX2xhYmVscyUyQyUyMHRydWVfcHJlZGljdGlvbnM=",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">postprocess</span>(<span class="hljs-params">predictions, labels</span>):
    predictions = predictions.detach().cpu().clone().numpy()
    labels = labels.detach().cpu().clone().numpy()

    <span class="hljs-comment"># 删除忽略的索引(特殊 tokens )并转换为标签</span>
    true_labels = [[label_names[l] <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> label <span class="hljs-keyword">if</span> l != -<span class="hljs-number">100</span>] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> labels]
    true_predictions = [
        [label_names[p] <span class="hljs-keyword">for</span> (p, l) <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(prediction, label) <span class="hljs-keyword">if</span> l != -<span class="hljs-number">100</span>]
        <span class="hljs-keyword">for</span> prediction, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(predictions, labels)
    ]
    <span class="hljs-keyword">return</span> true_labels, true_predictions`,wrap:!1}}),_l=new C({props:{code:"ZnJvbSUyMHRxZG0uYXV0byUyMGltcG9ydCUyMHRxZG0lMEFpbXBvcnQlMjB0b3JjaCUwQSUwQXByb2dyZXNzX2JhciUyMCUzRCUyMHRxZG0ocmFuZ2UobnVtX3RyYWluaW5nX3N0ZXBzKSklMEElMEFmb3IlMjBlcG9jaCUyMGluJTIwcmFuZ2UobnVtX3RyYWluX2Vwb2NocyklM0ElMEElMjAlMjAlMjAlMjAlMjMlMjAlRTglQUUlQUQlRTclQkIlODMlMEElMjAlMjAlMjAlMjBtb2RlbC50cmFpbigpJTBBJTIwJTIwJTIwJTIwZm9yJTIwYmF0Y2glMjBpbiUyMHRyYWluX2RhdGFsb2FkZXIlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvdXRwdXRzJTIwJTNEJTIwbW9kZWwoKipiYXRjaCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwb3V0cHV0cy5sb3NzJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IuYmFja3dhcmQobG9zcyklMEElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuc3RlcCgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbHJfc2NoZWR1bGVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG9wdGltaXplci56ZXJvX2dyYWQoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByb2dyZXNzX2Jhci51cGRhdGUoMSklMEElMEElMjAlMjAlMjAlMjAlMjMlMjAlRTglQUYlODQlRTQlQkMlQjAlMEElMjAlMjAlMjAlMjBtb2RlbC5ldmFsKCklMEElMjAlMjAlMjAlMjBmb3IlMjBiYXRjaCUyMGluJTIwZXZhbF9kYXRhbG9hZGVyJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwd2l0aCUyMHRvcmNoLm5vX2dyYWQoKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dHMlMjAlM0QlMjBtb2RlbCgqKmJhdGNoKSUwQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByZWRpY3Rpb25zJTIwJTNEJTIwb3V0cHV0cy5sb2dpdHMuYXJnbWF4KGRpbSUzRC0xKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxhYmVscyUyMCUzRCUyMGJhdGNoJTVCJTIybGFiZWxzJTIyJTVEJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIzJTIwJUU1JUExJUFCJUU1JTg1JTg1JUU2JUE4JUExJUU1JTlFJThCJUU3JTlBJTg0JUU5JUEyJTg0JUU2JUI1JThCJUU1JTkyJThDJUU2JUEwJTg3JUU3JUFEJUJFJUU1JTkwJThFJUU2JTg5JThEJUU4JTgzJUJEJUU4JUIwJTgzJUU3JTk0JUE4JTIwZ2F0aGVyZSgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJlZGljdGlvbnMlMjAlM0QlMjBhY2NlbGVyYXRvci5wYWRfYWNyb3NzX3Byb2Nlc3NlcyhwcmVkaWN0aW9ucyUyQyUyMGRpbSUzRDElMkMlMjBwYWRfaW5kZXglM0QtMTAwKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxhYmVscyUyMCUzRCUyMGFjY2VsZXJhdG9yLnBhZF9hY3Jvc3NfcHJvY2Vzc2VzKGxhYmVscyUyQyUyMGRpbSUzRDElMkMlMjBwYWRfaW5kZXglM0QtMTAwKSUwQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByZWRpY3Rpb25zX2dhdGhlcmVkJTIwJTNEJTIwYWNjZWxlcmF0b3IuZ2F0aGVyKHByZWRpY3Rpb25zKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxhYmVsc19nYXRoZXJlZCUyMCUzRCUyMGFjY2VsZXJhdG9yLmdhdGhlcihsYWJlbHMpJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdHJ1ZV9wcmVkaWN0aW9ucyUyQyUyMHRydWVfbGFiZWxzJTIwJTNEJTIwcG9zdHByb2Nlc3MocHJlZGljdGlvbnNfZ2F0aGVyZWQlMkMlMjBsYWJlbHNfZ2F0aGVyZWQpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbWV0cmljLmFkZF9iYXRjaChwcmVkaWN0aW9ucyUzRHRydWVfcHJlZGljdGlvbnMlMkMlMjByZWZlcmVuY2VzJTNEdHJ1ZV9sYWJlbHMpJTBBJTBBJTIwJTIwJTIwJTIwcmVzdWx0cyUyMCUzRCUyMG1ldHJpYy5jb21wdXRlKCklMEElMjAlMjAlMjAlMjBwcmludCglMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmJTIyZXBvY2glMjAlN0JlcG9jaCU3RCUzQSUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGtleSUzQSUyMHJlc3VsdHMlNUJmJTIyb3ZlcmFsbF8lN0JrZXklN0QlMjIlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmb3IlMjBrZXklMjBpbiUyMCU1QiUyMnByZWNpc2lvbiUyMiUyQyUyMCUyMnJlY2FsbCUyMiUyQyUyMCUyMmYxJTIyJTJDJTIwJTIyYWNjdXJhY3klMjIlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlN0QlMkMlMEElMjAlMjAlMjAlMjApJTBBJTBBJTIwJTIwJTIwJTIwJTIzJTIwJUU0JUJGJTlEJUU1JUFEJTk4JUU1JUI5JUI2JUU0JUI4JThBJUU0JUJDJUEwJTBBJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3Iud2FpdF9mb3JfZXZlcnlvbmUoKSUwQSUyMCUyMCUyMCUyMHVud3JhcHBlZF9tb2RlbCUyMCUzRCUyMGFjY2VsZXJhdG9yLnVud3JhcF9tb2RlbChtb2RlbCklMEElMjAlMjAlMjAlMjB1bndyYXBwZWRfbW9kZWwuc2F2ZV9wcmV0cmFpbmVkKG91dHB1dF9kaXIlMkMlMjBzYXZlX2Z1bmN0aW9uJTNEYWNjZWxlcmF0b3Iuc2F2ZSklMEElMjAlMjAlMjAlMjBpZiUyMGFjY2VsZXJhdG9yLmlzX21haW5fcHJvY2VzcyUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRva2VuaXplci5zYXZlX3ByZXRyYWluZWQob3V0cHV0X2RpciklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXBvLnB1c2hfdG9faHViKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNvbW1pdF9tZXNzYWdlJTNEZiUyMlRyYWluaW5nJTIwaW4lMjBwcm9ncmVzcyUyMGVwb2NoJTIwJTdCZXBvY2glN0QlMjIlMkMlMjBibG9ja2luZyUzREZhbHNlJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKQ==",highlighted:`<span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm
<span class="hljs-keyword">import</span> torch

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>()
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> eval_dataloader:
        <span class="hljs-keyword">with</span> torch.no_grad():
            outputs = model(**batch)

        predictions = outputs.logits.argmax(dim=-<span class="hljs-number">1</span>)
        labels = batch[<span class="hljs-string">&quot;labels&quot;</span>]

        <span class="hljs-comment"># 填充模型的预测和标签后才能调用 gathere()</span>
        predictions = accelerator.pad_across_processes(predictions, dim=<span class="hljs-number">1</span>, pad_index=-<span class="hljs-number">100</span>)
        labels = accelerator.pad_across_processes(labels, dim=<span class="hljs-number">1</span>, pad_index=-<span class="hljs-number">100</span>)

        predictions_gathered = accelerator.gather(predictions)
        labels_gathered = accelerator.gather(labels)

        true_predictions, true_labels = postprocess(predictions_gathered, labels_gathered)
        metric.add_batch(predictions=true_predictions, references=true_labels)

    results = metric.compute()
    <span class="hljs-built_in">print</span>(
        <span class="hljs-string">f&quot;epoch <span class="hljs-subst">{epoch}</span>:&quot;</span>,
        {
            key: results[<span class="hljs-string">f&quot;overall_<span class="hljs-subst">{key}</span>&quot;</span>]
            <span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> [<span class="hljs-string">&quot;precision&quot;</span>, <span class="hljs-string">&quot;recall&quot;</span>, <span class="hljs-string">&quot;f1&quot;</span>, <span class="hljs-string">&quot;accuracy&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}}),gl=new C({props:{code:"YWNjZWxlcmF0b3Iud2FpdF9mb3JfZXZlcnlvbmUoKSUwQXVud3JhcHBlZF9tb2RlbCUyMCUzRCUyMGFjY2VsZXJhdG9yLnVud3JhcF9tb2RlbChtb2RlbCklMEF1bndyYXBwZWRfbW9kZWwuc2F2ZV9wcmV0cmFpbmVkKG91dHB1dF9kaXIlMkMlMjBzYXZlX2Z1bmN0aW9uJTNEYWNjZWxlcmF0b3Iuc2F2ZSk=",highlighted:`accelerator.wait_for_everyone()
unwrapped_model = accelerator.unwrap_model(model)
unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save)`,wrap:!1}}),{c(){j(M.$$.fragment),u=a(),i=T("p"),i.innerHTML=f,_=a(),h=T("p"),h.innerHTML=x,Z=a(),j(b.$$.fragment),g=a(),B=T("p"),B.innerHTML=A,G=a(),j($.$$.fragment),W=a(),E=T("p"),E.innerHTML=es,Rl=a(),j(X.$$.fragment),vl=a(),j(D.$$.fragment),Q=a(),j(N.$$.fragment),El=a(),j(q.$$.fragment),il=a(),K=T("p"),K.innerHTML=ve,rl=a(),j(ll.$$.fragment),Gl=a(),Y=T("p"),Y.innerHTML=ss,Ql=a(),j(V.$$.fragment),Xl=a(),F=T("p"),F.innerHTML=Vl,Zl=a(),j(R.$$.fragment),Ie=a(),nl=T("p"),nl.innerHTML=H,Nl=a(),j(z.$$.fragment),Tl=a(),ol=T("p"),ol.textContent=Ul,jl=a(),al=T("p"),al.innerHTML=el,Yl=a(),j(S.$$.fragment),cl=a(),pl=T("p"),pl.textContent=sl,Al=a(),j(v.$$.fragment),$e=a(),Ml=T("p"),Ml.innerHTML=tl,Fl=a(),L=T("p"),L.textContent=r,I=a(),j(ml.$$.fragment),Ee=a(),wl=T("p"),wl.innerHTML=Ms,Hl=a(),j(ke.$$.fragment),Xe=a(),dl=T("p"),dl.innerHTML=ut,Ne=a(),j(bl.$$.fragment),Ye=a(),ul=T("p"),ul.textContent=is,zl=a(),j(_e.$$.fragment),Fe=a(),fl=T("p"),fl.innerHTML=rs,Sl=a(),j(Ll.$$.fragment),Dl=a(),ge=T("p"),ge.innerHTML=cs,ql=a(),j(Ol.$$.fragment),Pl=a(),j(Kl.$$.fragment),le=a(),xe=T("p"),xe.innerHTML=Js,ee=a(),j(se.$$.fragment),te=a(),ne=T("p"),ne.innerHTML=Ze,ys=a(),j(ae.$$.fragment),pe=a(),j(Ae.$$.fragment),He=a(),hl=T("p"),hl.textContent=ft,ze=a(),j(Cl.$$.fragment),Se=a(),Il=T("p"),Il.innerHTML=Ts,Me=a(),j(Be.$$.fragment),Le=a(),$l=T("p"),$l.innerHTML=os,ie=a(),j(We.$$.fragment),De=a(),Jl=T("p"),Jl.textContent=Us,re=a(),ce=T("ul"),ce.innerHTML=Re,qe=a(),kl=T("p"),kl.textContent=ht,Oe=a(),j(_l.$$.fragment),js=a(),Je=T("p"),Je.textContent=Ge,Pe=a(),j(gl.$$.fragment),ms=a(),ye=T("p"),ye.innerHTML=Qe,Ke=a(),xl=T("p"),xl.innerHTML=ws},l(e){m(M.$$.fragment,e),u=p(e),i=o(e,"P",{"data-svelte-h":!0}),U(i)!=="svelte-ozfnd8"&&(i.innerHTML=f),_=p(e),h=o(e,"P",{"data-svelte-h":!0}),U(h)!=="svelte-n6wyai"&&(h.innerHTML=x),Z=p(e),m(b.$$.fragment,e),g=p(e),B=o(e,"P",{"data-svelte-h":!0}),U(B)!=="svelte-17s7g3y"&&(B.innerHTML=A),G=p(e),m($.$$.fragment,e),W=p(e),E=o(e,"P",{"data-svelte-h":!0}),U(E)!=="svelte-1hk2prm"&&(E.innerHTML=es),Rl=p(e),m(X.$$.fragment,e),vl=p(e),m(D.$$.fragment,e),Q=p(e),m(N.$$.fragment,e),El=p(e),m(q.$$.fragment,e),il=p(e),K=o(e,"P",{"data-svelte-h":!0}),U(K)!=="svelte-t70ie3"&&(K.innerHTML=ve),rl=p(e),m(ll.$$.fragment,e),Gl=p(e),Y=o(e,"P",{"data-svelte-h":!0}),U(Y)!=="svelte-iyv620"&&(Y.innerHTML=ss),Ql=p(e),m(V.$$.fragment,e),Xl=p(e),F=o(e,"P",{"data-svelte-h":!0}),U(F)!=="svelte-gtn8hq"&&(F.innerHTML=Vl),Zl=p(e),m(R.$$.fragment,e),Ie=p(e),nl=o(e,"P",{"data-svelte-h":!0}),U(nl)!=="svelte-1cm2676"&&(nl.innerHTML=H),Nl=p(e),m(z.$$.fragment,e),Tl=p(e),ol=o(e,"P",{"data-svelte-h":!0}),U(ol)!=="svelte-yr23n0"&&(ol.textContent=Ul),jl=p(e),al=o(e,"P",{"data-svelte-h":!0}),U(al)!=="svelte-gohefw"&&(al.innerHTML=el),Yl=p(e),m(S.$$.fragment,e),cl=p(e),pl=o(e,"P",{"data-svelte-h":!0}),U(pl)!=="svelte-1wy42j7"&&(pl.textContent=sl),Al=p(e),m(v.$$.fragment,e),$e=p(e),Ml=o(e,"P",{"data-svelte-h":!0}),U(Ml)!=="svelte-ol5aum"&&(Ml.innerHTML=tl),Fl=p(e),L=o(e,"P",{"data-svelte-h":!0}),U(L)!=="svelte-5myn0z"&&(L.textContent=r),I=p(e),m(ml.$$.fragment,e),Ee=p(e),wl=o(e,"P",{"data-svelte-h":!0}),U(wl)!=="svelte-30wne3"&&(wl.innerHTML=Ms),Hl=p(e),m(ke.$$.fragment,e),Xe=p(e),dl=o(e,"P",{"data-svelte-h":!0}),U(dl)!=="svelte-rfg3xq"&&(dl.innerHTML=ut),Ne=p(e),m(bl.$$.fragment,e),Ye=p(e),ul=o(e,"P",{"data-svelte-h":!0}),U(ul)!=="svelte-1kx3rp4"&&(ul.textContent=is),zl=p(e),m(_e.$$.fragment,e),Fe=p(e),fl=o(e,"P",{"data-svelte-h":!0}),U(fl)!=="svelte-cqyjsu"&&(fl.innerHTML=rs),Sl=p(e),m(Ll.$$.fragment,e),Dl=p(e),ge=o(e,"P",{"data-svelte-h":!0}),U(ge)!=="svelte-84vkgh"&&(ge.innerHTML=cs),ql=p(e),m(Ol.$$.fragment,e),Pl=p(e),m(Kl.$$.fragment,e),le=p(e),xe=o(e,"P",{"data-svelte-h":!0}),U(xe)!=="svelte-2kz0f2"&&(xe.innerHTML=Js),ee=p(e),m(se.$$.fragment,e),te=p(e),ne=o(e,"P",{"data-svelte-h":!0}),U(ne)!=="svelte-wdsnw7"&&(ne.innerHTML=Ze),ys=p(e),m(ae.$$.fragment,e),pe=p(e),m(Ae.$$.fragment,e),He=p(e),hl=o(e,"P",{"data-svelte-h":!0}),U(hl)!=="svelte-16xyo2m"&&(hl.textContent=ft),ze=p(e),m(Cl.$$.fragment,e),Se=p(e),Il=o(e,"P",{"data-svelte-h":!0}),U(Il)!=="svelte-1fwndsw"&&(Il.innerHTML=Ts),Me=p(e),m(Be.$$.fragment,e),Le=p(e),$l=o(e,"P",{"data-svelte-h":!0}),U($l)!=="svelte-1c8q3e6"&&($l.innerHTML=os),ie=p(e),m(We.$$.fragment,e),De=p(e),Jl=o(e,"P",{"data-svelte-h":!0}),U(Jl)!=="svelte-ja2imj"&&(Jl.textContent=Us),re=p(e),ce=o(e,"UL",{"data-svelte-h":!0}),U(ce)!=="svelte-kwyf8d"&&(ce.innerHTML=Re),qe=p(e),kl=o(e,"P",{"data-svelte-h":!0}),U(kl)!=="svelte-2dz2we"&&(kl.textContent=ht),Oe=p(e),m(_l.$$.fragment,e),js=p(e),Je=o(e,"P",{"data-svelte-h":!0}),U(Je)!=="svelte-rpln4e"&&(Je.textContent=Ge),Pe=p(e),m(gl.$$.fragment,e),ms=p(e),ye=o(e,"P",{"data-svelte-h":!0}),U(ye)!=="svelte-sf1n9m"&&(ye.innerHTML=Qe),Ke=p(e),xl=o(e,"P",{"data-svelte-h":!0}),U(xl)!=="svelte-esp3m8"&&(xl.innerHTML=ws)},m(e,y){w(M,e,y),t(e,u,y),t(e,i,y),t(e,_,y),t(e,h,y),t(e,Z,y),w(b,e,y),t(e,g,y),t(e,B,y),t(e,G,y),w($,e,y),t(e,W,y),t(e,E,y),t(e,Rl,y),w(X,e,y),t(e,vl,y),w(D,e,y),t(e,Q,y),w(N,e,y),t(e,El,y),w(q,e,y),t(e,il,y),t(e,K,y),t(e,rl,y),w(ll,e,y),t(e,Gl,y),t(e,Y,y),t(e,Ql,y),w(V,e,y),t(e,Xl,y),t(e,F,y),t(e,Zl,y),w(R,e,y),t(e,Ie,y),t(e,nl,y),t(e,Nl,y),w(z,e,y),t(e,Tl,y),t(e,ol,y),t(e,jl,y),t(e,al,y),t(e,Yl,y),w(S,e,y),t(e,cl,y),t(e,pl,y),t(e,Al,y),w(v,e,y),t(e,$e,y),t(e,Ml,y),t(e,Fl,y),t(e,L,y),t(e,I,y),w(ml,e,y),t(e,Ee,y),t(e,wl,y),t(e,Hl,y),w(ke,e,y),t(e,Xe,y),t(e,dl,y),t(e,Ne,y),w(bl,e,y),t(e,Ye,y),t(e,ul,y),t(e,zl,y),w(_e,e,y),t(e,Fe,y),t(e,fl,y),t(e,Sl,y),w(Ll,e,y),t(e,Dl,y),t(e,ge,y),t(e,ql,y),w(Ol,e,y),t(e,Pl,y),w(Kl,e,y),t(e,le,y),t(e,xe,y),t(e,ee,y),w(se,e,y),t(e,te,y),t(e,ne,y),t(e,ys,y),w(ae,e,y),t(e,pe,y),w(Ae,e,y),t(e,He,y),t(e,hl,y),t(e,ze,y),w(Cl,e,y),t(e,Se,y),t(e,Il,y),t(e,Me,y),w(Be,e,y),t(e,Le,y),t(e,$l,y),t(e,ie,y),w(We,e,y),t(e,De,y),t(e,Jl,y),t(e,re,y),t(e,ce,y),t(e,qe,y),t(e,kl,y),t(e,Oe,y),w(_l,e,y),t(e,js,y),t(e,Je,y),t(e,Pe,y),w(gl,e,y),t(e,ms,y),t(e,ye,y),t(e,Ke,y),t(e,xl,y),Te=!0},i(e){Te||(c(M.$$.fragment,e),c(b.$$.fragment,e),c($.$$.fragment,e),c(X.$$.fragment,e),c(D.$$.fragment,e),c(N.$$.fragment,e),c(q.$$.fragment,e),c(ll.$$.fragment,e),c(V.$$.fragment,e),c(R.$$.fragment,e),c(z.$$.fragment,e),c(S.$$.fragment,e),c(v.$$.fragment,e),c(ml.$$.fragment,e),c(ke.$$.fragment,e),c(bl.$$.fragment,e),c(_e.$$.fragment,e),c(Ll.$$.fragment,e),c(Ol.$$.fragment,e),c(Kl.$$.fragment,e),c(se.$$.fragment,e),c(ae.$$.fragment,e),c(Ae.$$.fragment,e),c(Cl.$$.fragment,e),c(Be.$$.fragment,e),c(We.$$.fragment,e),c(_l.$$.fragment,e),c(gl.$$.fragment,e),Te=!0)},o(e){J(M.$$.fragment,e),J(b.$$.fragment,e),J($.$$.fragment,e),J(X.$$.fragment,e),J(D.$$.fragment,e),J(N.$$.fragment,e),J(q.$$.fragment,e),J(ll.$$.fragment,e),J(V.$$.fragment,e),J(R.$$.fragment,e),J(z.$$.fragment,e),J(S.$$.fragment,e),J(v.$$.fragment,e),J(ml.$$.fragment,e),J(ke.$$.fragment,e),J(bl.$$.fragment,e),J(_e.$$.fragment,e),J(Ll.$$.fragment,e),J(Ol.$$.fragment,e),J(Kl.$$.fragment,e),J(se.$$.fragment,e),J(ae.$$.fragment,e),J(Ae.$$.fragment,e),J(Cl.$$.fragment,e),J(Be.$$.fragment,e),J(We.$$.fragment,e),J(_l.$$.fragment,e),J(gl.$$.fragment,e),Te=!1},d(e){e&&(s(u),s(i),s(_),s(h),s(Z),s(g),s(B),s(G),s(W),s(E),s(Rl),s(vl),s(Q),s(El),s(il),s(K),s(rl),s(Gl),s(Y),s(Ql),s(Xl),s(F),s(Zl),s(Ie),s(nl),s(Nl),s(Tl),s(ol),s(jl),s(al),s(Yl),s(cl),s(pl),s(Al),s($e),s(Ml),s(Fl),s(L),s(I),s(Ee),s(wl),s(Hl),s(Xe),s(dl),s(Ne),s(Ye),s(ul),s(zl),s(Fe),s(fl),s(Sl),s(Dl),s(ge),s(ql),s(Pl),s(le),s(xe),s(ee),s(te),s(ne),s(ys),s(pe),s(He),s(hl),s(ze),s(Se),s(Il),s(Me),s(Le),s($l),s(ie),s(De),s(Jl),s(re),s(ce),s(qe),s(kl),s(Oe),s(js),s(Je),s(Pe),s(ms),s(ye),s(Ke),s(xl)),d(M,e),d(b,e),d($,e),d(X,e),d(D,e),d(N,e),d(q,e),d(ll,e),d(V,e),d(R,e),d(z,e),d(S,e),d(v,e),d(ml,e),d(ke,e),d(bl,e),d(_e,e),d(Ll,e),d(Ol,e),d(Kl,e),d(se,e),d(ae,e),d(Ae,e),d(Cl,e),d(Be,e),d(We,e),d(_l,e),d(gl,e)}}}function Wa(k){let M,u="⚠️ 如果你的模型的标签数量有错误，那么在后面调用 <code>Trainer.train()</code> 时，你会得到一个晦涩的错误（类似于“CUDA error：device-side assert triggered”）。这可能会令人烦恼，所以确保你做了这个检查，确认你的标签数量是正确。";return{c(){M=T("p"),M.innerHTML=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-dl32z"&&(M.innerHTML=u)},m(i,f){t(i,M,f)},p:Ve,d(i){i&&s(M)}}}function Ra(k){let M,u="💡 如果你使用的输出路径已经存在一个同名的文件夹，那么它需要是你想推送到 hub 的仓库的克隆在本地的版本。如果不是，你将在声明 <code>Trainer</code> 时遇到一个错误，并需要设置一个新的路径。";return{c(){M=T("p"),M.innerHTML=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1cgl3va"&&(M.innerHTML=u)},m(i,f){t(i,M,f)},p:Ve,d(i){i&&s(M)}}}function Ga(k){let M,u='🚨 如果你正在 TPU 上训练，你需要将上面单元格开始的所有代码移动到一个专门的训练函数中。更多详情请回顾 <a href="/course/chapter3">第三章</a> 。';return{c(){M=T("p"),M.innerHTML=u},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1iilxjx"&&(M.innerHTML=u)},m(i,f){t(i,M,f)},p:Ve,d(i){i&&s(M)}}}function Qa(k){let M,u,i,f,_,h,x,Z,b,g,B,A,G="我们将首先探讨的应用是 Token 分类。这个通用任务涵盖了所有可以表述为“给句子中的词或字贴上标签”的问题，例如：",$,W,E,es="<strong>实体命名识别 （NER）</strong>：找出句子中的实体（如人物、地点或组织）。这可以通过为每个实体指定一个类别的标签，如果没有实体则会输出无实体的标签。",Rl,X,vl="<strong>词性标注 （POS）</strong>：将句子中的每个单词标记为对应于特定的词性（如名词、动词、形容词等）。",D,Q,N,El="分块（chunking）",q,il,K="B-",ve,rl,ll="I-",Gl,Y,ss="O",Ql,V,Xl,F,Vl="当然，还有很多其他类型的 token 分类问题；这些只是几个有代表性的例子。在本节中，我们将在 NER 任务上微调模型 （BERT），然后该模型将能够生成如下预测：",Zl,R,Ie,nl,H,Nl='<img class="block dark:hidden lg:w-3/5" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/model-eval-bert-finetuned-ner.png" alt="One-hot encoded labels for question answering."/> <img class="hidden dark:block lg:w-3/5" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/model-eval-bert-finetuned-ner-dark.png" alt="One-hot encoded labels for question answering."/>',z,Tl,ol='你可以 <a href="https://huggingface.co/huggingface-course/bert-finetuned-ner?text=My+name+is+Sylvain+and+I+work+at+Hugging+Face+in+Brooklyn" rel="nofollow">在这里</a> 。找到我们将训练并上传到 Hub 的模型，可以尝试输入一些句子测试一下模型的预测结果。',Ul,jl,al,el,Yl='首先，我们需要一个适合 token 分类的数据集。在本节中，我们将使用 <a href="https://huggingface.co/datasets/conll2003" rel="nofollow">CoNLL-2003 数据集</a> ，该数据集来源于路透社的新闻报道。',S,cl,pl,sl,Al,v,$e="要加载 CoNLL-2003 数据集，我们可以使用🤗 Datasets 库的 <code>load_dataset()</code> 方法：",Ml,tl,Fl,L,r='这将下载并缓存数据集，就像和我们在 <a href="/course/chapter3">第三章</a> 加载 GLUE MRPC 数据集一样。查看 <code>raw_datasets</code> 对象可以让我们看到数据集中存在哪些列，以及训练集、验证集和测试集之间是如何划分的：',I,ml,Ee,wl,Ms,Hl,ke="可以看到数据集包含了我们之前提到的三项任务的标签：命名实体识别（NER）、词性标注（POS）以及分块（chunking）。这个数据集与其他数据集的一个显著区别在于，输入文本的那一列并非以句子或整片的文本的形式存储，而是以单词列表的形式（最后一列被称为 <code>tokens</code> ，不过 <code>tokens</code> 列保存的还是单词，也就是说，这些预先分词的输入仍需要经过 tokenizer 进行子词分词处理）。",Xe,dl,ut="我们来看看训练集的第一个元素：",Ne,bl,Ye,ul,is,zl,_e="由于我们要进行命名实体识别，让我们查看一下 NER 标签：",Fe,fl,rs,Sl,Ll,Dl,ge="这些是可以直接训练的整数类别标签，当我们想要检查数据时，直接看整数的标签不是很直观。就像在文本分类中，我们可以通过查看数据集的 <code>features</code> 属性来找到这些整数和标签名称之间的对应关系：",cs,ql,Ol,Pl,Kl,le,xe="因此，我们得到了 <code>ClassLabels</code> 类型的序列。序列中元素的类型存储在 <code>ner_feature</code> 的 <code>feature</code> 中，我们可以通过查看该 <code>feature</code> 的 <code>names</code> 属性来访问标签名称的列表：",Js,ee,se,te,ne,Ze,ys='我们在 <a href="/course/chapter6/3">第六章</a> 深入研究 <code>token-classification</code> 管道时已经看到过这些标签 我们在这里进行一个快速的回顾：',ae,pe,Ae="<li><code>O</code> 表示这个词不对应任何实体。</li> <li><code>B-PER</code> / <code>I-PER</code> 意味着这个词对应于人名实体的开头/内部。</li> <li><code>B-ORG</code> / <code>I-ORG</code> 的意思是这个词对应于组织名称实体的开头/内部。</li> <li><code>B-LOC</code> / <code>I-LOC</code> 指的是是这个词对应于地名实体的开头/内部。</li> <li><code>B-MISC</code> / <code>I-MISC</code> 表示这个词对应一个其他实体（不属于特定类别或类别之外）的开头 / 内部。</li>",He,hl,ft="接下来使用对这些标签名对数据集的 <code>ner_tags</code> 进行解码，将得到以下输出结果:",ze,Cl,Se,Il,Ts,Me,Be="我们还可以查看训练集中索引为 4 的数据，它是一个同时包含 <code>B-</code> 和 <code>I-</code> 标签的例子：",Le,$l,os,ie,We="正如我们在上面的输出中所看到的，跨越两个单词的实体，如“European Union”和“Werner Zwingmann”，数据集把第一个单词标注为了 <code>B-</code> 标签，将第二个单词标记为了 <code>I-</code> 标签。",De,Jl,Us,re,ce,Re,qe,kl,ht='像往常一样，我们的文本需要转换为 Token ID，然后模型才能理解它们。正如我们在 <a href="/course/chapter6/">第六章</a> 所学的那样。不过与 tokens 分类任务不同的是数据集已经完成了预分词，我们在处理的过程中不需要再次分词。幸运的是，tokenizer API 可以很容易地处理这种差异；我们只需要通过一个特殊的标志告诉 tokenizer即可。',Oe,_l,js="首先，让我们创建 <code>tokenizer</code> 对象。如前所述，我们将使用 BERT 预训练模型，因此我们将从下载并缓存关联的 <code>tokenizer</code> 开始：",Je,Ge,Pe,gl,ms='你可以更换把 <code>model_checkpoint</code> 更换为 <a href="https://huggingface.co/models" rel="nofollow">Hub</a> 上你喜欢的任何其他模型，或使用本地保存的预训练模型和 <code>tokenizer</code>。唯一的限制是 tokenizer 需要由 🤗 Tokenizers 库支持，并且有一个“快速”版本可用。你可以在 <a href="https://huggingface.co/transformers/#supported-frameworks" rel="nofollow">Huggingface 模型后端支持表</a> 上看到所有带有快速版本的 <code>tokenizer</code> 架构，或者你也可以通过查看它 <code>is_fast</code> 属性来检测正在使用的 <code>tokenizer</code> 对象是否由 🤗 Tokenizers 支持：',ye,Qe,Ke,xl,ws,Te,e="我们可以像往常一样使用我们的 <code>tokenizer</code> 对预先分词的输入进行 <code>tokenize</code> ，只需额外添加 <code>is_split_into_words=True</code> 参数：",y,ds,$t,bs,kt,us,In="如我们所见，<code>tokenizer</code> 在结果中添加了模型需要使用的特殊 tokens（在开头的 <code>[CLS]</code> ，在结尾的 <code>[SEP]</code> ），并且大部分单词保持不变。不过，单词 <code>lamb</code> 被分词为两个子词， <code>la</code> 和 <code>##mb</code> 。这导致了输入和标签之间的不匹配：标签列表只有 9 个元素，而我们的输入现在有 12 个 tokens。解决特殊 tokens 的问题很容易（我们已经知道了每个 <code>token</code> 开始和结束的位置），我们需要确保的是我们将所有的标签与正确的词对齐。",_t,fs,$n='幸运的是，由于我们使用的是快速 tokens 因此我们可以使用🤗 Tokenizers 超能力，这意味着我们可以轻松地将每个 token 映射到其相应的单词（如 <a href="/course/chapter6/3">第六章</a> 中所学）：',gt,hs,xt,Cs,Zt,Is,kn="只需要一点点工作，我们就可以扩展我们的标签列表。我们将添加的使其与 <code>token</code> 列表相匹配。 添加的第一条规则是，特殊 tokens 的标签设置为 <code>-100</code> 。这是因为默认情况下， <code>-100</code> 会被我们的损失函数（交叉熵）忽略。然后将每个 <code>token</code> 的标签设置为这个 <code>token</code> 所在单词开头的 <code>token</code> 的标签，因为同一个单词一定是同一个实体的一部分。最后将单词的内部 <code>tokens</code> 标签中的 <code>B-</code> 替换为 <code>I-</code>，因为该 <code>token</code> 不在实体的开头，<code>B-</code> 在每个实体中只应该出现一次：",At,$s,Bt,ks,_n="让我们在数据集的第一个元素上试一试：",Wt,_s,Rt,gs,Gt,xs,gn="正如我们所看到的，我们的函数为开头和结尾添加了两个特殊 tokens ：<code>-100</code> ，并为切分成两个 tokens 的单词添加了一个新的 <code>0</code> 标签。",Qt,ts,Vt,Zs,xn="为了预处理我们的整个数据集，我们需要对所有输入进行 <code>tokenize</code>，并使用 <code>align_labels_with_tokens()</code> 函数处理所有标签。为了充分利用快速 tokenizer 的优势，最好是同时对大量文本一起进行 <code>tokenize</code>，所以我们需要编写一个处理一组示例的函数，并使用带有 <code>batched=True</code> 参数的 <code>Dataset.map()</code> 方法。与我们之前的示例唯一不同的是，当 <code>tokenizer</code> 的输入是文本列表（或示例中单词的列表的列表）时， <code>word_ids()</code> 函数需要根据列表的索引获取 <code>token</code> 的 ID，所以我们也在下面的函数中添加了这个功能：",vt,As,Et,Bs,Zn="注意，我们还没有填充我们的输入，我们将在稍后使用数据整理器创建 <code>batch</code> 时进行。",Xt,Ws,An="我们现在可以一次性使用预处理函数处理整个数据集：",Nt,Rs,Yt,Gs,Bn='我们已经完成了最困难的部分！现在数据已经经过了预处理，实际的训练过程将会与我们在 <a href="/course/chapter3">第三章</a> 所做的很相似。',Ft,oe,Ue,ot,Qs,Ht,Vs,Wn='我们不能像 <a href="/course/chapter3">第三章</a> 那样直接使用 <code>DataCollatorWithPadding</code> ，因为那样只会填充输入（inputs ID、注意掩码和 tokens 类型 ID）。除了输入部分，在这里我们还需要对标签也使用与输入完全相同的方式填充，以保证它与输入的大小相同。我们将使用 <code>-100</code> 进行填充，以便在损失计算中忽略填充的内容。',zt,vs,Rn='以上的这些过程可以由 <a href="https://huggingface.co/transformers/main_classes/data_collator.html#datacollatorfortokenclassification" rel="nofollow"><code>DataCollatorForTokenClassification</code></a> 实现。它是一个带有填充功能的数据整理器，使用时只需要传入用于预处理输入的 <code>tokenizer</code> ：',St,je,me,Ut,Es,Gn="为了在几个样本上测试这个数据整理器，我们可以先在训练集中的几个示例上调用它：",Lt,Xs,Dt,Ns,qt,Ys,Qn="将数据整理器的结果与数据集中未经处理的第一个和第二个元素的标签进行比较。",Ot,Fs,Pt,Hs,Kt,we,de,jt,mt,zs,ln,be,ue,wt,Ss,en,Ls,Vn="该指标和常规的评测指标有些区别：它需要字符串形式的标签列表而不是整数，所以我们需要在将它们传递给指标之前将预测值和标签由数字解码为字符串。让我们先用一些测试数据看看它是如何工作的：",sn,Ds,vn="首先，获取第一个训练样本的标签。",tn,qs,nn,Os,an,Ps,En="然后我们可以通过更改索引 2 处的值来为这些标签创建假的预测值来测试该指标：",pn,Ks,Mn,lt,Xn="请注意，该指标的输入是预测列表（不是一个）和标签列表，输出结果如下：",rn,et,cn,fe,he,dt,bt,st,Jn,tt,Nn="我们已经向你展示了如何使用在模型中心微调的模型和推理小部件。在本地使用 <code>pipeline</code> 来使用它非常容易，你只需要指定正确的模型标签：",yn,nt,Tn,at,on,pt,Yn="太棒了！我们的模型与此管道的默认模型一样有效！",Un,Mt,jn,Ct,mn;_=new Ua({props:{fw:k[0]}}),x=new yl({props:{title:"Token 分类",local:"Token 分类",headingTag:"h1"}});const Fn=[wa,ma],it=[];function Hn(l,n){return l[0]==="pt"?0:1}b=Hn(k),g=it[b]=Fn[b](k),V=new aa({props:{id:"wVHdVlPScxA"}}),jl=new yl({props:{title:"准备数据",local:"准备数据",headingTag:"h2"}}),cl=new ps({props:{$$slots:{default:[da]},$$scope:{ctx:k}}}),sl=new yl({props:{title:"CoNLL-2003 数据集",local:"CoNLL-2003 数据集",headingTag:"h3"}}),tl=new C({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBJTBBcmF3X2RhdGFzZXRzJTIwJTNEJTIwbG9hZF9kYXRhc2V0KCUyMmNvbmxsMjAwMyUyMik=",highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

raw_datasets = load_dataset(<span class="hljs-string">&quot;conll2003&quot;</span>)`,wrap:!1}}),ml=new C({props:{code:"cmF3X2RhdGFzZXRz",highlighted:"raw_datasets",wrap:!1}}),wl=new C({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2NodW5rX3RhZ3MnJTJDJTIwJ2lkJyUyQyUyMCduZXJfdGFncyclMkMlMjAncG9zX3RhZ3MnJTJDJTIwJ3Rva2VucyclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDE0MDQxJTBBJTIwJTIwJTIwJTIwJTdEKSUwQSUyMCUyMCUyMCUyMHZhbGlkYXRpb24lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2NodW5rX3RhZ3MnJTJDJTIwJ2lkJyUyQyUyMCduZXJfdGFncyclMkMlMjAncG9zX3RhZ3MnJTJDJTIwJ3Rva2VucyclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDMyNTAlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTIwJTIwJTIwJTIwdGVzdCUzQSUyMERhdGFzZXQoJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZmVhdHVyZXMlM0ElMjAlNUInY2h1bmtfdGFncyclMkMlMjAnaWQnJTJDJTIwJ25lcl90YWdzJyUyQyUyMCdwb3NfdGFncyclMkMlMjAndG9rZW5zJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwMzQ1MyUwQSUyMCUyMCUyMCUyMCU3RCklMEElN0Qp",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;chunk_tags&#x27;</span>, <span class="hljs-string">&#x27;id&#x27;</span>, <span class="hljs-string">&#x27;ner_tags&#x27;</span>, <span class="hljs-string">&#x27;pos_tags&#x27;</span>, <span class="hljs-string">&#x27;tokens&#x27;</span>],
        num_rows: <span class="hljs-number">14041</span>
    })
    validation: Dataset({
        features: [<span class="hljs-string">&#x27;chunk_tags&#x27;</span>, <span class="hljs-string">&#x27;id&#x27;</span>, <span class="hljs-string">&#x27;ner_tags&#x27;</span>, <span class="hljs-string">&#x27;pos_tags&#x27;</span>, <span class="hljs-string">&#x27;tokens&#x27;</span>],
        num_rows: <span class="hljs-number">3250</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;chunk_tags&#x27;</span>, <span class="hljs-string">&#x27;id&#x27;</span>, <span class="hljs-string">&#x27;ner_tags&#x27;</span>, <span class="hljs-string">&#x27;pos_tags&#x27;</span>, <span class="hljs-string">&#x27;tokens&#x27;</span>],
        num_rows: <span class="hljs-number">3453</span>
    })
})`,wrap:!1}}),bl=new C({props:{code:"cmF3X2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUIwJTVEJTVCJTIydG9rZW5zJTIyJTVE",highlighted:'raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&quot;tokens&quot;</span>]',wrap:!1}}),ul=new C({props:{code:"JTVCJ0VVJyUyQyUyMCdyZWplY3RzJyUyQyUyMCdHZXJtYW4nJTJDJTIwJ2NhbGwnJTJDJTIwJ3RvJyUyQyUyMCdib3ljb3R0JyUyQyUyMCdCcml0aXNoJyUyQyUyMCdsYW1iJyUyQyUyMCcuJyU1RA==",highlighted:'[<span class="hljs-string">&#x27;EU&#x27;</span>, <span class="hljs-string">&#x27;rejects&#x27;</span>, <span class="hljs-string">&#x27;German&#x27;</span>, <span class="hljs-string">&#x27;call&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;boycott&#x27;</span>, <span class="hljs-string">&#x27;British&#x27;</span>, <span class="hljs-string">&#x27;lamb&#x27;</span>, <span class="hljs-string">&#x27;.&#x27;</span>]',wrap:!1}}),fl=new C({props:{code:"cmF3X2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUIwJTVEJTVCJTIybmVyX3RhZ3MlMjIlNUQ=",highlighted:'raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&quot;ner_tags&quot;</span>]',wrap:!1}}),Sl=new C({props:{code:"JTVCMyUyQyUyMDAlMkMlMjA3JTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwNyUyQyUyMDAlMkMlMjAwJTVE",highlighted:'[<span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]',wrap:!1}}),ql=new C({props:{code:"bmVyX2ZlYXR1cmUlMjAlM0QlMjByYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RC5mZWF0dXJlcyU1QiUyMm5lcl90YWdzJTIyJTVEJTBBbmVyX2ZlYXR1cmU=",highlighted:`ner_feature = raw_datasets[<span class="hljs-string">&quot;train&quot;</span>].features[<span class="hljs-string">&quot;ner_tags&quot;</span>]
ner_feature`,wrap:!1}}),Pl=new C({props:{code:"U2VxdWVuY2UoZmVhdHVyZSUzRENsYXNzTGFiZWwobnVtX2NsYXNzZXMlM0Q5JTJDJTIwbmFtZXMlM0QlNUInTyclMkMlMjAnQi1QRVInJTJDJTIwJ0ktUEVSJyUyQyUyMCdCLU9SRyclMkMlMjAnSS1PUkcnJTJDJTIwJ0ItTE9DJyUyQyUyMCdJLUxPQyclMkMlMjAnQi1NSVNDJyUyQyUyMCdJLU1JU0MnJTVEJTJDJTIwbmFtZXNfZmlsZSUzRE5vbmUlMkMlMjBpZCUzRE5vbmUpJTJDJTIwbGVuZ3RoJTNELTElMkMlMjBpZCUzRE5vbmUp",highlighted:'<span class="hljs-type">Sequence</span>(feature=ClassLabel(num_classes=<span class="hljs-number">9</span>, names=[<span class="hljs-string">&#x27;O&#x27;</span>, <span class="hljs-string">&#x27;B-PER&#x27;</span>, <span class="hljs-string">&#x27;I-PER&#x27;</span>, <span class="hljs-string">&#x27;B-ORG&#x27;</span>, <span class="hljs-string">&#x27;I-ORG&#x27;</span>, <span class="hljs-string">&#x27;B-LOC&#x27;</span>, <span class="hljs-string">&#x27;I-LOC&#x27;</span>, <span class="hljs-string">&#x27;B-MISC&#x27;</span>, <span class="hljs-string">&#x27;I-MISC&#x27;</span>], names_file=<span class="hljs-literal">None</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>)',wrap:!1}}),ee=new C({props:{code:"bGFiZWxfbmFtZXMlMjAlM0QlMjBuZXJfZmVhdHVyZS5mZWF0dXJlLm5hbWVzJTBBbGFiZWxfbmFtZXM=",highlighted:`label_names = ner_feature.feature.names
label_names`,wrap:!1}}),te=new C({props:{code:"JTVCJ08nJTJDJTIwJ0ItUEVSJyUyQyUyMCdJLVBFUiclMkMlMjAnQi1PUkcnJTJDJTIwJ0ktT1JHJyUyQyUyMCdCLUxPQyclMkMlMjAnSS1MT0MnJTJDJTIwJ0ItTUlTQyclMkMlMjAnSS1NSVNDJyU1RA==",highlighted:'[<span class="hljs-string">&#x27;O&#x27;</span>, <span class="hljs-string">&#x27;B-PER&#x27;</span>, <span class="hljs-string">&#x27;I-PER&#x27;</span>, <span class="hljs-string">&#x27;B-ORG&#x27;</span>, <span class="hljs-string">&#x27;I-ORG&#x27;</span>, <span class="hljs-string">&#x27;B-LOC&#x27;</span>, <span class="hljs-string">&#x27;I-LOC&#x27;</span>, <span class="hljs-string">&#x27;B-MISC&#x27;</span>, <span class="hljs-string">&#x27;I-MISC&#x27;</span>]',wrap:!1}}),Cl=new C({props:{code:"d29yZHMlMjAlM0QlMjByYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QjAlNUQlNUIlMjJ0b2tlbnMlMjIlNUQlMEFsYWJlbHMlMjAlM0QlMjByYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QjAlNUQlNUIlMjJuZXJfdGFncyUyMiU1RCUwQWxpbmUxJTIwJTNEJTIwJTIyJTIyJTBBbGluZTIlMjAlM0QlMjAlMjIlMjIlMEFmb3IlMjB3b3JkJTJDJTIwbGFiZWwlMjBpbiUyMHppcCh3b3JkcyUyQyUyMGxhYmVscyklM0ElMEElMjAlMjAlMjAlMjBmdWxsX2xhYmVsJTIwJTNEJTIwbGFiZWxfbmFtZXMlNUJsYWJlbCU1RCUwQSUyMCUyMCUyMCUyMG1heF9sZW5ndGglMjAlM0QlMjBtYXgobGVuKHdvcmQpJTJDJTIwbGVuKGZ1bGxfbGFiZWwpKSUwQSUyMCUyMCUyMCUyMGxpbmUxJTIwJTJCJTNEJTIwd29yZCUyMCUyQiUyMCUyMiUyMCUyMiUyMColMjAobWF4X2xlbmd0aCUyMC0lMjBsZW4od29yZCklMjAlMkIlMjAxKSUwQSUyMCUyMCUyMCUyMGxpbmUyJTIwJTJCJTNEJTIwZnVsbF9sYWJlbCUyMCUyQiUyMCUyMiUyMCUyMiUyMColMjAobWF4X2xlbmd0aCUyMC0lMjBsZW4oZnVsbF9sYWJlbCklMjAlMkIlMjAxKSUwQSUwQXByaW50KGxpbmUxKSUwQXByaW50KGxpbmUyKQ==",highlighted:`words = raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&quot;tokens&quot;</span>]
labels = raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&quot;ner_tags&quot;</span>]
line1 = <span class="hljs-string">&quot;&quot;</span>
line2 = <span class="hljs-string">&quot;&quot;</span>
<span class="hljs-keyword">for</span> word, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(words, labels):
    full_label = label_names[label]
    max_length = <span class="hljs-built_in">max</span>(<span class="hljs-built_in">len</span>(word), <span class="hljs-built_in">len</span>(full_label))
    line1 += word + <span class="hljs-string">&quot; &quot;</span> * (max_length - <span class="hljs-built_in">len</span>(word) + <span class="hljs-number">1</span>)
    line2 += full_label + <span class="hljs-string">&quot; &quot;</span> * (max_length - <span class="hljs-built_in">len</span>(full_label) + <span class="hljs-number">1</span>)

<span class="hljs-built_in">print</span>(line1)
<span class="hljs-built_in">print</span>(line2)`,wrap:!1}}),Il=new C({props:{code:"J0VVJTIwJTIwJTIwJTIwcmVqZWN0cyUyMEdlcm1hbiUyMGNhbGwlMjB0byUyMGJveWNvdHQlMjBCcml0aXNoJTIwbGFtYiUyMC4nJTBBJ0ItT1JHJTIwTyUyMCUyMCUyMCUyMCUyMCUyMCUyMEItTUlTQyUyME8lMjAlMjAlMjAlMjBPJTIwJTIwTyUyMCUyMCUyMCUyMCUyMCUyMCUyMEItTUlTQyUyMCUyME8lMjAlMjAlMjAlMjBPJw==",highlighted:`<span class="hljs-string">&#x27;EU    rejects German call to boycott British lamb .&#x27;</span>
<span class="hljs-string">&#x27;B-ORG O       B-MISC O    O  O       B-MISC  O    O&#x27;</span>`,wrap:!1}}),$l=new C({props:{code:"J0dlcm1hbnklMjAlNUMncyUyMHJlcHJlc2VudGF0aXZlJTIwdG8lMjB0aGUlMjBFdXJvcGVhbiUyMFVuaW9uJTIwJTVDJ3MlMjB2ZXRlcmluYXJ5JTIwY29tbWl0dGVlJTIwV2VybmVyJTIwWndpbmdtYW5uJTIwc2FpZCUyMG9uJTIwV2VkbmVzZGF5JTIwY29uc3VtZXJzJTIwc2hvdWxkJTIwYnV5JTIwc2hlZXBtZWF0JTIwZnJvbSUyMGNvdW50cmllcyUyMG90aGVyJTIwdGhhbiUyMEJyaXRhaW4lMjB1bnRpbCUyMHRoZSUyMHNjaWVudGlmaWMlMjBhZHZpY2UlMjB3YXMlMjBjbGVhcmVyJTIwLiclMEEnQi1MT0MlMjAlMjAlMjBPJTIwJTIwTyUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyME8lMjAlMjBPJTIwJTIwJTIwQi1PUkclMjAlMjAlMjAlMjBJLU9SRyUyME8lMjAlMjBPJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwTyUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMEItUEVSJTIwJTIwSS1QRVIlMjAlMjAlMjAlMjAlMjBPJTIwJTIwJTIwJTIwTyUyMCUyME8lMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBPJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwTyUyMCUyMCUyMCUyMCUyMCUyME8lMjAlMjAlMjBPJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwTyUyMCUyMCUyMCUyME8lMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBPJTIwJTIwJTIwJTIwJTIwTyUyMCUyMCUyMCUyMEItTE9DJTIwJTIwJTIwTyUyMCUyMCUyMCUyMCUyME8lMjAlMjAlMjBPJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwTyUyMCUyMCUyMCUyMCUyMCUyME8lMjAlMjAlMjBPJTIwJTIwJTIwJTIwJTIwJTIwJTIwTyc=",highlighted:`<span class="hljs-string">&#x27;Germany \\&#x27;s representative to the European Union \\&#x27;s veterinary committee Werner Zwingmann said on Wednesday consumers should buy sheepmeat from countries other than Britain until the scientific advice was clearer .&#x27;</span>
<span class="hljs-string">&#x27;B-LOC   O  O              O  O   B-ORG    I-ORG O  O          O         B-PER  I-PER     O    O  O         O         O      O   O         O    O         O     O    B-LOC   O     O   O          O      O   O       O&#x27;</span>`,wrap:!1}}),Jl=new ps({props:{$$slots:{default:[ba]},$$scope:{ctx:k}}}),re=new yl({props:{title:"处理数据",local:"处理数据",headingTag:"h3"}}),Re=new aa({props:{id:"iY2AZYdZAr0"}}),Ge=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEFtb2RlbF9jaGVja3BvaW50JTIwJTNEJTIwJTIyYmVydC1iYXNlLWNhc2VkJTIyJTBBdG9rZW5pemVyJTIwJTNEJTIwQXV0b1Rva2VuaXplci5mcm9tX3ByZXRyYWluZWQobW9kZWxfY2hlY2twb2ludCk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

model_checkpoint = <span class="hljs-string">&quot;bert-base-cased&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)`,wrap:!1}}),Qe=new C({props:{code:"dG9rZW5pemVyLmlzX2Zhc3Q=",highlighted:"tokenizer.is_fast",wrap:!1}}),xl=new C({props:{code:"VHJ1ZQ==",highlighted:'<span class="hljs-literal">True</span>',wrap:!1}}),ds=new C({props:{code:"aW5wdXRzJTIwJTNEJTIwdG9rZW5pemVyKHJhd19kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVEJTVCMCU1RCU1QiUyMnRva2VucyUyMiU1RCUyQyUyMGlzX3NwbGl0X2ludG9fd29yZHMlM0RUcnVlKSUwQWlucHV0cy50b2tlbnMoKQ==",highlighted:`inputs = tokenizer(raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&quot;tokens&quot;</span>], is_split_into_words=<span class="hljs-literal">True</span>)
inputs.tokens()`,wrap:!1}}),bs=new C({props:{code:"JTVCJyU1QkNMUyU1RCclMkMlMjAnRVUnJTJDJTIwJ3JlamVjdHMnJTJDJTIwJ0dlcm1hbiclMkMlMjAnY2FsbCclMkMlMjAndG8nJTJDJTIwJ2JveWNvdHQnJTJDJTIwJ0JyaXRpc2gnJTJDJTIwJ2xhJyUyQyUyMCclMjMlMjNtYiclMkMlMjAnLiclMkMlMjAnJTVCU0VQJTVEJyU1RA==",highlighted:'[<span class="hljs-string">&#x27;[CLS]&#x27;</span>, <span class="hljs-string">&#x27;EU&#x27;</span>, <span class="hljs-string">&#x27;rejects&#x27;</span>, <span class="hljs-string">&#x27;German&#x27;</span>, <span class="hljs-string">&#x27;call&#x27;</span>, <span class="hljs-string">&#x27;to&#x27;</span>, <span class="hljs-string">&#x27;boycott&#x27;</span>, <span class="hljs-string">&#x27;British&#x27;</span>, <span class="hljs-string">&#x27;la&#x27;</span>, <span class="hljs-string">&#x27;##mb&#x27;</span>, <span class="hljs-string">&#x27;.&#x27;</span>, <span class="hljs-string">&#x27;[SEP]&#x27;</span>]',wrap:!1}}),hs=new C({props:{code:"aW5wdXRzLndvcmRfaWRzKCk=",highlighted:"inputs.word_ids()",wrap:!1}}),Cs=new C({props:{code:"JTVCTm9uZSUyQyUyMDAlMkMlMjAxJTJDJTIwMiUyQyUyMDMlMkMlMjA0JTJDJTIwNSUyQyUyMDYlMkMlMjA3JTJDJTIwNyUyQyUyMDglMkMlMjBOb25lJTVE",highlighted:'[<span class="hljs-literal">None</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-literal">None</span>]',wrap:!1}}),$s=new C({props:{code:"ZGVmJTIwYWxpZ25fbGFiZWxzX3dpdGhfdG9rZW5zKGxhYmVscyUyQyUyMHdvcmRfaWRzKSUzQSUwQSUyMCUyMCUyMCUyMG5ld19sYWJlbHMlMjAlM0QlMjAlNUIlNUQlMEElMjAlMjAlMjAlMjBjdXJyZW50X3dvcmQlMjAlM0QlMjBOb25lJTBBJTIwJTIwJTIwJTIwZm9yJTIwd29yZF9pZCUyMGluJTIwd29yZF9pZHMlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpZiUyMHdvcmRfaWQlMjAhJTNEJTIwY3VycmVudF93b3JkJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIzJTIwJUU2JTk2JUIwJUU1JThEJTk1JUU4JUFGJThEJUU3JTlBJTg0JUU1JUJDJTgwJUU1JUE3JThCISUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGN1cnJlbnRfd29yZCUyMCUzRCUyMHdvcmRfaWQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsYWJlbCUyMCUzRCUyMC0xMDAlMjBpZiUyMHdvcmRfaWQlMjBpcyUyME5vbmUlMjBlbHNlJTIwbGFiZWxzJTVCd29yZF9pZCU1RCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG5ld19sYWJlbHMuYXBwZW5kKGxhYmVsKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGVsaWYlMjB3b3JkX2lkJTIwaXMlMjBOb25lJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIzJTIwJUU3JTg5JUI5JUU2JUFFJThBJUU3JTlBJTg0dG9rZW4lMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBuZXdfbGFiZWxzLmFwcGVuZCgtMTAwKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGVsc2UlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjMlMjAlRTQlQjglOEUlRTUlODklOEQlRTQlQjglODAlRTQlQjglQUElMjB0b2tlbnMlMjAlRTclQjElQkIlRTUlOUUlOEIlRTclOUIlQjglRTUlOTAlOEMlRTclOUElODQlRTUlOEQlOTUlRTglQUYlOEQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsYWJlbCUyMCUzRCUyMGxhYmVscyU1QndvcmRfaWQlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjMlMjAlRTUlQTYlODIlRTYlOUUlOUMlRTYlQTAlODclRTclQUQlQkUlRTYlOTglQUYlMjBCLVhYWCUyMCVFNiU4OCU5MSVFNCVCQiVBQyVFNSVCMCU4NiVFNSU4NSVCNiVFNiU5QiVCNCVFNiU5NCVCOSVFNCVCOCVCQSUyMEktWFhYJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaWYlMjBsYWJlbCUyMCUyNSUyMDIlMjAlM0QlM0QlMjAxJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbGFiZWwlMjAlMkIlM0QlMjAxJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbmV3X2xhYmVscy5hcHBlbmQobGFiZWwpJTBBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwbmV3X2xhYmVscw==",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">align_labels_with_tokens</span>(<span class="hljs-params">labels, word_ids</span>):
    new_labels = []
    current_word = <span class="hljs-literal">None</span>
    <span class="hljs-keyword">for</span> word_id <span class="hljs-keyword">in</span> word_ids:
        <span class="hljs-keyword">if</span> word_id != current_word:
            <span class="hljs-comment"># 新单词的开始!</span>
            current_word = word_id
            label = -<span class="hljs-number">100</span> <span class="hljs-keyword">if</span> word_id <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">else</span> labels[word_id]
            new_labels.append(label)
        <span class="hljs-keyword">elif</span> word_id <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
            <span class="hljs-comment"># 特殊的token</span>
            new_labels.append(-<span class="hljs-number">100</span>)
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># 与前一个 tokens 类型相同的单词</span>
            label = labels[word_id]
            <span class="hljs-comment"># 如果标签是 B-XXX 我们将其更改为 I-XXX</span>
            <span class="hljs-keyword">if</span> label % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>:
                label += <span class="hljs-number">1</span>
            new_labels.append(label)

    <span class="hljs-keyword">return</span> new_labels`,wrap:!1}}),_s=new C({props:{code:"bGFiZWxzJTIwJTNEJTIwcmF3X2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUIwJTVEJTVCJTIybmVyX3RhZ3MlMjIlNUQlMEF3b3JkX2lkcyUyMCUzRCUyMGlucHV0cy53b3JkX2lkcygpJTBBcHJpbnQobGFiZWxzKSUwQXByaW50KGFsaWduX2xhYmVsc193aXRoX3Rva2VucyhsYWJlbHMlMkMlMjB3b3JkX2lkcykp",highlighted:`labels = raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&quot;ner_tags&quot;</span>]
word_ids = inputs.word_ids()
<span class="hljs-built_in">print</span>(labels)
<span class="hljs-built_in">print</span>(align_labels_with_tokens(labels, word_ids))`,wrap:!1}}),gs=new C({props:{code:"JTVCMyUyQyUyMDAlMkMlMjA3JTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwNyUyQyUyMDAlMkMlMjAwJTVEJTBBJTVCLTEwMCUyQyUyMDMlMkMlMjAwJTJDJTIwNyUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDclMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAtMTAwJTVE",highlighted:`[<span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]
[-<span class="hljs-number">100</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">100</span>]`,wrap:!1}}),ts=new ps({props:{$$slots:{default:[ua]},$$scope:{ctx:k}}}),As=new C({props:{code:"ZGVmJTIwdG9rZW5pemVfYW5kX2FsaWduX2xhYmVscyhleGFtcGxlcyklM0ElMEElMjAlMjAlMjAlMjB0b2tlbml6ZWRfaW5wdXRzJTIwJTNEJTIwdG9rZW5pemVyKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGV4YW1wbGVzJTVCJTIydG9rZW5zJTIyJTVEJTJDJTIwdHJ1bmNhdGlvbiUzRFRydWUlMkMlMjBpc19zcGxpdF9pbnRvX3dvcmRzJTNEVHJ1ZSUwQSUyMCUyMCUyMCUyMCklMEElMjAlMjAlMjAlMjBhbGxfbGFiZWxzJTIwJTNEJTIwZXhhbXBsZXMlNUIlMjJuZXJfdGFncyUyMiU1RCUwQSUyMCUyMCUyMCUyMG5ld19sYWJlbHMlMjAlM0QlMjAlNUIlNUQlMEElMjAlMjAlMjAlMjBmb3IlMjBpJTJDJTIwbGFiZWxzJTIwaW4lMjBlbnVtZXJhdGUoYWxsX2xhYmVscyklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB3b3JkX2lkcyUyMCUzRCUyMHRva2VuaXplZF9pbnB1dHMud29yZF9pZHMoaSklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBuZXdfbGFiZWxzLmFwcGVuZChhbGlnbl9sYWJlbHNfd2l0aF90b2tlbnMobGFiZWxzJTJDJTIwd29yZF9pZHMpKSUwQSUwQSUyMCUyMCUyMCUyMHRva2VuaXplZF9pbnB1dHMlNUIlMjJsYWJlbHMlMjIlNUQlMjAlM0QlMjBuZXdfbGFiZWxzJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwdG9rZW5pemVkX2lucHV0cw==",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_and_align_labels</span>(<span class="hljs-params">examples</span>):
    tokenized_inputs = tokenizer(
        examples[<span class="hljs-string">&quot;tokens&quot;</span>], truncation=<span class="hljs-literal">True</span>, is_split_into_words=<span class="hljs-literal">True</span>
    )
    all_labels = examples[<span class="hljs-string">&quot;ner_tags&quot;</span>]
    new_labels = []
    <span class="hljs-keyword">for</span> i, labels <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(all_labels):
        word_ids = tokenized_inputs.word_ids(i)
        new_labels.append(align_labels_with_tokens(labels, word_ids))

    tokenized_inputs[<span class="hljs-string">&quot;labels&quot;</span>] = new_labels
    <span class="hljs-keyword">return</span> tokenized_inputs`,wrap:!1}}),Rs=new C({props:{code:"dG9rZW5pemVkX2RhdGFzZXRzJTIwJTNEJTIwcmF3X2RhdGFzZXRzLm1hcCglMEElMjAlMjAlMjAlMjB0b2tlbml6ZV9hbmRfYWxpZ25fbGFiZWxzJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hlZCUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjByZW1vdmVfY29sdW1ucyUzRHJhd19kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVELmNvbHVtbl9uYW1lcyUyQyUwQSk=",highlighted:`tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(
    tokenize_and_align_labels,
    batched=<span class="hljs-literal">True</span>,
    remove_columns=raw_datasets[<span class="hljs-string">&quot;train&quot;</span>].column_names,
)`,wrap:!1}});const zn=[ha,fa],rt=[];function Sn(l,n){return l[0]==="pt"?0:1}oe=Sn(k),Ue=rt[oe]=zn[oe](k),Qs=new yl({props:{title:"整理数据",local:"整理数据",headingTag:"h3"}});const Ln=[Ia,Ca],ct=[];function Dn(l,n){return l[0]==="pt"?0:1}je=Dn(k),me=ct[je]=Ln[je](k),Xs=new C({props:{code:"YmF0Y2glMjAlM0QlMjBkYXRhX2NvbGxhdG9yKCU1QnRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVEJTVCaSU1RCUyMGZvciUyMGklMjBpbiUyMHJhbmdlKDIpJTVEKSUwQWJhdGNoJTVCJTIybGFiZWxzJTIyJTVE",highlighted:`batch = data_collator([tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">2</span>)])
batch[<span class="hljs-string">&quot;labels&quot;</span>]`,wrap:!1}}),Ns=new C({props:{code:"dGVuc29yKCU1QiU1Qi0xMDAlMkMlMjAlMjAlMjAlMjAzJTJDJTIwJTIwJTIwJTIwMCUyQyUyMCUyMCUyMCUyMDclMkMlMjAlMjAlMjAlMjAwJTJDJTIwJTIwJTIwJTIwMCUyQyUyMCUyMCUyMCUyMDAlMkMlMjAlMjAlMjAlMjA3JTJDJTIwJTIwJTIwJTIwMCUyQyUyMCUyMCUyMCUyMDAlMkMlMjAlMjAlMjAlMjAwJTJDJTIwLTEwMCU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU1Qi0xMDAlMkMlMjAlMjAlMjAlMjAxJTJDJTIwJTIwJTIwJTIwMiUyQyUyMC0xMDAlMkMlMjAtMTAwJTJDJTIwLTEwMCUyQyUyMC0xMDAlMkMlMjAtMTAwJTJDJTIwLTEwMCUyQyUyMC0xMDAlMkMlMjAtMTAwJTJDJTIwLTEwMCU1RCU1RCk=",highlighted:`tensor([[-<span class="hljs-number">100</span>,    <span class="hljs-number">3</span>,    <span class="hljs-number">0</span>,    <span class="hljs-number">7</span>,    <span class="hljs-number">0</span>,    <span class="hljs-number">0</span>,    <span class="hljs-number">0</span>,    <span class="hljs-number">7</span>,    <span class="hljs-number">0</span>,    <span class="hljs-number">0</span>,    <span class="hljs-number">0</span>, -<span class="hljs-number">100</span>],
        [-<span class="hljs-number">100</span>,    <span class="hljs-number">1</span>,    <span class="hljs-number">2</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>]])`,wrap:!1}}),Fs=new C({props:{code:"Zm9yJTIwaSUyMGluJTIwcmFuZ2UoMiklM0ElMEElMjAlMjAlMjAlMjBwcmludCh0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QmklNUQlNUIlMjJsYWJlbHMlMjIlNUQp",highlighted:`<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-built_in">print</span>(tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>][i][<span class="hljs-string">&quot;labels&quot;</span>])`,wrap:!1}}),Hs=new C({props:{code:"JTVCLTEwMCUyQyUyMDMlMkMlMjAwJTJDJTIwNyUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDclMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAtMTAwJTVEJTBBJTVCLTEwMCUyQyUyMDElMkMlMjAyJTJDJTIwLTEwMCU1RA==",highlighted:`[-<span class="hljs-number">100</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">100</span>]
[-<span class="hljs-number">100</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, -<span class="hljs-number">100</span>]`,wrap:!1}});const qn=[ka,$a],Jt=[];function On(l,n){return l[0]==="pt"?0:1}we=On(k),de=Jt[we]=qn[we](k);let O=k[0]==="tf"&&pa(k);zs=new yl({props:{title:"评估指标",local:"评估指标",headingTag:"h3"}});const Pn=[Za,xa],yt=[];function Kn(l,n){return l[0]==="pt"?0:1}be=Kn(k),ue=yt[be]=Pn[be](k),Ss=new C({props:{code:"aW1wb3J0JTIwZXZhbHVhdGUlMEElMEFtZXRyaWMlMjAlM0QlMjBldmFsdWF0ZS5sb2FkKCUyMnNlcWV2YWwlMjIp",highlighted:`<span class="hljs-keyword">import</span> evaluate

metric = evaluate.load(<span class="hljs-string">&quot;seqeval&quot;</span>)`,wrap:!1}}),qs=new C({props:{code:"bGFiZWxzJTIwJTNEJTIwcmF3X2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUIwJTVEJTVCJTIybmVyX3RhZ3MlMjIlNUQlMEFsYWJlbHMlMjAlM0QlMjAlNUJsYWJlbF9uYW1lcyU1QmklNUQlMjBmb3IlMjBpJTIwaW4lMjBsYWJlbHMlNUQlMEFsYWJlbHM=",highlighted:`labels = raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&quot;ner_tags&quot;</span>]
labels = [label_names[i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> labels]
labels`,wrap:!1}}),Os=new C({props:{code:"JTVCJ0ItT1JHJyUyQyUyMCdPJyUyQyUyMCdCLU1JU0MnJTJDJTIwJ08nJTJDJTIwJ08nJTJDJTIwJ08nJTJDJTIwJ0ItTUlTQyclMkMlMjAnTyclMkMlMjAnTyclNUQ=",highlighted:'[<span class="hljs-string">&#x27;B-ORG&#x27;</span>, <span class="hljs-string">&#x27;O&#x27;</span>, <span class="hljs-string">&#x27;B-MISC&#x27;</span>, <span class="hljs-string">&#x27;O&#x27;</span>, <span class="hljs-string">&#x27;O&#x27;</span>, <span class="hljs-string">&#x27;O&#x27;</span>, <span class="hljs-string">&#x27;B-MISC&#x27;</span>, <span class="hljs-string">&#x27;O&#x27;</span>, <span class="hljs-string">&#x27;O&#x27;</span>]',wrap:!1}}),Ks=new C({props:{code:"cHJlZGljdGlvbnMlMjAlM0QlMjBsYWJlbHMuY29weSgpJTBBcHJlZGljdGlvbnMlNUIyJTVEJTIwJTNEJTIwJTIyTyUyMiUwQW1ldHJpYy5jb21wdXRlKHByZWRpY3Rpb25zJTNEJTVCcHJlZGljdGlvbnMlNUQlMkMlMjByZWZlcmVuY2VzJTNEJTVCbGFiZWxzJTVEKQ==",highlighted:`predictions = labels.copy()
predictions[<span class="hljs-number">2</span>] = <span class="hljs-string">&quot;O&quot;</span>
metric.compute(predictions=[predictions], references=[labels])`,wrap:!1}}),et=new C({props:{code:"JTdCJ01JU0MnJTNBJTIwJTdCJ3ByZWNpc2lvbiclM0ElMjAxLjAlMkMlMjAncmVjYWxsJyUzQSUyMDAuNSUyQyUyMCdmMSclM0ElMjAwLjY3JTJDJTIwJ251bWJlciclM0ElMjAyJTdEJTJDJTBBJTIwJ09SRyclM0ElMjAlN0IncHJlY2lzaW9uJyUzQSUyMDEuMCUyQyUyMCdyZWNhbGwnJTNBJTIwMS4wJTJDJTIwJ2YxJyUzQSUyMDEuMCUyQyUyMCdudW1iZXInJTNBJTIwMSU3RCUyQyUwQSUyMCdvdmVyYWxsX3ByZWNpc2lvbiclM0ElMjAxLjAlMkMlMEElMjAnb3ZlcmFsbF9yZWNhbGwnJTNBJTIwMC42NyUyQyUwQSUyMCdvdmVyYWxsX2YxJyUzQSUyMDAuOCUyQyUwQSUyMCdvdmVyYWxsX2FjY3VyYWN5JyUzQSUyMDAuODklN0Q=",highlighted:`{<span class="hljs-string">&#x27;MISC&#x27;</span>: {<span class="hljs-string">&#x27;precision&#x27;</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">&#x27;recall&#x27;</span>: <span class="hljs-number">0.5</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">0.67</span>, <span class="hljs-string">&#x27;number&#x27;</span>: <span class="hljs-number">2</span>},
 <span class="hljs-string">&#x27;ORG&#x27;</span>: {<span class="hljs-string">&#x27;precision&#x27;</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">&#x27;recall&#x27;</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">&#x27;number&#x27;</span>: <span class="hljs-number">1</span>},
 <span class="hljs-string">&#x27;overall_precision&#x27;</span>: <span class="hljs-number">1.0</span>,
 <span class="hljs-string">&#x27;overall_recall&#x27;</span>: <span class="hljs-number">0.67</span>,
 <span class="hljs-string">&#x27;overall_f1&#x27;</span>: <span class="hljs-number">0.8</span>,
 <span class="hljs-string">&#x27;overall_accuracy&#x27;</span>: <span class="hljs-number">0.89</span>}`,wrap:!1}});const la=[Ba,Aa],Tt=[];function ea(l,n){return l[0]==="pt"?0:1}fe=ea(k),he=Tt[fe]=la[fe](k);let P=k[0]==="pt"&&Ma(k);return st=new yl({props:{title:"使用微调模型",local:"使用微调模型",headingTag:"h2"}}),nt=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBJTIzJTIwJUU1JUIwJTg2JUU2JUFEJUE0JUU2JTlCJUJGJUU2JThEJUEyJUU0JUI4JUJBJUU0JUJEJUEwJUU4JTg3JUFBJUU1JUI3JUIxJUU3JTlBJTg0JTIwY2hlY2twb2ludCUwQW1vZGVsX2NoZWNrcG9pbnQlMjAlM0QlMjAlMjJodWdnaW5nZmFjZS1jb3Vyc2UlMkZiZXJ0LWZpbmV0dW5lZC1uZXIlMjIlMEF0b2tlbl9jbGFzc2lmaWVyJTIwJTNEJTIwcGlwZWxpbmUoJTBBJTIwJTIwJTIwJTIwJTIydG9rZW4tY2xhc3NpZmljYXRpb24lMjIlMkMlMjBtb2RlbCUzRG1vZGVsX2NoZWNrcG9pbnQlMkMlMjBhZ2dyZWdhdGlvbl9zdHJhdGVneSUzRCUyMnNpbXBsZSUyMiUwQSklMEF0b2tlbl9jbGFzc2lmaWVyKCUyMk15JTIwbmFtZSUyMGlzJTIwU3lsdmFpbiUyMGFuZCUyMEklMjB3b3JrJTIwYXQlMjBIdWdnaW5nJTIwRmFjZSUyMGluJTIwQnJvb2tseW4uJTIyKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

<span class="hljs-comment"># 将此替换为你自己的 checkpoint</span>
model_checkpoint = <span class="hljs-string">&quot;huggingface-course/bert-finetuned-ner&quot;</span>
token_classifier = pipeline(
    <span class="hljs-string">&quot;token-classification&quot;</span>, model=model_checkpoint, aggregation_strategy=<span class="hljs-string">&quot;simple&quot;</span>
)
token_classifier(<span class="hljs-string">&quot;My name is Sylvain and I work at Hugging Face in Brooklyn.&quot;</span>)`,wrap:!1}}),at=new C({props:{code:"JTVCJTdCJ2VudGl0eV9ncm91cCclM0ElMjAnUEVSJyUyQyUyMCdzY29yZSclM0ElMjAwLjk5ODg1MDYlMkMlMjAnd29yZCclM0ElMjAnU3lsdmFpbiclMkMlMjAnc3RhcnQnJTNBJTIwMTElMkMlMjAnZW5kJyUzQSUyMDE4JTdEJTJDJTBBJTIwJTdCJ2VudGl0eV9ncm91cCclM0ElMjAnT1JHJyUyQyUyMCdzY29yZSclM0ElMjAwLjk2NDc2MjUlMkMlMjAnd29yZCclM0ElMjAnSHVnZ2luZyUyMEZhY2UnJTJDJTIwJ3N0YXJ0JyUzQSUyMDMzJTJDJTIwJ2VuZCclM0ElMjA0NSU3RCUyQyUwQSUyMCU3QidlbnRpdHlfZ3JvdXAnJTNBJTIwJ0xPQyclMkMlMjAnc2NvcmUnJTNBJTIwMC45OTg2MTE4JTJDJTIwJ3dvcmQnJTNBJTIwJ0Jyb29rbHluJyUyQyUyMCdzdGFydCclM0ElMjA0OSUyQyUyMCdlbmQnJTNBJTIwNTclN0QlNUQ=",highlighted:`[{<span class="hljs-string">&#x27;entity_group&#x27;</span>: <span class="hljs-string">&#x27;PER&#x27;</span>, <span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.9988506</span>, <span class="hljs-string">&#x27;word&#x27;</span>: <span class="hljs-string">&#x27;Sylvain&#x27;</span>, <span class="hljs-string">&#x27;start&#x27;</span>: <span class="hljs-number">11</span>, <span class="hljs-string">&#x27;end&#x27;</span>: <span class="hljs-number">18</span>},
 {<span class="hljs-string">&#x27;entity_group&#x27;</span>: <span class="hljs-string">&#x27;ORG&#x27;</span>, <span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.9647625</span>, <span class="hljs-string">&#x27;word&#x27;</span>: <span class="hljs-string">&#x27;Hugging Face&#x27;</span>, <span class="hljs-string">&#x27;start&#x27;</span>: <span class="hljs-number">33</span>, <span class="hljs-string">&#x27;end&#x27;</span>: <span class="hljs-number">45</span>},
 {<span class="hljs-string">&#x27;entity_group&#x27;</span>: <span class="hljs-string">&#x27;LOC&#x27;</span>, <span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.9986118</span>, <span class="hljs-string">&#x27;word&#x27;</span>: <span class="hljs-string">&#x27;Brooklyn&#x27;</span>, <span class="hljs-string">&#x27;start&#x27;</span>: <span class="hljs-number">49</span>, <span class="hljs-string">&#x27;end&#x27;</span>: <span class="hljs-number">57</span>}]`,wrap:!1}}),Mt=new ja({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter7/2.mdx"}}),{c(){M=T("meta"),u=a(),i=T("p"),f=a(),j(_.$$.fragment),h=a(),j(x.$$.fragment),Z=a(),g.c(),B=a(),A=T("p"),A.textContent=G,$=a(),W=T("ul"),E=T("li"),E.innerHTML=es,Rl=a(),X=T("li"),X.innerHTML=vl,D=a(),Q=T("li"),N=T("strong"),N.textContent=El,q=wn("：找出属于同一实体的 tokens 这个任务（可以与词性标注或命名实体识别结合）可以被描述为将位于块开头的 token 赋予一个标签（通常是 “ "),il=T("code"),il.textContent=K,ve=wn(" ” （Begin），代表该token位于实体的开头），将位于块内的 tokens 赋予另一个标签（通常是 “ "),rl=T("code"),rl.textContent=ll,Gl=wn(" ”（inner）代表该token位于实体的内部），将不属于任何块的 tokens 赋予第三个标签（通常是 “ "),Y=T("code"),Y.textContent=ss,Ql=wn(" ” （outer）代表该token不属于任何实体）。"),j(V.$$.fragment),Xl=a(),F=T("p"),F.textContent=Vl,Zl=a(),R=T("iframe"),nl=a(),H=T("a"),H.innerHTML=Nl,z=a(),Tl=T("p"),Tl.innerHTML=ol,Ul=a(),j(jl.$$.fragment),al=a(),el=T("p"),el.innerHTML=Yl,S=a(),j(cl.$$.fragment),pl=a(),j(sl.$$.fragment),Al=a(),v=T("p"),v.innerHTML=$e,Ml=a(),j(tl.$$.fragment),Fl=a(),L=T("p"),L.innerHTML=r,I=a(),j(ml.$$.fragment),Ee=a(),j(wl.$$.fragment),Ms=a(),Hl=T("p"),Hl.innerHTML=ke,Xe=a(),dl=T("p"),dl.textContent=ut,Ne=a(),j(bl.$$.fragment),Ye=a(),j(ul.$$.fragment),is=a(),zl=T("p"),zl.textContent=_e,Fe=a(),j(fl.$$.fragment),rs=a(),j(Sl.$$.fragment),Ll=a(),Dl=T("p"),Dl.innerHTML=ge,cs=a(),j(ql.$$.fragment),Ol=a(),j(Pl.$$.fragment),Kl=a(),le=T("p"),le.innerHTML=xe,Js=a(),j(ee.$$.fragment),se=a(),j(te.$$.fragment),ne=a(),Ze=T("p"),Ze.innerHTML=ys,ae=a(),pe=T("ul"),pe.innerHTML=Ae,He=a(),hl=T("p"),hl.innerHTML=ft,ze=a(),j(Cl.$$.fragment),Se=a(),j(Il.$$.fragment),Ts=a(),Me=T("p"),Me.innerHTML=Be,Le=a(),j($l.$$.fragment),os=a(),ie=T("p"),ie.innerHTML=We,De=a(),j(Jl.$$.fragment),Us=a(),j(re.$$.fragment),ce=a(),j(Re.$$.fragment),qe=a(),kl=T("p"),kl.innerHTML=ht,Oe=a(),_l=T("p"),_l.innerHTML=js,Je=a(),j(Ge.$$.fragment),Pe=a(),gl=T("p"),gl.innerHTML=ms,ye=a(),j(Qe.$$.fragment),Ke=a(),j(xl.$$.fragment),ws=a(),Te=T("p"),Te.innerHTML=e,y=a(),j(ds.$$.fragment),$t=a(),j(bs.$$.fragment),kt=a(),us=T("p"),us.innerHTML=In,_t=a(),fs=T("p"),fs.innerHTML=$n,gt=a(),j(hs.$$.fragment),xt=a(),j(Cs.$$.fragment),Zt=a(),Is=T("p"),Is.innerHTML=kn,At=a(),j($s.$$.fragment),Bt=a(),ks=T("p"),ks.textContent=_n,Wt=a(),j(_s.$$.fragment),Rt=a(),j(gs.$$.fragment),Gt=a(),xs=T("p"),xs.innerHTML=gn,Qt=a(),j(ts.$$.fragment),Vt=a(),Zs=T("p"),Zs.innerHTML=xn,vt=a(),j(As.$$.fragment),Et=a(),Bs=T("p"),Bs.innerHTML=Zn,Xt=a(),Ws=T("p"),Ws.textContent=An,Nt=a(),j(Rs.$$.fragment),Yt=a(),Gs=T("p"),Gs.innerHTML=Bn,Ft=a(),Ue.c(),ot=a(),j(Qs.$$.fragment),Ht=a(),Vs=T("p"),Vs.innerHTML=Wn,zt=a(),vs=T("p"),vs.innerHTML=Rn,St=a(),me.c(),Ut=a(),Es=T("p"),Es.textContent=Gn,Lt=a(),j(Xs.$$.fragment),Dt=a(),j(Ns.$$.fragment),qt=a(),Ys=T("p"),Ys.textContent=Qn,Ot=a(),j(Fs.$$.fragment),Pt=a(),j(Hs.$$.fragment),Kt=a(),de.c(),jt=a(),O&&O.c(),mt=a(),j(zs.$$.fragment),ln=a(),ue.c(),wt=a(),j(Ss.$$.fragment),en=a(),Ls=T("p"),Ls.textContent=Vn,sn=a(),Ds=T("p"),Ds.textContent=vn,tn=a(),j(qs.$$.fragment),nn=a(),j(Os.$$.fragment),an=a(),Ps=T("p"),Ps.textContent=En,pn=a(),j(Ks.$$.fragment),Mn=a(),lt=T("p"),lt.textContent=Xn,rn=a(),j(et.$$.fragment),cn=a(),he.c(),dt=a(),P&&P.c(),bt=a(),j(st.$$.fragment),Jn=a(),tt=T("p"),tt.innerHTML=Nn,yn=a(),j(nt.$$.fragment),Tn=a(),j(at.$$.fragment),on=a(),pt=T("p"),pt.textContent=Yn,Un=a(),j(Mt.$$.fragment),jn=a(),Ct=T("p"),this.h()},l(l){const n=oa("svelte-u9bgzb",document.head);M=o(n,"META",{name:!0,content:!0}),n.forEach(s),u=p(l),i=o(l,"P",{}),It(i).forEach(s),f=p(l),m(_.$$.fragment,l),h=p(l),m(x.$$.fragment,l),Z=p(l),g.l(l),B=p(l),A=o(l,"P",{"data-svelte-h":!0}),U(A)!=="svelte-1x4jl1h"&&(A.textContent=G),$=p(l),W=o(l,"UL",{});var ls=It(W);E=o(ls,"LI",{"data-svelte-h":!0}),U(E)!=="svelte-azi5jw"&&(E.innerHTML=es),Rl=p(ls),X=o(ls,"LI",{"data-svelte-h":!0}),U(X)!=="svelte-sic89m"&&(X.innerHTML=vl),D=p(ls),Q=o(ls,"LI",{});var Bl=It(Q);N=o(Bl,"STRONG",{"data-svelte-h":!0}),U(N)!=="svelte-w4498p"&&(N.textContent=El),q=dn(Bl,"：找出属于同一实体的 tokens 这个任务（可以与词性标注或命名实体识别结合）可以被描述为将位于块开头的 token 赋予一个标签（通常是 “ "),il=o(Bl,"CODE",{"data-svelte-h":!0}),U(il)!=="svelte-5k0o1p"&&(il.textContent=K),ve=dn(Bl," ” （Begin），代表该token位于实体的开头），将位于块内的 tokens 赋予另一个标签（通常是 “ "),rl=o(Bl,"CODE",{"data-svelte-h":!0}),U(rl)!=="svelte-tj1nqu"&&(rl.textContent=ll),Gl=dn(Bl," ”（inner）代表该token位于实体的内部），将不属于任何块的 tokens 赋予第三个标签（通常是 “ "),Y=o(Bl,"CODE",{"data-svelte-h":!0}),U(Y)!=="svelte-htpve9"&&(Y.textContent=ss),Ql=dn(Bl," ” （outer）代表该token不属于任何实体）。"),m(V.$$.fragment,Bl),Bl.forEach(s),ls.forEach(s),Xl=p(l),F=o(l,"P",{"data-svelte-h":!0}),U(F)!=="svelte-kot2ou"&&(F.textContent=Vl),Zl=p(l),R=o(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),It(R).forEach(s),nl=p(l),H=o(l,"A",{class:!0,href:!0,"data-svelte-h":!0}),U(H)!=="svelte-1n7oe9l"&&(H.innerHTML=Nl),z=p(l),Tl=o(l,"P",{"data-svelte-h":!0}),U(Tl)!=="svelte-73rgas"&&(Tl.innerHTML=ol),Ul=p(l),m(jl.$$.fragment,l),al=p(l),el=o(l,"P",{"data-svelte-h":!0}),U(el)!=="svelte-14bmnl2"&&(el.innerHTML=Yl),S=p(l),m(cl.$$.fragment,l),pl=p(l),m(sl.$$.fragment,l),Al=p(l),v=o(l,"P",{"data-svelte-h":!0}),U(v)!=="svelte-x1xwn4"&&(v.innerHTML=$e),Ml=p(l),m(tl.$$.fragment,l),Fl=p(l),L=o(l,"P",{"data-svelte-h":!0}),U(L)!=="svelte-wpw2a4"&&(L.innerHTML=r),I=p(l),m(ml.$$.fragment,l),Ee=p(l),m(wl.$$.fragment,l),Ms=p(l),Hl=o(l,"P",{"data-svelte-h":!0}),U(Hl)!=="svelte-cynlu5"&&(Hl.innerHTML=ke),Xe=p(l),dl=o(l,"P",{"data-svelte-h":!0}),U(dl)!=="svelte-1p63zux"&&(dl.textContent=ut),Ne=p(l),m(bl.$$.fragment,l),Ye=p(l),m(ul.$$.fragment,l),is=p(l),zl=o(l,"P",{"data-svelte-h":!0}),U(zl)!=="svelte-1vzzi47"&&(zl.textContent=_e),Fe=p(l),m(fl.$$.fragment,l),rs=p(l),m(Sl.$$.fragment,l),Ll=p(l),Dl=o(l,"P",{"data-svelte-h":!0}),U(Dl)!=="svelte-19md2xm"&&(Dl.innerHTML=ge),cs=p(l),m(ql.$$.fragment,l),Ol=p(l),m(Pl.$$.fragment,l),Kl=p(l),le=o(l,"P",{"data-svelte-h":!0}),U(le)!=="svelte-1slaf6a"&&(le.innerHTML=xe),Js=p(l),m(ee.$$.fragment,l),se=p(l),m(te.$$.fragment,l),ne=p(l),Ze=o(l,"P",{"data-svelte-h":!0}),U(Ze)!=="svelte-5ju4ep"&&(Ze.innerHTML=ys),ae=p(l),pe=o(l,"UL",{"data-svelte-h":!0}),U(pe)!=="svelte-2nmvxu"&&(pe.innerHTML=Ae),He=p(l),hl=o(l,"P",{"data-svelte-h":!0}),U(hl)!=="svelte-ewhqhq"&&(hl.innerHTML=ft),ze=p(l),m(Cl.$$.fragment,l),Se=p(l),m(Il.$$.fragment,l),Ts=p(l),Me=o(l,"P",{"data-svelte-h":!0}),U(Me)!=="svelte-165eqh2"&&(Me.innerHTML=Be),Le=p(l),m($l.$$.fragment,l),os=p(l),ie=o(l,"P",{"data-svelte-h":!0}),U(ie)!=="svelte-1lzo2av"&&(ie.innerHTML=We),De=p(l),m(Jl.$$.fragment,l),Us=p(l),m(re.$$.fragment,l),ce=p(l),m(Re.$$.fragment,l),qe=p(l),kl=o(l,"P",{"data-svelte-h":!0}),U(kl)!=="svelte-6xg6xf"&&(kl.innerHTML=ht),Oe=p(l),_l=o(l,"P",{"data-svelte-h":!0}),U(_l)!=="svelte-1ual7yh"&&(_l.innerHTML=js),Je=p(l),m(Ge.$$.fragment,l),Pe=p(l),gl=o(l,"P",{"data-svelte-h":!0}),U(gl)!=="svelte-5xd95a"&&(gl.innerHTML=ms),ye=p(l),m(Qe.$$.fragment,l),Ke=p(l),m(xl.$$.fragment,l),ws=p(l),Te=o(l,"P",{"data-svelte-h":!0}),U(Te)!=="svelte-d9ve9u"&&(Te.innerHTML=e),y=p(l),m(ds.$$.fragment,l),$t=p(l),m(bs.$$.fragment,l),kt=p(l),us=o(l,"P",{"data-svelte-h":!0}),U(us)!=="svelte-6sjwz5"&&(us.innerHTML=In),_t=p(l),fs=o(l,"P",{"data-svelte-h":!0}),U(fs)!=="svelte-o4se3y"&&(fs.innerHTML=$n),gt=p(l),m(hs.$$.fragment,l),xt=p(l),m(Cs.$$.fragment,l),Zt=p(l),Is=o(l,"P",{"data-svelte-h":!0}),U(Is)!=="svelte-11gqnm8"&&(Is.innerHTML=kn),At=p(l),m($s.$$.fragment,l),Bt=p(l),ks=o(l,"P",{"data-svelte-h":!0}),U(ks)!=="svelte-yre4ar"&&(ks.textContent=_n),Wt=p(l),m(_s.$$.fragment,l),Rt=p(l),m(gs.$$.fragment,l),Gt=p(l),xs=o(l,"P",{"data-svelte-h":!0}),U(xs)!=="svelte-1miyi07"&&(xs.innerHTML=gn),Qt=p(l),m(ts.$$.fragment,l),Vt=p(l),Zs=o(l,"P",{"data-svelte-h":!0}),U(Zs)!=="svelte-gg5kto"&&(Zs.innerHTML=xn),vt=p(l),m(As.$$.fragment,l),Et=p(l),Bs=o(l,"P",{"data-svelte-h":!0}),U(Bs)!=="svelte-bkp2dr"&&(Bs.innerHTML=Zn),Xt=p(l),Ws=o(l,"P",{"data-svelte-h":!0}),U(Ws)!=="svelte-5a9dvb"&&(Ws.textContent=An),Nt=p(l),m(Rs.$$.fragment,l),Yt=p(l),Gs=o(l,"P",{"data-svelte-h":!0}),U(Gs)!=="svelte-1fkixfz"&&(Gs.innerHTML=Bn),Ft=p(l),Ue.l(l),ot=p(l),m(Qs.$$.fragment,l),Ht=p(l),Vs=o(l,"P",{"data-svelte-h":!0}),U(Vs)!=="svelte-49pp0b"&&(Vs.innerHTML=Wn),zt=p(l),vs=o(l,"P",{"data-svelte-h":!0}),U(vs)!=="svelte-1m0fvib"&&(vs.innerHTML=Rn),St=p(l),me.l(l),Ut=p(l),Es=o(l,"P",{"data-svelte-h":!0}),U(Es)!=="svelte-9fwnij"&&(Es.textContent=Gn),Lt=p(l),m(Xs.$$.fragment,l),Dt=p(l),m(Ns.$$.fragment,l),qt=p(l),Ys=o(l,"P",{"data-svelte-h":!0}),U(Ys)!=="svelte-1ssx759"&&(Ys.textContent=Qn),Ot=p(l),m(Fs.$$.fragment,l),Pt=p(l),m(Hs.$$.fragment,l),Kt=p(l),de.l(l),jt=p(l),O&&O.l(l),mt=p(l),m(zs.$$.fragment,l),ln=p(l),ue.l(l),wt=p(l),m(Ss.$$.fragment,l),en=p(l),Ls=o(l,"P",{"data-svelte-h":!0}),U(Ls)!=="svelte-bwv7fa"&&(Ls.textContent=Vn),sn=p(l),Ds=o(l,"P",{"data-svelte-h":!0}),U(Ds)!=="svelte-16tivyj"&&(Ds.textContent=vn),tn=p(l),m(qs.$$.fragment,l),nn=p(l),m(Os.$$.fragment,l),an=p(l),Ps=o(l,"P",{"data-svelte-h":!0}),U(Ps)!=="svelte-16o87vn"&&(Ps.textContent=En),pn=p(l),m(Ks.$$.fragment,l),Mn=p(l),lt=o(l,"P",{"data-svelte-h":!0}),U(lt)!=="svelte-1iraram"&&(lt.textContent=Xn),rn=p(l),m(et.$$.fragment,l),cn=p(l),he.l(l),dt=p(l),P&&P.l(l),bt=p(l),m(st.$$.fragment,l),Jn=p(l),tt=o(l,"P",{"data-svelte-h":!0}),U(tt)!=="svelte-scdapb"&&(tt.innerHTML=Nn),yn=p(l),m(nt.$$.fragment,l),Tn=p(l),m(at.$$.fragment,l),on=p(l),pt=o(l,"P",{"data-svelte-h":!0}),U(pt)!=="svelte-1vu89s1"&&(pt.textContent=Yn),Un=p(l),m(Mt.$$.fragment,l),jn=p(l),Ct=o(l,"P",{}),It(Ct).forEach(s),this.h()},h(){Ce(M,"name","hf:doc:metadata"),Ce(M,"content",Va),ca(R.src,Ie="https://course-demos-bert-finetuned-ner.hf.space")||Ce(R,"src",Ie),Ce(R,"frameborder","0"),Ce(R,"height","350"),Ce(R,"title","Gradio app"),Ce(R,"class","block dark:hidden container p-0 flex-grow space-iframe"),Ce(R,"allow","accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking"),Ce(R,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),Ce(H,"class","flex justify-center"),Ce(H,"href","/huggingface-course/bert-finetuned-ner")},m(l,n){Wl(document.head,M),t(l,u,n),t(l,i,n),t(l,f,n),w(_,l,n),t(l,h,n),w(x,l,n),t(l,Z,n),it[b].m(l,n),t(l,B,n),t(l,A,n),t(l,$,n),t(l,W,n),Wl(W,E),Wl(W,Rl),Wl(W,X),Wl(W,D),Wl(W,Q),Wl(Q,N),Wl(Q,q),Wl(Q,il),Wl(Q,ve),Wl(Q,rl),Wl(Q,Gl),Wl(Q,Y),Wl(Q,Ql),w(V,Q,null),t(l,Xl,n),t(l,F,n),t(l,Zl,n),t(l,R,n),t(l,nl,n),t(l,H,n),t(l,z,n),t(l,Tl,n),t(l,Ul,n),w(jl,l,n),t(l,al,n),t(l,el,n),t(l,S,n),w(cl,l,n),t(l,pl,n),w(sl,l,n),t(l,Al,n),t(l,v,n),t(l,Ml,n),w(tl,l,n),t(l,Fl,n),t(l,L,n),t(l,I,n),w(ml,l,n),t(l,Ee,n),w(wl,l,n),t(l,Ms,n),t(l,Hl,n),t(l,Xe,n),t(l,dl,n),t(l,Ne,n),w(bl,l,n),t(l,Ye,n),w(ul,l,n),t(l,is,n),t(l,zl,n),t(l,Fe,n),w(fl,l,n),t(l,rs,n),w(Sl,l,n),t(l,Ll,n),t(l,Dl,n),t(l,cs,n),w(ql,l,n),t(l,Ol,n),w(Pl,l,n),t(l,Kl,n),t(l,le,n),t(l,Js,n),w(ee,l,n),t(l,se,n),w(te,l,n),t(l,ne,n),t(l,Ze,n),t(l,ae,n),t(l,pe,n),t(l,He,n),t(l,hl,n),t(l,ze,n),w(Cl,l,n),t(l,Se,n),w(Il,l,n),t(l,Ts,n),t(l,Me,n),t(l,Le,n),w($l,l,n),t(l,os,n),t(l,ie,n),t(l,De,n),w(Jl,l,n),t(l,Us,n),w(re,l,n),t(l,ce,n),w(Re,l,n),t(l,qe,n),t(l,kl,n),t(l,Oe,n),t(l,_l,n),t(l,Je,n),w(Ge,l,n),t(l,Pe,n),t(l,gl,n),t(l,ye,n),w(Qe,l,n),t(l,Ke,n),w(xl,l,n),t(l,ws,n),t(l,Te,n),t(l,y,n),w(ds,l,n),t(l,$t,n),w(bs,l,n),t(l,kt,n),t(l,us,n),t(l,_t,n),t(l,fs,n),t(l,gt,n),w(hs,l,n),t(l,xt,n),w(Cs,l,n),t(l,Zt,n),t(l,Is,n),t(l,At,n),w($s,l,n),t(l,Bt,n),t(l,ks,n),t(l,Wt,n),w(_s,l,n),t(l,Rt,n),w(gs,l,n),t(l,Gt,n),t(l,xs,n),t(l,Qt,n),w(ts,l,n),t(l,Vt,n),t(l,Zs,n),t(l,vt,n),w(As,l,n),t(l,Et,n),t(l,Bs,n),t(l,Xt,n),t(l,Ws,n),t(l,Nt,n),w(Rs,l,n),t(l,Yt,n),t(l,Gs,n),t(l,Ft,n),rt[oe].m(l,n),t(l,ot,n),w(Qs,l,n),t(l,Ht,n),t(l,Vs,n),t(l,zt,n),t(l,vs,n),t(l,St,n),ct[je].m(l,n),t(l,Ut,n),t(l,Es,n),t(l,Lt,n),w(Xs,l,n),t(l,Dt,n),w(Ns,l,n),t(l,qt,n),t(l,Ys,n),t(l,Ot,n),w(Fs,l,n),t(l,Pt,n),w(Hs,l,n),t(l,Kt,n),Jt[we].m(l,n),t(l,jt,n),O&&O.m(l,n),t(l,mt,n),w(zs,l,n),t(l,ln,n),yt[be].m(l,n),t(l,wt,n),w(Ss,l,n),t(l,en,n),t(l,Ls,n),t(l,sn,n),t(l,Ds,n),t(l,tn,n),w(qs,l,n),t(l,nn,n),w(Os,l,n),t(l,an,n),t(l,Ps,n),t(l,pn,n),w(Ks,l,n),t(l,Mn,n),t(l,lt,n),t(l,rn,n),w(et,l,n),t(l,cn,n),Tt[fe].m(l,n),t(l,dt,n),P&&P.m(l,n),t(l,bt,n),w(st,l,n),t(l,Jn,n),t(l,tt,n),t(l,yn,n),w(nt,l,n),t(l,Tn,n),w(at,l,n),t(l,on,n),t(l,pt,n),t(l,Un,n),w(Mt,l,n),t(l,jn,n),t(l,Ct,n),mn=!0},p(l,[n]){const ls={};n&1&&(ls.fw=l[0]),_.$set(ls);let Bl=b;b=Hn(l),b!==Bl&&(as(),J(it[Bl],1,1,()=>{it[Bl]=null}),ns(),g=it[b],g||(g=it[b]=Fn[b](l),g.c()),c(g,1),g.m(B.parentNode,B));const sa={};n&2&&(sa.$$scope={dirty:n,ctx:l}),cl.$set(sa);const ta={};n&2&&(ta.$$scope={dirty:n,ctx:l}),Jl.$set(ta);const na={};n&2&&(na.$$scope={dirty:n,ctx:l}),ts.$set(na);let bn=oe;oe=Sn(l),oe!==bn&&(as(),J(rt[bn],1,1,()=>{rt[bn]=null}),ns(),Ue=rt[oe],Ue||(Ue=rt[oe]=zn[oe](l),Ue.c()),c(Ue,1),Ue.m(ot.parentNode,ot));let un=je;je=Dn(l),je!==un&&(as(),J(ct[un],1,1,()=>{ct[un]=null}),ns(),me=ct[je],me||(me=ct[je]=Ln[je](l),me.c()),c(me,1),me.m(Ut.parentNode,Ut));let fn=we;we=On(l),we!==fn&&(as(),J(Jt[fn],1,1,()=>{Jt[fn]=null}),ns(),de=Jt[we],de||(de=Jt[we]=qn[we](l),de.c()),c(de,1),de.m(jt.parentNode,jt)),l[0]==="tf"?O?n&1&&c(O,1):(O=pa(l),O.c(),c(O,1),O.m(mt.parentNode,mt)):O&&(as(),J(O,1,1,()=>{O=null}),ns());let hn=be;be=Kn(l),be!==hn&&(as(),J(yt[hn],1,1,()=>{yt[hn]=null}),ns(),ue=yt[be],ue||(ue=yt[be]=Pn[be](l),ue.c()),c(ue,1),ue.m(wt.parentNode,wt));let Cn=fe;fe=ea(l),fe!==Cn&&(as(),J(Tt[Cn],1,1,()=>{Tt[Cn]=null}),ns(),he=Tt[fe],he||(he=Tt[fe]=la[fe](l),he.c()),c(he,1),he.m(dt.parentNode,dt)),l[0]==="pt"?P?n&1&&c(P,1):(P=Ma(l),P.c(),c(P,1),P.m(bt.parentNode,bt)):P&&(as(),J(P,1,1,()=>{P=null}),ns())},i(l){mn||(c(_.$$.fragment,l),c(x.$$.fragment,l),c(g),c(V.$$.fragment,l),c(jl.$$.fragment,l),c(cl.$$.fragment,l),c(sl.$$.fragment,l),c(tl.$$.fragment,l),c(ml.$$.fragment,l),c(wl.$$.fragment,l),c(bl.$$.fragment,l),c(ul.$$.fragment,l),c(fl.$$.fragment,l),c(Sl.$$.fragment,l),c(ql.$$.fragment,l),c(Pl.$$.fragment,l),c(ee.$$.fragment,l),c(te.$$.fragment,l),c(Cl.$$.fragment,l),c(Il.$$.fragment,l),c($l.$$.fragment,l),c(Jl.$$.fragment,l),c(re.$$.fragment,l),c(Re.$$.fragment,l),c(Ge.$$.fragment,l),c(Qe.$$.fragment,l),c(xl.$$.fragment,l),c(ds.$$.fragment,l),c(bs.$$.fragment,l),c(hs.$$.fragment,l),c(Cs.$$.fragment,l),c($s.$$.fragment,l),c(_s.$$.fragment,l),c(gs.$$.fragment,l),c(ts.$$.fragment,l),c(As.$$.fragment,l),c(Rs.$$.fragment,l),c(Ue),c(Qs.$$.fragment,l),c(me),c(Xs.$$.fragment,l),c(Ns.$$.fragment,l),c(Fs.$$.fragment,l),c(Hs.$$.fragment,l),c(de),c(O),c(zs.$$.fragment,l),c(ue),c(Ss.$$.fragment,l),c(qs.$$.fragment,l),c(Os.$$.fragment,l),c(Ks.$$.fragment,l),c(et.$$.fragment,l),c(he),c(P),c(st.$$.fragment,l),c(nt.$$.fragment,l),c(at.$$.fragment,l),c(Mt.$$.fragment,l),mn=!0)},o(l){J(_.$$.fragment,l),J(x.$$.fragment,l),J(g),J(V.$$.fragment,l),J(jl.$$.fragment,l),J(cl.$$.fragment,l),J(sl.$$.fragment,l),J(tl.$$.fragment,l),J(ml.$$.fragment,l),J(wl.$$.fragment,l),J(bl.$$.fragment,l),J(ul.$$.fragment,l),J(fl.$$.fragment,l),J(Sl.$$.fragment,l),J(ql.$$.fragment,l),J(Pl.$$.fragment,l),J(ee.$$.fragment,l),J(te.$$.fragment,l),J(Cl.$$.fragment,l),J(Il.$$.fragment,l),J($l.$$.fragment,l),J(Jl.$$.fragment,l),J(re.$$.fragment,l),J(Re.$$.fragment,l),J(Ge.$$.fragment,l),J(Qe.$$.fragment,l),J(xl.$$.fragment,l),J(ds.$$.fragment,l),J(bs.$$.fragment,l),J(hs.$$.fragment,l),J(Cs.$$.fragment,l),J($s.$$.fragment,l),J(_s.$$.fragment,l),J(gs.$$.fragment,l),J(ts.$$.fragment,l),J(As.$$.fragment,l),J(Rs.$$.fragment,l),J(Ue),J(Qs.$$.fragment,l),J(me),J(Xs.$$.fragment,l),J(Ns.$$.fragment,l),J(Fs.$$.fragment,l),J(Hs.$$.fragment,l),J(de),J(O),J(zs.$$.fragment,l),J(ue),J(Ss.$$.fragment,l),J(qs.$$.fragment,l),J(Os.$$.fragment,l),J(Ks.$$.fragment,l),J(et.$$.fragment,l),J(he),J(P),J(st.$$.fragment,l),J(nt.$$.fragment,l),J(at.$$.fragment,l),J(Mt.$$.fragment,l),mn=!1},d(l){l&&(s(u),s(i),s(f),s(h),s(Z),s(B),s(A),s($),s(W),s(Xl),s(F),s(Zl),s(R),s(nl),s(H),s(z),s(Tl),s(Ul),s(al),s(el),s(S),s(pl),s(Al),s(v),s(Ml),s(Fl),s(L),s(I),s(Ee),s(Ms),s(Hl),s(Xe),s(dl),s(Ne),s(Ye),s(is),s(zl),s(Fe),s(rs),s(Ll),s(Dl),s(cs),s(Ol),s(Kl),s(le),s(Js),s(se),s(ne),s(Ze),s(ae),s(pe),s(He),s(hl),s(ze),s(Se),s(Ts),s(Me),s(Le),s(os),s(ie),s(De),s(Us),s(ce),s(qe),s(kl),s(Oe),s(_l),s(Je),s(Pe),s(gl),s(ye),s(Ke),s(ws),s(Te),s(y),s($t),s(kt),s(us),s(_t),s(fs),s(gt),s(xt),s(Zt),s(Is),s(At),s(Bt),s(ks),s(Wt),s(Rt),s(Gt),s(xs),s(Qt),s(Vt),s(Zs),s(vt),s(Et),s(Bs),s(Xt),s(Ws),s(Nt),s(Yt),s(Gs),s(Ft),s(ot),s(Ht),s(Vs),s(zt),s(vs),s(St),s(Ut),s(Es),s(Lt),s(Dt),s(qt),s(Ys),s(Ot),s(Pt),s(Kt),s(jt),s(mt),s(ln),s(wt),s(en),s(Ls),s(sn),s(Ds),s(tn),s(nn),s(an),s(Ps),s(pn),s(Mn),s(lt),s(rn),s(cn),s(dt),s(bt),s(Jn),s(tt),s(yn),s(Tn),s(on),s(pt),s(Un),s(jn),s(Ct)),s(M),d(_,l),d(x,l),it[b].d(l),d(V),d(jl,l),d(cl,l),d(sl,l),d(tl,l),d(ml,l),d(wl,l),d(bl,l),d(ul,l),d(fl,l),d(Sl,l),d(ql,l),d(Pl,l),d(ee,l),d(te,l),d(Cl,l),d(Il,l),d($l,l),d(Jl,l),d(re,l),d(Re,l),d(Ge,l),d(Qe,l),d(xl,l),d(ds,l),d(bs,l),d(hs,l),d(Cs,l),d($s,l),d(_s,l),d(gs,l),d(ts,l),d(As,l),d(Rs,l),rt[oe].d(l),d(Qs,l),ct[je].d(l),d(Xs,l),d(Ns,l),d(Fs,l),d(Hs,l),Jt[we].d(l),O&&O.d(l),d(zs,l),yt[be].d(l),d(Ss,l),d(qs,l),d(Os,l),d(Ks,l),d(et,l),Tt[fe].d(l),P&&P.d(l),d(st,l),d(nt,l),d(at,l),d(Mt,l)}}}const Va='{"title":"Token 分类","local":"Token 分类","sections":[{"title":"准备数据","local":"准备数据","sections":[{"title":"CoNLL-2003 数据集","local":"CoNLL-2003 数据集","sections":[],"depth":3},{"title":"处理数据","local":"处理数据","sections":[],"depth":3}],"depth":2},{"title":"使用 Trainer API 微调模型","local":"使用 Trainer API 微调模型","sections":[],"depth":2},{"title":"使用 Keras 微调模型","local":"使用 Keras 微调模型","sections":[{"title":"整理数据","local":"整理数据","sections":[],"depth":3},{"title":"初始化模型","local":"初始化模型","sections":[],"depth":3},{"title":"微调模型","local":"微调模型","sections":[],"depth":3},{"title":"评估指标","local":"评估指标","sections":[],"depth":3},{"title":"定义模型","local":"定义模型","sections":[],"depth":3},{"title":"微调模型","local":"微调模型","sections":[],"depth":3}],"depth":2},{"title":"自定义训练循环","local":"自定义训练循环","sections":[{"title":"做好训练前的准备","local":"做好训练前的准备","sections":[],"depth":3},{"title":"训练循环","local":"训练循环","sections":[],"depth":3}],"depth":2},{"title":"使用微调模型","local":"使用微调模型","sections":[],"depth":2}],"depth":1}';function va(k,M,u){let i="pt";return Ja(()=>{const f=new URLSearchParams(window.location.search);u(0,i=f.get("fw")||"pt")}),[i]}class La extends ya{constructor(M){super(),Ta(this,M,va,Qa,ra,{})}}export{La as component};
