import{s as Ja,a as ya,o as Ta,n as He}from"../chunks/scheduler.cc52f4b9.js";import{S as oa,i as Ua,e as T,s as a,c as j,q as dn,h as ja,a as o,d as s,b as p,f as It,g as m,j as U,r as un,k as _e,l as _l,m as t,n as w,o as y,u as ns,t as c,p as d,v as as}from"../chunks/index.9480bb2b.js";import{T as ps}from"../chunks/Tip.f7b80743.js";import{C as ma,H as Tl,E as wa}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.009dc98d.js";import{Y as Ma}from"../chunks/Youtube.7b12d9e8.js";import{C}from"../chunks/CodeBlock.184aadb6.js";import{D as ca}from"../chunks/DocNotebookDropdown.6e3674fe.js";import{F as da}from"../chunks/FrameworkSwitchCourse.42d12053.js";function ua(k){let M,b;return M=new ca({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),b=!0},i(i){b||(c(M.$$.fragment,i),b=!0)},o(i){y(M.$$.fragment,i),b=!1},d(i){d(M,i)}}}function ba(k){let M,b;return M=new ca({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),b=!0},i(i){b||(c(M.$$.fragment,i),b=!0)},o(i){y(M.$$.fragment,i),b=!1},d(i){d(M,i)}}}function fa(k){let M,b='💡 只要你的数据集由分词文本和对应的标签组成，就能够将这里描述的数据处理过程应用到自己的数据集中。如果需要复习如何在 <code>Dataset</code> 中加载自定义数据集，请复习 <a href="/course/chapter5">第五章</a> 。';return{c(){M=T("p"),M.innerHTML=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-d627kc"&&(M.innerHTML=b)},m(i,f){t(i,M,f)},p:He,d(i){i&&s(M)}}}function ha(k){let M,b="✏️ <strong>轮到你了！</strong> 检查同一个句子的词性标注 （POS）或分块（chunking）列，查看输出的结果。";return{c(){M=T("p"),M.innerHTML=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-13f8y4m"&&(M.innerHTML=b)},m(i,f){t(i,M,f)},p:He,d(i){i&&s(M)}}}function Ca(k){let M,b="✏️ <strong>轮到你了！</strong> 有些研究人员更喜欢只为每个单只分配一个标签，对该单词其他部分的 token 分配 <code>-100</code> 标签。这是为了避免那些分解成许多子词的长单词对损失作出过多的贡献。请按照这个思路，改变之前的函数，使标签与 inputs ID 对齐。";return{c(){M=T("p"),M.innerHTML=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-j440e3"&&(M.innerHTML=b)},m(i,f){t(i,M,f)},p:He,d(i){i&&s(M)}}}function Ia(k){let M,b,i,f="使用 <code>Keras</code> 的代码将与之前非常相似；唯一的变化是数据如何整理成 <code>batch</code> 以及计算评估的分数。",_;return M=new Tl({props:{title:"使用 Keras 微调模型",local:"使用 Keras 微调模型",headingTag:"h2"}}),{c(){j(M.$$.fragment),b=a(),i=T("p"),i.innerHTML=f},l(h){m(M.$$.fragment,h),b=p(h),i=o(h,"P",{"data-svelte-h":!0}),U(i)!=="svelte-61noe8"&&(i.innerHTML=f)},m(h,g){w(M,h,g),t(h,b,g),t(h,i,g),_=!0},i(h){_||(c(M.$$.fragment,h),_=!0)},o(h){y(M.$$.fragment,h),_=!1},d(h){h&&(s(b),s(i)),d(M,h)}}}function $a(k){let M,b,i,f="使用 <code>Trainer</code> 的代码会和以前一样，唯一的变化是数据如何整理成 <code>batch</code> 以及计算评估的分数。",_;return M=new Tl({props:{title:"使用 Trainer API 微调模型",local:"使用 Trainer API 微调模型",headingTag:"h2"}}),{c(){j(M.$$.fragment),b=a(),i=T("p"),i.innerHTML=f},l(h){m(M.$$.fragment,h),b=p(h),i=o(h,"P",{"data-svelte-h":!0}),U(i)!=="svelte-1g43t55"&&(i.innerHTML=f)},m(h,g){w(M,h,g),t(h,b,g),t(h,i,g),_=!0},i(h){_||(c(M.$$.fragment,h),_=!0)},o(h){y(M.$$.fragment,h),_=!1},d(h){h&&(s(b),s(i)),d(M,h)}}}function ka(k){let M,b;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),b=!0},i(i){b||(c(M.$$.fragment,i),b=!0)},o(i){y(M.$$.fragment,i),b=!1},d(i){d(M,i)}}}function _a(k){let M,b;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),b=!0},i(i){b||(c(M.$$.fragment,i),b=!0)},o(i){y(M.$$.fragment,i),b=!1},d(i){d(M,i)}}}function ga(k){let M,b="我们的数据整理器已准备就绪！现在让我们使用 <code>to_tf_dataset()</code> 方法创建一个 <code>tf.data.Dataset</code> 。除此之外，你还可以使用 <code>model.prepare_tf_dataset()</code> 来使用更少的模板代码来创建 <code>Dataset</code>——你将在本章的其他小节中看到这样的操作。",i,f,_,h,g="下一站：模型本身。",B;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=b,i=a(),j(f.$$.fragment),_=a(),h=T("p"),h.textContent=g},l(u){M=o(u,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1b2bdhk"&&(M.innerHTML=b),i=p(u),m(f.$$.fragment,u),_=p(u),h=o(u,"P",{"data-svelte-h":!0}),U(h)!=="svelte-d0y39"&&(h.textContent=g)},m(u,Z){t(u,M,Z),t(u,i,Z),w(f,u,Z),t(u,_,Z),t(u,h,Z),B=!0},i(u){B||(c(f.$$.fragment,u),B=!0)},o(u){y(f.$$.fragment,u),B=!1},d(u){u&&(s(M),s(i),s(_),s(h)),d(f,u)}}}function xa(k){let M,b="正如我们所看到的，已经使用 <code>-100</code> 将第二组标签填充到了与第一组标签相同的长度。";return{c(){M=T("p"),M.innerHTML=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1stm0yh"&&(M.innerHTML=b)},m(i,f){t(i,M,f)},i:He,o:He,d(i){i&&s(M)}}}function ia(k){let M,b,i,f="由于我们正在研究 Token 分类问题，因此我们将使用 <code>TFAutoModelForTokenClassification</code> 类。实例化此模型时要记得传递我们标签的数量，最简单方法是将该数字传递给 <code>num_labels</code> 参数，但是如果我们想要创建一个就像我们在本节开头看到的那样的推理小部件，那么就需要用最标准的方法正确设置标签的对应关系。",_,h,g="最标准的方法是用两个字典 <code>id2label</code> 和 <code>label2id</code> 来设置它们，这两个字典包含从 ID 到标签的映射以及反向的映射：",B,u,Z,A,x="现在我们只需将这两个字典传递给 <code>TFAutoModelForTokenClassification.from_pretrained()</code> 方法，它们就会被保存在模型的配置中，然后被正确地保存和上传到 Hub：",W,$,E,Y,rl='就像我们在 <a href="/course/chapter3">第三章</a> 中实例化 <code>TFAutoModelForTokenClassification</code> 类一样 创建模型会发出一个警告，提示一些权重未被使用（来自预训练模型头部的权重）和一些其他权重被随机初始化（来自新 Token 分类头部的权重），别担心，我们马上就要训练这些权重。首先让我们确认一下我们的模型是否具有正确的标签数量：',cl,K,gl,X,Bl,S,Q,N,Wl,L,Rl="现在，我们已准备好训练模型了！不过，我们在这之前还要做两件事：登录到 Hugging Face 并设置我们的训练超参数。如果你在 notebook 上工作，有一个便捷函数可以帮助你做到这一点：",Gl,D,Jl,ll,ze="这将显示一个小部件，你可以在其中输入你的 Hugging Face 账号和密码。",yl,el,me="如果你不是在 notebook 上工作，只需在终端中输入以下代码：",F,ol,al,sl,Ql="登录后，我们可以编译我们模型所需要的所有配置。🤗 Transformers 提供了一个便捷的 <code>create_optimizer()</code> 函数，与 <code>TensorFlow</code> 内置的 <code>Adam</code> 优化器相比，它可以提供一个带有恰当的权重衰减和学习率衰减机制的 <code>AdamW</code> 优化器，不过这两个机制都将提高模型的性能：",R,xl,Cl,G,ge="请注意，我们没有给 <code>compile()</code> 提供 <code>loss</code> 参数。这是因为模型实际上可以使用默认的方式自动计算损失 —— 如果你在编译时没有提供损失的计算方法或在输入中提供真实的标签值（就像我们在数据集中所做的那样），那么模型将使用内部默认的 <code>loss</code> 计算方法进行训练，具体的计算方式取决于你选择的任务和模型类型。",pl,tl,xe="接下来，我们定义一个 <code>PushToHubCallback</code> 回调函数，以便在训练期间将模型上传到 Hub，并在拟合模型时添加该回调函数：",Ml,nl,Il,V,Ze='你可以使用 <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> 。',il,q,$l,v,Vl="请注意，在训练过程中每次保存模型时（这里是每个 epooch），它都会在后台上传到 Hub。这样，如有需要，你将能够在另一台机器上继续你的训练。",H,Zl,vl="到此阶段，你可以在模型 Hub 上使用推理小部件来测试你的模型并与你的朋友分享。你已经成功地在一个 tokens 分类任务上微调了一个模型——恭喜你！但我们的模型真的好用吗？我们应该找出一些指标来对模型进评估。",z;return M=new Tl({props:{title:"初始化模型",local:"初始化模型",headingTag:"h3"}}),u=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}}),K=new C({props:{code:"bW9kZWwuY29uZmlnLm51bV9sYWJlbHM=",highlighted:"model.config.num_labels",wrap:!1}}),X=new C({props:{code:"OQ==",highlighted:'<span class="hljs-number">9</span>',wrap:!1}}),S=new ps({props:{warning:!0,$$slots:{default:[Za]},$$scope:{ctx:k}}}),N=new Tl({props:{title:"微调模型",local:"微调模型",headingTag:"h3"}}),D=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}}),ol=new C({props:{code:"aHVnZ2luZ2ZhY2UtY2xpJTIwbG9naW4=",highlighted:"huggingface-cli login",wrap:!1}}),xl=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}}),nl=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}}),q=new ps({props:{$$slots:{default:[Aa]},$$scope:{ctx:k}}}),{c(){j(M.$$.fragment),b=a(),i=T("p"),i.innerHTML=f,_=a(),h=T("p"),h.innerHTML=g,B=a(),j(u.$$.fragment),Z=a(),A=T("p"),A.innerHTML=x,W=a(),j($.$$.fragment),E=a(),Y=T("p"),Y.innerHTML=rl,cl=a(),j(K.$$.fragment),gl=a(),j(X.$$.fragment),Bl=a(),j(S.$$.fragment),Q=a(),j(N.$$.fragment),Wl=a(),L=T("p"),L.textContent=Rl,Gl=a(),j(D.$$.fragment),Jl=a(),ll=T("p"),ll.textContent=ze,yl=a(),el=T("p"),el.textContent=me,F=a(),j(ol.$$.fragment),al=a(),sl=T("p"),sl.innerHTML=Ql,R=a(),j(xl.$$.fragment),Cl=a(),G=T("p"),G.innerHTML=ge,pl=a(),tl=T("p"),tl.innerHTML=xe,Ml=a(),j(nl.$$.fragment),Il=a(),V=T("p"),V.innerHTML=Ze,il=a(),j(q.$$.fragment),$l=a(),v=T("p"),v.textContent=Vl,H=a(),Zl=T("p"),Zl.textContent=vl},l(r){m(M.$$.fragment,r),b=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=g),B=p(r),m(u.$$.fragment,r),Z=p(r),A=o(r,"P",{"data-svelte-h":!0}),U(A)!=="svelte-s2j55p"&&(A.innerHTML=x),W=p(r),m($.$$.fragment,r),E=p(r),Y=o(r,"P",{"data-svelte-h":!0}),U(Y)!=="svelte-1xetjnr"&&(Y.innerHTML=rl),cl=p(r),m(K.$$.fragment,r),gl=p(r),m(X.$$.fragment,r),Bl=p(r),m(S.$$.fragment,r),Q=p(r),m(N.$$.fragment,r),Wl=p(r),L=o(r,"P",{"data-svelte-h":!0}),U(L)!=="svelte-hllk6x"&&(L.textContent=Rl),Gl=p(r),m(D.$$.fragment,r),Jl=p(r),ll=o(r,"P",{"data-svelte-h":!0}),U(ll)!=="svelte-1n7l0od"&&(ll.textContent=ze),yl=p(r),el=o(r,"P",{"data-svelte-h":!0}),U(el)!=="svelte-hcdc88"&&(el.textContent=me),F=p(r),m(ol.$$.fragment,r),al=p(r),sl=o(r,"P",{"data-svelte-h":!0}),U(sl)!=="svelte-p52opu"&&(sl.innerHTML=Ql),R=p(r),m(xl.$$.fragment,r),Cl=p(r),G=o(r,"P",{"data-svelte-h":!0}),U(G)!=="svelte-1q8ooz4"&&(G.innerHTML=ge),pl=p(r),tl=o(r,"P",{"data-svelte-h":!0}),U(tl)!=="svelte-i2wzac"&&(tl.innerHTML=xe),Ml=p(r),m(nl.$$.fragment,r),Il=p(r),V=o(r,"P",{"data-svelte-h":!0}),U(V)!=="svelte-1uv0ajc"&&(V.innerHTML=Ze),il=p(r),m(q.$$.fragment,r),$l=p(r),v=o(r,"P",{"data-svelte-h":!0}),U(v)!=="svelte-1bv2kv1"&&(v.textContent=Vl),H=p(r),Zl=o(r,"P",{"data-svelte-h":!0}),U(Zl)!=="svelte-1dg2htp"&&(Zl.textContent=vl)},m(r,I){w(M,r,I),t(r,b,I),t(r,i,I),t(r,_,I),t(r,h,I),t(r,B,I),w(u,r,I),t(r,Z,I),t(r,A,I),t(r,W,I),w($,r,I),t(r,E,I),t(r,Y,I),t(r,cl,I),w(K,r,I),t(r,gl,I),w(X,r,I),t(r,Bl,I),w(S,r,I),t(r,Q,I),w(N,r,I),t(r,Wl,I),t(r,L,I),t(r,Gl,I),w(D,r,I),t(r,Jl,I),t(r,ll,I),t(r,yl,I),t(r,el,I),t(r,F,I),w(ol,r,I),t(r,al,I),t(r,sl,I),t(r,R,I),w(xl,r,I),t(r,Cl,I),t(r,G,I),t(r,pl,I),t(r,tl,I),t(r,Ml,I),w(nl,r,I),t(r,Il,I),t(r,V,I),t(r,il,I),w(q,r,I),t(r,$l,I),t(r,v,I),t(r,H,I),t(r,Zl,I),z=!0},i(r){z||(c(M.$$.fragment,r),c(u.$$.fragment,r),c($.$$.fragment,r),c(K.$$.fragment,r),c(X.$$.fragment,r),c(S.$$.fragment,r),c(N.$$.fragment,r),c(D.$$.fragment,r),c(ol.$$.fragment,r),c(xl.$$.fragment,r),c(nl.$$.fragment,r),c(q.$$.fragment,r),z=!0)},o(r){y(M.$$.fragment,r),y(u.$$.fragment,r),y($.$$.fragment,r),y(K.$$.fragment,r),y(X.$$.fragment,r),y(S.$$.fragment,r),y(N.$$.fragment,r),y(D.$$.fragment,r),y(ol.$$.fragment,r),y(xl.$$.fragment,r),y(nl.$$.fragment,r),y(q.$$.fragment,r),z=!1},d(r){r&&(s(b),s(i),s(_),s(h),s(B),s(Z),s(A),s(W),s(E),s(Y),s(cl),s(gl),s(Bl),s(Q),s(Wl),s(L),s(Gl),s(Jl),s(ll),s(yl),s(el),s(F),s(al),s(sl),s(R),s(Cl),s(G),s(pl),s(tl),s(Ml),s(Il),s(V),s(il),s($l),s(v),s(H),s(Zl)),d(M,r),d(u,r),d($,r),d(K,r),d(X,r),d(S,r),d(N,r),d(D,r),d(ol,r),d(xl,r),d(nl,r),d(q,r)}}}function Za(k){let M,b="⚠️ 如果你的模型的标签数量错误，那么在后面调用 <code>model.fit()</code> 时，你会得到一个晦涩的错误。这可能会令人烦恼，所以确保你做了这个检查，确认你的标签数量是正确的。";return{c(){M=T("p"),M.innerHTML=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-17fhtwo"&&(M.innerHTML=b)},m(i,f){t(i,M,f)},p:He,d(i){i&&s(M)}}}function Aa(k){let M,b="💡 如果设置了模型保存的路径，并且在那里已经存在了一个非空的同名文件夹，那么该目录应该是 Hub 仓库克隆在本地的版本，如果不是，则会在调用 model.ﬁt() 方法时收到一个错误，并需要设置一个新的路径。";return{c(){M=T("p"),M.textContent=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1tb8y9b"&&(M.textContent=b)},m(i,f){t(i,M,f)},p:He,d(i){i&&s(M)}}}function Ba(k){let M,b='用于评估 Token 分类预测的经典框架是 <a href="https://github.com/chakki-works/seqeval" rel="nofollow"><em>seqeval</em></a> 。要使用此指标，我们首先需要安装 seqeval 库：',i,f,_,h,g='然后我们可以通过 <code>evaluate.load()</code> 函数加载它，就像我们在 <a href="/course/chapter3">第三章</a> 中所做的那样：',B;return f=new C({props:{code:"IXBpcCUyMGluc3RhbGwlMjBzZXFldmFs",highlighted:"!pip install seqeval",wrap:!1}}),{c(){M=T("p"),M.innerHTML=b,i=a(),j(f.$$.fragment),_=a(),h=T("p"),h.innerHTML=g},l(u){M=o(u,"P",{"data-svelte-h":!0}),U(M)!=="svelte-us56qu"&&(M.innerHTML=b),i=p(u),m(f.$$.fragment,u),_=p(u),h=o(u,"P",{"data-svelte-h":!0}),U(h)!=="svelte-1qq971n"&&(h.innerHTML=g)},m(u,Z){t(u,M,Z),t(u,i,Z),w(f,u,Z),t(u,_,Z),t(u,h,Z),B=!0},i(u){B||(c(f.$$.fragment,u),B=!0)},o(u){y(f.$$.fragment,u),B=!1},d(u){u&&(s(M),s(i),s(_),s(h)),d(f,u)}}}function Wa(k){let M,b="要让 <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,g,B,u,Z='然后我们可以通过 <code>evaluate.load()</code> 函数加载它，就像我们在 <a href="/course/chapter3">第三章</a> 中所做的那样：',A;return g=new C({props:{code:"IXBpcCUyMGluc3RhbGwlMjBzZXFldmFs",highlighted:"!pip install seqeval",wrap:!1}}),{c(){M=T("p"),M.innerHTML=b,i=a(),f=T("p"),f.innerHTML=_,h=a(),j(g.$$.fragment),B=a(),u=T("p"),u.innerHTML=Z},l(x){M=o(x,"P",{"data-svelte-h":!0}),U(M)!=="svelte-firjku"&&(M.innerHTML=b),i=p(x),f=o(x,"P",{"data-svelte-h":!0}),U(f)!=="svelte-us56qu"&&(f.innerHTML=_),h=p(x),m(g.$$.fragment,x),B=p(x),u=o(x,"P",{"data-svelte-h":!0}),U(u)!=="svelte-1qq971n"&&(u.innerHTML=Z)},m(x,W){t(x,M,W),t(x,i,W),t(x,f,W),t(x,h,W),w(g,x,W),t(x,B,W),t(x,u,W),A=!0},i(x){A||(c(g.$$.fragment,x),A=!0)},o(x){y(g.$$.fragment,x),A=!1},d(x){x&&(s(M),s(i),s(f),s(h),s(B),s(u)),d(g,x)}}}function Ra(k){let M,b="它返回了大量的信息！我们获得每个单独实体以及整体的准确率、召回率和 F1 分数。现在，让我们看看如果我们尝试使用真实的模型预测来计算分数。",i,f,_="在使用基于TensorFlow 的框架时，我们通常不会把预测拼接在一起，因为这样会导致序列长度不统一。这意味着我们不能直接使用 <code>model.predict()</code> 方法 —— 但这并不能阻止我们。我们可以逐批获取一些预测并在在此的过程中将它们拼接成一个大的列表，删除表示 <code>masking/padding</code> 的 <code>-100</code> tokens 然后在合并后的大列表上计算度量值：",h,g,B,u,Z,A,x="与我们的模型相比，你的模型的表现如何？如果你看到类似的数字，那么你的训练就成功了！",W;return g=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}}),u=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=b,i=a(),f=T("p"),f.innerHTML=_,h=a(),j(g.$$.fragment),B=a(),j(u.$$.fragment),Z=a(),A=T("p"),A.textContent=x},l($){M=o($,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1tgxz9r"&&(M.textContent=b),i=p($),f=o($,"P",{"data-svelte-h":!0}),U(f)!=="svelte-vq488"&&(f.innerHTML=_),h=p($),m(g.$$.fragment,$),B=p($),m(u.$$.fragment,$),Z=p($),A=o($,"P",{"data-svelte-h":!0}),U(A)!=="svelte-1by56bc"&&(A.textContent=x)},m($,E){t($,M,E),t($,i,E),t($,f,E),t($,h,E),w(g,$,E),t($,B,E),w(u,$,E),t($,Z,E),t($,A,E),W=!0},i($){W||(c(g.$$.fragment,$),c(u.$$.fragment,$),W=!0)},o($){y(g.$$.fragment,$),y(u.$$.fragment,$),W=!1},d($){$&&(s(M),s(i),s(f),s(h),s(B),s(Z),s(A)),d(g,$),d(u,$)}}}function Ga(k){let M,b="结果返回很多信息！包括每个单独实体及整体的准确率、召回率和 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,g="现在已经完成了，我们下面就可以开始定义我们的 <code>Trainer</code> 了。接下来我们只需要一个 <code>model</code> 并对其微调！",B;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=b,i=a(),j(f.$$.fragment),_=a(),h=T("p"),h.innerHTML=g},l(u){M=o(u,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1sy48jj"&&(M.innerHTML=b),i=p(u),m(f.$$.fragment,u),_=p(u),h=o(u,"P",{"data-svelte-h":!0}),U(h)!=="svelte-1f2ntp9"&&(h.innerHTML=g)},m(u,Z){t(u,M,Z),t(u,i,Z),w(f,u,Z),t(u,_,Z),t(u,h,Z),B=!0},i(u){B||(c(f.$$.fragment,u),B=!0)},o(u){y(f.$$.fragment,u),B=!1},d(u){u&&(s(M),s(i),s(_),s(h)),d(f,u)}}}function ra(k){let M,b,i,f="由于我们正在研究 Token 分类问题，因此我们将使用 <code>AutoModelForTokenClassification</code> 类。定义此模型时要记得传递我们标签的数量，最简单方法是将该数字传递给 <code>num_labels</code> 参数，但是如果我们想要一个就像我们在本节开头看到的那样的推理小部件，就需要用最标准的方法正确设置标签的对应关系。",_,h,g="最标准的方式是用两个字典 <code>id2label</code> 和 <code>label2id</code> 来设置标签，这两个字典包含从 ID 到标签的映射以及反向的映射：",B,u,Z,A,x="现在我们只需将它们传递给 <code>AutoModelForTokenClassification.from_pretrained()</code> 方法，它们就会被保存在模型的配置中，然后被正确地保存和上传到 Hub：",W,$,E,Y,rl='就像我们在 <a href="/course/chapter3">第三章</a> 中定义 <code>AutoModelForSequenceClassification</code> 类一样 创建模型会发出一个警告，提示一些权重未被使用（来自预训练头部的权重）和一些其他权重被随机初始化（来自新 Token 分类头部的权重），不过不用担心，我们马上就要训练这些权重。在这之前，让我们先确认一下我们的模型是否具有正确的标签数量：',cl,K,gl,X,Bl,S,Q,N,Wl,L,Rl="我们现在准备好训练我们的模型了！在定义 <code>Trainer</code> 之前，我们只需要做最后两件事：登录 Hugging Face 并设置我们的训练参数。如果你在 notebook 上工作，有一个方便的小工具可以帮助你：",Gl,D,Jl,ll,ze="登录后，我们就可以设置我们的 <code>TrainingArguments</code> ：",yl,el,me,F,ol='你已经对大多数内容有所了解了：我们设置了一些超参数（如学习率、训练的轮数和权重衰减），并设定 <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> 。',al,sl,Ql,R,xl="最后，我们将所有内容传递给 <code>Trainer</code> 并启动训练：",Cl,G,ge,pl,tl="请注意，在训练过程中每次保存模型时（这里是每个 epooch），它都会在后台上传到 Hub。这样，如有必要，你将能够在另一台机器上继续你的训练。",xe,Ml,nl="训练完成后，我们使用 <code>push_to_hub()</code> 上传模型的最新版本",Il,V,Ze,il,q="如果你想检查一下是否上传成功，这个命令会返回刚刚执行的提交的 URL：",$l,v,Vl,H,Zl="同时 <code>Trainer</code> 还创建并上传了一张包含所有评估结果的模型卡。到此阶段，你可以在模型 Hub 上使用推理小部件来测试你的模型并与你的朋友分享。你已经成功地在一个 tokens 分类任务上微调了一个模型——恭喜你！",vl,z,r="如果你想更深入地了解训练循环，我们现在将向你展示如何使用 🤗 Accelerate 做同样的事情。",I,Ae,Se,Ul,Ms='现在我们来看看完整的训练循环，这样你就可以轻松地定制你需要的部分。它与我们在 <a href="/course/chapter3/4">第三章</a> 中所做的内容很相似，但对评估部分有一些改动。',El,Xl,Nl,Be,is="首先我们需要为我们的数据集构建 <code>DataLoader</code> 。我们将 <code>data_collator</code> 传递给 <code>collate_fn</code> 参数并随机打乱训练集，但不打乱验证集：",Yl,We,Le,jl,rs="接下来我们重新实例化我们的模型，以确保我们不是继续之前的微调，而是重新开始从 BERT 微调预训练模型：",Fl,Hl,zl,Re,cs="然后我们需要一个优化器。我们将使用经典 <code>AdamW</code> ，它类似于 <code>Adam</code> ，但在权重衰减的方式上进行了改进：",Sl,Ll,Dl,ql,Ge="当我们拥有了所有这些对象之后，我们就可以将它们发传递给 <code>accelerator.prepare()</code> 方法：",Js,Ol,Pl,Kl,le,ee,Qe="现在我们已经将我们的 <code>train_dataloader</code> 传递给了 <code>accelerator.prepare()</code> 方法，我们还可以使用 <code>len()</code> 来计算训练步骤的数量。请记住，我们应该在准备好 <code>dataloader</code> 后再使用 <code>len()</code> ，因为改动 <code>dataloader</code> 会改变训练长度的数量。这里我们将使用一个从学习率衰减到 0 的经典线性学习率调度：",ys,se,te,ne,Ve="最后，为了将我们的模型推送到 Hub，我们需要在一个工作文件夹中创建一个 <code>Repository</code> 对象。如果你还没有登录的话，首先需要登录到 Hugging Face，然后根据模型 ID 来确定仓库名称（你可以将 <code>repo_name</code> 替换为你喜欢的名字；只需要包含你的用户名即可，你可以使用 <code>get_full_repo_name()</code> 函数的查看目前的 <code>repo_name</code> ）：",De,ml,Ts,ae,pe,ve,os="然后我们可以将该仓库克隆到本地文件夹中。如果本地已经存在同名的文件夹，这个本地文件夹必须是我们正在使用的仓库克隆在本地的版本：",Me,Ee,qe,wl,Us="我们现在可以通过调用 <code>repo.push_to_hub()</code> 方法上传保存在 <code>output_dir</code> 中的所有内容。它帮助我们在每个训练周期结束时上传中间模型。",ie,re,ce,Xe,js="我们现在准备编写完整的训练循环。为了简化其评估部分，我们定义了一个 <code>postprocess()</code> 函数，该函数会接收模型的预测和真实的标签，并将它们转换为字符串列表，也就是我们的 <code>metric</code> （评估函数）对象需要的输入格式。",Je,ye,Te,Ne,ms="然后我们可以编写训练循环。在定义一个进度条来跟踪训练的进度后，循环分为三个部分：",Al,oe,Ye="<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>",Oe,dl,ws="以下是完整的训练循环代码：",Ue,Fe,Pe,ul,ft="如果这是你第一次看到使用 🤗 Accelerate 保存模型，让我们花点时间来了解一下这个过程中的三行代码：",Ke,bl,ls,fl,ht=`第一行是不言自明的：它告诉所有的进程先等待，直到所有的进程都处于这个阶段再继续（阻塞）。这是为了确保在保存之前，我们在每个进程中都有相同的模型。
第二行代码用于获取 <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> 。`,es,hl,ds='完成这些操作后，你应该拥有一个与 <code>Trainer</code> 训练出的模型结果相当类似的模型。你可以在 <a href="https://huggingface.co/huggingface-course/bert-finetuned-ner-accelerate" rel="nofollow">huggingface-course/bert-finetuned-ner-accelerate</a> 查看我们使用这些代码训练的模型。如果你想在训练循环中测试任何调整，你可以直接通过编辑上面显示的代码来实现它们！',je;return M=new Tl({props:{title:"定义模型",local:"定义模型",headingTag:"h3"}}),u=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}}),K=new C({props:{code:"bW9kZWwuY29uZmlnLm51bV9sYWJlbHM=",highlighted:"model.config.num_labels",wrap:!1}}),X=new C({props:{code:"OQ==",highlighted:'<span class="hljs-number">9</span>',wrap:!1}}),S=new ps({props:{warning:!0,$$slots:{default:[Qa]},$$scope:{ctx:k}}}),N=new Tl({props:{title:"微调模型",local:"微调模型",headingTag:"h3"}}),D=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}}),el=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}}),sl=new ps({props:{$$slots:{default:[Va]},$$scope:{ctx:k}}}),G=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}}),V=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}}),Ae=new Tl({props:{title:"自定义训练循环",local:"自定义训练循环",headingTag:"h2"}}),Xl=new Tl({props:{title:"做好训练前的准备",local:"做好训练前的准备",headingTag:"h3"}}),We=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}}),Hl=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:[va]},$$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}}),ml=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}}),Ee=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}}),re=new Tl({props:{title:"训练循环",local:"训练循环",headingTag:"h3"}}),ye=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}}),Fe=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}}),bl=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),b=a(),i=T("p"),i.innerHTML=f,_=a(),h=T("p"),h.innerHTML=g,B=a(),j(u.$$.fragment),Z=a(),A=T("p"),A.innerHTML=x,W=a(),j($.$$.fragment),E=a(),Y=T("p"),Y.innerHTML=rl,cl=a(),j(K.$$.fragment),gl=a(),j(X.$$.fragment),Bl=a(),j(S.$$.fragment),Q=a(),j(N.$$.fragment),Wl=a(),L=T("p"),L.innerHTML=Rl,Gl=a(),j(D.$$.fragment),Jl=a(),ll=T("p"),ll.innerHTML=ze,yl=a(),j(el.$$.fragment),me=a(),F=T("p"),F.innerHTML=ol,al=a(),j(sl.$$.fragment),Ql=a(),R=T("p"),R.innerHTML=xl,Cl=a(),j(G.$$.fragment),ge=a(),pl=T("p"),pl.textContent=tl,xe=a(),Ml=T("p"),Ml.innerHTML=nl,Il=a(),j(V.$$.fragment),Ze=a(),il=T("p"),il.textContent=q,$l=a(),j(v.$$.fragment),Vl=a(),H=T("p"),H.innerHTML=Zl,vl=a(),z=T("p"),z.textContent=r,I=a(),j(Ae.$$.fragment),Se=a(),Ul=T("p"),Ul.innerHTML=Ms,El=a(),j(Xl.$$.fragment),Nl=a(),Be=T("p"),Be.innerHTML=is,Yl=a(),j(We.$$.fragment),Le=a(),jl=T("p"),jl.textContent=rs,Fl=a(),j(Hl.$$.fragment),zl=a(),Re=T("p"),Re.innerHTML=cs,Sl=a(),j(Ll.$$.fragment),Dl=a(),ql=T("p"),ql.innerHTML=Ge,Js=a(),j(Ol.$$.fragment),Pl=a(),j(Kl.$$.fragment),le=a(),ee=T("p"),ee.innerHTML=Qe,ys=a(),j(se.$$.fragment),te=a(),ne=T("p"),ne.innerHTML=Ve,De=a(),j(ml.$$.fragment),Ts=a(),j(ae.$$.fragment),pe=a(),ve=T("p"),ve.textContent=os,Me=a(),j(Ee.$$.fragment),qe=a(),wl=T("p"),wl.innerHTML=Us,ie=a(),j(re.$$.fragment),ce=a(),Xe=T("p"),Xe.innerHTML=js,Je=a(),j(ye.$$.fragment),Te=a(),Ne=T("p"),Ne.textContent=ms,Al=a(),oe=T("ul"),oe.innerHTML=Ye,Oe=a(),dl=T("p"),dl.textContent=ws,Ue=a(),j(Fe.$$.fragment),Pe=a(),ul=T("p"),ul.textContent=ft,Ke=a(),j(bl.$$.fragment),ls=a(),fl=T("p"),fl.innerHTML=ht,es=a(),hl=T("p"),hl.innerHTML=ds},l(e){m(M.$$.fragment,e),b=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=g),B=p(e),m(u.$$.fragment,e),Z=p(e),A=o(e,"P",{"data-svelte-h":!0}),U(A)!=="svelte-17s7g3y"&&(A.innerHTML=x),W=p(e),m($.$$.fragment,e),E=p(e),Y=o(e,"P",{"data-svelte-h":!0}),U(Y)!=="svelte-1hk2prm"&&(Y.innerHTML=rl),cl=p(e),m(K.$$.fragment,e),gl=p(e),m(X.$$.fragment,e),Bl=p(e),m(S.$$.fragment,e),Q=p(e),m(N.$$.fragment,e),Wl=p(e),L=o(e,"P",{"data-svelte-h":!0}),U(L)!=="svelte-t70ie3"&&(L.innerHTML=Rl),Gl=p(e),m(D.$$.fragment,e),Jl=p(e),ll=o(e,"P",{"data-svelte-h":!0}),U(ll)!=="svelte-iyv620"&&(ll.innerHTML=ze),yl=p(e),m(el.$$.fragment,e),me=p(e),F=o(e,"P",{"data-svelte-h":!0}),U(F)!=="svelte-gtn8hq"&&(F.innerHTML=ol),al=p(e),m(sl.$$.fragment,e),Ql=p(e),R=o(e,"P",{"data-svelte-h":!0}),U(R)!=="svelte-1cm2676"&&(R.innerHTML=xl),Cl=p(e),m(G.$$.fragment,e),ge=p(e),pl=o(e,"P",{"data-svelte-h":!0}),U(pl)!=="svelte-yr23n0"&&(pl.textContent=tl),xe=p(e),Ml=o(e,"P",{"data-svelte-h":!0}),U(Ml)!=="svelte-gohefw"&&(Ml.innerHTML=nl),Il=p(e),m(V.$$.fragment,e),Ze=p(e),il=o(e,"P",{"data-svelte-h":!0}),U(il)!=="svelte-1wy42j7"&&(il.textContent=q),$l=p(e),m(v.$$.fragment,e),Vl=p(e),H=o(e,"P",{"data-svelte-h":!0}),U(H)!=="svelte-ol5aum"&&(H.innerHTML=Zl),vl=p(e),z=o(e,"P",{"data-svelte-h":!0}),U(z)!=="svelte-5myn0z"&&(z.textContent=r),I=p(e),m(Ae.$$.fragment,e),Se=p(e),Ul=o(e,"P",{"data-svelte-h":!0}),U(Ul)!=="svelte-30wne3"&&(Ul.innerHTML=Ms),El=p(e),m(Xl.$$.fragment,e),Nl=p(e),Be=o(e,"P",{"data-svelte-h":!0}),U(Be)!=="svelte-rfg3xq"&&(Be.innerHTML=is),Yl=p(e),m(We.$$.fragment,e),Le=p(e),jl=o(e,"P",{"data-svelte-h":!0}),U(jl)!=="svelte-1kx3rp4"&&(jl.textContent=rs),Fl=p(e),m(Hl.$$.fragment,e),zl=p(e),Re=o(e,"P",{"data-svelte-h":!0}),U(Re)!=="svelte-cqyjsu"&&(Re.innerHTML=cs),Sl=p(e),m(Ll.$$.fragment,e),Dl=p(e),ql=o(e,"P",{"data-svelte-h":!0}),U(ql)!=="svelte-84vkgh"&&(ql.innerHTML=Ge),Js=p(e),m(Ol.$$.fragment,e),Pl=p(e),m(Kl.$$.fragment,e),le=p(e),ee=o(e,"P",{"data-svelte-h":!0}),U(ee)!=="svelte-2kz0f2"&&(ee.innerHTML=Qe),ys=p(e),m(se.$$.fragment,e),te=p(e),ne=o(e,"P",{"data-svelte-h":!0}),U(ne)!=="svelte-wdsnw7"&&(ne.innerHTML=Ve),De=p(e),m(ml.$$.fragment,e),Ts=p(e),m(ae.$$.fragment,e),pe=p(e),ve=o(e,"P",{"data-svelte-h":!0}),U(ve)!=="svelte-16xyo2m"&&(ve.textContent=os),Me=p(e),m(Ee.$$.fragment,e),qe=p(e),wl=o(e,"P",{"data-svelte-h":!0}),U(wl)!=="svelte-1fwndsw"&&(wl.innerHTML=Us),ie=p(e),m(re.$$.fragment,e),ce=p(e),Xe=o(e,"P",{"data-svelte-h":!0}),U(Xe)!=="svelte-1c8q3e6"&&(Xe.innerHTML=js),Je=p(e),m(ye.$$.fragment,e),Te=p(e),Ne=o(e,"P",{"data-svelte-h":!0}),U(Ne)!=="svelte-ja2imj"&&(Ne.textContent=ms),Al=p(e),oe=o(e,"UL",{"data-svelte-h":!0}),U(oe)!=="svelte-kwyf8d"&&(oe.innerHTML=Ye),Oe=p(e),dl=o(e,"P",{"data-svelte-h":!0}),U(dl)!=="svelte-2dz2we"&&(dl.textContent=ws),Ue=p(e),m(Fe.$$.fragment,e),Pe=p(e),ul=o(e,"P",{"data-svelte-h":!0}),U(ul)!=="svelte-rpln4e"&&(ul.textContent=ft),Ke=p(e),m(bl.$$.fragment,e),ls=p(e),fl=o(e,"P",{"data-svelte-h":!0}),U(fl)!=="svelte-sf1n9m"&&(fl.innerHTML=ht),es=p(e),hl=o(e,"P",{"data-svelte-h":!0}),U(hl)!=="svelte-esp3m8"&&(hl.innerHTML=ds)},m(e,J){w(M,e,J),t(e,b,J),t(e,i,J),t(e,_,J),t(e,h,J),t(e,B,J),w(u,e,J),t(e,Z,J),t(e,A,J),t(e,W,J),w($,e,J),t(e,E,J),t(e,Y,J),t(e,cl,J),w(K,e,J),t(e,gl,J),w(X,e,J),t(e,Bl,J),w(S,e,J),t(e,Q,J),w(N,e,J),t(e,Wl,J),t(e,L,J),t(e,Gl,J),w(D,e,J),t(e,Jl,J),t(e,ll,J),t(e,yl,J),w(el,e,J),t(e,me,J),t(e,F,J),t(e,al,J),w(sl,e,J),t(e,Ql,J),t(e,R,J),t(e,Cl,J),w(G,e,J),t(e,ge,J),t(e,pl,J),t(e,xe,J),t(e,Ml,J),t(e,Il,J),w(V,e,J),t(e,Ze,J),t(e,il,J),t(e,$l,J),w(v,e,J),t(e,Vl,J),t(e,H,J),t(e,vl,J),t(e,z,J),t(e,I,J),w(Ae,e,J),t(e,Se,J),t(e,Ul,J),t(e,El,J),w(Xl,e,J),t(e,Nl,J),t(e,Be,J),t(e,Yl,J),w(We,e,J),t(e,Le,J),t(e,jl,J),t(e,Fl,J),w(Hl,e,J),t(e,zl,J),t(e,Re,J),t(e,Sl,J),w(Ll,e,J),t(e,Dl,J),t(e,ql,J),t(e,Js,J),w(Ol,e,J),t(e,Pl,J),w(Kl,e,J),t(e,le,J),t(e,ee,J),t(e,ys,J),w(se,e,J),t(e,te,J),t(e,ne,J),t(e,De,J),w(ml,e,J),t(e,Ts,J),w(ae,e,J),t(e,pe,J),t(e,ve,J),t(e,Me,J),w(Ee,e,J),t(e,qe,J),t(e,wl,J),t(e,ie,J),w(re,e,J),t(e,ce,J),t(e,Xe,J),t(e,Je,J),w(ye,e,J),t(e,Te,J),t(e,Ne,J),t(e,Al,J),t(e,oe,J),t(e,Oe,J),t(e,dl,J),t(e,Ue,J),w(Fe,e,J),t(e,Pe,J),t(e,ul,J),t(e,Ke,J),w(bl,e,J),t(e,ls,J),t(e,fl,J),t(e,es,J),t(e,hl,J),je=!0},i(e){je||(c(M.$$.fragment,e),c(u.$$.fragment,e),c($.$$.fragment,e),c(K.$$.fragment,e),c(X.$$.fragment,e),c(S.$$.fragment,e),c(N.$$.fragment,e),c(D.$$.fragment,e),c(el.$$.fragment,e),c(sl.$$.fragment,e),c(G.$$.fragment,e),c(V.$$.fragment,e),c(v.$$.fragment,e),c(Ae.$$.fragment,e),c(Xl.$$.fragment,e),c(We.$$.fragment,e),c(Hl.$$.fragment,e),c(Ll.$$.fragment,e),c(Ol.$$.fragment,e),c(Kl.$$.fragment,e),c(se.$$.fragment,e),c(ml.$$.fragment,e),c(ae.$$.fragment,e),c(Ee.$$.fragment,e),c(re.$$.fragment,e),c(ye.$$.fragment,e),c(Fe.$$.fragment,e),c(bl.$$.fragment,e),je=!0)},o(e){y(M.$$.fragment,e),y(u.$$.fragment,e),y($.$$.fragment,e),y(K.$$.fragment,e),y(X.$$.fragment,e),y(S.$$.fragment,e),y(N.$$.fragment,e),y(D.$$.fragment,e),y(el.$$.fragment,e),y(sl.$$.fragment,e),y(G.$$.fragment,e),y(V.$$.fragment,e),y(v.$$.fragment,e),y(Ae.$$.fragment,e),y(Xl.$$.fragment,e),y(We.$$.fragment,e),y(Hl.$$.fragment,e),y(Ll.$$.fragment,e),y(Ol.$$.fragment,e),y(Kl.$$.fragment,e),y(se.$$.fragment,e),y(ml.$$.fragment,e),y(ae.$$.fragment,e),y(Ee.$$.fragment,e),y(re.$$.fragment,e),y(ye.$$.fragment,e),y(Fe.$$.fragment,e),y(bl.$$.fragment,e),je=!1},d(e){e&&(s(b),s(i),s(_),s(h),s(B),s(Z),s(A),s(W),s(E),s(Y),s(cl),s(gl),s(Bl),s(Q),s(Wl),s(L),s(Gl),s(Jl),s(ll),s(yl),s(me),s(F),s(al),s(Ql),s(R),s(Cl),s(ge),s(pl),s(xe),s(Ml),s(Il),s(Ze),s(il),s($l),s(Vl),s(H),s(vl),s(z),s(I),s(Se),s(Ul),s(El),s(Nl),s(Be),s(Yl),s(Le),s(jl),s(Fl),s(zl),s(Re),s(Sl),s(Dl),s(ql),s(Js),s(Pl),s(le),s(ee),s(ys),s(te),s(ne),s(De),s(Ts),s(pe),s(ve),s(Me),s(qe),s(wl),s(ie),s(ce),s(Xe),s(Je),s(Te),s(Ne),s(Al),s(oe),s(Oe),s(dl),s(Ue),s(Pe),s(ul),s(Ke),s(ls),s(fl),s(es),s(hl)),d(M,e),d(u,e),d($,e),d(K,e),d(X,e),d(S,e),d(N,e),d(D,e),d(el,e),d(sl,e),d(G,e),d(V,e),d(v,e),d(Ae,e),d(Xl,e),d(We,e),d(Hl,e),d(Ll,e),d(Ol,e),d(Kl,e),d(se,e),d(ml,e),d(ae,e),d(Ee,e),d(re,e),d(ye,e),d(Fe,e),d(bl,e)}}}function Qa(k){let M,b="⚠️ 如果你的模型的标签数量有错误，那么在后面调用 <code>Trainer.train()</code> 时，你会得到一个晦涩的错误（类似于“CUDA error：device-side assert triggered”）。这可能会令人烦恼，所以确保你做了这个检查，确认你的标签数量是正确。";return{c(){M=T("p"),M.innerHTML=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-dl32z"&&(M.innerHTML=b)},m(i,f){t(i,M,f)},p:He,d(i){i&&s(M)}}}function Va(k){let M,b="💡 如果你使用的输出路径已经存在一个同名的文件夹，那么它需要是你想推送到 hub 的仓库的克隆在本地的版本。如果不是，你将在声明 <code>Trainer</code> 时遇到一个错误，并需要设置一个新的路径。";return{c(){M=T("p"),M.innerHTML=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1cgl3va"&&(M.innerHTML=b)},m(i,f){t(i,M,f)},p:He,d(i){i&&s(M)}}}function va(k){let M,b='🚨 如果你正在 TPU 上训练，你需要将上面单元格开始的所有代码移动到一个专门的训练函数中。更多详情请回顾 <a href="/course/chapter3">第三章</a> 。';return{c(){M=T("p"),M.innerHTML=b},l(i){M=o(i,"P",{"data-svelte-h":!0}),U(M)!=="svelte-1iilxjx"&&(M.innerHTML=b)},m(i,f){t(i,M,f)},p:He,d(i){i&&s(M)}}}function Ea(k){let M,b,i,f,_,h,g,B,u,Z,A,x,W,$,E="我们将首先探讨的应用是 Token 分类。这个通用任务涵盖了所有可以表述为“给句子中的词或字贴上标签”的问题，例如：",Y,rl,cl,K="<strong>实体命名识别 （NER）</strong>：找出句子中的实体（如人物、地点或组织）。这可以通过为每个实体指定一个类别的标签，如果没有实体则会输出无实体的标签。",gl,X,Bl="<strong>词性标注 （POS）</strong>：将句子中的每个单词标记为对应于特定的词性（如名词、动词、形容词等）。",S,Q,N,Wl="分块（chunking）",L,Rl,Gl="B-",D,Jl,ll="I-",ze,yl,el="O",me,F,ol,al,sl="当然，还有很多其他类型的 token 分类问题；这些只是几个有代表性的例子。在本节中，我们将在 NER 任务上微调模型 （BERT），然后该模型将能够生成如下预测：",Ql,R,xl,Cl,G,ge='<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."/>',pl,tl,xe='你可以 <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 的模型，可以尝试输入一些句子测试一下模型的预测结果。',Ml,nl,Il,V,Ze='首先，我们需要一个适合 token 分类的数据集。在本节中，我们将使用 <a href="https://huggingface.co/datasets/conll2003" rel="nofollow">CoNLL-2003 数据集</a> ，该数据集来源于路透社的新闻报道。',il,q,$l,v,Vl,H,Zl="要加载 CoNLL-2003 数据集，我们可以使用🤗 Datasets 库的 <code>load_dataset()</code> 方法：",vl,z,r,I,Ae='这将下载并缓存数据集，就像和我们在 <a href="/course/chapter3">第三章</a> 加载 GLUE MRPC 数据集一样。查看 <code>raw_datasets</code> 对象可以让我们看到数据集中存在哪些列，以及训练集、验证集和测试集之间是如何划分的：',Se,Ul,Ms,El,Xl,Nl,Be="可以看到数据集包含了我们之前提到的三项任务的标签：命名实体识别（NER）、词性标注（POS）以及分块（chunking）。这个数据集与其他数据集的一个显著区别在于，输入文本的那一列并非以句子或整片的文本的形式存储，而是以单词列表的形式（最后一列被称为 <code>tokens</code> ，不过 <code>tokens</code> 列保存的还是单词，也就是说，这些预先分词的输入仍需要经过 tokenizer 进行子词分词处理）。",is,Yl,We="我们来看看训练集的第一个元素：",Le,jl,rs,Fl,Hl,zl,Re="由于我们要进行命名实体识别，让我们查看一下 NER 标签：",cs,Sl,Ll,Dl,ql,Ge,Js="这些是可以直接训练的整数类别标签，当我们想要检查数据时，直接看整数的标签不是很直观。就像在文本分类中，我们可以通过查看数据集的 <code>features</code> 属性来找到这些整数和标签名称之间的对应关系：",Ol,Pl,Kl,le,ee,Qe,ys="因此，我们得到了 <code>ClassLabels</code> 类型的序列。序列中元素的类型存储在 <code>ner_feature</code> 的 <code>feature</code> 中，我们可以通过查看该 <code>feature</code> 的 <code>names</code> 属性来访问标签名称的列表：",se,te,ne,Ve,De,ml,Ts='我们在 <a href="/course/chapter6/3">第六章</a> 深入研究 <code>token-classification</code> 管道时已经看到过这些标签 我们在这里进行一个快速的回顾：',ae,pe,ve="<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>",os,Me,Ee="接下来使用对这些标签名对数据集的 <code>ner_tags</code> 进行解码，将得到以下输出结果:",qe,wl,Us,ie,re,ce,Xe="我们还可以查看训练集中索引为 4 的数据，它是一个同时包含 <code>B-</code> 和 <code>I-</code> 标签的例子：",js,Je,ye,Te,Ne="正如我们在上面的输出中所看到的，跨越两个单词的实体，如“European Union”和“Werner Zwingmann”，数据集把第一个单词标注为了 <code>B-</code> 标签，将第二个单词标记为了 <code>I-</code> 标签。",ms,Al,oe,Ye,Oe,dl,ws,Ue,Fe='像往常一样，我们的文本需要转换为 Token ID，然后模型才能理解它们。正如我们在 <a href="/course/chapter6/">第六章</a> 所学的那样。不过与 tokens 分类任务不同的是数据集已经完成了预分词，我们在处理的过程中不需要再次分词。幸运的是，tokenizer API 可以很容易地处理这种差异；我们只需要通过一个特殊的标志告诉 tokenizer即可。',Pe,ul,ft="首先，让我们创建 <code>tokenizer</code> 对象。如前所述，我们将使用 BERT 预训练模型，因此我们将从下载并缓存关联的 <code>tokenizer</code> 开始：",Ke,bl,ls,fl,ht='你可以更换把 <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 支持：',es,hl,ds,je,e,J,$n="我们可以像往常一样使用我们的 <code>tokenizer</code> 对预先分词的输入进行 <code>tokenize</code> ，只需额外添加 <code>is_split_into_words=True</code> 参数：",$t,us,kt,bs,_t,fs,kn="如我们所见，<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> 开始和结束的位置），我们需要确保的是我们将所有的标签与正确的词对齐。",gt,hs,_n='幸运的是，由于我们使用的是快速 tokens 因此我们可以使用🤗 Tokenizers 超能力，这意味着我们可以轻松地将每个 token 映射到其相应的单词（如 <a href="/course/chapter6/3">第六章</a> 中所学）：',xt,Cs,Zt,Is,At,$s,gn="只需要一点点工作，我们就可以扩展我们的标签列表。我们将添加的使其与 <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> 在每个实体中只应该出现一次：",Bt,ks,Wt,_s,xn="让我们在数据集的第一个元素上试一试：",Rt,gs,Gt,xs,Qt,Zs,Zn="正如我们所看到的，我们的函数为开头和结尾添加了两个特殊 tokens ：<code>-100</code> ，并为切分成两个 tokens 的单词添加了一个新的 <code>0</code> 标签。",Vt,ts,vt,As,An="为了预处理我们的整个数据集，我们需要对所有输入进行 <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，所以我们也在下面的函数中添加了这个功能：",Et,Bs,Xt,Ws,Bn="注意，我们还没有填充我们的输入，我们将在稍后使用数据整理器创建 <code>batch</code> 时进行。",Nt,Rs,Wn="我们现在可以一次性使用预处理函数处理整个数据集：",Yt,Gs,Ft,Qs,Rn='我们已经完成了最困难的部分！现在数据已经经过了预处理，实际的训练过程将会与我们在 <a href="/course/chapter3">第三章</a> 所做的很相似。',Ht,we,de,Ut,Vs,zt,vs,Gn='我们不能像 <a href="/course/chapter3">第三章</a> 那样直接使用 <code>DataCollatorWithPadding</code> ，因为那样只会填充输入（inputs ID、注意掩码和 tokens 类型 ID）。除了输入部分，在这里我们还需要对标签也使用与输入完全相同的方式填充，以保证它与输入的大小相同。我们将使用 <code>-100</code> 进行填充，以便在损失计算中忽略填充的内容。',St,Es,Qn='以上的这些过程可以由 <a href="https://huggingface.co/transformers/main_classes/data_collator.html#datacollatorfortokenclassification" rel="nofollow"><code>DataCollatorForTokenClassification</code></a> 实现。它是一个带有填充功能的数据整理器，使用时只需要传入用于预处理输入的 <code>tokenizer</code> ：',Lt,ue,be,jt,Xs,Vn="为了在几个样本上测试这个数据整理器，我们可以先在训练集中的几个示例上调用它：",Dt,Ns,qt,Ys,Ot,Fs,vn="将数据整理器的结果与数据集中未经处理的第一个和第二个元素的标签进行比较。",Pt,Hs,Kt,zs,ln,fe,he,mt,wt,Ss,en,Ce,Ie,dt,Ls,sn,Ds,En="该指标和常规的评测指标有些区别：它需要字符串形式的标签列表而不是整数，所以我们需要在将它们传递给指标之前将预测值和标签由数字解码为字符串。让我们先用一些测试数据看看它是如何工作的：",tn,qs,Xn="首先，获取第一个训练样本的标签。",nn,Os,an,Ps,pn,Ks,Nn="然后我们可以通过更改索引 2 处的值来为这些标签创建假的预测值来测试该指标：",Mn,lt,rn,et,Yn="请注意，该指标的输入是预测列表（不是一个）和标签列表，输出结果如下：",cn,st,Jn,$e,ke,ut,bt,tt,yn,nt,Fn="我们已经向你展示了如何使用在模型中心微调的模型和推理小部件。在本地使用 <code>pipeline</code> 来使用它非常容易，你只需要指定正确的模型标签：",Tn,at,on,pt,Un,Mt,Hn="太棒了！我们的模型与此管道的默认模型一样有效！",jn,it,mn,Ct,wn;_=new da({props:{fw:k[0]}}),g=new ma({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),u=new Tl({props:{title:"Token 分类",local:"Token 分类",headingTag:"h1"}});const zn=[ba,ua],rt=[];function Sn(l,n){return l[0]==="pt"?0:1}A=Sn(k),x=rt[A]=zn[A](k),F=new Ma({props:{id:"wVHdVlPScxA"}}),nl=new Tl({props:{title:"准备数据",local:"准备数据",headingTag:"h2"}}),q=new ps({props:{$$slots:{default:[fa]},$$scope:{ctx:k}}}),v=new Tl({props:{title:"CoNLL-2003 数据集",local:"CoNLL-2003 数据集",headingTag:"h3"}}),z=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}}),Ul=new C({props:{code:"cmF3X2RhdGFzZXRz",highlighted:"raw_datasets",wrap:!1}}),El=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}}),jl=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}}),Fl=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}}),Sl=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}}),Dl=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}}),Pl=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}}),le=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}}),te=new C({props:{code:"bGFiZWxfbmFtZXMlMjAlM0QlMjBuZXJfZmVhdHVyZS5mZWF0dXJlLm5hbWVzJTBBbGFiZWxfbmFtZXM=",highlighted:`label_names = ner_feature.feature.names
label_names`,wrap:!1}}),Ve=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}}),wl=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}}),ie=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}}),Je=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}}),Al=new ps({props:{$$slots:{default:[ha]},$$scope:{ctx:k}}}),Ye=new Tl({props:{title:"处理数据",local:"处理数据",headingTag:"h3"}}),dl=new Ma({props:{id:"iY2AZYdZAr0"}}),bl=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}}),hl=new C({props:{code:"dG9rZW5pemVyLmlzX2Zhc3Q=",highlighted:"tokenizer.is_fast",wrap:!1}}),je=new C({props:{code:"VHJ1ZQ==",highlighted:'<span class="hljs-literal">True</span>',wrap:!1}}),us=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}}),Cs=new C({props:{code:"aW5wdXRzLndvcmRfaWRzKCk=",highlighted:"inputs.word_ids()",wrap:!1}}),Is=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}}),ks=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}}),gs=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}}),xs=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:[Ca]},$$scope:{ctx:k}}}),Bs=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}}),Gs=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 Ln=[$a,Ia],ct=[];function Dn(l,n){return l[0]==="pt"?0:1}we=Dn(k),de=ct[we]=Ln[we](k),Vs=new Tl({props:{title:"整理数据",local:"整理数据",headingTag:"h3"}});const qn=[_a,ka],Jt=[];function On(l,n){return l[0]==="pt"?0:1}ue=On(k),be=Jt[ue]=qn[ue](k),Ns=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}}),Ys=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}}),Hs=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}}),zs=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 Pn=[xa,ga],yt=[];function Kn(l,n){return l[0]==="pt"?0:1}fe=Kn(k),he=yt[fe]=Pn[fe](k);let O=k[0]==="tf"&&ia(k);Ss=new Tl({props:{title:"评估指标",local:"评估指标",headingTag:"h3"}});const la=[Wa,Ba],Tt=[];function ea(l,n){return l[0]==="pt"?0:1}Ce=ea(k),Ie=Tt[Ce]=la[Ce](k),Ls=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}}),Os=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}}),Ps=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}}),lt=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}}),st=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 sa=[Ga,Ra],ot=[];function ta(l,n){return l[0]==="pt"?0:1}$e=ta(k),ke=ot[$e]=sa[$e](k);let P=k[0]==="pt"&&ra(k);return tt=new Tl({props:{title:"使用微调模型",local:"使用微调模型",headingTag:"h2"}}),at=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}}),pt=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}}),it=new wa({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter7/2.mdx"}}),{c(){M=T("meta"),b=a(),i=T("p"),f=a(),j(_.$$.fragment),h=a(),j(g.$$.fragment),B=a(),j(u.$$.fragment),Z=a(),x.c(),W=a(),$=T("p"),$.textContent=E,Y=a(),rl=T("ul"),cl=T("li"),cl.innerHTML=K,gl=a(),X=T("li"),X.innerHTML=Bl,S=a(),Q=T("li"),N=T("strong"),N.textContent=Wl,L=dn("：找出属于同一实体的 tokens 这个任务（可以与词性标注或命名实体识别结合）可以被描述为将位于块开头的 token 赋予一个标签（通常是 “ "),Rl=T("code"),Rl.textContent=Gl,D=dn(" ” （Begin），代表该token位于实体的开头），将位于块内的 tokens 赋予另一个标签（通常是 “ "),Jl=T("code"),Jl.textContent=ll,ze=dn(" ”（inner）代表该token位于实体的内部），将不属于任何块的 tokens 赋予第三个标签（通常是 “ "),yl=T("code"),yl.textContent=el,me=dn(" ” （outer）代表该token不属于任何实体）。"),j(F.$$.fragment),ol=a(),al=T("p"),al.textContent=sl,Ql=a(),R=T("iframe"),Cl=a(),G=T("a"),G.innerHTML=ge,pl=a(),tl=T("p"),tl.innerHTML=xe,Ml=a(),j(nl.$$.fragment),Il=a(),V=T("p"),V.innerHTML=Ze,il=a(),j(q.$$.fragment),$l=a(),j(v.$$.fragment),Vl=a(),H=T("p"),H.innerHTML=Zl,vl=a(),j(z.$$.fragment),r=a(),I=T("p"),I.innerHTML=Ae,Se=a(),j(Ul.$$.fragment),Ms=a(),j(El.$$.fragment),Xl=a(),Nl=T("p"),Nl.innerHTML=Be,is=a(),Yl=T("p"),Yl.textContent=We,Le=a(),j(jl.$$.fragment),rs=a(),j(Fl.$$.fragment),Hl=a(),zl=T("p"),zl.textContent=Re,cs=a(),j(Sl.$$.fragment),Ll=a(),j(Dl.$$.fragment),ql=a(),Ge=T("p"),Ge.innerHTML=Js,Ol=a(),j(Pl.$$.fragment),Kl=a(),j(le.$$.fragment),ee=a(),Qe=T("p"),Qe.innerHTML=ys,se=a(),j(te.$$.fragment),ne=a(),j(Ve.$$.fragment),De=a(),ml=T("p"),ml.innerHTML=Ts,ae=a(),pe=T("ul"),pe.innerHTML=ve,os=a(),Me=T("p"),Me.innerHTML=Ee,qe=a(),j(wl.$$.fragment),Us=a(),j(ie.$$.fragment),re=a(),ce=T("p"),ce.innerHTML=Xe,js=a(),j(Je.$$.fragment),ye=a(),Te=T("p"),Te.innerHTML=Ne,ms=a(),j(Al.$$.fragment),oe=a(),j(Ye.$$.fragment),Oe=a(),j(dl.$$.fragment),ws=a(),Ue=T("p"),Ue.innerHTML=Fe,Pe=a(),ul=T("p"),ul.innerHTML=ft,Ke=a(),j(bl.$$.fragment),ls=a(),fl=T("p"),fl.innerHTML=ht,es=a(),j(hl.$$.fragment),ds=a(),j(je.$$.fragment),e=a(),J=T("p"),J.innerHTML=$n,$t=a(),j(us.$$.fragment),kt=a(),j(bs.$$.fragment),_t=a(),fs=T("p"),fs.innerHTML=kn,gt=a(),hs=T("p"),hs.innerHTML=_n,xt=a(),j(Cs.$$.fragment),Zt=a(),j(Is.$$.fragment),At=a(),$s=T("p"),$s.innerHTML=gn,Bt=a(),j(ks.$$.fragment),Wt=a(),_s=T("p"),_s.textContent=xn,Rt=a(),j(gs.$$.fragment),Gt=a(),j(xs.$$.fragment),Qt=a(),Zs=T("p"),Zs.innerHTML=Zn,Vt=a(),j(ts.$$.fragment),vt=a(),As=T("p"),As.innerHTML=An,Et=a(),j(Bs.$$.fragment),Xt=a(),Ws=T("p"),Ws.innerHTML=Bn,Nt=a(),Rs=T("p"),Rs.textContent=Wn,Yt=a(),j(Gs.$$.fragment),Ft=a(),Qs=T("p"),Qs.innerHTML=Rn,Ht=a(),de.c(),Ut=a(),j(Vs.$$.fragment),zt=a(),vs=T("p"),vs.innerHTML=Gn,St=a(),Es=T("p"),Es.innerHTML=Qn,Lt=a(),be.c(),jt=a(),Xs=T("p"),Xs.textContent=Vn,Dt=a(),j(Ns.$$.fragment),qt=a(),j(Ys.$$.fragment),Ot=a(),Fs=T("p"),Fs.textContent=vn,Pt=a(),j(Hs.$$.fragment),Kt=a(),j(zs.$$.fragment),ln=a(),he.c(),mt=a(),O&&O.c(),wt=a(),j(Ss.$$.fragment),en=a(),Ie.c(),dt=a(),j(Ls.$$.fragment),sn=a(),Ds=T("p"),Ds.textContent=En,tn=a(),qs=T("p"),qs.textContent=Xn,nn=a(),j(Os.$$.fragment),an=a(),j(Ps.$$.fragment),pn=a(),Ks=T("p"),Ks.textContent=Nn,Mn=a(),j(lt.$$.fragment),rn=a(),et=T("p"),et.textContent=Yn,cn=a(),j(st.$$.fragment),Jn=a(),ke.c(),ut=a(),P&&P.c(),bt=a(),j(tt.$$.fragment),yn=a(),nt=T("p"),nt.innerHTML=Fn,Tn=a(),j(at.$$.fragment),on=a(),j(pt.$$.fragment),Un=a(),Mt=T("p"),Mt.textContent=Hn,jn=a(),j(it.$$.fragment),mn=a(),Ct=T("p"),this.h()},l(l){const n=ja("svelte-u9bgzb",document.head);M=o(n,"META",{name:!0,content:!0}),n.forEach(s),b=p(l),i=o(l,"P",{}),It(i).forEach(s),f=p(l),m(_.$$.fragment,l),h=p(l),m(g.$$.fragment,l),B=p(l),m(u.$$.fragment,l),Z=p(l),x.l(l),W=p(l),$=o(l,"P",{"data-svelte-h":!0}),U($)!=="svelte-1x4jl1h"&&($.textContent=E),Y=p(l),rl=o(l,"UL",{});var ss=It(rl);cl=o(ss,"LI",{"data-svelte-h":!0}),U(cl)!=="svelte-azi5jw"&&(cl.innerHTML=K),gl=p(ss),X=o(ss,"LI",{"data-svelte-h":!0}),U(X)!=="svelte-sic89m"&&(X.innerHTML=Bl),S=p(ss),Q=o(ss,"LI",{});var kl=It(Q);N=o(kl,"STRONG",{"data-svelte-h":!0}),U(N)!=="svelte-w4498p"&&(N.textContent=Wl),L=un(kl,"：找出属于同一实体的 tokens 这个任务（可以与词性标注或命名实体识别结合）可以被描述为将位于块开头的 token 赋予一个标签（通常是 “ "),Rl=o(kl,"CODE",{"data-svelte-h":!0}),U(Rl)!=="svelte-5k0o1p"&&(Rl.textContent=Gl),D=un(kl," ” （Begin），代表该token位于实体的开头），将位于块内的 tokens 赋予另一个标签（通常是 “ "),Jl=o(kl,"CODE",{"data-svelte-h":!0}),U(Jl)!=="svelte-tj1nqu"&&(Jl.textContent=ll),ze=un(kl," ”（inner）代表该token位于实体的内部），将不属于任何块的 tokens 赋予第三个标签（通常是 “ "),yl=o(kl,"CODE",{"data-svelte-h":!0}),U(yl)!=="svelte-htpve9"&&(yl.textContent=el),me=un(kl," ” （outer）代表该token不属于任何实体）。"),m(F.$$.fragment,kl),kl.forEach(s),ss.forEach(s),ol=p(l),al=o(l,"P",{"data-svelte-h":!0}),U(al)!=="svelte-kot2ou"&&(al.textContent=sl),Ql=p(l),R=o(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),It(R).forEach(s),Cl=p(l),G=o(l,"A",{class:!0,href:!0,"data-svelte-h":!0}),U(G)!=="svelte-1n7oe9l"&&(G.innerHTML=ge),pl=p(l),tl=o(l,"P",{"data-svelte-h":!0}),U(tl)!=="svelte-73rgas"&&(tl.innerHTML=xe),Ml=p(l),m(nl.$$.fragment,l),Il=p(l),V=o(l,"P",{"data-svelte-h":!0}),U(V)!=="svelte-14bmnl2"&&(V.innerHTML=Ze),il=p(l),m(q.$$.fragment,l),$l=p(l),m(v.$$.fragment,l),Vl=p(l),H=o(l,"P",{"data-svelte-h":!0}),U(H)!=="svelte-x1xwn4"&&(H.innerHTML=Zl),vl=p(l),m(z.$$.fragment,l),r=p(l),I=o(l,"P",{"data-svelte-h":!0}),U(I)!=="svelte-wpw2a4"&&(I.innerHTML=Ae),Se=p(l),m(Ul.$$.fragment,l),Ms=p(l),m(El.$$.fragment,l),Xl=p(l),Nl=o(l,"P",{"data-svelte-h":!0}),U(Nl)!=="svelte-cynlu5"&&(Nl.innerHTML=Be),is=p(l),Yl=o(l,"P",{"data-svelte-h":!0}),U(Yl)!=="svelte-1p63zux"&&(Yl.textContent=We),Le=p(l),m(jl.$$.fragment,l),rs=p(l),m(Fl.$$.fragment,l),Hl=p(l),zl=o(l,"P",{"data-svelte-h":!0}),U(zl)!=="svelte-1vzzi47"&&(zl.textContent=Re),cs=p(l),m(Sl.$$.fragment,l),Ll=p(l),m(Dl.$$.fragment,l),ql=p(l),Ge=o(l,"P",{"data-svelte-h":!0}),U(Ge)!=="svelte-19md2xm"&&(Ge.innerHTML=Js),Ol=p(l),m(Pl.$$.fragment,l),Kl=p(l),m(le.$$.fragment,l),ee=p(l),Qe=o(l,"P",{"data-svelte-h":!0}),U(Qe)!=="svelte-1slaf6a"&&(Qe.innerHTML=ys),se=p(l),m(te.$$.fragment,l),ne=p(l),m(Ve.$$.fragment,l),De=p(l),ml=o(l,"P",{"data-svelte-h":!0}),U(ml)!=="svelte-5ju4ep"&&(ml.innerHTML=Ts),ae=p(l),pe=o(l,"UL",{"data-svelte-h":!0}),U(pe)!=="svelte-2nmvxu"&&(pe.innerHTML=ve),os=p(l),Me=o(l,"P",{"data-svelte-h":!0}),U(Me)!=="svelte-ewhqhq"&&(Me.innerHTML=Ee),qe=p(l),m(wl.$$.fragment,l),Us=p(l),m(ie.$$.fragment,l),re=p(l),ce=o(l,"P",{"data-svelte-h":!0}),U(ce)!=="svelte-165eqh2"&&(ce.innerHTML=Xe),js=p(l),m(Je.$$.fragment,l),ye=p(l),Te=o(l,"P",{"data-svelte-h":!0}),U(Te)!=="svelte-1lzo2av"&&(Te.innerHTML=Ne),ms=p(l),m(Al.$$.fragment,l),oe=p(l),m(Ye.$$.fragment,l),Oe=p(l),m(dl.$$.fragment,l),ws=p(l),Ue=o(l,"P",{"data-svelte-h":!0}),U(Ue)!=="svelte-6xg6xf"&&(Ue.innerHTML=Fe),Pe=p(l),ul=o(l,"P",{"data-svelte-h":!0}),U(ul)!=="svelte-1ual7yh"&&(ul.innerHTML=ft),Ke=p(l),m(bl.$$.fragment,l),ls=p(l),fl=o(l,"P",{"data-svelte-h":!0}),U(fl)!=="svelte-5xd95a"&&(fl.innerHTML=ht),es=p(l),m(hl.$$.fragment,l),ds=p(l),m(je.$$.fragment,l),e=p(l),J=o(l,"P",{"data-svelte-h":!0}),U(J)!=="svelte-d9ve9u"&&(J.innerHTML=$n),$t=p(l),m(us.$$.fragment,l),kt=p(l),m(bs.$$.fragment,l),_t=p(l),fs=o(l,"P",{"data-svelte-h":!0}),U(fs)!=="svelte-6sjwz5"&&(fs.innerHTML=kn),gt=p(l),hs=o(l,"P",{"data-svelte-h":!0}),U(hs)!=="svelte-o4se3y"&&(hs.innerHTML=_n),xt=p(l),m(Cs.$$.fragment,l),Zt=p(l),m(Is.$$.fragment,l),At=p(l),$s=o(l,"P",{"data-svelte-h":!0}),U($s)!=="svelte-11gqnm8"&&($s.innerHTML=gn),Bt=p(l),m(ks.$$.fragment,l),Wt=p(l),_s=o(l,"P",{"data-svelte-h":!0}),U(_s)!=="svelte-yre4ar"&&(_s.textContent=xn),Rt=p(l),m(gs.$$.fragment,l),Gt=p(l),m(xs.$$.fragment,l),Qt=p(l),Zs=o(l,"P",{"data-svelte-h":!0}),U(Zs)!=="svelte-1miyi07"&&(Zs.innerHTML=Zn),Vt=p(l),m(ts.$$.fragment,l),vt=p(l),As=o(l,"P",{"data-svelte-h":!0}),U(As)!=="svelte-gg5kto"&&(As.innerHTML=An),Et=p(l),m(Bs.$$.fragment,l),Xt=p(l),Ws=o(l,"P",{"data-svelte-h":!0}),U(Ws)!=="svelte-bkp2dr"&&(Ws.innerHTML=Bn),Nt=p(l),Rs=o(l,"P",{"data-svelte-h":!0}),U(Rs)!=="svelte-5a9dvb"&&(Rs.textContent=Wn),Yt=p(l),m(Gs.$$.fragment,l),Ft=p(l),Qs=o(l,"P",{"data-svelte-h":!0}),U(Qs)!=="svelte-1fkixfz"&&(Qs.innerHTML=Rn),Ht=p(l),de.l(l),Ut=p(l),m(Vs.$$.fragment,l),zt=p(l),vs=o(l,"P",{"data-svelte-h":!0}),U(vs)!=="svelte-49pp0b"&&(vs.innerHTML=Gn),St=p(l),Es=o(l,"P",{"data-svelte-h":!0}),U(Es)!=="svelte-1m0fvib"&&(Es.innerHTML=Qn),Lt=p(l),be.l(l),jt=p(l),Xs=o(l,"P",{"data-svelte-h":!0}),U(Xs)!=="svelte-9fwnij"&&(Xs.textContent=Vn),Dt=p(l),m(Ns.$$.fragment,l),qt=p(l),m(Ys.$$.fragment,l),Ot=p(l),Fs=o(l,"P",{"data-svelte-h":!0}),U(Fs)!=="svelte-1ssx759"&&(Fs.textContent=vn),Pt=p(l),m(Hs.$$.fragment,l),Kt=p(l),m(zs.$$.fragment,l),ln=p(l),he.l(l),mt=p(l),O&&O.l(l),wt=p(l),m(Ss.$$.fragment,l),en=p(l),Ie.l(l),dt=p(l),m(Ls.$$.fragment,l),sn=p(l),Ds=o(l,"P",{"data-svelte-h":!0}),U(Ds)!=="svelte-bwv7fa"&&(Ds.textContent=En),tn=p(l),qs=o(l,"P",{"data-svelte-h":!0}),U(qs)!=="svelte-16tivyj"&&(qs.textContent=Xn),nn=p(l),m(Os.$$.fragment,l),an=p(l),m(Ps.$$.fragment,l),pn=p(l),Ks=o(l,"P",{"data-svelte-h":!0}),U(Ks)!=="svelte-16o87vn"&&(Ks.textContent=Nn),Mn=p(l),m(lt.$$.fragment,l),rn=p(l),et=o(l,"P",{"data-svelte-h":!0}),U(et)!=="svelte-1iraram"&&(et.textContent=Yn),cn=p(l),m(st.$$.fragment,l),Jn=p(l),ke.l(l),ut=p(l),P&&P.l(l),bt=p(l),m(tt.$$.fragment,l),yn=p(l),nt=o(l,"P",{"data-svelte-h":!0}),U(nt)!=="svelte-scdapb"&&(nt.innerHTML=Fn),Tn=p(l),m(at.$$.fragment,l),on=p(l),m(pt.$$.fragment,l),Un=p(l),Mt=o(l,"P",{"data-svelte-h":!0}),U(Mt)!=="svelte-1vu89s1"&&(Mt.textContent=Hn),jn=p(l),m(it.$$.fragment,l),mn=p(l),Ct=o(l,"P",{}),It(Ct).forEach(s),this.h()},h(){_e(M,"name","hf:doc:metadata"),_e(M,"content",Xa),ya(R.src,xl="https://course-demos-bert-finetuned-ner.hf.space")||_e(R,"src",xl),_e(R,"frameborder","0"),_e(R,"height","350"),_e(R,"title","Gradio app"),_e(R,"class","block dark:hidden container p-0 flex-grow space-iframe"),_e(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"),_e(R,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),_e(G,"class","flex justify-center"),_e(G,"href","/huggingface-course/bert-finetuned-ner")},m(l,n){_l(document.head,M),t(l,b,n),t(l,i,n),t(l,f,n),w(_,l,n),t(l,h,n),w(g,l,n),t(l,B,n),w(u,l,n),t(l,Z,n),rt[A].m(l,n),t(l,W,n),t(l,$,n),t(l,Y,n),t(l,rl,n),_l(rl,cl),_l(rl,gl),_l(rl,X),_l(rl,S),_l(rl,Q),_l(Q,N),_l(Q,L),_l(Q,Rl),_l(Q,D),_l(Q,Jl),_l(Q,ze),_l(Q,yl),_l(Q,me),w(F,Q,null),t(l,ol,n),t(l,al,n),t(l,Ql,n),t(l,R,n),t(l,Cl,n),t(l,G,n),t(l,pl,n),t(l,tl,n),t(l,Ml,n),w(nl,l,n),t(l,Il,n),t(l,V,n),t(l,il,n),w(q,l,n),t(l,$l,n),w(v,l,n),t(l,Vl,n),t(l,H,n),t(l,vl,n),w(z,l,n),t(l,r,n),t(l,I,n),t(l,Se,n),w(Ul,l,n),t(l,Ms,n),w(El,l,n),t(l,Xl,n),t(l,Nl,n),t(l,is,n),t(l,Yl,n),t(l,Le,n),w(jl,l,n),t(l,rs,n),w(Fl,l,n),t(l,Hl,n),t(l,zl,n),t(l,cs,n),w(Sl,l,n),t(l,Ll,n),w(Dl,l,n),t(l,ql,n),t(l,Ge,n),t(l,Ol,n),w(Pl,l,n),t(l,Kl,n),w(le,l,n),t(l,ee,n),t(l,Qe,n),t(l,se,n),w(te,l,n),t(l,ne,n),w(Ve,l,n),t(l,De,n),t(l,ml,n),t(l,ae,n),t(l,pe,n),t(l,os,n),t(l,Me,n),t(l,qe,n),w(wl,l,n),t(l,Us,n),w(ie,l,n),t(l,re,n),t(l,ce,n),t(l,js,n),w(Je,l,n),t(l,ye,n),t(l,Te,n),t(l,ms,n),w(Al,l,n),t(l,oe,n),w(Ye,l,n),t(l,Oe,n),w(dl,l,n),t(l,ws,n),t(l,Ue,n),t(l,Pe,n),t(l,ul,n),t(l,Ke,n),w(bl,l,n),t(l,ls,n),t(l,fl,n),t(l,es,n),w(hl,l,n),t(l,ds,n),w(je,l,n),t(l,e,n),t(l,J,n),t(l,$t,n),w(us,l,n),t(l,kt,n),w(bs,l,n),t(l,_t,n),t(l,fs,n),t(l,gt,n),t(l,hs,n),t(l,xt,n),w(Cs,l,n),t(l,Zt,n),w(Is,l,n),t(l,At,n),t(l,$s,n),t(l,Bt,n),w(ks,l,n),t(l,Wt,n),t(l,_s,n),t(l,Rt,n),w(gs,l,n),t(l,Gt,n),w(xs,l,n),t(l,Qt,n),t(l,Zs,n),t(l,Vt,n),w(ts,l,n),t(l,vt,n),t(l,As,n),t(l,Et,n),w(Bs,l,n),t(l,Xt,n),t(l,Ws,n),t(l,Nt,n),t(l,Rs,n),t(l,Yt,n),w(Gs,l,n),t(l,Ft,n),t(l,Qs,n),t(l,Ht,n),ct[we].m(l,n),t(l,Ut,n),w(Vs,l,n),t(l,zt,n),t(l,vs,n),t(l,St,n),t(l,Es,n),t(l,Lt,n),Jt[ue].m(l,n),t(l,jt,n),t(l,Xs,n),t(l,Dt,n),w(Ns,l,n),t(l,qt,n),w(Ys,l,n),t(l,Ot,n),t(l,Fs,n),t(l,Pt,n),w(Hs,l,n),t(l,Kt,n),w(zs,l,n),t(l,ln,n),yt[fe].m(l,n),t(l,mt,n),O&&O.m(l,n),t(l,wt,n),w(Ss,l,n),t(l,en,n),Tt[Ce].m(l,n),t(l,dt,n),w(Ls,l,n),t(l,sn,n),t(l,Ds,n),t(l,tn,n),t(l,qs,n),t(l,nn,n),w(Os,l,n),t(l,an,n),w(Ps,l,n),t(l,pn,n),t(l,Ks,n),t(l,Mn,n),w(lt,l,n),t(l,rn,n),t(l,et,n),t(l,cn,n),w(st,l,n),t(l,Jn,n),ot[$e].m(l,n),t(l,ut,n),P&&P.m(l,n),t(l,bt,n),w(tt,l,n),t(l,yn,n),t(l,nt,n),t(l,Tn,n),w(at,l,n),t(l,on,n),w(pt,l,n),t(l,Un,n),t(l,Mt,n),t(l,jn,n),w(it,l,n),t(l,mn,n),t(l,Ct,n),wn=!0},p(l,[n]){const ss={};n&1&&(ss.fw=l[0]),_.$set(ss);let kl=A;A=Sn(l),A!==kl&&(as(),y(rt[kl],1,1,()=>{rt[kl]=null}),ns(),x=rt[A],x||(x=rt[A]=zn[A](l),x.c()),c(x,1),x.m(W.parentNode,W));const na={};n&2&&(na.$$scope={dirty:n,ctx:l}),q.$set(na);const aa={};n&2&&(aa.$$scope={dirty:n,ctx:l}),Al.$set(aa);const pa={};n&2&&(pa.$$scope={dirty:n,ctx:l}),ts.$set(pa);let bn=we;we=Dn(l),we!==bn&&(as(),y(ct[bn],1,1,()=>{ct[bn]=null}),ns(),de=ct[we],de||(de=ct[we]=Ln[we](l),de.c()),c(de,1),de.m(Ut.parentNode,Ut));let fn=ue;ue=On(l),ue!==fn&&(as(),y(Jt[fn],1,1,()=>{Jt[fn]=null}),ns(),be=Jt[ue],be||(be=Jt[ue]=qn[ue](l),be.c()),c(be,1),be.m(jt.parentNode,jt));let hn=fe;fe=Kn(l),fe!==hn&&(as(),y(yt[hn],1,1,()=>{yt[hn]=null}),ns(),he=yt[fe],he||(he=yt[fe]=Pn[fe](l),he.c()),c(he,1),he.m(mt.parentNode,mt)),l[0]==="tf"?O?n&1&&c(O,1):(O=ia(l),O.c(),c(O,1),O.m(wt.parentNode,wt)):O&&(as(),y(O,1,1,()=>{O=null}),ns());let Cn=Ce;Ce=ea(l),Ce!==Cn&&(as(),y(Tt[Cn],1,1,()=>{Tt[Cn]=null}),ns(),Ie=Tt[Ce],Ie||(Ie=Tt[Ce]=la[Ce](l),Ie.c()),c(Ie,1),Ie.m(dt.parentNode,dt));let In=$e;$e=ta(l),$e!==In&&(as(),y(ot[In],1,1,()=>{ot[In]=null}),ns(),ke=ot[$e],ke||(ke=ot[$e]=sa[$e](l),ke.c()),c(ke,1),ke.m(ut.parentNode,ut)),l[0]==="pt"?P?n&1&&c(P,1):(P=ra(l),P.c(),c(P,1),P.m(bt.parentNode,bt)):P&&(as(),y(P,1,1,()=>{P=null}),ns())},i(l){wn||(c(_.$$.fragment,l),c(g.$$.fragment,l),c(u.$$.fragment,l),c(x),c(F.$$.fragment,l),c(nl.$$.fragment,l),c(q.$$.fragment,l),c(v.$$.fragment,l),c(z.$$.fragment,l),c(Ul.$$.fragment,l),c(El.$$.fragment,l),c(jl.$$.fragment,l),c(Fl.$$.fragment,l),c(Sl.$$.fragment,l),c(Dl.$$.fragment,l),c(Pl.$$.fragment,l),c(le.$$.fragment,l),c(te.$$.fragment,l),c(Ve.$$.fragment,l),c(wl.$$.fragment,l),c(ie.$$.fragment,l),c(Je.$$.fragment,l),c(Al.$$.fragment,l),c(Ye.$$.fragment,l),c(dl.$$.fragment,l),c(bl.$$.fragment,l),c(hl.$$.fragment,l),c(je.$$.fragment,l),c(us.$$.fragment,l),c(bs.$$.fragment,l),c(Cs.$$.fragment,l),c(Is.$$.fragment,l),c(ks.$$.fragment,l),c(gs.$$.fragment,l),c(xs.$$.fragment,l),c(ts.$$.fragment,l),c(Bs.$$.fragment,l),c(Gs.$$.fragment,l),c(de),c(Vs.$$.fragment,l),c(be),c(Ns.$$.fragment,l),c(Ys.$$.fragment,l),c(Hs.$$.fragment,l),c(zs.$$.fragment,l),c(he),c(O),c(Ss.$$.fragment,l),c(Ie),c(Ls.$$.fragment,l),c(Os.$$.fragment,l),c(Ps.$$.fragment,l),c(lt.$$.fragment,l),c(st.$$.fragment,l),c(ke),c(P),c(tt.$$.fragment,l),c(at.$$.fragment,l),c(pt.$$.fragment,l),c(it.$$.fragment,l),wn=!0)},o(l){y(_.$$.fragment,l),y(g.$$.fragment,l),y(u.$$.fragment,l),y(x),y(F.$$.fragment,l),y(nl.$$.fragment,l),y(q.$$.fragment,l),y(v.$$.fragment,l),y(z.$$.fragment,l),y(Ul.$$.fragment,l),y(El.$$.fragment,l),y(jl.$$.fragment,l),y(Fl.$$.fragment,l),y(Sl.$$.fragment,l),y(Dl.$$.fragment,l),y(Pl.$$.fragment,l),y(le.$$.fragment,l),y(te.$$.fragment,l),y(Ve.$$.fragment,l),y(wl.$$.fragment,l),y(ie.$$.fragment,l),y(Je.$$.fragment,l),y(Al.$$.fragment,l),y(Ye.$$.fragment,l),y(dl.$$.fragment,l),y(bl.$$.fragment,l),y(hl.$$.fragment,l),y(je.$$.fragment,l),y(us.$$.fragment,l),y(bs.$$.fragment,l),y(Cs.$$.fragment,l),y(Is.$$.fragment,l),y(ks.$$.fragment,l),y(gs.$$.fragment,l),y(xs.$$.fragment,l),y(ts.$$.fragment,l),y(Bs.$$.fragment,l),y(Gs.$$.fragment,l),y(de),y(Vs.$$.fragment,l),y(be),y(Ns.$$.fragment,l),y(Ys.$$.fragment,l),y(Hs.$$.fragment,l),y(zs.$$.fragment,l),y(he),y(O),y(Ss.$$.fragment,l),y(Ie),y(Ls.$$.fragment,l),y(Os.$$.fragment,l),y(Ps.$$.fragment,l),y(lt.$$.fragment,l),y(st.$$.fragment,l),y(ke),y(P),y(tt.$$.fragment,l),y(at.$$.fragment,l),y(pt.$$.fragment,l),y(it.$$.fragment,l),wn=!1},d(l){l&&(s(b),s(i),s(f),s(h),s(B),s(Z),s(W),s($),s(Y),s(rl),s(ol),s(al),s(Ql),s(R),s(Cl),s(G),s(pl),s(tl),s(Ml),s(Il),s(V),s(il),s($l),s(Vl),s(H),s(vl),s(r),s(I),s(Se),s(Ms),s(Xl),s(Nl),s(is),s(Yl),s(Le),s(rs),s(Hl),s(zl),s(cs),s(Ll),s(ql),s(Ge),s(Ol),s(Kl),s(ee),s(Qe),s(se),s(ne),s(De),s(ml),s(ae),s(pe),s(os),s(Me),s(qe),s(Us),s(re),s(ce),s(js),s(ye),s(Te),s(ms),s(oe),s(Oe),s(ws),s(Ue),s(Pe),s(ul),s(Ke),s(ls),s(fl),s(es),s(ds),s(e),s(J),s($t),s(kt),s(_t),s(fs),s(gt),s(hs),s(xt),s(Zt),s(At),s($s),s(Bt),s(Wt),s(_s),s(Rt),s(Gt),s(Qt),s(Zs),s(Vt),s(vt),s(As),s(Et),s(Xt),s(Ws),s(Nt),s(Rs),s(Yt),s(Ft),s(Qs),s(Ht),s(Ut),s(zt),s(vs),s(St),s(Es),s(Lt),s(jt),s(Xs),s(Dt),s(qt),s(Ot),s(Fs),s(Pt),s(Kt),s(ln),s(mt),s(wt),s(en),s(dt),s(sn),s(Ds),s(tn),s(qs),s(nn),s(an),s(pn),s(Ks),s(Mn),s(rn),s(et),s(cn),s(Jn),s(ut),s(bt),s(yn),s(nt),s(Tn),s(on),s(Un),s(Mt),s(jn),s(mn),s(Ct)),s(M),d(_,l),d(g,l),d(u,l),rt[A].d(l),d(F),d(nl,l),d(q,l),d(v,l),d(z,l),d(Ul,l),d(El,l),d(jl,l),d(Fl,l),d(Sl,l),d(Dl,l),d(Pl,l),d(le,l),d(te,l),d(Ve,l),d(wl,l),d(ie,l),d(Je,l),d(Al,l),d(Ye,l),d(dl,l),d(bl,l),d(hl,l),d(je,l),d(us,l),d(bs,l),d(Cs,l),d(Is,l),d(ks,l),d(gs,l),d(xs,l),d(ts,l),d(Bs,l),d(Gs,l),ct[we].d(l),d(Vs,l),Jt[ue].d(l),d(Ns,l),d(Ys,l),d(Hs,l),d(zs,l),yt[fe].d(l),O&&O.d(l),d(Ss,l),Tt[Ce].d(l),d(Ls,l),d(Os,l),d(Ps,l),d(lt,l),d(st,l),ot[$e].d(l),P&&P.d(l),d(tt,l),d(at,l),d(pt,l),d(it,l)}}}const Xa='{"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 Na(k,M,b){let i="pt";return Ta(()=>{const f=new URLSearchParams(window.location.search);b(0,i=f.get("fw")||"pt")}),[i]}class Oa extends oa{constructor(M){super(),Ua(this,M,Na,Ea,Ja,{})}}export{Oa as component};
