import{s as Fa,a as Aa,o as Ya,n as Ll}from"../chunks/scheduler.cc52f4b9.js";import{S as za,i as Ha,e as y,s as n,c as T,h as Sa,a as U,d as e,b as a,f as Xn,g as m,j as u,k as Il,l as Da,m as s,n as o,o as J,u as As,t as c,p as j,v as Ws}from"../chunks/index.9480bb2b.js";import{T as te}from"../chunks/Tip.f7b80743.js";import{C as qa,H as gl,E as La}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.009dc98d.js";import{Y as xn}from"../chunks/Youtube.7b12d9e8.js";import{C as h}from"../chunks/CodeBlock.184aadb6.js";import{D as va}from"../chunks/DocNotebookDropdown.6e3674fe.js";import{F as Pa}from"../chunks/FrameworkSwitchCourse.42d12053.js";function Oa(x){let p,b;return p=new va({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/section4_tf.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter7/section4_tf.ipynb"}]}}),{c(){T(p.$$.fragment)},l(r){m(p.$$.fragment,r)},m(r,w){o(p,r,w),b=!0},i(r){b||(c(p.$$.fragment,r),b=!0)},o(r){J(p.$$.fragment,r),b=!1},d(r){j(p,r)}}}function Ka(x){let p,b;return p=new va({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/section4_pt.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter7/section4_pt.ipynb"}]}}),{c(){T(p.$$.fragment)},l(r){m(p.$$.fragment,r)},m(r,w){o(p,r,w),b=!0},i(r){b||(c(p.$$.fragment,r),b=!0)},o(r){J(p.$$.fragment,r),b=!1},d(r){j(p,r)}}}function lp(x){let p,b="✏️ <strong>轮到你了！</strong> 另一个在法语中经常使用的英语单词是“email”。在训练数据集中找到使用这个词的第一个样本。在数据集中它是如何翻译的？预训练模型如何翻译同一个英文句子？";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-l4ry1q"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},p:Ll,d(r){r&&e(p)}}}function ep(x){let p,b="💡 如果你在使用一个多语言的 tokenizer，比如 mBART，mBART-50，或者 M2M100，你需要通过设置 <code>tokenizer.src_lang</code> 和 <code>tokenizer.tgt_lang</code> 来在 tokenizer 中指定输入和目标的语言代码。";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-bd9swy"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},p:Ll,d(r){r&&e(p)}}}function sp(x){let p,b="💡 如果你正在使用 T5 模型（更具体地说，一个 <code>t5-xxx</code> checkpoint ），模型会期望文本输入有一个前缀指示目前的任务，比如 <code>translate: English to French:</code> 。";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-so9xz0"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},p:Ll,d(r){r&&e(p)}}}function tp(x){let p,b="⚠️ 我们不需要对待遇测的目标设置注意力掩码，因为模型序列到序列的不会需要它。不过，我们应该将填充（padding） token 对应的标签设置为 <code>-100</code> ，以便在 loss 计算中忽略它们。由于我们正在使用动态填充，这将在稍后由我们的数据整理器完成，但是如果你在此处就打算进行填充，你应该调整预处理函数，将所有填充（padding） token 对应的标签设置为 <code>-100</code> 。";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-1orli4b"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},p:Ll,d(r){r&&e(p)}}}function np(x){let p,b,r,w="首先，我们需要一个模型来进行微调。我们将使用常用的 <code>AutoModel</code> API：",g,f,Z,V,_;return p=new gl({props:{title:"使用 Keras 微调模型",local:"使用 Keras 微调模型",headingTag:"h2"}}),f=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRGQXV0b01vZGVsRm9yU2VxMlNlcUxNJTBBJTBBbW9kZWwlMjAlM0QlMjBURkF1dG9Nb2RlbEZvclNlcTJTZXFMTS5mcm9tX3ByZXRyYWluZWQobW9kZWxfY2hlY2twb2ludCUyQyUyMGZyb21fcHQlM0RUcnVlKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSeq2SeqLM

model = TFAutoModelForSeq2SeqLM.from_pretrained(model_checkpoint, from_pt=<span class="hljs-literal">True</span>)`,wrap:!1}}),V=new te({props:{warning:!1,$$slots:{default:[pp]},$$scope:{ctx:x}}}),{c(){T(p.$$.fragment),b=n(),r=y("p"),r.innerHTML=w,g=n(),T(f.$$.fragment),Z=n(),T(V.$$.fragment)},l(k){m(p.$$.fragment,k),b=a(k),r=U(k,"P",{"data-svelte-h":!0}),u(r)!=="svelte-itbeyk"&&(r.innerHTML=w),g=a(k),m(f.$$.fragment,k),Z=a(k),m(V.$$.fragment,k)},m(k,C){o(p,k,C),s(k,b,C),s(k,r,C),s(k,g,C),o(f,k,C),s(k,Z,C),o(V,k,C),_=!0},i(k){_||(c(p.$$.fragment,k),c(f.$$.fragment,k),c(V.$$.fragment,k),_=!0)},o(k){J(p.$$.fragment,k),J(f.$$.fragment,k),J(V.$$.fragment,k),_=!1},d(k){k&&(e(b),e(r),e(g),e(Z)),j(p,k),j(f,k),j(V,k)}}}function ap(x){let p,b,r,w='使用 <code>Trainer</code> 的代码将与以前相同，只是稍作改动：我们在这里将使用 <a href="https://huggingface.co/transformers/main_classes/trainer.html#seq2seqtrainer" rel="nofollow"><code>Seq2SeqTrainer</code></a> ，它是 <code>Trainer</code> 的子类，它使用 <code>generate()</code> 方法来预测输入的输出结果，并且可以正确处理这种序列到序列的评估。当我们讨论评估指标时，我们将更详细地探讨这一点。',g,f,Z="首先，我们需要一个模型来进行微调。我们将使用常用的 <code>AutoModel</code> API：",V,_,k;return p=new gl({props:{title:"使用 Trainer API 微调模型",local:"使用 Trainer API 微调模型",headingTag:"h2"}}),_=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvclNlcTJTZXFMTSUwQSUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yU2VxMlNlcUxNLmZyb21fcHJldHJhaW5lZChtb2RlbF9jaGVja3BvaW50KQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSeq2SeqLM

model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)`,wrap:!1}}),{c(){T(p.$$.fragment),b=n(),r=y("p"),r.innerHTML=w,g=n(),f=y("p"),f.innerHTML=Z,V=n(),T(_.$$.fragment)},l(C){m(p.$$.fragment,C),b=a(C),r=U(C,"P",{"data-svelte-h":!0}),u(r)!=="svelte-1v395ca"&&(r.innerHTML=w),g=a(C),f=U(C,"P",{"data-svelte-h":!0}),u(f)!=="svelte-itbeyk"&&(f.innerHTML=Z),V=a(C),m(_.$$.fragment,C)},m(C,R){o(p,C,R),s(C,b,R),s(C,r,R),s(C,g,R),s(C,f,R),s(C,V,R),o(_,C,R),k=!0},i(C){k||(c(p.$$.fragment,C),c(_.$$.fragment,C),k=!0)},o(C){J(p.$$.fragment,C),J(_.$$.fragment,C),k=!1},d(C){C&&(e(b),e(r),e(g),e(f),e(V)),j(p,C),j(_,C)}}}function pp(x){let p,b="💡 <code>Helsinki-NLP/opus-mt-en-fr</code> checkpoint 只有 PyTorch 的权重，所以如果你尝试使用 <code>from_pretrained()</code> 方法加载模型并且忘记设置<code>from_pt=True</code> 参数的时候，你会得到一个错误。当你设定 <code>from_pt=True</code> 时，🤗transormer 会自动下载并为你转换 PyTorch 权重。如你所见，使用🤗transormer 在两种框架之间切换非常简单。";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-101ggn6"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},p:Ll,d(r){r&&e(p)}}}function Mp(x){let p,b;return p=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvckZvclNlcTJTZXElMEElMEFkYXRhX2NvbGxhdG9yJTIwJTNEJTIwRGF0YUNvbGxhdG9yRm9yU2VxMlNlcSh0b2tlbml6ZXIlMkMlMjBtb2RlbCUzRG1vZGVsJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJ0ZiUyMik=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForSeq2Seq

data_collator = DataCollatorForSeq2Seq(tokenizer, model=model, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`,wrap:!1}}),{c(){T(p.$$.fragment)},l(r){m(p.$$.fragment,r)},m(r,w){o(p,r,w),b=!0},i(r){b||(c(p.$$.fragment,r),b=!0)},o(r){J(p.$$.fragment,r),b=!1},d(r){j(p,r)}}}function rp(x){let p,b;return p=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvckZvclNlcTJTZXElMEElMEFkYXRhX2NvbGxhdG9yJTIwJTNEJTIwRGF0YUNvbGxhdG9yRm9yU2VxMlNlcSh0b2tlbml6ZXIlMkMlMjBtb2RlbCUzRG1vZGVsKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForSeq2Seq

data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)`,wrap:!1}}),{c(){T(p.$$.fragment)},l(r){m(p.$$.fragment,r)},m(r,w){o(p,r,w),b=!0},i(r){b||(c(p.$$.fragment,r),b=!0)},o(r){J(p.$$.fragment,r),b=!1},d(r){j(p,r)}}}function ip(x){let p,b="我们现在可以使用 <code>data_collator</code> 将我们的每个数据集转换为 <code>tf.data.Dataset</code> ，这样就完成了数据整理：",r,w,g;return w=new h({props:{code:"dGZfdHJhaW5fZGF0YXNldCUyMCUzRCUyMG1vZGVsLnByZXBhcmVfdGZfZGF0YXNldCglMEElMjAlMjAlMjAlMjB0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMGNvbGxhdGVfZm4lM0RkYXRhX2NvbGxhdG9yJTJDJTBBJTIwJTIwJTIwJTIwc2h1ZmZsZSUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjBiYXRjaF9zaXplJTNEMzIlMkMlMEEpJTBBdGZfZXZhbF9kYXRhc2V0JTIwJTNEJTIwbW9kZWwucHJlcGFyZV90Zl9kYXRhc2V0KCUwQSUyMCUyMCUyMCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnZhbGlkYXRpb24lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBjb2xsYXRlX2ZuJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMHNodWZmbGUlM0RGYWxzZSUyQyUwQSUyMCUyMCUyMCUyMGJhdGNoX3NpemUlM0QxNiUyQyUwQSk=",highlighted:`tf_train_dataset = model.prepare_tf_dataset(
    tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">True</span>,
    batch_size=<span class="hljs-number">32</span>,
)
tf_eval_dataset = model.prepare_tf_dataset(
    tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>],
    collate_fn=data_collator,
    shuffle=<span class="hljs-literal">False</span>,
    batch_size=<span class="hljs-number">16</span>,
)`,wrap:!1}}),{c(){p=y("p"),p.innerHTML=b,r=n(),T(w.$$.fragment)},l(f){p=U(f,"P",{"data-svelte-h":!0}),u(p)!=="svelte-11wa54c"&&(p.innerHTML=b),r=a(f),m(w.$$.fragment,f)},m(f,Z){s(f,p,Z),s(f,r,Z),o(w,f,Z),g=!0},i(f){g||(c(w.$$.fragment,f),g=!0)},o(f){J(w.$$.fragment,f),g=!1},d(f){f&&(e(p),e(r)),j(w,f)}}}function cp(x){let p,b="把 <code>data_collator</code> 传递给 <code>Seq2SeqTrainer</code> 后就完成了数据整理。接下来，让我们看一下评估指标。";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-bytd29"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},i:Ll,o:Ll,d(r){r&&e(p)}}}function Wa(x){let p,b="<code>Seq2SeqTrainer</code> 是 <code>Trainer</code> 类的一个子类，它的主要增强特性是在评估或预测时使用 <code>generate()</code> 方法。在训练过程中，模型会利用 <code>decoder_input_ids</code> 和一个特殊的注意力掩码来加速训练。这种方法允许模型在预测下一个token时看到部分目标序列，但确保它不会使用预测token之后的信息。这种优化策略显著提高了训练效率。然而，在实际的推理过程中，我们没有真实的标签值，因此无法生成 <code>decoder_input_ids</code> 和相应的注意力掩码。这意味着我们无法在推理时使用这种训练时的优化方法。",r,w,g='为了确保评估结果能够准确反映模型在实际使用中的表现，我们应该在评估阶段模拟真实推理的条件。这意味着我们需要使用在 <a href="/course/chapter1/6">第一章</a> 中介绍的 🤗 Transformers 库中的 <code>generate()</code> 方法。该方法能够逐个生成token，真实地模拟推理过程，而不是依赖于训练时的优化技巧。要启用这个功能，我们需要在训练时添加 <code>predict_with_generate=True</code> 参数。这样做可以确保我们的评估结果更加接近模型在实际应用中的表现。';return{c(){p=y("p"),p.innerHTML=b,r=n(),w=y("p"),w.innerHTML=g},l(f){p=U(f,"P",{"data-svelte-h":!0}),u(p)!=="svelte-1y7422"&&(p.innerHTML=b),r=a(f),w=U(f,"P",{"data-svelte-h":!0}),u(w)!=="svelte-5149qw"&&(w.innerHTML=g)},m(f,Z){s(f,p,Z),s(f,r,Z),s(f,w,Z)},d(f){f&&(e(p),e(r),e(w))}}}function Jp(x){let p,b="为了将模型的输出转化为评估指标可以使用的文本，我们将使用 <code>tokenizer.batch_decode()</code> 方法。因为 tokenizer 会自动处理填充的 tokens，所以我们只需要清理标签中的所有 <code>-100</code> token：",r,w,g;return w=new h({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBJTBBZGVmJTIwY29tcHV0ZV9tZXRyaWNzKGV2YWxfcHJlZHMpJTNBJTBBJTIwJTIwJTIwJTIwcHJlZHMlMkMlMjBsYWJlbHMlMjAlM0QlMjBldmFsX3ByZWRzJTBBJTIwJTIwJTIwJTIwJTIzJTIwJUU1JUE2JTgyJUU2JTlFJTlDJUU2JUE4JUExJUU1JTlFJThCJUU4JUJGJTk0JUU1JTlCJTlFJUU3JTlBJTg0JUU1JTg2JTg1JUU1JUFFJUI5JUU4JUI2JTg1JUU4JUJGJTg3JUU0JUJBJTg2JUU5JUEyJTg0JUU2JUI1JThCJUU3JTlBJTg0bG9naXRzJTBBJTIwJTIwJTIwJTIwaWYlMjBpc2luc3RhbmNlKHByZWRzJTJDJTIwdHVwbGUpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJlZHMlMjAlM0QlMjBwcmVkcyU1QjAlNUQlMEElMEElMjAlMjAlMjAlMjBkZWNvZGVkX3ByZWRzJTIwJTNEJTIwdG9rZW5pemVyLmJhdGNoX2RlY29kZShwcmVkcyUyQyUyMHNraXBfc3BlY2lhbF90b2tlbnMlM0RUcnVlKSUwQSUwQSUyMCUyMCUyMCUyMCUyMyUyMCVFNyU5NCVCMSVFNCVCQSU4RSVFNiU4OCU5MSVFNCVCQiVBQyVFNiU5NyVBMCVFNiVCMyU5NSVFOCVBNyVBMyVFNyVBMCU4MSUyMC0xMDAlMkMlRTUlOUIlQTAlRTYlQUQlQTQlRTUlQjAlODYlRTYlQTAlODclRTclQUQlQkUlRTQlQjglQUQlRTclOUElODQlMjAtMTAwJTIwJUU2JTlCJUJGJUU2JThEJUEyJUU2JThFJTg5JTBBJTIwJTIwJTIwJTIwbGFiZWxzJTIwJTNEJTIwbnAud2hlcmUobGFiZWxzJTIwISUzRCUyMC0xMDAlMkMlMjBsYWJlbHMlMkMlMjB0b2tlbml6ZXIucGFkX3Rva2VuX2lkKSUwQSUyMCUyMCUyMCUyMGRlY29kZWRfbGFiZWxzJTIwJTNEJTIwdG9rZW5pemVyLmJhdGNoX2RlY29kZShsYWJlbHMlMkMlMjBza2lwX3NwZWNpYWxfdG9rZW5zJTNEVHJ1ZSklMEElMEElMjAlMjAlMjAlMjAlMjMlMjAlRTQlQjglODAlRTQlQkElOUIlRTclQUUlODAlRTUlOEQlOTUlRTclOUElODQlRTUlOTAlOEUlRTUlQTQlODQlRTclOTAlODYlMEElMjAlMjAlMjAlMjBkZWNvZGVkX3ByZWRzJTIwJTNEJTIwJTVCcHJlZC5zdHJpcCgpJTIwZm9yJTIwcHJlZCUyMGluJTIwZGVjb2RlZF9wcmVkcyU1RCUwQSUyMCUyMCUyMCUyMGRlY29kZWRfbGFiZWxzJTIwJTNEJTIwJTVCJTVCbGFiZWwuc3RyaXAoKSU1RCUyMGZvciUyMGxhYmVsJTIwaW4lMjBkZWNvZGVkX2xhYmVscyU1RCUwQSUwQSUyMCUyMCUyMCUyMHJlc3VsdCUyMCUzRCUyMG1ldHJpYy5jb21wdXRlKHByZWRpY3Rpb25zJTNEZGVjb2RlZF9wcmVkcyUyQyUyMHJlZmVyZW5jZXMlM0RkZWNvZGVkX2xhYmVscyklMEElMjAlMjAlMjAlMjByZXR1cm4lMjAlN0IlMjJibGV1JTIyJTNBJTIwcmVzdWx0JTVCJTIyc2NvcmUlMjIlNUQlN0Q=",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>):
    preds, labels = eval_preds
    <span class="hljs-comment"># 如果模型返回的内容超过了预测的logits</span>
    <span class="hljs-keyword">if</span> <span class="hljs-built_in">isinstance</span>(preds, <span class="hljs-built_in">tuple</span>):
        preds = preds[<span class="hljs-number">0</span>]

    decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=<span class="hljs-literal">True</span>)

    <span class="hljs-comment"># 由于我们无法解码 -100,因此将标签中的 -100 替换掉</span>
    labels = np.where(labels != -<span class="hljs-number">100</span>, labels, tokenizer.pad_token_id)
    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>)

    <span class="hljs-comment"># 一些简单的后处理</span>
    decoded_preds = [pred.strip() <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> decoded_preds]
    decoded_labels = [[label.strip()] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> decoded_labels]

    result = metric.compute(predictions=decoded_preds, references=decoded_labels)
    <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;bleu&quot;</span>: result[<span class="hljs-string">&quot;score&quot;</span>]}`,wrap:!1}}),{c(){p=y("p"),p.innerHTML=b,r=n(),T(w.$$.fragment)},l(f){p=U(f,"P",{"data-svelte-h":!0}),u(p)!=="svelte-gr0ukh"&&(p.innerHTML=b),r=a(f),m(w.$$.fragment,f)},m(f,Z){s(f,p,Z),s(f,r,Z),o(w,f,Z),g=!0},i(f){g||(c(w.$$.fragment,f),g=!0)},o(f){J(w.$$.fragment,f),g=!1},d(f){f&&(e(p),e(r)),j(w,f)}}}function yp(x){let p,b="为了将模型的输出转化为评估指标可以使用的文本，我们将利用 <code>tokenizer.batch_decode()</code> 方法。因为 tokenizer 会自动处理填充 <code>tokens</code>，所以我们只需要清理所有标签中的 填充的<code>-100</code> 。让我们定义一个函数，这个函数会接收一个模型和一个数据集，并在其上计算 BLEU 指标。",r,w,g='我们还将使用一个显著提升性能的技巧 - 使用 <a href="https://www.tensorflow.org/xla" rel="nofollow">XLA</a> ，TensorFlow 的线性代数加速编译器，编译我们的生成代码。XLA 对模型的计算图进行了各种优化，从而显著提升了速度和内存使用率。如 Hugging Face 的 <a href="https://huggingface.co/blog/tf-xla-generate" rel="nofollow">博客</a> 所述，当我们的输入形状比较整齐时，XLA 能够很好地提高计算效率。因此，我们需要用填充整理器制作一个新的数据集，把输入补齐到 128 的倍数，然后我们使用 <code>@tf.function(jit_compile=True)</code> 装饰器装饰我们的生成函数，这将把整个函数标记为使用 XLA 编译。',f,Z,V;return Z=new h({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBaW1wb3J0JTIwdGVuc29yZmxvdyUyMGFzJTIwdGYlMEFmcm9tJTIwdHFkbSUyMGltcG9ydCUyMHRxZG0lMEElMEFnZW5lcmF0aW9uX2RhdGFfY29sbGF0b3IlMjAlM0QlMjBEYXRhQ29sbGF0b3JGb3JTZXEyU2VxKCUwQSUyMCUyMCUyMCUyMHRva2VuaXplciUyQyUyMG1vZGVsJTNEbW9kZWwlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMnRmJTIyJTJDJTIwcGFkX3RvX211bHRpcGxlX29mJTNEMTI4JTBBKSUwQSUwQXRmX2dlbmVyYXRlX2RhdGFzZXQlMjAlM0QlMjBtb2RlbC5wcmVwYXJlX3RmX2RhdGFzZXQoJTBBJTIwJTIwJTIwJTIwdG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydmFsaWRhdGlvbiUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMGNvbGxhdGVfZm4lM0RnZW5lcmF0aW9uX2RhdGFfY29sbGF0b3IlMkMlMEElMjAlMjAlMjAlMjBzaHVmZmxlJTNERmFsc2UlMkMlMEElMjAlMjAlMjAlMjBiYXRjaF9zaXplJTNEOCUyQyUwQSklMEElMEElMEElNDB0Zi5mdW5jdGlvbihqaXRfY29tcGlsZSUzRFRydWUpJTBBZGVmJTIwZ2VuZXJhdGVfd2l0aF94bGEoYmF0Y2gpJTNBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwbW9kZWwuZ2VuZXJhdGUoJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaW5wdXRfaWRzJTNEYmF0Y2glNUIlMjJpbnB1dF9pZHMlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBhdHRlbnRpb25fbWFzayUzRGJhdGNoJTVCJTIyYXR0ZW50aW9uX21hc2slMjIlNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBtYXhfbmV3X3Rva2VucyUzRDEyOCUyQyUwQSUyMCUyMCUyMCUyMCklMEElMEElMEFkZWYlMjBjb21wdXRlX21ldHJpY3MoKSUzQSUwQSUyMCUyMCUyMCUyMGFsbF9wcmVkcyUyMCUzRCUyMCU1QiU1RCUwQSUyMCUyMCUyMCUyMGFsbF9sYWJlbHMlMjAlM0QlMjAlNUIlNUQlMEElMjAlMjAlMjAlMjBzYW1wbGVkX2RhdGFzZXQlMjAlM0QlMjB0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ2YWxpZGF0aW9uJTIyJTVELnNodWZmbGUoKS5zZWxlY3QocmFuZ2UoMjAwKSklMEElMjAlMjAlMjAlMjB0Zl9nZW5lcmF0ZV9kYXRhc2V0JTIwJTNEJTIwc2FtcGxlZF9kYXRhc2V0LnRvX3RmX2RhdGFzZXQoJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwY29sdW1ucyUzRCU1QiUyMmlucHV0X2lkcyUyMiUyQyUyMCUyMmF0dGVudGlvbl9tYXNrJTIyJTJDJTIwJTIybGFiZWxzJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwY29sbGF0ZV9mbiUzRGRhdGFfY29sbGF0b3IlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBzaHVmZmxlJTNERmFsc2UlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBiYXRjaF9zaXplJTNENCUyQyUwQSUyMCUyMCUyMCUyMCklMEElMjAlMjAlMjAlMjBmb3IlMjBiYXRjaCUyQyUyMGxhYmVscyUyMGluJTIwdHFkbSh0Zl9nZW5lcmF0ZV9kYXRhc2V0KSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByZWRpY3Rpb25zJTIwJTNEJTIwZ2VuZXJhdGVfd2l0aF94bGEoYmF0Y2gpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZGVjb2RlZF9wcmVkcyUyMCUzRCUyMHRva2VuaXplci5iYXRjaF9kZWNvZGUocHJlZGljdGlvbnMlMkMlMjBza2lwX3NwZWNpYWxfdG9rZW5zJTNEVHJ1ZSklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsYWJlbHMlMjAlM0QlMjBsYWJlbHMubnVtcHkoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxhYmVscyUyMCUzRCUyMG5wLndoZXJlKGxhYmVscyUyMCElM0QlMjAtMTAwJTJDJTIwbGFiZWxzJTJDJTIwdG9rZW5pemVyLnBhZF90b2tlbl9pZCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBkZWNvZGVkX2xhYmVscyUyMCUzRCUyMHRva2VuaXplci5iYXRjaF9kZWNvZGUobGFiZWxzJTJDJTIwc2tpcF9zcGVjaWFsX3Rva2VucyUzRFRydWUpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZGVjb2RlZF9wcmVkcyUyMCUzRCUyMCU1QnByZWQuc3RyaXAoKSUyMGZvciUyMHByZWQlMjBpbiUyMGRlY29kZWRfcHJlZHMlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBkZWNvZGVkX2xhYmVscyUyMCUzRCUyMCU1QiU1QmxhYmVsLnN0cmlwKCklNUQlMjBmb3IlMjBsYWJlbCUyMGluJTIwZGVjb2RlZF9sYWJlbHMlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBhbGxfcHJlZHMuZXh0ZW5kKGRlY29kZWRfcHJlZHMpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWxsX2xhYmVscy5leHRlbmQoZGVjb2RlZF9sYWJlbHMpJTBBJTBBJTIwJTIwJTIwJTIwcmVzdWx0JTIwJTNEJTIwbWV0cmljLmNvbXB1dGUocHJlZGljdGlvbnMlM0RhbGxfcHJlZHMlMkMlMjByZWZlcmVuY2VzJTNEYWxsX2xhYmVscyklMEElMjAlMjAlMjAlMjByZXR1cm4lMjAlN0IlMjJibGV1JTIyJTNBJTIwcmVzdWx0JTVCJTIyc2NvcmUlMjIlNUQlN0Q=",highlighted:`<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm

generation_data_collator = DataCollatorForSeq2Seq(
    tokenizer, model=model, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>, pad_to_multiple_of=<span class="hljs-number">128</span>
)

tf_generate_dataset = model.prepare_tf_dataset(
    tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>],
    collate_fn=generation_data_collator,
    shuffle=<span class="hljs-literal">False</span>,
    batch_size=<span class="hljs-number">8</span>,
)


<span class="hljs-meta">@tf.function(<span class="hljs-params">jit_compile=<span class="hljs-literal">True</span></span>)</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">generate_with_xla</span>(<span class="hljs-params">batch</span>):
    <span class="hljs-keyword">return</span> model.generate(
        input_ids=batch[<span class="hljs-string">&quot;input_ids&quot;</span>],
        attention_mask=batch[<span class="hljs-string">&quot;attention_mask&quot;</span>],
        max_new_tokens=<span class="hljs-number">128</span>,
    )


<span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>():
    all_preds = []
    all_labels = []
    sampled_dataset = tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>].shuffle().select(<span class="hljs-built_in">range</span>(<span class="hljs-number">200</span>))
    tf_generate_dataset = sampled_dataset.to_tf_dataset(
        columns=[<span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>],
        collate_fn=data_collator,
        shuffle=<span class="hljs-literal">False</span>,
        batch_size=<span class="hljs-number">4</span>,
    )
    <span class="hljs-keyword">for</span> batch, labels <span class="hljs-keyword">in</span> tqdm(tf_generate_dataset):
        predictions = generate_with_xla(batch)
        decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=<span class="hljs-literal">True</span>)
        labels = labels.numpy()
        labels = np.where(labels != -<span class="hljs-number">100</span>, labels, tokenizer.pad_token_id)
        decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>)
        decoded_preds = [pred.strip() <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> decoded_preds]
        decoded_labels = [[label.strip()] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> decoded_labels]
        all_preds.extend(decoded_preds)
        all_labels.extend(decoded_labels)

    result = metric.compute(predictions=all_preds, references=all_labels)
    <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;bleu&quot;</span>: result[<span class="hljs-string">&quot;score&quot;</span>]}`,wrap:!1}}),{c(){p=y("p"),p.innerHTML=b,r=n(),w=y("p"),w.innerHTML=g,f=n(),T(Z.$$.fragment)},l(_){p=U(_,"P",{"data-svelte-h":!0}),u(p)!=="svelte-18d2huy"&&(p.innerHTML=b),r=a(_),w=U(_,"P",{"data-svelte-h":!0}),u(w)!=="svelte-t5bv7v"&&(w.innerHTML=g),f=a(_),m(Z.$$.fragment,_)},m(_,k){s(_,p,k),s(_,r,k),s(_,w,k),s(_,f,k),o(Z,_,k),V=!0},i(_){V||(c(Z.$$.fragment,_),V=!0)},o(_){J(Z.$$.fragment,_),V=!1},d(_){_&&(e(p),e(r),e(w),e(f)),j(Z,_)}}}function Up(x){let p,b="完成这些步骤之后，我们就可以定义我们的 <code>Seq2SeqTrainingArguments</code> 了。与 <code>Trainer</code> 一样，它是 <code>TrainingArguments</code> 的子类，其中包含更多可以设置的字段：",r,w,g,f,Z="除了通常的超参数（如学习率、训练轮数、批次大小和一些权重衰减）之外，这里的部分参数与我们在前面章节看到的有一些不同：",V,_,k="<li>我们没有设置定期进行评估，因为评估需要耗费一定的时间；我们将只在训练开始之前和结束之后评估我们的模型一次。</li> <li>我们设置 <code>fp16=True</code> ，这可以加快在支持 fp16 的 GPU 上的训练速度。</li> <li>和之前我们讨论的一样，我们设置 <code>predict_with_generate=True</code> 。</li> <li>我们设置了 <code>push_to_hub=True</code> ，在每个 epoch 结束时将模型上传到 Hub。</li>",C,R,Q='请注意，你可以使用 <code>hub_model_id</code> 参数指定要推送到的存储库的名称（当你想把模型推送到指定的组织的时候，就必须使用此参数）。例如，当我们将模型推送到 <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code> 组织</a> 时，我们在 <code>Seq2SeqTrainingArguments</code> 添加了 <code>hub_model_id=&quot;huggingface-course/marian-finetuned-kde4-en- to-fr&quot;</code> 。默认情况下，该仓库将保存在你的账户中，并以你设置的输出目录命名，因此在我们的例子中它是 <code>&quot;sgugger/marian-finetuned-kde4-en-to-fr&quot;</code> 。',pl,K,X,A,P="最后，我们将所有内容传递给 <code>Seq2SeqTrainer</code> ：",ml,W,E,v,O="在开始训练之前，我们先查看一下我们的模型目前的 BLEU 分数，以确保我们的微调并未使情况变得更糟。这个命令需要一些时间，所以你可以在执行期间去喝杯咖啡：",ol,F,z,L,N,Ml,ll="BLEU 得分为 39 并不算太差，这反映了我们的模型已经擅长将英语句子翻译成法语句子。",B,el,d="接下来是训练，这也需要一些时间：",G,jl,Jl,sl,kl="请注意，在训练过程中，每当保存模型时（这里是每个 epoch），它都会在后台将模型上传到 Hub。这样，如有必要，你将能够在另一台机器上继续你的训练。",fl,H,Hl="训练完成后，我们再次评估我们的模型——希望我们会看到 BLEU 分数有所提高！",yl,rl,bl,S,Zl,tl,_l="可以看到近 14 点的改进，这很棒！",hl,D,Sl="最后，我们使用 <code>push_to_hub()</code> 方法来确保我们上传了模型最新的版本。 <code>Trainer</code> 还创建了一张包含所有评估结果的模型卡片并上传到 Hub 。这个模型卡片包含了可以帮助 Hub 为推理演示选择小部件的元数据，通常情况下我们不需要做额外的更改，因为它可以从模型类中推断出正确的小部件，但在这个示例中，它只能通过模型类推断这是一个序列到序列的问题，所以我们补充一下具体的模型类别。",Ul,il,Cl,Y,Pl="如果你想检查命令执行的结果，此命令将返回它刚刚执行的提交的 URL，你可以打开 url 进行检查：",$l,q,Dl,Tl,cl="在此阶段，你可以在 Model Hub 上使用推理小部件来测试你的模型，并与你的朋友分享。你已经成功地在翻译任务上进行了模型的微调，恭喜你！",xl,nl,ul="如果你想更深入地了解训练循环，我们现在将向你展示如何使用 🤗 Accelerate 做同样的事情。",dl;return w=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFNlcTJTZXFUcmFpbmluZ0FyZ3VtZW50cyUwQSUwQWFyZ3MlMjAlM0QlMjBTZXEyU2VxVHJhaW5pbmdBcmd1bWVudHMoJTBBJTIwJTIwJTIwJTIwZiUyMm1hcmlhbi1maW5ldHVuZWQta2RlNC1lbi10by1mciUyMiUyQyUwQSUyMCUyMCUyMCUyMGV2YWx1YXRpb25fc3RyYXRlZ3klM0QlMjJubyUyMiUyQyUwQSUyMCUyMCUyMCUyMHNhdmVfc3RyYXRlZ3klM0QlMjJlcG9jaCUyMiUyQyUwQSUyMCUyMCUyMCUyMGxlYXJuaW5nX3JhdGUlM0QyZS01JTJDJTBBJTIwJTIwJTIwJTIwcGVyX2RldmljZV90cmFpbl9iYXRjaF9zaXplJTNEMzIlMkMlMEElMjAlMjAlMjAlMjBwZXJfZGV2aWNlX2V2YWxfYmF0Y2hfc2l6ZSUzRDY0JTJDJTBBJTIwJTIwJTIwJTIwd2VpZ2h0X2RlY2F5JTNEMC4wMSUyQyUwQSUyMCUyMCUyMCUyMHNhdmVfdG90YWxfbGltaXQlM0QzJTJDJTBBJTIwJTIwJTIwJTIwbnVtX3RyYWluX2Vwb2NocyUzRDMlMkMlMEElMjAlMjAlMjAlMjBwcmVkaWN0X3dpdGhfZ2VuZXJhdGUlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwZnAxNiUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjBwdXNoX3RvX2h1YiUzRFRydWUlMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Seq2SeqTrainingArguments

args = Seq2SeqTrainingArguments(
    <span class="hljs-string">f&quot;marian-finetuned-kde4-en-to-fr&quot;</span>,
    evaluation_strategy=<span class="hljs-string">&quot;no&quot;</span>,
    save_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
    learning_rate=<span class="hljs-number">2e-5</span>,
    per_device_train_batch_size=<span class="hljs-number">32</span>,
    per_device_eval_batch_size=<span class="hljs-number">64</span>,
    weight_decay=<span class="hljs-number">0.01</span>,
    save_total_limit=<span class="hljs-number">3</span>,
    num_train_epochs=<span class="hljs-number">3</span>,
    predict_with_generate=<span class="hljs-literal">True</span>,
    fp16=<span class="hljs-literal">True</span>,
    push_to_hub=<span class="hljs-literal">True</span>,
)`,wrap:!1}}),K=new te({props:{$$slots:{default:[mp]},$$scope:{ctx:x}}}),W=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFNlcTJTZXFUcmFpbmVyJTBBJTBBdHJhaW5lciUyMCUzRCUyMFNlcTJTZXFUcmFpbmVyKCUwQSUyMCUyMCUyMCUyMG1vZGVsJTJDJTBBJTIwJTIwJTIwJTIwYXJncyUyQyUwQSUyMCUyMCUyMCUyMHRyYWluX2RhdGFzZXQlM0R0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMGV2YWxfZGF0YXNldCUzRHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnZhbGlkYXRpb24lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBkYXRhX2NvbGxhdG9yJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMHRva2VuaXplciUzRHRva2VuaXplciUyQyUwQSUyMCUyMCUyMCUyMGNvbXB1dGVfbWV0cmljcyUzRGNvbXB1dGVfbWV0cmljcyUyQyUwQSk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Seq2SeqTrainer

trainer = Seq2SeqTrainer(
    model,
    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,
    tokenizer=tokenizer,
    compute_metrics=compute_metrics,
)`,wrap:!1}}),F=new h({props:{code:"dHJhaW5lci5ldmFsdWF0ZShtYXhfbGVuZ3RoJTNEbWF4X2xlbmd0aCk=",highlighted:"trainer.evaluate(max_length=max_length)",wrap:!1}}),L=new h({props:{code:"JTdCJ2V2YWxfbG9zcyclM0ElMjAxLjY5NjQ0MDgxNTkyNTU5ODElMkMlMEElMjAnZXZhbF9ibGV1JyUzQSUyMDM5LjI2ODY1MDYxMDA3NjE2JTJDJTBBJTIwJ2V2YWxfcnVudGltZSclM0ElMjA5NjUuODg4NCUyQyUwQSUyMCdldmFsX3NhbXBsZXNfcGVyX3NlY29uZCclM0ElMjAyMS43NiUyQyUwQSUyMCdldmFsX3N0ZXBzX3Blcl9zZWNvbmQnJTNBJTIwMC4zNDElN0Q=",highlighted:`{<span class="hljs-string">&#x27;eval_loss&#x27;</span>: <span class="hljs-number">1.6964408159255981</span>,
 <span class="hljs-string">&#x27;eval_bleu&#x27;</span>: <span class="hljs-number">39.26865061007616</span>,
 <span class="hljs-string">&#x27;eval_runtime&#x27;</span>: <span class="hljs-number">965.8884</span>,
 <span class="hljs-string">&#x27;eval_samples_per_second&#x27;</span>: <span class="hljs-number">21.76</span>,
 <span class="hljs-string">&#x27;eval_steps_per_second&#x27;</span>: <span class="hljs-number">0.341</span>}`,wrap:!1}}),jl=new h({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:"trainer.train()",wrap:!1}}),rl=new h({props:{code:"dHJhaW5lci5ldmFsdWF0ZShtYXhfbGVuZ3RoJTNEbWF4X2xlbmd0aCk=",highlighted:"trainer.evaluate(max_length=max_length)",wrap:!1}}),S=new h({props:{code:"JTdCJ2V2YWxfbG9zcyclM0ElMjAwLjg1NTg1MDU3NzM1NDQzMTIlMkMlMEElMjAnZXZhbF9ibGV1JyUzQSUyMDUyLjk0MTYxMzM3Nzc1NTc2JTJDJTBBJTIwJ2V2YWxfcnVudGltZSclM0ElMjA3MTQuMjU3NiUyQyUwQSUyMCdldmFsX3NhbXBsZXNfcGVyX3NlY29uZCclM0ElMjAyOS40MjYlMkMlMEElMjAnZXZhbF9zdGVwc19wZXJfc2Vjb25kJyUzQSUyMDAuNDYxJTJDJTBBJTIwJ2Vwb2NoJyUzQSUyMDMuMCU3RA==",highlighted:`{<span class="hljs-string">&#x27;eval_loss&#x27;</span>: <span class="hljs-number">0.8558505773544312</span>,
 <span class="hljs-string">&#x27;eval_bleu&#x27;</span>: <span class="hljs-number">52.94161337775576</span>,
 <span class="hljs-string">&#x27;eval_runtime&#x27;</span>: <span class="hljs-number">714.2576</span>,
 <span class="hljs-string">&#x27;eval_samples_per_second&#x27;</span>: <span class="hljs-number">29.426</span>,
 <span class="hljs-string">&#x27;eval_steps_per_second&#x27;</span>: <span class="hljs-number">0.461</span>,
 <span class="hljs-string">&#x27;epoch&#x27;</span>: <span class="hljs-number">3.0</span>}`,wrap:!1}}),il=new h({props:{code:"dHJhaW5lci5wdXNoX3RvX2h1Yih0YWdzJTNEJTIydHJhbnNsYXRpb24lMjIlMkMlMjBjb21taXRfbWVzc2FnZSUzRCUyMlRyYWluaW5nJTIwY29tcGxldGUlMjIp",highlighted:'trainer.push_to_hub(tags=<span class="hljs-string">&quot;translation&quot;</span>, commit_message=<span class="hljs-string">&quot;Training complete&quot;</span>)',wrap:!1}}),q=new h({props:{code:"J2h0dHBzJTNBJTJGJTJGaHVnZ2luZ2ZhY2UuY28lMkZzZ3VnZ2VyJTJGbWFyaWFuLWZpbmV0dW5lZC1rZGU0LWVuLXRvLWZyJTJGY29tbWl0JTJGMzYwMWQ2MjFlM2JhYWUyYmM2M2QzMzExNDUyNTM1ZjhmNThmNmVmMyc=",highlighted:'<span class="hljs-string">&#x27;https://huggingface.co/sgugger/marian-finetuned-kde4-en-to-fr/commit/3601d621e3baae2bc63d3311452535f8f58f6ef3&#x27;</span>',wrap:!1}}),{c(){p=y("p"),p.innerHTML=b,r=n(),T(w.$$.fragment),g=n(),f=y("p"),f.textContent=Z,V=n(),_=y("ul"),_.innerHTML=k,C=n(),R=y("p"),R.innerHTML=Q,pl=n(),T(K.$$.fragment),X=n(),A=y("p"),A.innerHTML=P,ml=n(),T(W.$$.fragment),E=n(),v=y("p"),v.textContent=O,ol=n(),T(F.$$.fragment),z=n(),T(L.$$.fragment),N=n(),Ml=y("p"),Ml.textContent=ll,B=n(),el=y("p"),el.textContent=d,G=n(),T(jl.$$.fragment),Jl=n(),sl=y("p"),sl.textContent=kl,fl=n(),H=y("p"),H.textContent=Hl,yl=n(),T(rl.$$.fragment),bl=n(),T(S.$$.fragment),Zl=n(),tl=y("p"),tl.textContent=_l,hl=n(),D=y("p"),D.innerHTML=Sl,Ul=n(),T(il.$$.fragment),Cl=n(),Y=y("p"),Y.textContent=Pl,$l=n(),T(q.$$.fragment),Dl=n(),Tl=y("p"),Tl.textContent=cl,xl=n(),nl=y("p"),nl.textContent=ul},l(i){p=U(i,"P",{"data-svelte-h":!0}),u(p)!=="svelte-1qmt64b"&&(p.innerHTML=b),r=a(i),m(w.$$.fragment,i),g=a(i),f=U(i,"P",{"data-svelte-h":!0}),u(f)!=="svelte-p1srp5"&&(f.textContent=Z),V=a(i),_=U(i,"UL",{"data-svelte-h":!0}),u(_)!=="svelte-1bsmz7p"&&(_.innerHTML=k),C=a(i),R=U(i,"P",{"data-svelte-h":!0}),u(R)!=="svelte-1chdo7e"&&(R.innerHTML=Q),pl=a(i),m(K.$$.fragment,i),X=a(i),A=U(i,"P",{"data-svelte-h":!0}),u(A)!=="svelte-jj26i9"&&(A.innerHTML=P),ml=a(i),m(W.$$.fragment,i),E=a(i),v=U(i,"P",{"data-svelte-h":!0}),u(v)!=="svelte-1v7m6nv"&&(v.textContent=O),ol=a(i),m(F.$$.fragment,i),z=a(i),m(L.$$.fragment,i),N=a(i),Ml=U(i,"P",{"data-svelte-h":!0}),u(Ml)!=="svelte-xajbnv"&&(Ml.textContent=ll),B=a(i),el=U(i,"P",{"data-svelte-h":!0}),u(el)!=="svelte-mtrcqq"&&(el.textContent=d),G=a(i),m(jl.$$.fragment,i),Jl=a(i),sl=U(i,"P",{"data-svelte-h":!0}),u(sl)!=="svelte-bm11br"&&(sl.textContent=kl),fl=a(i),H=U(i,"P",{"data-svelte-h":!0}),u(H)!=="svelte-143pqop"&&(H.textContent=Hl),yl=a(i),m(rl.$$.fragment,i),bl=a(i),m(S.$$.fragment,i),Zl=a(i),tl=U(i,"P",{"data-svelte-h":!0}),u(tl)!=="svelte-31z6tc"&&(tl.textContent=_l),hl=a(i),D=U(i,"P",{"data-svelte-h":!0}),u(D)!=="svelte-2jw0f7"&&(D.innerHTML=Sl),Ul=a(i),m(il.$$.fragment,i),Cl=a(i),Y=U(i,"P",{"data-svelte-h":!0}),u(Y)!=="svelte-hof6xv"&&(Y.textContent=Pl),$l=a(i),m(q.$$.fragment,i),Dl=a(i),Tl=U(i,"P",{"data-svelte-h":!0}),u(Tl)!=="svelte-1hcxxrd"&&(Tl.textContent=cl),xl=a(i),nl=U(i,"P",{"data-svelte-h":!0}),u(nl)!=="svelte-5myn0z"&&(nl.textContent=ul)},m(i,I){s(i,p,I),s(i,r,I),o(w,i,I),s(i,g,I),s(i,f,I),s(i,V,I),s(i,_,I),s(i,C,I),s(i,R,I),s(i,pl,I),o(K,i,I),s(i,X,I),s(i,A,I),s(i,ml,I),o(W,i,I),s(i,E,I),s(i,v,I),s(i,ol,I),o(F,i,I),s(i,z,I),o(L,i,I),s(i,N,I),s(i,Ml,I),s(i,B,I),s(i,el,I),s(i,G,I),o(jl,i,I),s(i,Jl,I),s(i,sl,I),s(i,fl,I),s(i,H,I),s(i,yl,I),o(rl,i,I),s(i,bl,I),o(S,i,I),s(i,Zl,I),s(i,tl,I),s(i,hl,I),s(i,D,I),s(i,Ul,I),o(il,i,I),s(i,Cl,I),s(i,Y,I),s(i,$l,I),o(q,i,I),s(i,Dl,I),s(i,Tl,I),s(i,xl,I),s(i,nl,I),dl=!0},i(i){dl||(c(w.$$.fragment,i),c(K.$$.fragment,i),c(W.$$.fragment,i),c(F.$$.fragment,i),c(L.$$.fragment,i),c(jl.$$.fragment,i),c(rl.$$.fragment,i),c(S.$$.fragment,i),c(il.$$.fragment,i),c(q.$$.fragment,i),dl=!0)},o(i){J(w.$$.fragment,i),J(K.$$.fragment,i),J(W.$$.fragment,i),J(F.$$.fragment,i),J(L.$$.fragment,i),J(jl.$$.fragment,i),J(rl.$$.fragment,i),J(S.$$.fragment,i),J(il.$$.fragment,i),J(q.$$.fragment,i),dl=!1},d(i){i&&(e(p),e(r),e(g),e(f),e(V),e(_),e(C),e(R),e(pl),e(X),e(A),e(ml),e(E),e(v),e(ol),e(z),e(N),e(Ml),e(B),e(el),e(G),e(Jl),e(sl),e(fl),e(H),e(yl),e(bl),e(Zl),e(tl),e(hl),e(D),e(Ul),e(Cl),e(Y),e($l),e(Dl),e(Tl),e(xl),e(nl)),j(w,i),j(K,i),j(W,i),j(F,i),j(L,i),j(jl,i),j(rl,i),j(S,i),j(il,i),j(q,i)}}}function Tp(x){let p,b="在我们开始之前，让我们看看我们在没有任何训练的情况下我们的模型效果怎么样。",r,w,g,f,Z,V,_="现在可以准备编译和训练模型了。运行 <code>tf.keras.mixed_precision.set_global_policy(&quot;mixed_ﬂoat16&quot;)</code> 后， Keras 将使用 ﬂoat16 精度进行训练，这样可以显著提高支持半精度 GPU（Nvidia 20xx/V100 或更高版本）的训练速度",k,C,R,Q,pl='接下来就像我们在 <a href="/course/chapter7/2">第 2 节</a> 中学到的，我们将定义一个 <code>PushToHubCallback</code> 回调函数，并拟合模型的时候添加该回调函数，这样就可以训练期间将我们的模型上传到 Hub。',K,X,A,P,ml='请注意，你可以使用 <code>hub_model_id</code> 参数指定要推送到的模型仓库的名称（当你想把模型推送到指定的组织的时候，就必须使用此参数）。例如，当我们将模型推送到 <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code> 组织</a> 时，我们在 <code>Seq2SeqTrainingArguments</code>添加了 <code>hub_model_id=&quot;huggingface-course/marian-finetuned-kde4-en- to-fr&quot;</code> 。默认情况下，该仓库将保存在你的账户里，并以你设置的输出目录命名，因此在我们的例子中它是 <code>&quot;sgugger/marian-finetuned-kde4-en-to-fr&quot;</code> 。',W,E,v,O,ol="最后，让我们看看训练结束后我们的模型的 BLEU 的分数：",F,z,L,N,Ml,ll,B="在这个阶段，你可以使用模型中心上的推理小部件来测试你的模型并与你的朋友分享。恭喜你，你已经成功地在翻译任务上微调了一个模型！",el;return w=new h({props:{code:"cHJpbnQoY29tcHV0ZV9tZXRyaWNzKCkp",highlighted:'<span class="hljs-built_in">print</span>(compute_metrics())',wrap:!1}}),f=new h({props:{code:"JTdCJ2JsZXUnJTNBJTIwMzMuMjY5ODM3MDE0NTQ3MzMlN0Q=",highlighted:'{&#x27;bleu&#x27;: <span class="hljs-number">33.26983701454733</span>}',wrap:!1}}),C=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMGNyZWF0ZV9vcHRpbWl6ZXIlMEFmcm9tJTIwdHJhbnNmb3JtZXJzLmtlcmFzX2NhbGxiYWNrcyUyMGltcG9ydCUyMFB1c2hUb0h1YkNhbGxiYWNrJTBBaW1wb3J0JTIwdGVuc29yZmxvdyUyMGFzJTIwdGYlMEElMEElMjMlMjAlRTglQUUlQUQlRTclQkIlODMlRTYlQUQlQTUlRTYlOTUlQjAlRTYlOTglQUYlRTYlOTUlQjAlRTYlOEQlQUUlRTklOUIlODYlRTQlQjglQUQlRTclOUElODQlRTYlQTAlQjclRTYlOUMlQUMlRTYlOTUlQjAlRTklODclOEYlMkMlRTklOTklQTQlRTQlQkIlQTUlMjBiYXRjaCUyMCVFNSVBNCVBNyVFNSVCMCU4RiUyQyVFNyU4NCVCNiVFNSU5MCU4RSVFNCVCOSU5OCVFNCVCQiVBNSVFNiU4MCVCQiVFNyU5QSU4NCUyMGVwb2NoJTIwJUU2JTk1JUIwJUUzJTgwJTgyJTBBJTIzJTIwJUU2JUIzJUE4JUU2JTg0JThGJUU4JUJGJTk5JUU5JTg3JThDJUU3JTlBJTg0JTIwdGZfdHJhaW5fZGF0YXNldCUyMCVFNiU5OCVBRiUyMGJhdGNoJTIwJUU1JUJEJUEyJUU1JUJDJThGJUU3JTlBJTg0JTIwdGYuZGF0YS5EYXRhc2V0JTJDJTBBJTIzJTIwJUU4JTgwJThDJUU0JUI4JThEJUU2JTk4JUFGJUU1JThFJTlGJUU1JUE3JThCJUU3JTlBJTg0JTIwSHVnZ2luZyUyMEZhY2UlMjBEYXRhc2V0JTIwJTJDJUU2JTg5JTgwJUU0JUJCJUE1JUU0JUJEJUJGJUU3JTk0JUE4JTIwbGVuKCklMjAlRTglQUUlQTElRTclQUUlOTclRTUlQUUlODMlRTclOUElODQlRTklOTUlQkYlRTUlQkElQTYlRTUlQjclQjIlRTclQkIlOEYlRTYlOTglQUYlMjBudW1fc2FtcGxlcyUyMCUyRiUyRiUyMGJhdGNoX3NpemUlRTMlODAlODIlMEElMEFudW1fZXBvY2hzJTIwJTNEJTIwMyUwQW51bV90cmFpbl9zdGVwcyUyMCUzRCUyMGxlbih0Zl90cmFpbl9kYXRhc2V0KSUyMColMjBudW1fZXBvY2hzJTBBJTBBb3B0aW1pemVyJTJDJTIwc2NoZWR1bGUlMjAlM0QlMjBjcmVhdGVfb3B0aW1pemVyKCUwQSUyMCUyMCUyMCUyMGluaXRfbHIlM0Q1ZS01JTJDJTBBJTIwJTIwJTIwJTIwbnVtX3dhcm11cF9zdGVwcyUzRDAlMkMlMEElMjAlMjAlMjAlMjBudW1fdHJhaW5fc3RlcHMlM0RudW1fdHJhaW5fc3RlcHMlMkMlMEElMjAlMjAlMjAlMjB3ZWlnaHRfZGVjYXlfcmF0ZSUzRDAuMDElMkMlMEEpJTBBbW9kZWwuY29tcGlsZShvcHRpbWl6ZXIlM0RvcHRpbWl6ZXIpJTBBJTBBJTIzJTIwJUU0JUJEJUJGJUU3JTk0JUE4JTIwZmxvYXQxNiUyMCVFNiVCNyVCNyVFNSU5MCU4OCVFNyVCMiVCRSVFNSVCQSVBNiVFOCVCRiU5QiVFOCVBMSU4QyVFOCVBRSVBRCVFNyVCQiU4MyUwQXRmLmtlcmFzLm1peGVkX3ByZWNpc2lvbi5zZXRfZ2xvYmFsX3BvbGljeSglMjJtaXhlZF9mbG9hdDE2JTIyKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> create_optimizer
<span class="hljs-keyword">from</span> transformers.keras_callbacks <span class="hljs-keyword">import</span> PushToHubCallback
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf

<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">5e-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)

<span class="hljs-comment"># 使用 float16 混合精度进行训练</span>
tf.keras.mixed_precision.set_global_policy(<span class="hljs-string">&quot;mixed_float16&quot;</span>)`,wrap:!1}}),X=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycy5rZXJhc19jYWxsYmFja3MlMjBpbXBvcnQlMjBQdXNoVG9IdWJDYWxsYmFjayUwQSUwQWNhbGxiYWNrJTIwJTNEJTIwUHVzaFRvSHViQ2FsbGJhY2soJTBBJTIwJTIwJTIwJTIwb3V0cHV0X2RpciUzRCUyMm1hcmlhbi1maW5ldHVuZWQta2RlNC1lbi10by1mciUyMiUyQyUyMHRva2VuaXplciUzRHRva2VuaXplciUwQSklMEElMEFtb2RlbC5maXQoJTBBJTIwJTIwJTIwJTIwdGZfdHJhaW5fZGF0YXNldCUyQyUwQSUyMCUyMCUyMCUyMHZhbGlkYXRpb25fZGF0YSUzRHRmX2V2YWxfZGF0YXNldCUyQyUwQSUyMCUyMCUyMCUyMGNhbGxiYWNrcyUzRCU1QmNhbGxiYWNrJTVEJTJDJTBBJTIwJTIwJTIwJTIwZXBvY2hzJTNEbnVtX2Vwb2NocyUyQyUwQSk=",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;marian-finetuned-kde4-en-to-fr&quot;</span>, tokenizer=tokenizer
)

model.fit(
    tf_train_dataset,
    validation_data=tf_eval_dataset,
    callbacks=[callback],
    epochs=num_epochs,
)`,wrap:!1}}),E=new te({props:{$$slots:{default:[op]},$$scope:{ctx:x}}}),z=new h({props:{code:"cHJpbnQoY29tcHV0ZV9tZXRyaWNzKCkp",highlighted:'<span class="hljs-built_in">print</span>(compute_metrics())',wrap:!1}}),N=new h({props:{code:"JTdCJ2JsZXUnJTNBJTIwNTcuMzM0MDY2MjcxNTQ1ODY1JTdE",highlighted:'{&#x27;bleu&#x27;: <span class="hljs-number">57.334066271545865</span>}',wrap:!1}}),{c(){p=y("p"),p.textContent=b,r=n(),T(w.$$.fragment),g=n(),T(f.$$.fragment),Z=n(),V=y("p"),V.innerHTML=_,k=n(),T(C.$$.fragment),R=n(),Q=y("p"),Q.innerHTML=pl,K=n(),T(X.$$.fragment),A=n(),P=y("p"),P.innerHTML=ml,W=n(),T(E.$$.fragment),v=n(),O=y("p"),O.textContent=ol,F=n(),T(z.$$.fragment),L=n(),T(N.$$.fragment),Ml=n(),ll=y("p"),ll.textContent=B},l(d){p=U(d,"P",{"data-svelte-h":!0}),u(p)!=="svelte-lgd8u2"&&(p.textContent=b),r=a(d),m(w.$$.fragment,d),g=a(d),m(f.$$.fragment,d),Z=a(d),V=U(d,"P",{"data-svelte-h":!0}),u(V)!=="svelte-m497wj"&&(V.innerHTML=_),k=a(d),m(C.$$.fragment,d),R=a(d),Q=U(d,"P",{"data-svelte-h":!0}),u(Q)!=="svelte-cj4fke"&&(Q.innerHTML=pl),K=a(d),m(X.$$.fragment,d),A=a(d),P=U(d,"P",{"data-svelte-h":!0}),u(P)!=="svelte-6k5dz8"&&(P.innerHTML=ml),W=a(d),m(E.$$.fragment,d),v=a(d),O=U(d,"P",{"data-svelte-h":!0}),u(O)!=="svelte-1c01lu3"&&(O.textContent=ol),F=a(d),m(z.$$.fragment,d),L=a(d),m(N.$$.fragment,d),Ml=a(d),ll=U(d,"P",{"data-svelte-h":!0}),u(ll)!=="svelte-8h8zkn"&&(ll.textContent=B)},m(d,G){s(d,p,G),s(d,r,G),o(w,d,G),s(d,g,G),o(f,d,G),s(d,Z,G),s(d,V,G),s(d,k,G),o(C,d,G),s(d,R,G),s(d,Q,G),s(d,K,G),o(X,d,G),s(d,A,G),s(d,P,G),s(d,W,G),o(E,d,G),s(d,v,G),s(d,O,G),s(d,F,G),o(z,d,G),s(d,L,G),o(N,d,G),s(d,Ml,G),s(d,ll,G),el=!0},i(d){el||(c(w.$$.fragment,d),c(f.$$.fragment,d),c(C.$$.fragment,d),c(X.$$.fragment,d),c(E.$$.fragment,d),c(z.$$.fragment,d),c(N.$$.fragment,d),el=!0)},o(d){J(w.$$.fragment,d),J(f.$$.fragment,d),J(C.$$.fragment,d),J(X.$$.fragment,d),J(E.$$.fragment,d),J(z.$$.fragment,d),J(N.$$.fragment,d),el=!1},d(d){d&&(e(p),e(r),e(g),e(Z),e(V),e(k),e(R),e(Q),e(K),e(A),e(P),e(W),e(v),e(O),e(F),e(L),e(Ml),e(ll)),j(w,d),j(f,d),j(C,d),j(X,d),j(E,d),j(z,d),j(N,d)}}}function mp(x){let p,b="💡如果你使用的输出目录已经存在一个同名的文件夹，则它应该是推送的仓库克隆在本地的版本。如果不是，你将在定义你的 <code>Seq2SeqTrainer</code> 名称时会遇到错误，并且需要设置一个新名称。";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-19lswvy"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},p:Ll,d(r){r&&e(p)}}}function op(x){let p,b="💡如果正在使用的输出目录已经存在一个同名的文件夹，则它应该是目标推送仓库的在本地克隆在本地的版本。如果不是，当调用 <code>model.fit()</code> 时会收到错误，并需要设置一个新的路径。";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-2lusqh"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},p:Ll,d(r){r&&e(p)}}}function Ea(x){let p,b,r,w='我们现在来看一下完整的训练循环，这样你就可以轻松定制你需要的部分。它将与我们在 <a href="https://chat.openai.com/course/chapter7/2" rel="nofollow">第 2 节</a> 和 <a href="https://chat.openai.com/course/chapter3/4" rel="nofollow">第 3 节</a> 中做的非常相似。',g,f,Z,V,_="由于这里的步骤在之前的章节已经出现过很多次，因此这里只做简略说明。首先，我们将数据集设置为 <code>torch</code> 格式，这样可以将数据集的格式转换为 <code>PyTorch</code> 张量，然后我们用数据集构建 <code>DataLoader</code> ：",k,C,R,Q,pl="接下来我们重新实例化我们的模型，以确保我们不会继续上一节的微调，而是再次从预训练模型开始重新训练：",K,X,A,P,ml="然后我们需要一个优化器：",W,E,v,O,ol="准备好这些对象，我们就可以将它们发送到 <code>accelerator.prepare()</code> 方法中。请记住，如果你想在 Colab Notebook 上使用 TPU 进行训练，你需要将所有这些代码移动到一个训练函数中，并且这个训练函数不应该包含实例化 <code>Accelerator</code> <code>的代码。换句话说，Accelerator</code> 的实例化应该在这个函数之外进行。这么做的原因是，TPU 在 Colab 中的工作方式有些特殊。TPU 运行时会重新执行整个单元格的代码，因此如果 <code>Accelerator</code> 的实例化在训练函数内部，它可能会被多次实例化，导致错误。",F,z,L,N,Ml="现在我们已经将我们的 <code>train_dataloader</code> 发送到 <code>accelerator.prepare()</code> 方法中了，现在我们可以使用它的长度来计算训练步骤的数量。请记住，我们应该始终在准备好数据加载器后再执行此操作，因为更改数据加载器会改变 <code>DataLoader</code> 的长度。然后，我们使用学习率衰减到 0 的经典线性学习率调度：",ll,B,el,d,G="最后，为了将我们的模型推送到 Hugging Face Hub，我们需要在一个工作文件夹中创建一个 <code>Repository</code> 对象。如果你尚未登录 Hugging Face，请先进行登录。我们将根据模型 ID 来确定仓库名称。你可以使用自己选择的名称替换 <code>repo_name</code>，但请确保包含你的用户名。如果你不确定当前的用户名，可以使用<code>get_full_repo_name()</code> 函数来查看：",jl,Jl,sl,kl,fl,H,Hl="然后我们可以在本地文件夹中克隆该存储库。如果已经存在一个同名的文件夹，这个本地文件夹应该是我们正在使用的存储库克隆到本地的版本：",yl,rl,bl,S,Zl="现在，我们可以通过调用 <code>repo.push_to_hub()</code> 方法上传我们在 <code>output_dir</code> 中保存的所有文件。这将帮助我们在每个 epoch 结束时上传中间模型。",tl,_l,hl,D,Sl="我们现在准备编写完整的训练循环。为了简化其评估部分，我们定义了这个 <code>postprocess()</code> 函数用于接收预测值和参考翻译对于的标签值，并将其转换为 <code>metric</code> 对象所需要的字符串列表。：",Ul,il,Cl,Y,Pl='训练循环看起来和本章 <a href="/course/chapter7/2">第 2 节</a> 与 <a href="/course/chapter3">第三章</a> 中代码很相似，只是在评估部分有一些不同 —— 所以让我们重点关注一下这一点！',$l,q,Dl="首先要注意的是，我们使用 <code>generate()</code> 方法来计算预测，但这是我们基础模型上的一个方法，而不是🤗 Accelerate 在 <code>prepare()</code> 方法中创建的封装模型。这就是为什么我们首先 <code>unwrap_model</code> ，然后调用此方法。",Tl,cl,xl="首先要注意的是，我们用来计算预测的 <code>generate()</code> 函数是基础模型上的一个方法，而不是🤗 Accelerate 在 <code>prepare()</code> 函数中创建的封装模型。这就是在调用此函数之前先调用<code>unwrap_model</code>，的原因。",nl,ul,dl='第二个要注意的是，就像 <a href="https://chat.openai.com/course/chapter7/2" rel="nofollow">token 分类</a> 一样，在训练和评估这两个过程可能以不同的形状对输入和标签进行了填充，所以我们在调用 <code>gather()</code> 函数之前使用 <code>accelerator.pad_across_processes()</code> 方法，使预测和标签具有相同的形状。如果我们不这么做，那么评估的过程将会出错或被永远挂起。',i,I,Vl,Gl,Rl,Bl,ql='训练完成之后，你就有了一个模型，最终的 BLEU 分数应该与 <code>Seq2SeqTrainer</code> 训练的模型非常相似。你可以在 <a href="https://huggingface.co/huggingface-course/marian-finetuned-kde4-en-to-fr-accelerate" rel="nofollow">huggingface-course/marian-finetuned-kde4-en-to-fr-accelerate</a> 上查看我们使用此代码训练的模型。如果你想测试对训练循环的任何调整，你可以直接通过编辑上面的代码来实现！',ne;return p=new gl({props:{title:"自定义训练循环",local:"自定义训练循环",headingTag:"h2"}}),f=new gl({props:{title:"准备训练所需的一切",local:"准备训练所需的一切",headingTag:"h3"}}),C=new h({props:{code:"ZnJvbSUyMHRvcmNoLnV0aWxzLmRhdGElMjBpbXBvcnQlMjBEYXRhTG9hZGVyJTBBJTBBdG9rZW5pemVkX2RhdGFzZXRzLnNldF9mb3JtYXQoJTIydG9yY2glMjIpJTBBdHJhaW5fZGF0YWxvYWRlciUyMCUzRCUyMERhdGFMb2FkZXIoJTBBJTIwJTIwJTIwJTIwdG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBzaHVmZmxlJTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMGNvbGxhdGVfZm4lM0RkYXRhX2NvbGxhdG9yJTJDJTBBJTIwJTIwJTIwJTIwYmF0Y2hfc2l6ZSUzRDglMkMlMEEpJTBBZXZhbF9kYXRhbG9hZGVyJTIwJTNEJTIwRGF0YUxvYWRlciglMEElMjAlMjAlMjAlMjB0b2tlbml6ZWRfZGF0YXNldHMlNUIlMjJ2YWxpZGF0aW9uJTIyJTVEJTJDJTIwY29sbGF0ZV9mbiUzRGRhdGFfY29sbGF0b3IlMkMlMjBiYXRjaF9zaXplJTNEOCUwQSk=",highlighted:`<span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader

tokenized_datasets.set_format(<span class="hljs-string">&quot;torch&quot;</span>)
train_dataloader = DataLoader(
    tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>],
    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}}),X=new h({props:{code:"bW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JTZXEyU2VxTE0uZnJvbV9wcmV0cmFpbmVkKG1vZGVsX2NoZWNrcG9pbnQp",highlighted:"model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)",wrap:!1}}),E=new h({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}}),z=new h({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUwQWFjY2VsZXJhdG9yJTIwJTNEJTIwQWNjZWxlcmF0b3IoKSUwQW1vZGVsJTJDJTIwb3B0aW1pemVyJTJDJTIwdHJhaW5fZGF0YWxvYWRlciUyQyUyMGV2YWxfZGF0YWxvYWRlciUyMCUzRCUyMGFjY2VsZXJhdG9yLnByZXBhcmUoJTBBJTIwJTIwJTIwJTIwbW9kZWwlMkMlMjBvcHRpbWl6ZXIlMkMlMjB0cmFpbl9kYXRhbG9hZGVyJTJDJTIwZXZhbF9kYXRhbG9hZGVyJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator

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

num_train_epochs = <span class="hljs-number">3</span>
num_update_steps_per_epoch = <span class="hljs-built_in">len</span>(train_dataloader)
num_training_steps = num_train_epochs * num_update_steps_per_epoch

lr_scheduler = get_scheduler(
    <span class="hljs-string">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">0</span>,
    num_training_steps=num_training_steps,
)`,wrap:!1}}),Jl=new h({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMFJlcG9zaXRvcnklMkMlMjBnZXRfZnVsbF9yZXBvX25hbWUlMEElMEFtb2RlbF9uYW1lJTIwJTNEJTIwJTIybWFyaWFuLWZpbmV0dW5lZC1rZGU0LWVuLXRvLWZyLWFjY2VsZXJhdGUlMjIlMEFyZXBvX25hbWUlMjAlM0QlMjBnZXRfZnVsbF9yZXBvX25hbWUobW9kZWxfbmFtZSklMEFyZXBvX25hbWU=",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;marian-finetuned-kde4-en-to-fr-accelerate&quot;</span>
repo_name = get_full_repo_name(model_name)
repo_name`,wrap:!1}}),kl=new h({props:{code:"J3NndWdnZXIlMkZtYXJpYW4tZmluZXR1bmVkLWtkZTQtZW4tdG8tZnItYWNjZWxlcmF0ZSc=",highlighted:'<span class="hljs-string">&#x27;sgugger/marian-finetuned-kde4-en-to-fr-accelerate&#x27;</span>',wrap:!1}}),rl=new h({props:{code:"b3V0cHV0X2RpciUyMCUzRCUyMCUyMm1hcmlhbi1maW5ldHVuZWQta2RlNC1lbi10by1mci1hY2NlbGVyYXRlJTIyJTBBcmVwbyUyMCUzRCUyMFJlcG9zaXRvcnkob3V0cHV0X2RpciUyQyUyMGNsb25lX2Zyb20lM0RyZXBvX25hbWUp",highlighted:`output_dir = <span class="hljs-string">&quot;marian-finetuned-kde4-en-to-fr-accelerate&quot;</span>
repo = Repository(output_dir, clone_from=repo_name)`,wrap:!1}}),_l=new gl({props:{title:"训练循环",local:"训练循环",headingTag:"h3"}}),il=new h({props:{code:"ZGVmJTIwcG9zdHByb2Nlc3MocHJlZGljdGlvbnMlMkMlMjBsYWJlbHMpJTNBJTBBJTIwJTIwJTIwJTIwcHJlZGljdGlvbnMlMjAlM0QlMjBwcmVkaWN0aW9ucy5jcHUoKS5udW1weSgpJTBBJTIwJTIwJTIwJTIwbGFiZWxzJTIwJTNEJTIwbGFiZWxzLmNwdSgpLm51bXB5KCklMEElMEElMjAlMjAlMjAlMjBkZWNvZGVkX3ByZWRzJTIwJTNEJTIwdG9rZW5pemVyLmJhdGNoX2RlY29kZShwcmVkaWN0aW9ucyUyQyUyMHNraXBfc3BlY2lhbF90b2tlbnMlM0RUcnVlKSUwQSUwQSUyMCUyMCUyMCUyMCUyMyUyMCVFNiU5QiVCRiVFNiU4RCVBMiVFNiVBMCU4NyVFNyVBRCVCRSVFNCVCOCVBRCVFNyU5QSU4NCUyMC0xMDAlMkMlRTUlOUIlQTAlRTQlQjglQkElRTYlODglOTElRTQlQkIlQUMlRTYlOTclQTAlRTYlQjMlOTUlRTglQTclQTMlRTclQTAlODElRTUlQUUlODMlRTQlQkIlQUMlRTMlODAlODIlMEElMjAlMjAlMjAlMjBsYWJlbHMlMjAlM0QlMjBucC53aGVyZShsYWJlbHMlMjAhJTNEJTIwLTEwMCUyQyUyMGxhYmVscyUyQyUyMHRva2VuaXplci5wYWRfdG9rZW5faWQpJTBBJTIwJTIwJTIwJTIwZGVjb2RlZF9sYWJlbHMlMjAlM0QlMjB0b2tlbml6ZXIuYmF0Y2hfZGVjb2RlKGxhYmVscyUyQyUyMHNraXBfc3BlY2lhbF90b2tlbnMlM0RUcnVlKSUwQSUwQSUyMCUyMCUyMCUyMCUyMyUyMCVFNCVCOCU4MCVFNCVCQSU5QiVFNyVBRSU4MCVFNSU4RCU5NSVFNyU5QSU4NCVFNSU5MCU4RSVFNSVBNCU4NCVFNyU5MCU4NiUwQSUyMCUyMCUyMCUyMGRlY29kZWRfcHJlZHMlMjAlM0QlMjAlNUJwcmVkLnN0cmlwKCklMjBmb3IlMjBwcmVkJTIwaW4lMjBkZWNvZGVkX3ByZWRzJTVEJTBBJTIwJTIwJTIwJTIwZGVjb2RlZF9sYWJlbHMlMjAlM0QlMjAlNUIlNUJsYWJlbC5zdHJpcCgpJTVEJTIwZm9yJTIwbGFiZWwlMjBpbiUyMGRlY29kZWRfbGFiZWxzJTVEJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwZGVjb2RlZF9wcmVkcyUyQyUyMGRlY29kZWRfbGFiZWxz",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">postprocess</span>(<span class="hljs-params">predictions, labels</span>):
    predictions = predictions.cpu().numpy()
    labels = labels.cpu().numpy()

    decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=<span class="hljs-literal">True</span>)

    <span class="hljs-comment"># 替换标签中的 -100,因为我们无法解码它们。</span>
    labels = np.where(labels != -<span class="hljs-number">100</span>, labels, tokenizer.pad_token_id)
    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>)

    <span class="hljs-comment"># 一些简单的后处理</span>
    decoded_preds = [pred.strip() <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> decoded_preds]
    decoded_labels = [[label.strip()] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> decoded_labels]
    <span class="hljs-keyword">return</span> decoded_preds, decoded_labels`,wrap:!1}}),I=new h({props:{code:"ZnJvbSUyMHRxZG0uYXV0byUyMGltcG9ydCUyMHRxZG0lMEFpbXBvcnQlMjB0b3JjaCUwQSUwQXByb2dyZXNzX2JhciUyMCUzRCUyMHRxZG0ocmFuZ2UobnVtX3RyYWluaW5nX3N0ZXBzKSklMEElMEFmb3IlMjBlcG9jaCUyMGluJTIwcmFuZ2UobnVtX3RyYWluX2Vwb2NocyklM0ElMEElMjAlMjAlMjAlMjAlMjMlMjAlRTglQUUlQUQlRTclQkIlODMlMEElMjAlMjAlMjAlMjBtb2RlbC50cmFpbigpJTBBJTIwJTIwJTIwJTIwZm9yJTIwYmF0Y2glMjBpbiUyMHRyYWluX2RhdGFsb2FkZXIlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvdXRwdXRzJTIwJTNEJTIwbW9kZWwoKipiYXRjaCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwb3V0cHV0cy5sb3NzJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IuYmFja3dhcmQobG9zcyklMEElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuc3RlcCgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbHJfc2NoZWR1bGVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG9wdGltaXplci56ZXJvX2dyYWQoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByb2dyZXNzX2Jhci51cGRhdGUoMSklMEElMEElMjAlMjAlMjAlMjAlMjMlMjAlRTglQUYlODQlRTQlQkMlQjAlMEElMjAlMjAlMjAlMjBtb2RlbC5ldmFsKCklMEElMjAlMjAlMjAlMjBmb3IlMjBiYXRjaCUyMGluJTIwdHFkbShldmFsX2RhdGFsb2FkZXIpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwd2l0aCUyMHRvcmNoLm5vX2dyYWQoKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGdlbmVyYXRlZF90b2tlbnMlMjAlM0QlMjBhY2NlbGVyYXRvci51bndyYXBfbW9kZWwobW9kZWwpLmdlbmVyYXRlKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGJhdGNoJTVCJTIyaW5wdXRfaWRzJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYXR0ZW50aW9uX21hc2slM0RiYXRjaCU1QiUyMmF0dGVudGlvbl9tYXNrJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbWF4X2xlbmd0aCUzRDEyOCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsYWJlbHMlMjAlM0QlMjBiYXRjaCU1QiUyMmxhYmVscyUyMiU1RCUwQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMyUyMCVFOSU5QyU4MCVFOCVBNiU4MSVFNSVBMSVBQiVFNSU4NSU4NSVFOSVBMiU4NCVFNiVCNSU4QiVFNSU5MiU4QyVFNiVBMCU4NyVFNyVBRCVCRSVFNiU4OSU4RCVFOCU4MyVCRCVFOCVCMCU4MyVFNyU5NCVBOGdhdGhlcigpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZ2VuZXJhdGVkX3Rva2VucyUyMCUzRCUyMGFjY2VsZXJhdG9yLnBhZF9hY3Jvc3NfcHJvY2Vzc2VzKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGdlbmVyYXRlZF90b2tlbnMlMkMlMjBkaW0lM0QxJTJDJTIwcGFkX2luZGV4JTNEdG9rZW5pemVyLnBhZF90b2tlbl9pZCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsYWJlbHMlMjAlM0QlMjBhY2NlbGVyYXRvci5wYWRfYWNyb3NzX3Byb2Nlc3NlcyhsYWJlbHMlMkMlMjBkaW0lM0QxJTJDJTIwcGFkX2luZGV4JTNELTEwMCklMEElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcmVkaWN0aW9uc19nYXRoZXJlZCUyMCUzRCUyMGFjY2VsZXJhdG9yLmdhdGhlcihnZW5lcmF0ZWRfdG9rZW5zKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxhYmVsc19nYXRoZXJlZCUyMCUzRCUyMGFjY2VsZXJhdG9yLmdhdGhlcihsYWJlbHMpJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZGVjb2RlZF9wcmVkcyUyQyUyMGRlY29kZWRfbGFiZWxzJTIwJTNEJTIwcG9zdHByb2Nlc3MocHJlZGljdGlvbnNfZ2F0aGVyZWQlMkMlMjBsYWJlbHNfZ2F0aGVyZWQpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbWV0cmljLmFkZF9iYXRjaChwcmVkaWN0aW9ucyUzRGRlY29kZWRfcHJlZHMlMkMlMjByZWZlcmVuY2VzJTNEZGVjb2RlZF9sYWJlbHMpJTBBJTBBJTIwJTIwJTIwJTIwcmVzdWx0cyUyMCUzRCUyMG1ldHJpYy5jb21wdXRlKCklMEElMjAlMjAlMjAlMjBwcmludChmJTIyZXBvY2glMjAlN0JlcG9jaCU3RCUyQyUyMEJMRVUlMjBzY29yZSUzQSUyMCU3QnJlc3VsdHMlNUInc2NvcmUnJTVEJTNBLjJmJTdEJTIyKSUwQSUwQSUyMCUyMCUyMCUyMCUyMyUyMCVFNCVCRiU5RCVFNSVBRCU5OCVFNSU5MiU4QyVFNCVCOCU4QSVFNCVCQyVBMCUwQSUyMCUyMCUyMCUyMGFjY2VsZXJhdG9yLndhaXRfZm9yX2V2ZXJ5b25lKCklMEElMjAlMjAlMjAlMjB1bndyYXBwZWRfbW9kZWwlMjAlM0QlMjBhY2NlbGVyYXRvci51bndyYXBfbW9kZWwobW9kZWwpJTBBJTIwJTIwJTIwJTIwdW53cmFwcGVkX21vZGVsLnNhdmVfcHJldHJhaW5lZChvdXRwdXRfZGlyJTJDJTIwc2F2ZV9mdW5jdGlvbiUzRGFjY2VsZXJhdG9yLnNhdmUpJTBBJTIwJTIwJTIwJTIwaWYlMjBhY2NlbGVyYXRvci5pc19tYWluX3Byb2Nlc3MlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB0b2tlbml6ZXIuc2F2ZV9wcmV0cmFpbmVkKG91dHB1dF9kaXIpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmVwby5wdXNoX3RvX2h1YiglMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjb21taXRfbWVzc2FnZSUzRGYlMjJUcmFpbmluZyUyMGluJTIwcHJvZ3Jlc3MlMjBlcG9jaCUyMCU3QmVwb2NoJTdEJTIyJTJDJTIwYmxvY2tpbmclM0RGYWxzZSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCk=",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> tqdm(eval_dataloader):
        <span class="hljs-keyword">with</span> torch.no_grad():
            generated_tokens = accelerator.unwrap_model(model).generate(
                batch[<span class="hljs-string">&quot;input_ids&quot;</span>],
                attention_mask=batch[<span class="hljs-string">&quot;attention_mask&quot;</span>],
                max_length=<span class="hljs-number">128</span>,
            )
        labels = batch[<span class="hljs-string">&quot;labels&quot;</span>]

        <span class="hljs-comment"># 需要填充预测和标签才能调用gather()</span>
        generated_tokens = accelerator.pad_across_processes(
            generated_tokens, dim=<span class="hljs-number">1</span>, pad_index=tokenizer.pad_token_id
        )
        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(generated_tokens)
        labels_gathered = accelerator.gather(labels)

        decoded_preds, decoded_labels = postprocess(predictions_gathered, labels_gathered)
        metric.add_batch(predictions=decoded_preds, references=decoded_labels)

    results = metric.compute()
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;epoch <span class="hljs-subst">{epoch}</span>, BLEU score: <span class="hljs-subst">{results[<span class="hljs-string">&#x27;score&#x27;</span>]:<span class="hljs-number">.2</span>f}</span>&quot;</span>)

    <span class="hljs-comment"># 保存和上传</span>
    accelerator.wait_for_everyone()
    unwrapped_model = accelerator.unwrap_model(model)
    unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save)
    <span class="hljs-keyword">if</span> accelerator.is_main_process:
        tokenizer.save_pretrained(output_dir)
        repo.push_to_hub(
            commit_message=<span class="hljs-string">f&quot;Training in progress epoch <span class="hljs-subst">{epoch}</span>&quot;</span>, blocking=<span class="hljs-literal">False</span>
        )`,wrap:!1}}),Gl=new h({props:{code:"ZXBvY2glMjAwJTJDJTIwQkxFVSUyMHNjb3JlJTNBJTIwNTMuNDclMEFlcG9jaCUyMDElMkMlMjBCTEVVJTIwc2NvcmUlM0ElMjA1NC4yNCUwQWVwb2NoJTIwMiUyQyUyMEJMRVUlMjBzY29yZSUzQSUyMDU0LjQ0",highlighted:`epoch <span class="hljs-number">0</span>, BLEU score: <span class="hljs-number">53.47</span>
epoch <span class="hljs-number">1</span>, BLEU score: <span class="hljs-number">54.24</span>
epoch <span class="hljs-number">2</span>, BLEU score: <span class="hljs-number">54.44</span>`,wrap:!1}}),{c(){T(p.$$.fragment),b=n(),r=y("p"),r.innerHTML=w,g=n(),T(f.$$.fragment),Z=n(),V=y("p"),V.innerHTML=_,k=n(),T(C.$$.fragment),R=n(),Q=y("p"),Q.textContent=pl,K=n(),T(X.$$.fragment),A=n(),P=y("p"),P.textContent=ml,W=n(),T(E.$$.fragment),v=n(),O=y("p"),O.innerHTML=ol,F=n(),T(z.$$.fragment),L=n(),N=y("p"),N.innerHTML=Ml,ll=n(),T(B.$$.fragment),el=n(),d=y("p"),d.innerHTML=G,jl=n(),T(Jl.$$.fragment),sl=n(),T(kl.$$.fragment),fl=n(),H=y("p"),H.textContent=Hl,yl=n(),T(rl.$$.fragment),bl=n(),S=y("p"),S.innerHTML=Zl,tl=n(),T(_l.$$.fragment),hl=n(),D=y("p"),D.innerHTML=Sl,Ul=n(),T(il.$$.fragment),Cl=n(),Y=y("p"),Y.innerHTML=Pl,$l=n(),q=y("p"),q.innerHTML=Dl,Tl=n(),cl=y("p"),cl.innerHTML=xl,nl=n(),ul=y("p"),ul.innerHTML=dl,i=n(),T(I.$$.fragment),Vl=n(),T(Gl.$$.fragment),Rl=n(),Bl=y("p"),Bl.innerHTML=ql},l(M){m(p.$$.fragment,M),b=a(M),r=U(M,"P",{"data-svelte-h":!0}),u(r)!=="svelte-16nlo74"&&(r.innerHTML=w),g=a(M),m(f.$$.fragment,M),Z=a(M),V=U(M,"P",{"data-svelte-h":!0}),u(V)!=="svelte-gj0900"&&(V.innerHTML=_),k=a(M),m(C.$$.fragment,M),R=a(M),Q=U(M,"P",{"data-svelte-h":!0}),u(Q)!=="svelte-3cw9vf"&&(Q.textContent=pl),K=a(M),m(X.$$.fragment,M),A=a(M),P=U(M,"P",{"data-svelte-h":!0}),u(P)!=="svelte-boavug"&&(P.textContent=ml),W=a(M),m(E.$$.fragment,M),v=a(M),O=U(M,"P",{"data-svelte-h":!0}),u(O)!=="svelte-hnego"&&(O.innerHTML=ol),F=a(M),m(z.$$.fragment,M),L=a(M),N=U(M,"P",{"data-svelte-h":!0}),u(N)!=="svelte-19v4qb8"&&(N.innerHTML=Ml),ll=a(M),m(B.$$.fragment,M),el=a(M),d=U(M,"P",{"data-svelte-h":!0}),u(d)!=="svelte-320na4"&&(d.innerHTML=G),jl=a(M),m(Jl.$$.fragment,M),sl=a(M),m(kl.$$.fragment,M),fl=a(M),H=U(M,"P",{"data-svelte-h":!0}),u(H)!=="svelte-9w674v"&&(H.textContent=Hl),yl=a(M),m(rl.$$.fragment,M),bl=a(M),S=U(M,"P",{"data-svelte-h":!0}),u(S)!=="svelte-1ka4pss"&&(S.innerHTML=Zl),tl=a(M),m(_l.$$.fragment,M),hl=a(M),D=U(M,"P",{"data-svelte-h":!0}),u(D)!=="svelte-1m015ho"&&(D.innerHTML=Sl),Ul=a(M),m(il.$$.fragment,M),Cl=a(M),Y=U(M,"P",{"data-svelte-h":!0}),u(Y)!=="svelte-1s9jf3x"&&(Y.innerHTML=Pl),$l=a(M),q=U(M,"P",{"data-svelte-h":!0}),u(q)!=="svelte-1b6hsb7"&&(q.innerHTML=Dl),Tl=a(M),cl=U(M,"P",{"data-svelte-h":!0}),u(cl)!=="svelte-1025olm"&&(cl.innerHTML=xl),nl=a(M),ul=U(M,"P",{"data-svelte-h":!0}),u(ul)!=="svelte-3nape9"&&(ul.innerHTML=dl),i=a(M),m(I.$$.fragment,M),Vl=a(M),m(Gl.$$.fragment,M),Rl=a(M),Bl=U(M,"P",{"data-svelte-h":!0}),u(Bl)!=="svelte-n3cu4c"&&(Bl.innerHTML=ql)},m(M,$){o(p,M,$),s(M,b,$),s(M,r,$),s(M,g,$),o(f,M,$),s(M,Z,$),s(M,V,$),s(M,k,$),o(C,M,$),s(M,R,$),s(M,Q,$),s(M,K,$),o(X,M,$),s(M,A,$),s(M,P,$),s(M,W,$),o(E,M,$),s(M,v,$),s(M,O,$),s(M,F,$),o(z,M,$),s(M,L,$),s(M,N,$),s(M,ll,$),o(B,M,$),s(M,el,$),s(M,d,$),s(M,jl,$),o(Jl,M,$),s(M,sl,$),o(kl,M,$),s(M,fl,$),s(M,H,$),s(M,yl,$),o(rl,M,$),s(M,bl,$),s(M,S,$),s(M,tl,$),o(_l,M,$),s(M,hl,$),s(M,D,$),s(M,Ul,$),o(il,M,$),s(M,Cl,$),s(M,Y,$),s(M,$l,$),s(M,q,$),s(M,Tl,$),s(M,cl,$),s(M,nl,$),s(M,ul,$),s(M,i,$),o(I,M,$),s(M,Vl,$),o(Gl,M,$),s(M,Rl,$),s(M,Bl,$),ne=!0},i(M){ne||(c(p.$$.fragment,M),c(f.$$.fragment,M),c(C.$$.fragment,M),c(X.$$.fragment,M),c(E.$$.fragment,M),c(z.$$.fragment,M),c(B.$$.fragment,M),c(Jl.$$.fragment,M),c(kl.$$.fragment,M),c(rl.$$.fragment,M),c(_l.$$.fragment,M),c(il.$$.fragment,M),c(I.$$.fragment,M),c(Gl.$$.fragment,M),ne=!0)},o(M){J(p.$$.fragment,M),J(f.$$.fragment,M),J(C.$$.fragment,M),J(X.$$.fragment,M),J(E.$$.fragment,M),J(z.$$.fragment,M),J(B.$$.fragment,M),J(Jl.$$.fragment,M),J(kl.$$.fragment,M),J(rl.$$.fragment,M),J(_l.$$.fragment,M),J(il.$$.fragment,M),J(I.$$.fragment,M),J(Gl.$$.fragment,M),ne=!1},d(M){M&&(e(b),e(r),e(g),e(Z),e(V),e(k),e(R),e(Q),e(K),e(A),e(P),e(W),e(v),e(O),e(F),e(L),e(N),e(ll),e(el),e(d),e(jl),e(sl),e(fl),e(H),e(yl),e(bl),e(S),e(tl),e(hl),e(D),e(Ul),e(Cl),e(Y),e($l),e(q),e(Tl),e(cl),e(nl),e(ul),e(i),e(Vl),e(Rl),e(Bl)),j(p,M),j(f,M),j(C,M),j(X,M),j(E,M),j(z,M),j(B,M),j(Jl,M),j(kl,M),j(rl,M),j(_l,M),j(il,M),j(I,M),j(Gl,M)}}}function jp(x){let p,b="✏️ <strong>轮到你了！</strong> 把之前找到的包含单词“email”样本输入模型，会返回什么结果？";return{c(){p=y("p"),p.innerHTML=b},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-10t97dq"&&(p.innerHTML=b)},m(r,w){s(r,p,w)},p:Ll,d(r){r&&e(p)}}}function up(x){let p,b,r,w,g,f,Z,V,_,k,C,R,Q,pl,K='现在让我们深入研究翻译。这是另一个 <a href="/course/chapter1/7">sequence-to-sequence 任务</a> ，着这是一个可以表述为输入是一个序列输出另一个序列的问题。从这个意义上说，这个问题非常类似 <a href="/course/chapter7/6">文本摘要</a> ，并且你可以将我们将在此处学习到的一些技巧迁移到其他的序列到序列问题，例如：',X,A,P="<li><strong>风格迁移</strong> 创建一个模型将某种风格迁移到一段文本（例如，正式的风格迁移到休闲的风格，或从莎士比亚英语迁移到现代英语）。</li> <li><strong>生成问题的回答</strong> 创建一个模型，在给定上下文的情况下生成问题的答案。</li>",ml,W,E,v,O='如果你有足够大的两种（或更多）语言的文本语料库，你可以从头开始训练一个新的翻译模型，就像我们在 <a href="/course/chapter7/6">因果语言建模</a> 部分中所做的那样。然而，微调现有的翻译模型会更快，无论是从像 mT5 或 mBART 这样的多语言模型微调到特定的语言对，还是从特定语料库的一种语言到另一种语言的专用翻译模型。',ol,F,z='在这一节中，我们将在 <a href="https://huggingface.co/datasets/kde4" rel="nofollow">KDE4 数据集</a> 上微调一个预训练的 Marian 模型，用来把英语翻译成法语的（因为很多 Hugging Face 的员工都会说这两种语言）。KDE4 数据集是一个 <a href="https://apps.kde.org/" rel="nofollow">KDE 应用</a> 本地化的数据集。我们将使用的模型已经在从 <a href="https://opus.nlpl.eu/" rel="nofollow">Opus 数据集</a> （实际上包含 KDE4 数据集）中提取的法语和英语文本的大型语料库上进行了预先训练。不过，即使我们使用的预训练模型在其预训练期间使用了这部分数据集，我们也会看到，经过微调后，我们可以得到一个更好的版本。',L,N,Ml="完成后，我们将拥有一个模型，可以进行这样的翻译：",ll,B,el,d,G,jl='<img class="block dark:hidden lg:w-3/5" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/modeleval-marian-finetuned-kde4-en-to-fr.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/modeleval-marian-finetuned-kde4-en-to-fr-dark.png" alt="One-hot encoded labels for question answering."/>',Jl,sl,kl='与前面几节一样，你可以使用以下代码找到我们将训练并上传到 Hub 的实际模型，并 <a href="https://huggingface.co/huggingface-course/marian-finetuned-kde4-en-to-fr?text=This+plugin+allows+you+to+automatically+translate+web+pages+between+several+languages." rel="nofollow">在这里</a> 查看模型输出的结果。',fl,H,Hl,yl,rl='为了从头开始微调或训练翻译模型，我们需要一个适合该任务的数据集。如前所述，我们将使用 <a href="https://huggingface.co/datasets/kde4" rel="nofollow">KDE4 数据集</a> 。只要数据集中有互译的两种语言的句子对，就可以很容易地调整本节的代码以使用自己的数据集进行微调。如果你需要复习如何将自定义数据加载到 <code>Dataset</code> ，可以复习一下 <a href="/course/chapter5">第五章</a> 。',bl,S,Zl,tl,_l="像往常一样，我们使用 <code>load_dataset()</code> 函数下载数据集：",hl,D,Sl,Ul,il='如果你想使用其他的语言对，你可以使用语言代码来设置你想使用的语言对。该数据集共有 92 种语言可用；你可以通过展开 <a href="https://huggingface.co/datasets/kde4" rel="nofollow">数据集卡片</a> 上的语言标签来查看数据集支持的语言标签。',Cl,Y,Pl,$l,q,Dl="我们来看看数据集：",Tl,cl,xl,nl,ul,dl,i='我们下载的数据集有 210,173 对句子，在一次训练过程中，除了训练集，我们也需要创建自己的验证集。正如我们在 <a href="/course/chapter5">第五章</a> 学的的那样， <code>Dataset</code> 有一个 <code>train_test_split()</code> 方法可以帮助我们。我们将设置一个固定的随机数种子以保证结果可以复现：',I,Vl,Gl,Rl,Bl,ql,ne="我们可以像下面这样将 <code>test</code> 键重命名为 <code>validation</code>：",M,$,qs,ae,An="现在让我们看一下数据集的一个元素：",Ls,pe,Ps,Me,Os,re,Wn="我们得到一个包含我们选择的两种语言的两个句子的字典。这个充满技术计算机科学术语的数据集的一个特殊之处在于它们都完全用法语翻译。然而现实中，法国工程师在交谈时，大多数计算机科学专用词汇都用英语表述。例如，“threads”这个词很可能出现在法语句子中，尤其是在技术对话中；但在这个数据集中，它被翻译成更准确的“fils de Discussion”。我们使用的预训练模型已经在一个更大的法语和英语句子语料库上进行了预训练，所以输出的是原始的英语表达：",Ks,ie,lt,ce,et,Je,En="这种情况的另一个例子可以在“plugin”这个词上看到，它并非正式的法语词汇，但大多数母语是法语的人都能够看懂并且不会去翻译它。不过，在 KDE4 数据集中，这个词被翻译成了更正式的法语词汇“module d’extension”：",st,ye,tt,Ue,nt,Te,vn="然而，我们的预训练模型坚持使用简练而熟悉的英文单词：",at,me,pt,oe,Mt,je,Fn="看看我们的微调模型是否能学习到数据集的这些特殊特性。（剧透警告：它能）。",rt,ue,it,Ol,ct,de,Jt,we,yt,fe,Yn='你现在应该可以预测我们的下一步该做些什么了：将所有文本转换为 token IDs 的集合，这样模型才可以理解它们。对于这个任务，我们需要同时对原始文本和翻译后的文本同时进行 tokenize。首先，我们需要创建 <code>tokenizer</code> 对象。如前所述，我们将使用 Marian 英语到法语的预训练模型。如果你使用下面的代码微调另一对语言，请记得更改下面代码中的 checkpoint。 <a href="https://huggingface.co/Helsinki-NLP" rel="nofollow">Helsinki-NLP</a> 组织提供了超过一千个多语言模型。',Ut,be,Tt,he,zn='你也可以将 <code>model_checkpoint</code> 替换为你从 <a href="https://huggingface.co/models" rel="nofollow">Hub</a> 中选择的其他模型，或者一个保存了预训练模型和 tokenizer 的本地文件夹。',mt,Kl,ot,Ce,Hn="我们的数据准备相当简单。只有一点要记住；你需要确保 tokenizer 处理的目标是输出语言（在这里是法语）。你可以通过将目标语言传递给 tokenizer 的 <code>__call__</code> 方法的 <code>text_targets</code> 参数来完成此操作。",jt,$e,Sn="为了演示设置的方法，让我们处理训练集中的一个样本：",ut,Ie,dt,ke,wt,_e,Dn="我们可以看到，输出包含了与英语句子的 <code>inputs IDs</code>，而与法语句子的 IDs 存储在 <code>labels</code> 字段中。如果你忘记设置 labels 的 <code>tokenizer</code>，默认情况下 <code>labels</code> 将由输入的 <code>tokenizer</code>（语言类型不一样） 进行 <code>tokenize</code>，对于 Marian 模型来说，效果不会很好。",ft,ge,bt,Ze,ht,xe,qn="如你所见，如果用英语的 tokenizer 来预处理法语句子，会产生更多的 tokens，因为这个 tokenizer 不认识任何法语单词（除了那些在英语里也出现的，比如“discussion”）。",Ct,Ve,Ln="最后一步是定义我们数据集的预处理函数：",$t,Ge,It,Re,Pn="请注意，上述代码也为输入和输出设置了相同的最大长度。由于要处理的文本看起来很短，因此在这里将最大长度设置为 128。",kt,le,_t,ee,gt,Be,On="我们现在可以一次性使用上述预处理处理数据集的所有数据。",Zt,Ne,xt,Qe,Kn="现在数据已经过预处理，我们准备好微调我们的预训练模型了！",Vt,Nl,Ql,Es,Xe,la="注意，这次我们使用的是一个已经在翻译任务上进行过训练的模型，实际上已经可以直接使用了，所以没有收到关于缺少权重或重新初始化的权重的警告。",Gt,Ae,Rt,We,ea='在这个任务中，我们需要一个数据整理器来动态批处理填充。因此，我们不能像 <a href="/course/chapter3">第三章</a> 那样直接使用 <code>DataCollatorWithPadding</code> ，因为它只填充输入的部分（inputs ID、注意掩码和 token 类型 ID）。我们的标签也应该被填充到所有标签中最大的长度。而且，如前所述，用于填充标签的填充值应为 <code>-100</code> ，而不是 tokenizer 默认的的填充 token，这样才可以在确保在损失计算中忽略这些填充值。',Bt,Ee,sa='上述的这些需求都可以由 <a href="https://huggingface.co/transformers/main_classes/data_collator.html#datacollatorforseq2seq" rel="nofollow"><code>DataCollatorForSeq2Seq</code></a> 完成。它与 <code>DataCollatorWithPadding</code> 一样，它接收用于预处理输入的 <code>tokenizer</code> ，同时它也接收一个 <code>model</code> 参数。这是因为数据整理器还将负责准备解码器 <code>inputs ID</code>，它们是标签偏移之后形成的，开头带有特殊 <code>token</code> 。由于对于不同的模型架构有稍微不同的偏移方式，所以 <code>DataCollatorForSeq2Seq</code> 还需要接收 <code>model</code> 对象：',Nt,Xl,Al,vs,ve,ta="为了在几个样本上进行测试，我们在已经完成 tokenize 的训练集中的部分数据上调用它，测试一下其功能：",Qt,Fe,Xt,Ye,At,ze,na="我们可以检查我们的标签是否已经用 <code>-100</code> 填充到 batch 的最大长度：",Wt,He,Et,Se,vt,De,aa="我们还可以查看解码器的 inputs ID，可以看到它们是标签经过偏移后的结果：",Ft,qe,Yt,Le,zt,Pe,pa="以下是我们数据集中第一个和第二个元素的标签：",Ht,Oe,St,Ke,Dt,Wl,El,Fs,ls,qt,es,Lt,Ys,ss,Ma='用于翻译的传统指标是 <a href="https://en.wikipedia.org/wiki/BLEU" rel="nofollow">BLEU 分数</a> ，它最初由 Kishore Papineni 等人在 2002 年的 <a href="https://aclanthology.org/P02-1040.pdf" rel="nofollow">一篇文章</a> 中引入。BLEU 分数评估翻译与参考翻译的接近程度。它不衡量模型生成输出的可理解性或语法正确性，而是使用统计规则来确保生成输出中的所有单词也出现在参考的输出中。此外，还有一些规则对重复的词进行惩罚，如果这些词在输出中重复出现（模型输出像“the the the the the”这样的句子）；或者输出的句子长度比目标中的短（模型输出像“the”这样的句子）都会被惩罚。',Pt,ts,ra='BLEU 的一个缺点是的输入是已分词的文本，这使得比较使用不同分词器的模型之间的分数变得困难。因此，当今用于评估翻译模型的最常用指标是 <a href="https://github.com/mjpost/sacrebleu" rel="nofollow">SacreBLEU</a> ，它通过标准化的分词步骤解决了这个缺点（和其他的一些缺点）。要使用此指标，我们首先需要安装 SacreBLEU 库：',Ot,ns,Kt,as,ia='然后我们可以就像在 <a href="/course/chapter3">第三章</a> 那样通过 <code>evaluate.load()</code> 加载它',ln,ps,en,Ms,ca="SacreBLEU 指标中待评估的预测和参考的目标译文输入的格式都是文本。它的设计是为了支持多个参考翻译，因为同一句话通常有多种可接受的翻译——虽然我们使用的数据集只提供一个，但在 NLP 中找到将多个句子作为标签的数据集是很常见的。因此，预测结果应该是一个句子列表，而参考应该是一个句子列表的列表。",sn,rs,Ja="让我们尝试一个例子：",tn,is,nn,cs,an,Js,ya='达到了 46.75 的 BLEU 分数，这是相当不错的——作为参考，原始 Transformer 模型在 <a href="https://arxiv.org/pdf/1706.03762.pdf" rel="nofollow">“Attention Is All You Need” 论文</a> 类似的英语和法语翻译任务中获得了 41.8 的 BLEU 分数！（关于其他指标的含义，例如 <code>counts</code> 和 <code>bp</code> ，可以参见 <a href="https://github.com/mjpost/sacrebleu/blob/078c440168c6adc89ba75fe6d63f0d922d42bcfe/sacrebleu/metrics/bleu.py#L74" rel="nofollow">SacreBLEU仓库</a> ）另一方面，如果我们尝试将翻译模型中经常出现的两种糟糕的预测类型（大量重复或太短）输入给指标计算的函数，我们将得到相当糟糕的 BLEU 分数：',pn,ys,Mn,Us,rn,Ts,cn,ms,Jn,os,Ua="分数可以从 0 到 100，越高越好。",yn,vl,Fl,zs,js,Ta="现在这已经完成了，我们已经准备好微调我们的模型了！",Un,us,Tn,ds,ma="第一步是登录 Hugging Face，这样你就可以在训练过程中将结果上传到 Hub中。有一个方便的功能可以帮助你在 notebook 中完成登陆：",mn,ws,on,fs,oa="这将显示一个小部件，你可以在其中输入你的 Hugging Face 登录凭据。",jn,bs,ja="如果你不是在 notebook 上运行代码，可以在终端中输入以下命令：",un,hs,dn,Yl,zl,Hs,Ss,Cs,wn,$s,ua="我们已经向你展示了如何在模型 Hub 上使用我们微调的模型。要在本地的 <code>pipeline</code> 中使用它，我们只需要指定正确的模型标识符：",fn,Is,bn,ks,hn,_s,da="和预想的一样，我们的预训练模型适应了我们微调它的语料库，没有保留英语单词“threads”，而是将它翻译成官方的法语版本。对于“plugin”也是如此：",Cn,gs,$n,Zs,In,xs,wa="这是另一个领域适应的好例子！",kn,se,_n,Vs,gn,Ds,Zn;g=new Pa({props:{fw:x[0]}}),Z=new qa({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),_=new gl({props:{title:"翻译",local:"翻译",headingTag:"h1"}});const fa=[Ka,Oa],Gs=[];function ba(l,t){return l[0]==="pt"?0:1}C=ba(x),R=Gs[C]=fa[C](x),W=new xn({props:{id:"1JvfrvZgi6c"}}),H=new gl({props:{title:"准备数据",local:"准备数据",headingTag:"h2"}}),S=new gl({props:{title:"KDE4 数据集",local:"KDE4 数据集",headingTag:"h3"}}),D=new h({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBJTBBcmF3X2RhdGFzZXRzJTIwJTNEJTIwbG9hZF9kYXRhc2V0KCUyMmtkZTQlMjIlMkMlMjBsYW5nMSUzRCUyMmVuJTIyJTJDJTIwbGFuZzIlM0QlMjJmciUyMik=",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;kde4&quot;</span>, lang1=<span class="hljs-string">&quot;en&quot;</span>, lang2=<span class="hljs-string">&quot;fr&quot;</span>)`,wrap:!1}}),cl=new h({props:{code:"cmF3X2RhdGFzZXRz",highlighted:"raw_datasets",wrap:!1}}),nl=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2lkJyUyQyUyMCd0cmFuc2xhdGlvbiclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDIxMDE3MyUwQSUyMCUyMCUyMCUyMCU3RCklMEElN0Qp",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;id&#x27;</span>, <span class="hljs-string">&#x27;translation&#x27;</span>],
        num_rows: <span class="hljs-number">210173</span>
    })
})`,wrap:!1}}),Vl=new h({props:{code:"c3BsaXRfZGF0YXNldHMlMjAlM0QlMjByYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RC50cmFpbl90ZXN0X3NwbGl0KHRyYWluX3NpemUlM0QwLjklMkMlMjBzZWVkJTNEMjApJTBBc3BsaXRfZGF0YXNldHM=",highlighted:`split_datasets = raw_datasets[<span class="hljs-string">&quot;train&quot;</span>].train_test_split(train_size=<span class="hljs-number">0.9</span>, seed=<span class="hljs-number">20</span>)
split_datasets`,wrap:!1}}),Rl=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2lkJyUyQyUyMCd0cmFuc2xhdGlvbiclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDE4OTE1NSUwQSUyMCUyMCUyMCUyMCU3RCklMEElMjAlMjAlMjAlMjB0ZXN0JTNBJTIwRGF0YXNldCglN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlcyUzQSUyMCU1QidpZCclMkMlMjAndHJhbnNsYXRpb24nJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX3Jvd3MlM0ElMjAyMTAxOCUwQSUyMCUyMCUyMCUyMCU3RCklMEElN0Qp",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;id&#x27;</span>, <span class="hljs-string">&#x27;translation&#x27;</span>],
        num_rows: <span class="hljs-number">189155</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;id&#x27;</span>, <span class="hljs-string">&#x27;translation&#x27;</span>],
        num_rows: <span class="hljs-number">21018</span>
    })
})`,wrap:!1}}),$=new h({props:{code:"c3BsaXRfZGF0YXNldHMlNUIlMjJ2YWxpZGF0aW9uJTIyJTVEJTIwJTNEJTIwc3BsaXRfZGF0YXNldHMucG9wKCUyMnRlc3QlMjIp",highlighted:'split_datasets[<span class="hljs-string">&quot;validation&quot;</span>] = split_datasets.pop(<span class="hljs-string">&quot;test&quot;</span>)',wrap:!1}}),pe=new h({props:{code:"c3BsaXRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QjElNUQlNUIlMjJ0cmFuc2xhdGlvbiUyMiU1RA==",highlighted:'split_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">1</span>][<span class="hljs-string">&quot;translation&quot;</span>]',wrap:!1}}),Me=new h({props:{code:"JTdCJ2VuJyUzQSUyMCdEZWZhdWx0JTIwdG8lMjBleHBhbmRlZCUyMHRocmVhZHMnJTJDJTBBJTIwJ2ZyJyUzQSUyMCdQYXIlMjBkJUMzJUE5ZmF1dCUyQyUyMGQlQzMlQTl2ZWxvcHBlciUyMGxlcyUyMGZpbHMlMjBkZSUyMGRpc2N1c3Npb24nJTdE",highlighted:`{<span class="hljs-string">&#x27;en&#x27;</span>: <span class="hljs-string">&#x27;Default to expanded threads&#x27;</span>,
 <span class="hljs-string">&#x27;fr&#x27;</span>: <span class="hljs-string">&#x27;Par défaut, développer les fils de discussion&#x27;</span>}`,wrap:!1}}),ie=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBbW9kZWxfY2hlY2twb2ludCUyMCUzRCUyMCUyMkhlbHNpbmtpLU5MUCUyRm9wdXMtbXQtZW4tZnIlMjIlMEF0cmFuc2xhdG9yJTIwJTNEJTIwcGlwZWxpbmUoJTIydHJhbnNsYXRpb24lMjIlMkMlMjBtb2RlbCUzRG1vZGVsX2NoZWNrcG9pbnQpJTBBdHJhbnNsYXRvciglMjJEZWZhdWx0JTIwdG8lMjBleHBhbmRlZCUyMHRocmVhZHMlMjIp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

model_checkpoint = <span class="hljs-string">&quot;Helsinki-NLP/opus-mt-en-fr&quot;</span>
translator = pipeline(<span class="hljs-string">&quot;translation&quot;</span>, model=model_checkpoint)
translator(<span class="hljs-string">&quot;Default to expanded threads&quot;</span>)`,wrap:!1}}),ce=new h({props:{code:"JTVCJTdCJ3RyYW5zbGF0aW9uX3RleHQnJTNBJTIwJ1BhciUyMGQlQzMlQTlmYXV0JTIwcG91ciUyMGxlcyUyMHRocmVhZHMlMjAlQzMlQTlsYXJnaXMnJTdEJTVE",highlighted:'[{<span class="hljs-string">&#x27;translation_text&#x27;</span>: <span class="hljs-string">&#x27;Par défaut pour les threads élargis&#x27;</span>}]',wrap:!1}}),ye=new h({props:{code:"c3BsaXRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QjE3MiU1RCU1QiUyMnRyYW5zbGF0aW9uJTIyJTVE",highlighted:'split_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">172</span>][<span class="hljs-string">&quot;translation&quot;</span>]',wrap:!1}}),Ue=new h({props:{code:"JTdCJ2VuJyUzQSUyMCdVbmFibGUlMjB0byUyMGltcG9ydCUyMCUyNTElMjB1c2luZyUyMHRoZSUyME9GWCUyMGltcG9ydGVyJTIwcGx1Z2luLiUyMFRoaXMlMjBmaWxlJTIwaXMlMjBub3QlMjB0aGUlMjBjb3JyZWN0JTIwZm9ybWF0LiclMkMlMEElMjAnZnInJTNBJTIwJTIySW1wb3NzaWJsZSUyMGQnaW1wb3J0ZXIlMjAlMjUxJTIwZW4lMjB1dGlsaXNhbnQlMjBsZSUyMG1vZHVsZSUyMGQnZXh0ZW5zaW9uJTIwZCdpbXBvcnRhdGlvbiUyME9GWC4lMjBDZSUyMGZpY2hpZXIlMjBuJ2ElMjBwYXMlMjB1biUyMGZvcm1hdCUyMGNvcnJlY3QuJTIyJTdE",highlighted:`{<span class="hljs-string">&#x27;en&#x27;</span>: <span class="hljs-string">&#x27;Unable to import %1 using the OFX importer plugin. This file is not the correct format.&#x27;</span>,
 <span class="hljs-string">&#x27;fr&#x27;</span>: <span class="hljs-string">&quot;Impossible d&#x27;importer %1 en utilisant le module d&#x27;extension d&#x27;importation OFX. Ce fichier n&#x27;a pas un format correct.&quot;</span>}`,wrap:!1}}),me=new h({props:{code:"dHJhbnNsYXRvciglMEElMjAlMjAlMjAlMjAlMjJVbmFibGUlMjB0byUyMGltcG9ydCUyMCUyNTElMjB1c2luZyUyMHRoZSUyME9GWCUyMGltcG9ydGVyJTIwcGx1Z2luLiUyMFRoaXMlMjBmaWxlJTIwaXMlMjBub3QlMjB0aGUlMjBjb3JyZWN0JTIwZm9ybWF0LiUyMiUwQSk=",highlighted:`translator(
    <span class="hljs-string">&quot;Unable to import %1 using the OFX importer plugin. This file is not the correct format.&quot;</span>
)`,wrap:!1}}),oe=new h({props:{code:"JTVCJTdCJ3RyYW5zbGF0aW9uX3RleHQnJTNBJTIwJTIySW1wb3NzaWJsZSUyMGQnaW1wb3J0ZXIlMjAlMjUxJTIwZW4lMjB1dGlsaXNhbnQlMjBsZSUyMHBsdWdpbiUyMGQnaW1wb3J0YXRldXIlMjBPRlguJTIwQ2UlMjBmaWNoaWVyJTIwbidlc3QlMjBwYXMlMjBsZSUyMGJvbiUyMGZvcm1hdC4lMjIlN0QlNUQ=",highlighted:'[{<span class="hljs-string">&#x27;translation_text&#x27;</span>: <span class="hljs-string">&quot;Impossible d&#x27;importer %1 en utilisant le plugin d&#x27;importateur OFX. Ce fichier n&#x27;est pas le bon format.&quot;</span>}]',wrap:!1}}),ue=new xn({props:{id:"0Oxphw4Q9fo"}}),Ol=new te({props:{$$slots:{default:[lp]},$$scope:{ctx:x}}}),de=new gl({props:{title:"处理数据",local:"处理数据",headingTag:"h3"}}),we=new xn({props:{id:"XAR8jnZZuUs"}}),be=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEFtb2RlbF9jaGVja3BvaW50JTIwJTNEJTIwJTIySGVsc2lua2ktTkxQJTJGb3B1cy1tdC1lbi1mciUyMiUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKG1vZGVsX2NoZWNrcG9pbnQlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMnB0JTIyKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

model_checkpoint = <span class="hljs-string">&quot;Helsinki-NLP/opus-mt-en-fr&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)`,wrap:!1}}),Kl=new te({props:{$$slots:{default:[ep]},$$scope:{ctx:x}}}),Ie=new h({props:{code:"ZW5fc2VudGVuY2UlMjAlM0QlMjBzcGxpdF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVEJTVCMSU1RCU1QiUyMnRyYW5zbGF0aW9uJTIyJTVEJTVCJTIyZW4lMjIlNUQlMEFmcl9zZW50ZW5jZSUyMCUzRCUyMHNwbGl0X2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUIxJTVEJTVCJTIydHJhbnNsYXRpb24lMjIlNUQlNUIlMjJmciUyMiU1RCUwQSUwQWlucHV0cyUyMCUzRCUyMHRva2VuaXplcihlbl9zZW50ZW5jZSUyQyUyMHRleHRfdGFyZ2V0JTNEZnJfc2VudGVuY2UpJTBBaW5wdXRz",highlighted:`en_sentence = split_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">1</span>][<span class="hljs-string">&quot;translation&quot;</span>][<span class="hljs-string">&quot;en&quot;</span>]
fr_sentence = split_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">1</span>][<span class="hljs-string">&quot;translation&quot;</span>][<span class="hljs-string">&quot;fr&quot;</span>]

inputs = tokenizer(en_sentence, text_target=fr_sentence)
inputs`,wrap:!1}}),ke=new h({props:{code:"JTdCJ2lucHV0X2lkcyclM0ElMjAlNUI0NzU5MSUyQyUyMDEyJTJDJTIwOTg0MiUyQyUyMDE5NjM0JTJDJTIwOSUyQyUyMDAlNUQlMkMlMjAnYXR0ZW50aW9uX21hc2snJTNBJTIwJTVCMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTVEJTJDJTIwJ2xhYmVscyclM0ElMjAlNUI1NzclMkMlMjA1ODkxJTJDJTIwMiUyQyUyMDMxODQlMkMlMjAxNiUyQyUyMDI1NDIlMkMlMjA1JTJDJTIwMTcxMCUyQyUyMDAlNUQlN0Q=",highlighted:'{<span class="hljs-string">&#x27;input_ids&#x27;</span>: [<span class="hljs-number">47591</span>, <span class="hljs-number">12</span>, <span class="hljs-number">9842</span>, <span class="hljs-number">19634</span>, <span class="hljs-number">9</span>, <span class="hljs-number">0</span>], <span class="hljs-string">&#x27;attention_mask&#x27;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], <span class="hljs-string">&#x27;labels&#x27;</span>: [<span class="hljs-number">577</span>, <span class="hljs-number">5891</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3184</span>, <span class="hljs-number">16</span>, <span class="hljs-number">2542</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1710</span>, <span class="hljs-number">0</span>]}',wrap:!1}}),ge=new h({props:{code:"d3JvbmdfdGFyZ2V0cyUyMCUzRCUyMHRva2VuaXplcihmcl9zZW50ZW5jZSklMEFwcmludCh0b2tlbml6ZXIuY29udmVydF9pZHNfdG9fdG9rZW5zKHdyb25nX3RhcmdldHMlNUIlMjJpbnB1dF9pZHMlMjIlNUQpKSUwQXByaW50KHRva2VuaXplci5jb252ZXJ0X2lkc190b190b2tlbnMoaW5wdXRzJTVCJTIybGFiZWxzJTIyJTVEKSk=",highlighted:`wrong_targets = tokenizer(fr_sentence)
<span class="hljs-built_in">print</span>(tokenizer.convert_ids_to_tokens(wrong_targets[<span class="hljs-string">&quot;input_ids&quot;</span>]))
<span class="hljs-built_in">print</span>(tokenizer.convert_ids_to_tokens(inputs[<span class="hljs-string">&quot;labels&quot;</span>]))`,wrap:!1}}),Ze=new h({props:{code:"JTVCJyVFMiU5NiU4MVBhciclMkMlMjAnJUUyJTk2JTgxZCVDMyVBOSclMkMlMjAnZiclMkMlMjAnYXV0JyUyQyUyMCclMkMnJTJDJTIwJyVFMiU5NiU4MWQlQzMlQTknJTJDJTIwJ3ZlJyUyQyUyMCdsb3AnJTJDJTIwJ3BlciclMkMlMjAnJUUyJTk2JTgxbGVzJyUyQyUyMCclRTIlOTYlODFmaWwnJTJDJTIwJ3MnJTJDJTIwJyVFMiU5NiU4MWRlJyUyQyUyMCclRTIlOTYlODFkaXNjdXNzaW9uJyUyQyUyMCclM0MlMkZzJTNFJyU1RCUwQSU1QiclRTIlOTYlODFQYXInJTJDJTIwJyVFMiU5NiU4MWQlQzMlQTlmYXV0JyUyQyUyMCclMkMnJTJDJTIwJyVFMiU5NiU4MWQlQzMlQTl2ZWxvcHBlciclMkMlMjAnJUUyJTk2JTgxbGVzJyUyQyUyMCclRTIlOTYlODFmaWxzJyUyQyUyMCclRTIlOTYlODFkZSclMkMlMjAnJUUyJTk2JTgxZGlzY3Vzc2lvbiclMkMlMjAnJTNDJTJGcyUzRSclNUQ=",highlighted:`[<span class="hljs-string">&#x27;▁Par&#x27;</span>, <span class="hljs-string">&#x27;▁dé&#x27;</span>, <span class="hljs-string">&#x27;f&#x27;</span>, <span class="hljs-string">&#x27;aut&#x27;</span>, <span class="hljs-string">&#x27;,&#x27;</span>, <span class="hljs-string">&#x27;▁dé&#x27;</span>, <span class="hljs-string">&#x27;ve&#x27;</span>, <span class="hljs-string">&#x27;lop&#x27;</span>, <span class="hljs-string">&#x27;per&#x27;</span>, <span class="hljs-string">&#x27;▁les&#x27;</span>, <span class="hljs-string">&#x27;▁fil&#x27;</span>, <span class="hljs-string">&#x27;s&#x27;</span>, <span class="hljs-string">&#x27;▁de&#x27;</span>, <span class="hljs-string">&#x27;▁discussion&#x27;</span>, <span class="hljs-string">&#x27;&lt;/s&gt;&#x27;</span>]
[<span class="hljs-string">&#x27;▁Par&#x27;</span>, <span class="hljs-string">&#x27;▁défaut&#x27;</span>, <span class="hljs-string">&#x27;,&#x27;</span>, <span class="hljs-string">&#x27;▁développer&#x27;</span>, <span class="hljs-string">&#x27;▁les&#x27;</span>, <span class="hljs-string">&#x27;▁fils&#x27;</span>, <span class="hljs-string">&#x27;▁de&#x27;</span>, <span class="hljs-string">&#x27;▁discussion&#x27;</span>, <span class="hljs-string">&#x27;&lt;/s&gt;&#x27;</span>]`,wrap:!1}}),Ge=new h({props:{code:"bWF4X2xlbmd0aCUyMCUzRCUyMDEyOCUwQSUwQSUwQWRlZiUyMHByZXByb2Nlc3NfZnVuY3Rpb24oZXhhbXBsZXMpJTNBJTBBJTIwJTIwJTIwJTIwaW5wdXRzJTIwJTNEJTIwJTVCZXglNUIlMjJlbiUyMiU1RCUyMGZvciUyMGV4JTIwaW4lMjBleGFtcGxlcyU1QiUyMnRyYW5zbGF0aW9uJTIyJTVEJTVEJTBBJTIwJTIwJTIwJTIwdGFyZ2V0cyUyMCUzRCUyMCU1QmV4JTVCJTIyZnIlMjIlNUQlMjBmb3IlMjBleCUyMGluJTIwZXhhbXBsZXMlNUIlMjJ0cmFuc2xhdGlvbiUyMiU1RCU1RCUwQSUyMCUyMCUyMCUyMG1vZGVsX2lucHV0cyUyMCUzRCUyMHRva2VuaXplciglMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBpbnB1dHMlMkMlMjB0ZXh0X3RhcmdldCUzRHRhcmdldHMlMkMlMjBtYXhfbGVuZ3RoJTNEbWF4X2xlbmd0aCUyQyUyMHRydW5jYXRpb24lM0RUcnVlJTBBJTIwJTIwJTIwJTIwKSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMG1vZGVsX2lucHV0cw==",highlighted:`max_length = <span class="hljs-number">128</span>


<span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>):
    inputs = [ex[<span class="hljs-string">&quot;en&quot;</span>] <span class="hljs-keyword">for</span> ex <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;translation&quot;</span>]]
    targets = [ex[<span class="hljs-string">&quot;fr&quot;</span>] <span class="hljs-keyword">for</span> ex <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;translation&quot;</span>]]
    model_inputs = tokenizer(
        inputs, text_target=targets, max_length=max_length, truncation=<span class="hljs-literal">True</span>
    )
    <span class="hljs-keyword">return</span> model_inputs`,wrap:!1}}),le=new te({props:{$$slots:{default:[sp]},$$scope:{ctx:x}}}),ee=new te({props:{warning:!0,$$slots:{default:[tp]},$$scope:{ctx:x}}}),Ne=new h({props:{code:"dG9rZW5pemVkX2RhdGFzZXRzJTIwJTNEJTIwc3BsaXRfZGF0YXNldHMubWFwKCUwQSUyMCUyMCUyMCUyMHByZXByb2Nlc3NfZnVuY3Rpb24lMkMlMEElMjAlMjAlMjAlMjBiYXRjaGVkJTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMHJlbW92ZV9jb2x1bW5zJTNEc3BsaXRfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RC5jb2x1bW5fbmFtZXMlMkMlMEEp",highlighted:`tokenized_datasets = split_datasets.<span class="hljs-built_in">map</span>(
    preprocess_function,
    batched=<span class="hljs-literal">True</span>,
    remove_columns=split_datasets[<span class="hljs-string">&quot;train&quot;</span>].column_names,
)`,wrap:!1}});const ha=[ap,np],Rs=[];function Ca(l,t){return l[0]==="pt"?0:1}Nl=Ca(x),Ql=Rs[Nl]=ha[Nl](x),Ae=new gl({props:{title:"数据整理",local:"数据整理",headingTag:"h3"}});const $a=[rp,Mp],Bs=[];function Ia(l,t){return l[0]==="pt"?0:1}Xl=Ia(x),Al=Bs[Xl]=$a[Xl](x),Fe=new h({props:{code:"YmF0Y2glMjAlM0QlMjBkYXRhX2NvbGxhdG9yKCU1QnRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVEJTVCaSU1RCUyMGZvciUyMGklMjBpbiUyMHJhbmdlKDElMkMlMjAzKSU1RCklMEFiYXRjaC5rZXlzKCk=",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">1</span>, <span class="hljs-number">3</span>)])
batch.keys()`,wrap:!1}}),Ye=new h({props:{code:"ZGljdF9rZXlzKCU1QidhdHRlbnRpb25fbWFzayclMkMlMjAnaW5wdXRfaWRzJyUyQyUyMCdsYWJlbHMnJTJDJTIwJ2RlY29kZXJfaW5wdXRfaWRzJyU1RCk=",highlighted:'dict_keys([<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;labels&#x27;</span>, <span class="hljs-string">&#x27;decoder_input_ids&#x27;</span>])',wrap:!1}}),He=new h({props:{code:"YmF0Y2glNUIlMjJsYWJlbHMlMjIlNUQ=",highlighted:'batch[<span class="hljs-string">&quot;labels&quot;</span>]',wrap:!1}}),Se=new h({props:{code:"dGVuc29yKCU1QiU1QiUyMCUyMDU3NyUyQyUyMCUyMDU4OTElMkMlMjAlMjAlMjAlMjAlMjAyJTJDJTIwJTIwMzE4NCUyQyUyMCUyMCUyMCUyMDE2JTJDJTIwJTIwMjU0MiUyQyUyMCUyMCUyMCUyMCUyMDUlMkMlMjAlMjAxNzEwJTJDJTIwJTIwJTIwJTIwJTIwMCUyQyUyMCUyMC0xMDAlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAtMTAwJTJDJTIwJTIwLTEwMCUyQyUyMCUyMC0xMDAlMkMlMjAlMjAtMTAwJTJDJTIwJTIwLTEwMCUyQyUyMCUyMC0xMDAlNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlNUIlMjAxMjExJTJDJTIwJTIwJTIwJTIwJTIwMyUyQyUyMCUyMCUyMCUyMDQ5JTJDJTIwJTIwOTQwOSUyQyUyMCUyMDEyMTElMkMlMjAlMjAlMjAlMjAlMjAzJTJDJTIwMjkxNDAlMkMlMjAlMjAlMjA4MTclMkMlMjAlMjAzMTI0JTJDJTIwJTIwJTIwODE3JTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwNTUwJTJDJTIwJTIwNzAzMiUyQyUyMCUyMDU4MjElMkMlMjAlMjA3OTA3JTJDJTIwMTI2NDklMkMlMjAlMjAlMjAlMjAlMjAwJTVEJTVEKQ==",highlighted:`tensor([[  <span class="hljs-number">577</span>,  <span class="hljs-number">5891</span>,     <span class="hljs-number">2</span>,  <span class="hljs-number">3184</span>,    <span class="hljs-number">16</span>,  <span class="hljs-number">2542</span>,     <span class="hljs-number">5</span>,  <span class="hljs-number">1710</span>,     <span class="hljs-number">0</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">1211</span>,     <span class="hljs-number">3</span>,    <span class="hljs-number">49</span>,  <span class="hljs-number">9409</span>,  <span class="hljs-number">1211</span>,     <span class="hljs-number">3</span>, <span class="hljs-number">29140</span>,   <span class="hljs-number">817</span>,  <span class="hljs-number">3124</span>,   <span class="hljs-number">817</span>,
           <span class="hljs-number">550</span>,  <span class="hljs-number">7032</span>,  <span class="hljs-number">5821</span>,  <span class="hljs-number">7907</span>, <span class="hljs-number">12649</span>,     <span class="hljs-number">0</span>]])`,wrap:!1}}),qe=new h({props:{code:"YmF0Y2glNUIlMjJkZWNvZGVyX2lucHV0X2lkcyUyMiU1RA==",highlighted:'batch[<span class="hljs-string">&quot;decoder_input_ids&quot;</span>]',wrap:!1}}),Le=new h({props:{code:"dGVuc29yKCU1QiU1QjU5NTEzJTJDJTIwJTIwJTIwNTc3JTJDJTIwJTIwNTg5MSUyQyUyMCUyMCUyMCUyMCUyMDIlMkMlMjAlMjAzMTg0JTJDJTIwJTIwJTIwJTIwMTYlMkMlMjAlMjAyNTQyJTJDJTIwJTIwJTIwJTIwJTIwNSUyQyUyMCUyMDE3MTAlMkMlMjAlMjAlMjAlMjAlMjAwJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwNTk1MTMlMkMlMjA1OTUxMyUyQyUyMDU5NTEzJTJDJTIwNTk1MTMlMkMlMjA1OTUxMyUyQyUyMDU5NTEzJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTVCNTk1MTMlMkMlMjAlMjAxMjExJTJDJTIwJTIwJTIwJTIwJTIwMyUyQyUyMCUyMCUyMCUyMDQ5JTJDJTIwJTIwOTQwOSUyQyUyMCUyMDEyMTElMkMlMjAlMjAlMjAlMjAlMjAzJTJDJTIwMjkxNDAlMkMlMjAlMjAlMjA4MTclMkMlMjAlMjAzMTI0JTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwODE3JTJDJTIwJTIwJTIwNTUwJTJDJTIwJTIwNzAzMiUyQyUyMCUyMDU4MjElMkMlMjAlMjA3OTA3JTJDJTIwMTI2NDklNUQlNUQp",highlighted:`tensor([[<span class="hljs-number">59513</span>,   <span class="hljs-number">577</span>,  <span class="hljs-number">5891</span>,     <span class="hljs-number">2</span>,  <span class="hljs-number">3184</span>,    <span class="hljs-number">16</span>,  <span class="hljs-number">2542</span>,     <span class="hljs-number">5</span>,  <span class="hljs-number">1710</span>,     <span class="hljs-number">0</span>,
         <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>],
        [<span class="hljs-number">59513</span>,  <span class="hljs-number">1211</span>,     <span class="hljs-number">3</span>,    <span class="hljs-number">49</span>,  <span class="hljs-number">9409</span>,  <span class="hljs-number">1211</span>,     <span class="hljs-number">3</span>, <span class="hljs-number">29140</span>,   <span class="hljs-number">817</span>,  <span class="hljs-number">3124</span>,
           <span class="hljs-number">817</span>,   <span class="hljs-number">550</span>,  <span class="hljs-number">7032</span>,  <span class="hljs-number">5821</span>,  <span class="hljs-number">7907</span>, <span class="hljs-number">12649</span>]])`,wrap:!1}}),Oe=new h({props:{code:"Zm9yJTIwaSUyMGluJTIwcmFuZ2UoMSUyQyUyMDMpJTNBJTBBJTIwJTIwJTIwJTIwcHJpbnQodG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUJpJTVEJTVCJTIybGFiZWxzJTIyJTVEKQ==",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">1</span>, <span class="hljs-number">3</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}}),Ke=new h({props:{code:"JTVCNTc3JTJDJTIwNTg5MSUyQyUyMDIlMkMlMjAzMTg0JTJDJTIwMTYlMkMlMjAyNTQyJTJDJTIwNSUyQyUyMDE3MTAlMkMlMjAwJTVEJTBBJTVCMTIxMSUyQyUyMDMlMkMlMjA0OSUyQyUyMDk0MDklMkMlMjAxMjExJTJDJTIwMyUyQyUyMDI5MTQwJTJDJTIwODE3JTJDJTIwMzEyNCUyQyUyMDgxNyUyQyUyMDU1MCUyQyUyMDcwMzIlMkMlMjA1ODIxJTJDJTIwNzkwNyUyQyUyMDEyNjQ5JTJDJTIwMCU1RA==",highlighted:`[<span class="hljs-number">577</span>, <span class="hljs-number">5891</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3184</span>, <span class="hljs-number">16</span>, <span class="hljs-number">2542</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1710</span>, <span class="hljs-number">0</span>]
[<span class="hljs-number">1211</span>, <span class="hljs-number">3</span>, <span class="hljs-number">49</span>, <span class="hljs-number">9409</span>, <span class="hljs-number">1211</span>, <span class="hljs-number">3</span>, <span class="hljs-number">29140</span>, <span class="hljs-number">817</span>, <span class="hljs-number">3124</span>, <span class="hljs-number">817</span>, <span class="hljs-number">550</span>, <span class="hljs-number">7032</span>, <span class="hljs-number">5821</span>, <span class="hljs-number">7907</span>, <span class="hljs-number">12649</span>, <span class="hljs-number">0</span>]`,wrap:!1}});const ka=[cp,ip],Ns=[];function _a(l,t){return l[0]==="pt"?0:1}Wl=_a(x),El=Ns[Wl]=ka[Wl](x),ls=new gl({props:{title:"评估指标",local:"评估指标",headingTag:"h3"}}),es=new xn({props:{id:"M05L1DhFqcw"}});let wl=x[0]==="pt"&&Wa();ns=new h({props:{code:"IXBpcCUyMGluc3RhbGwlMjBzYWNyZWJsZXU=",highlighted:"!pip install sacrebleu",wrap:!1}}),ps=new h({props:{code:"aW1wb3J0JTIwZXZhbHVhdGUlMEElMEFtZXRyaWMlMjAlM0QlMjBldmFsdWF0ZS5sb2FkKCUyMnNhY3JlYmxldSUyMik=",highlighted:`<span class="hljs-keyword">import</span> evaluate

metric = evaluate.load(<span class="hljs-string">&quot;sacrebleu&quot;</span>)`,wrap:!1}}),is=new h({props:{code:"cHJlZGljdGlvbnMlMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlMjJUaGlzJTIwcGx1Z2luJTIwbGV0cyUyMHlvdSUyMHRyYW5zbGF0ZSUyMHdlYiUyMHBhZ2VzJTIwYmV0d2VlbiUyMHNldmVyYWwlMjBsYW5ndWFnZXMlMjBhdXRvbWF0aWNhbGx5LiUyMiUwQSU1RCUwQXJlZmVyZW5jZXMlMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlNUIlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJUaGlzJTIwcGx1Z2luJTIwYWxsb3dzJTIweW91JTIwdG8lMjBhdXRvbWF0aWNhbGx5JTIwdHJhbnNsYXRlJTIwd2ViJTIwcGFnZXMlMjBiZXR3ZWVuJTIwc2V2ZXJhbCUyMGxhbmd1YWdlcy4lMjIlMEElMjAlMjAlMjAlMjAlNUQlMEElNUQlMEFtZXRyaWMuY29tcHV0ZShwcmVkaWN0aW9ucyUzRHByZWRpY3Rpb25zJTJDJTIwcmVmZXJlbmNlcyUzRHJlZmVyZW5jZXMp",highlighted:`predictions = [
    <span class="hljs-string">&quot;This plugin lets you translate web pages between several languages automatically.&quot;</span>
]
references = [
    [
        <span class="hljs-string">&quot;This plugin allows you to automatically translate web pages between several languages.&quot;</span>
    ]
]
metric.compute(predictions=predictions, references=references)`,wrap:!1}}),cs=new h({props:{code:"JTdCJ3Njb3JlJyUzQSUyMDQ2Ljc1MDQ2OTY4Mjk5MDE2NSUyQyUwQSUyMCdjb3VudHMnJTNBJTIwJTVCMTElMkMlMjA2JTJDJTIwNCUyQyUyMDMlNUQlMkMlMEElMjAndG90YWxzJyUzQSUyMCU1QjEyJTJDJTIwMTElMkMlMjAxMCUyQyUyMDklNUQlMkMlMEElMjAncHJlY2lzaW9ucyclM0ElMjAlNUI5MS42NyUyQyUyMDU0LjU0JTJDJTIwNDAuMCUyQyUyMDMzLjMzJTVEJTJDJTBBJTIwJ2JwJyUzQSUyMDAuOTIwMDQ0NDE0NjI5MzIzMyUyQyUwQSUyMCdzeXNfbGVuJyUzQSUyMDEyJTJDJTBBJTIwJ3JlZl9sZW4nJTNBJTIwMTMlN0Q=",highlighted:`{<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">46.750469682990165</span>,
 <span class="hljs-string">&#x27;counts&#x27;</span>: [<span class="hljs-number">11</span>, <span class="hljs-number">6</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>],
 <span class="hljs-string">&#x27;totals&#x27;</span>: [<span class="hljs-number">12</span>, <span class="hljs-number">11</span>, <span class="hljs-number">10</span>, <span class="hljs-number">9</span>],
 <span class="hljs-string">&#x27;precisions&#x27;</span>: [<span class="hljs-number">91.67</span>, <span class="hljs-number">54.54</span>, <span class="hljs-number">40.0</span>, <span class="hljs-number">33.33</span>],
 <span class="hljs-string">&#x27;bp&#x27;</span>: <span class="hljs-number">0.9200444146293233</span>,
 <span class="hljs-string">&#x27;sys_len&#x27;</span>: <span class="hljs-number">12</span>,
 <span class="hljs-string">&#x27;ref_len&#x27;</span>: <span class="hljs-number">13</span>}`,wrap:!1}}),ys=new h({props:{code:"cHJlZGljdGlvbnMlMjAlM0QlMjAlNUIlMjJUaGlzJTIwVGhpcyUyMFRoaXMlMjBUaGlzJTIyJTVEJTBBcmVmZXJlbmNlcyUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMCU1QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMlRoaXMlMjBwbHVnaW4lMjBhbGxvd3MlMjB5b3UlMjB0byUyMGF1dG9tYXRpY2FsbHklMjB0cmFuc2xhdGUlMjB3ZWIlMjBwYWdlcyUyMGJldHdlZW4lMjBzZXZlcmFsJTIwbGFuZ3VhZ2VzLiUyMiUwQSUyMCUyMCUyMCUyMCU1RCUwQSU1RCUwQW1ldHJpYy5jb21wdXRlKHByZWRpY3Rpb25zJTNEcHJlZGljdGlvbnMlMkMlMjByZWZlcmVuY2VzJTNEcmVmZXJlbmNlcyk=",highlighted:`predictions = [<span class="hljs-string">&quot;This This This This&quot;</span>]
references = [
    [
        <span class="hljs-string">&quot;This plugin allows you to automatically translate web pages between several languages.&quot;</span>
    ]
]
metric.compute(predictions=predictions, references=references)`,wrap:!1}}),Us=new h({props:{code:"JTdCJ3Njb3JlJyUzQSUyMDEuNjgzNjAyNjkzMTY3Njg5JTJDJTBBJTIwJ2NvdW50cyclM0ElMjAlNUIxJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTVEJTJDJTBBJTIwJ3RvdGFscyclM0ElMjAlNUI0JTJDJTIwMyUyQyUyMDIlMkMlMjAxJTVEJTJDJTBBJTIwJ3ByZWNpc2lvbnMnJTNBJTIwJTVCMjUuMCUyQyUyMDE2LjY3JTJDJTIwMTIuNSUyQyUyMDEyLjUlNUQlMkMlMEElMjAnYnAnJTNBJTIwMC4xMDUzOTkyMjQ1NjE4NjQzMyUyQyUwQSUyMCdzeXNfbGVuJyUzQSUyMDQlMkMlMEElMjAncmVmX2xlbiclM0ElMjAxMyU3RA==",highlighted:`{<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">1.683602693167689</span>,
 <span class="hljs-string">&#x27;counts&#x27;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
 <span class="hljs-string">&#x27;totals&#x27;</span>: [<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>],
 <span class="hljs-string">&#x27;precisions&#x27;</span>: [<span class="hljs-number">25.0</span>, <span class="hljs-number">16.67</span>, <span class="hljs-number">12.5</span>, <span class="hljs-number">12.5</span>],
 <span class="hljs-string">&#x27;bp&#x27;</span>: <span class="hljs-number">0.10539922456186433</span>,
 <span class="hljs-string">&#x27;sys_len&#x27;</span>: <span class="hljs-number">4</span>,
 <span class="hljs-string">&#x27;ref_len&#x27;</span>: <span class="hljs-number">13</span>}`,wrap:!1}}),Ts=new h({props:{code:"cHJlZGljdGlvbnMlMjAlM0QlMjAlNUIlMjJUaGlzJTIwcGx1Z2luJTIyJTVEJTBBcmVmZXJlbmNlcyUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMCU1QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMlRoaXMlMjBwbHVnaW4lMjBhbGxvd3MlMjB5b3UlMjB0byUyMGF1dG9tYXRpY2FsbHklMjB0cmFuc2xhdGUlMjB3ZWIlMjBwYWdlcyUyMGJldHdlZW4lMjBzZXZlcmFsJTIwbGFuZ3VhZ2VzLiUyMiUwQSUyMCUyMCUyMCUyMCU1RCUwQSU1RCUwQW1ldHJpYy5jb21wdXRlKHByZWRpY3Rpb25zJTNEcHJlZGljdGlvbnMlMkMlMjByZWZlcmVuY2VzJTNEcmVmZXJlbmNlcyk=",highlighted:`predictions = [<span class="hljs-string">&quot;This plugin&quot;</span>]
references = [
    [
        <span class="hljs-string">&quot;This plugin allows you to automatically translate web pages between several languages.&quot;</span>
    ]
]
metric.compute(predictions=predictions, references=references)`,wrap:!1}}),ms=new h({props:{code:"JTdCJ3Njb3JlJyUzQSUyMDAuMCUyQyUwQSUyMCdjb3VudHMnJTNBJTIwJTVCMiUyQyUyMDElMkMlMjAwJTJDJTIwMCU1RCUyQyUwQSUyMCd0b3RhbHMnJTNBJTIwJTVCMiUyQyUyMDElMkMlMjAwJTJDJTIwMCU1RCUyQyUwQSUyMCdwcmVjaXNpb25zJyUzQSUyMCU1QjEwMC4wJTJDJTIwMTAwLjAlMkMlMjAwLjAlMkMlMjAwLjAlNUQlMkMlMEElMjAnYnAnJTNBJTIwMC4wMDQwODY3NzE0Mzg0NjQwNjclMkMlMEElMjAnc3lzX2xlbiclM0ElMjAyJTJDJTBBJTIwJ3JlZl9sZW4nJTNBJTIwMTMlN0Q=",highlighted:`{<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.0</span>,
 <span class="hljs-string">&#x27;counts&#x27;</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
 <span class="hljs-string">&#x27;totals&#x27;</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
 <span class="hljs-string">&#x27;precisions&#x27;</span>: [<span class="hljs-number">100.0</span>, <span class="hljs-number">100.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>],
 <span class="hljs-string">&#x27;bp&#x27;</span>: <span class="hljs-number">0.004086771438464067</span>,
 <span class="hljs-string">&#x27;sys_len&#x27;</span>: <span class="hljs-number">2</span>,
 <span class="hljs-string">&#x27;ref_len&#x27;</span>: <span class="hljs-number">13</span>}`,wrap:!1}});const ga=[yp,Jp],Qs=[];function Za(l,t){return l[0]==="tf"?0:1}vl=Za(x),Fl=Qs[vl]=ga[vl](x),us=new gl({props:{title:"微调模型",local:"微调模型",headingTag:"h3"}}),ws=new h({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`,wrap:!1}}),hs=new h({props:{code:"aHVnZ2luZ2ZhY2UtY2xpJTIwbG9naW4=",highlighted:"huggingface-cli login",wrap:!1}});const xa=[Tp,Up],Xs=[];function Va(l,t){return l[0]==="tf"?0:1}Yl=Va(x),zl=Xs[Yl]=xa[Yl](x);let al=x[0]==="pt"&&Ea();return Cs=new gl({props:{title:"使用微调后的模型",local:"使用微调后的模型",headingTag:"h2"}}),Is=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBJTIzJTIwJUU1JUIwJTg2JUU1JTg1JUI2JUU2JTlCJUJGJUU2JThEJUEyJUU2JTg4JTkwJUU0JUJEJUEwJUU4JTg3JUFBJUU1JUI3JUIxJUU3JTlBJTg0JTIwY2hlY2twb2ludCUwQW1vZGVsX2NoZWNrcG9pbnQlMjAlM0QlMjAlMjJodWdnaW5nZmFjZS1jb3Vyc2UlMkZtYXJpYW4tZmluZXR1bmVkLWtkZTQtZW4tdG8tZnIlMjIlMEF0cmFuc2xhdG9yJTIwJTNEJTIwcGlwZWxpbmUoJTIydHJhbnNsYXRpb24lMjIlMkMlMjBtb2RlbCUzRG1vZGVsX2NoZWNrcG9pbnQpJTBBdHJhbnNsYXRvciglMjJEZWZhdWx0JTIwdG8lMjBleHBhbmRlZCUyMHRocmVhZHMlMjIp",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/marian-finetuned-kde4-en-to-fr&quot;</span>
translator = pipeline(<span class="hljs-string">&quot;translation&quot;</span>, model=model_checkpoint)
translator(<span class="hljs-string">&quot;Default to expanded threads&quot;</span>)`,wrap:!1}}),ks=new h({props:{code:"JTVCJTdCJ3RyYW5zbGF0aW9uX3RleHQnJTNBJTIwJ1BhciUyMGQlQzMlQTlmYXV0JTJDJTIwZCVDMyVBOXZlbG9wcGVyJTIwbGVzJTIwZmlscyUyMGRlJTIwZGlzY3Vzc2lvbiclN0QlNUQ=",highlighted:'[{<span class="hljs-string">&#x27;translation_text&#x27;</span>: <span class="hljs-string">&#x27;Par défaut, développer les fils de discussion&#x27;</span>}]',wrap:!1}}),gs=new h({props:{code:"dHJhbnNsYXRvciglMEElMjAlMjAlMjAlMjAlMjJVbmFibGUlMjB0byUyMGltcG9ydCUyMCUyNTElMjB1c2luZyUyMHRoZSUyME9GWCUyMGltcG9ydGVyJTIwcGx1Z2luLiUyMFRoaXMlMjBmaWxlJTIwaXMlMjBub3QlMjB0aGUlMjBjb3JyZWN0JTIwZm9ybWF0LiUyMiUwQSk=",highlighted:`translator(
    <span class="hljs-string">&quot;Unable to import %1 using the OFX importer plugin. This file is not the correct format.&quot;</span>
)`,wrap:!1}}),Zs=new h({props:{code:"JTVCJTdCJ3RyYW5zbGF0aW9uX3RleHQnJTNBJTIwJTIySW1wb3NzaWJsZSUyMGQnaW1wb3J0ZXIlMjAlMjUxJTIwZW4lMjB1dGlsaXNhbnQlMjBsZSUyMG1vZHVsZSUyMGV4dGVybmUlMjBkJ2ltcG9ydGF0aW9uJTIwT0ZYLiUyMENlJTIwZmljaGllciUyMG4nZXN0JTIwcGFzJTIwbGUlMjBib24lMjBmb3JtYXQuJTIyJTdEJTVE",highlighted:'[{<span class="hljs-string">&#x27;translation_text&#x27;</span>: <span class="hljs-string">&quot;Impossible d&#x27;importer %1 en utilisant le module externe d&#x27;importation OFX. Ce fichier n&#x27;est pas le bon format.&quot;</span>}]',wrap:!1}}),se=new te({props:{$$slots:{default:[jp]},$$scope:{ctx:x}}}),Vs=new La({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter7/4.mdx"}}),{c(){p=y("meta"),b=n(),r=y("p"),w=n(),T(g.$$.fragment),f=n(),T(Z.$$.fragment),V=n(),T(_.$$.fragment),k=n(),R.c(),Q=n(),pl=y("p"),pl.innerHTML=K,X=n(),A=y("ul"),A.innerHTML=P,ml=n(),T(W.$$.fragment),E=n(),v=y("p"),v.innerHTML=O,ol=n(),F=y("p"),F.innerHTML=z,L=n(),N=y("p"),N.textContent=Ml,ll=n(),B=y("iframe"),d=n(),G=y("a"),G.innerHTML=jl,Jl=n(),sl=y("p"),sl.innerHTML=kl,fl=n(),T(H.$$.fragment),Hl=n(),yl=y("p"),yl.innerHTML=rl,bl=n(),T(S.$$.fragment),Zl=n(),tl=y("p"),tl.innerHTML=_l,hl=n(),T(D.$$.fragment),Sl=n(),Ul=y("p"),Ul.innerHTML=il,Cl=n(),Y=y("img"),$l=n(),q=y("p"),q.textContent=Dl,Tl=n(),T(cl.$$.fragment),xl=n(),T(nl.$$.fragment),ul=n(),dl=y("p"),dl.innerHTML=i,I=n(),T(Vl.$$.fragment),Gl=n(),T(Rl.$$.fragment),Bl=n(),ql=y("p"),ql.innerHTML=ne,M=n(),T($.$$.fragment),qs=n(),ae=y("p"),ae.textContent=An,Ls=n(),T(pe.$$.fragment),Ps=n(),T(Me.$$.fragment),Os=n(),re=y("p"),re.textContent=Wn,Ks=n(),T(ie.$$.fragment),lt=n(),T(ce.$$.fragment),et=n(),Je=y("p"),Je.textContent=En,st=n(),T(ye.$$.fragment),tt=n(),T(Ue.$$.fragment),nt=n(),Te=y("p"),Te.textContent=vn,at=n(),T(me.$$.fragment),pt=n(),T(oe.$$.fragment),Mt=n(),je=y("p"),je.textContent=Fn,rt=n(),T(ue.$$.fragment),it=n(),T(Ol.$$.fragment),ct=n(),T(de.$$.fragment),Jt=n(),T(we.$$.fragment),yt=n(),fe=y("p"),fe.innerHTML=Yn,Ut=n(),T(be.$$.fragment),Tt=n(),he=y("p"),he.innerHTML=zn,mt=n(),T(Kl.$$.fragment),ot=n(),Ce=y("p"),Ce.innerHTML=Hn,jt=n(),$e=y("p"),$e.textContent=Sn,ut=n(),T(Ie.$$.fragment),dt=n(),T(ke.$$.fragment),wt=n(),_e=y("p"),_e.innerHTML=Dn,ft=n(),T(ge.$$.fragment),bt=n(),T(Ze.$$.fragment),ht=n(),xe=y("p"),xe.textContent=qn,Ct=n(),Ve=y("p"),Ve.textContent=Ln,$t=n(),T(Ge.$$.fragment),It=n(),Re=y("p"),Re.textContent=Pn,kt=n(),T(le.$$.fragment),_t=n(),T(ee.$$.fragment),gt=n(),Be=y("p"),Be.textContent=On,Zt=n(),T(Ne.$$.fragment),xt=n(),Qe=y("p"),Qe.textContent=Kn,Vt=n(),Ql.c(),Es=n(),Xe=y("p"),Xe.textContent=la,Gt=n(),T(Ae.$$.fragment),Rt=n(),We=y("p"),We.innerHTML=ea,Bt=n(),Ee=y("p"),Ee.innerHTML=sa,Nt=n(),Al.c(),vs=n(),ve=y("p"),ve.textContent=ta,Qt=n(),T(Fe.$$.fragment),Xt=n(),T(Ye.$$.fragment),At=n(),ze=y("p"),ze.innerHTML=na,Wt=n(),T(He.$$.fragment),Et=n(),T(Se.$$.fragment),vt=n(),De=y("p"),De.textContent=aa,Ft=n(),T(qe.$$.fragment),Yt=n(),T(Le.$$.fragment),zt=n(),Pe=y("p"),Pe.textContent=pa,Ht=n(),T(Oe.$$.fragment),St=n(),T(Ke.$$.fragment),Dt=n(),El.c(),Fs=n(),T(ls.$$.fragment),qt=n(),T(es.$$.fragment),Lt=n(),wl&&wl.c(),Ys=n(),ss=y("p"),ss.innerHTML=Ma,Pt=n(),ts=y("p"),ts.innerHTML=ra,Ot=n(),T(ns.$$.fragment),Kt=n(),as=y("p"),as.innerHTML=ia,ln=n(),T(ps.$$.fragment),en=n(),Ms=y("p"),Ms.textContent=ca,sn=n(),rs=y("p"),rs.textContent=Ja,tn=n(),T(is.$$.fragment),nn=n(),T(cs.$$.fragment),an=n(),Js=y("p"),Js.innerHTML=ya,pn=n(),T(ys.$$.fragment),Mn=n(),T(Us.$$.fragment),rn=n(),T(Ts.$$.fragment),cn=n(),T(ms.$$.fragment),Jn=n(),os=y("p"),os.textContent=Ua,yn=n(),Fl.c(),zs=n(),js=y("p"),js.textContent=Ta,Un=n(),T(us.$$.fragment),Tn=n(),ds=y("p"),ds.textContent=ma,mn=n(),T(ws.$$.fragment),on=n(),fs=y("p"),fs.textContent=oa,jn=n(),bs=y("p"),bs.textContent=ja,un=n(),T(hs.$$.fragment),dn=n(),zl.c(),Hs=n(),al&&al.c(),Ss=n(),T(Cs.$$.fragment),wn=n(),$s=y("p"),$s.innerHTML=ua,fn=n(),T(Is.$$.fragment),bn=n(),T(ks.$$.fragment),hn=n(),_s=y("p"),_s.textContent=da,Cn=n(),T(gs.$$.fragment),$n=n(),T(Zs.$$.fragment),In=n(),xs=y("p"),xs.textContent=wa,kn=n(),T(se.$$.fragment),_n=n(),T(Vs.$$.fragment),gn=n(),Ds=y("p"),this.h()},l(l){const t=Sa("svelte-u9bgzb",document.head);p=U(t,"META",{name:!0,content:!0}),t.forEach(e),b=a(l),r=U(l,"P",{}),Xn(r).forEach(e),w=a(l),m(g.$$.fragment,l),f=a(l),m(Z.$$.fragment,l),V=a(l),m(_.$$.fragment,l),k=a(l),R.l(l),Q=a(l),pl=U(l,"P",{"data-svelte-h":!0}),u(pl)!=="svelte-18zyinn"&&(pl.innerHTML=K),X=a(l),A=U(l,"UL",{"data-svelte-h":!0}),u(A)!=="svelte-j95dky"&&(A.innerHTML=P),ml=a(l),m(W.$$.fragment,l),E=a(l),v=U(l,"P",{"data-svelte-h":!0}),u(v)!=="svelte-1bvu257"&&(v.innerHTML=O),ol=a(l),F=U(l,"P",{"data-svelte-h":!0}),u(F)!=="svelte-1psbmv4"&&(F.innerHTML=z),L=a(l),N=U(l,"P",{"data-svelte-h":!0}),u(N)!=="svelte-1unz1qk"&&(N.textContent=Ml),ll=a(l),B=U(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),Xn(B).forEach(e),d=a(l),G=U(l,"A",{class:!0,href:!0,"data-svelte-h":!0}),u(G)!=="svelte-1ifl6xg"&&(G.innerHTML=jl),Jl=a(l),sl=U(l,"P",{"data-svelte-h":!0}),u(sl)!=="svelte-137tcdv"&&(sl.innerHTML=kl),fl=a(l),m(H.$$.fragment,l),Hl=a(l),yl=U(l,"P",{"data-svelte-h":!0}),u(yl)!=="svelte-ay6bgq"&&(yl.innerHTML=rl),bl=a(l),m(S.$$.fragment,l),Zl=a(l),tl=U(l,"P",{"data-svelte-h":!0}),u(tl)!=="svelte-18daai"&&(tl.innerHTML=_l),hl=a(l),m(D.$$.fragment,l),Sl=a(l),Ul=U(l,"P",{"data-svelte-h":!0}),u(Ul)!=="svelte-11b1ocr"&&(Ul.innerHTML=il),Cl=a(l),Y=U(l,"IMG",{src:!0,alt:!0,width:!0}),$l=a(l),q=U(l,"P",{"data-svelte-h":!0}),u(q)!=="svelte-vvikr8"&&(q.textContent=Dl),Tl=a(l),m(cl.$$.fragment,l),xl=a(l),m(nl.$$.fragment,l),ul=a(l),dl=U(l,"P",{"data-svelte-h":!0}),u(dl)!=="svelte-13mqn0"&&(dl.innerHTML=i),I=a(l),m(Vl.$$.fragment,l),Gl=a(l),m(Rl.$$.fragment,l),Bl=a(l),ql=U(l,"P",{"data-svelte-h":!0}),u(ql)!=="svelte-jkjlop"&&(ql.innerHTML=ne),M=a(l),m($.$$.fragment,l),qs=a(l),ae=U(l,"P",{"data-svelte-h":!0}),u(ae)!=="svelte-17klq8b"&&(ae.textContent=An),Ls=a(l),m(pe.$$.fragment,l),Ps=a(l),m(Me.$$.fragment,l),Os=a(l),re=U(l,"P",{"data-svelte-h":!0}),u(re)!=="svelte-6ttj3c"&&(re.textContent=Wn),Ks=a(l),m(ie.$$.fragment,l),lt=a(l),m(ce.$$.fragment,l),et=a(l),Je=U(l,"P",{"data-svelte-h":!0}),u(Je)!=="svelte-a94pa5"&&(Je.textContent=En),st=a(l),m(ye.$$.fragment,l),tt=a(l),m(Ue.$$.fragment,l),nt=a(l),Te=U(l,"P",{"data-svelte-h":!0}),u(Te)!=="svelte-ipwn4g"&&(Te.textContent=vn),at=a(l),m(me.$$.fragment,l),pt=a(l),m(oe.$$.fragment,l),Mt=a(l),je=U(l,"P",{"data-svelte-h":!0}),u(je)!=="svelte-n6sjbk"&&(je.textContent=Fn),rt=a(l),m(ue.$$.fragment,l),it=a(l),m(Ol.$$.fragment,l),ct=a(l),m(de.$$.fragment,l),Jt=a(l),m(we.$$.fragment,l),yt=a(l),fe=U(l,"P",{"data-svelte-h":!0}),u(fe)!=="svelte-1mokqbf"&&(fe.innerHTML=Yn),Ut=a(l),m(be.$$.fragment,l),Tt=a(l),he=U(l,"P",{"data-svelte-h":!0}),u(he)!=="svelte-sct5i7"&&(he.innerHTML=zn),mt=a(l),m(Kl.$$.fragment,l),ot=a(l),Ce=U(l,"P",{"data-svelte-h":!0}),u(Ce)!=="svelte-ld1hou"&&(Ce.innerHTML=Hn),jt=a(l),$e=U(l,"P",{"data-svelte-h":!0}),u($e)!=="svelte-11p5ywo"&&($e.textContent=Sn),ut=a(l),m(Ie.$$.fragment,l),dt=a(l),m(ke.$$.fragment,l),wt=a(l),_e=U(l,"P",{"data-svelte-h":!0}),u(_e)!=="svelte-z2on5m"&&(_e.innerHTML=Dn),ft=a(l),m(ge.$$.fragment,l),bt=a(l),m(Ze.$$.fragment,l),ht=a(l),xe=U(l,"P",{"data-svelte-h":!0}),u(xe)!=="svelte-1r2tov4"&&(xe.textContent=qn),Ct=a(l),Ve=U(l,"P",{"data-svelte-h":!0}),u(Ve)!=="svelte-1qyv0at"&&(Ve.textContent=Ln),$t=a(l),m(Ge.$$.fragment,l),It=a(l),Re=U(l,"P",{"data-svelte-h":!0}),u(Re)!=="svelte-durau1"&&(Re.textContent=Pn),kt=a(l),m(le.$$.fragment,l),_t=a(l),m(ee.$$.fragment,l),gt=a(l),Be=U(l,"P",{"data-svelte-h":!0}),u(Be)!=="svelte-1f2d1pj"&&(Be.textContent=On),Zt=a(l),m(Ne.$$.fragment,l),xt=a(l),Qe=U(l,"P",{"data-svelte-h":!0}),u(Qe)!=="svelte-aijya6"&&(Qe.textContent=Kn),Vt=a(l),Ql.l(l),Es=a(l),Xe=U(l,"P",{"data-svelte-h":!0}),u(Xe)!=="svelte-1mvoyq7"&&(Xe.textContent=la),Gt=a(l),m(Ae.$$.fragment,l),Rt=a(l),We=U(l,"P",{"data-svelte-h":!0}),u(We)!=="svelte-venz08"&&(We.innerHTML=ea),Bt=a(l),Ee=U(l,"P",{"data-svelte-h":!0}),u(Ee)!=="svelte-6qluvz"&&(Ee.innerHTML=sa),Nt=a(l),Al.l(l),vs=a(l),ve=U(l,"P",{"data-svelte-h":!0}),u(ve)!=="svelte-a4euy9"&&(ve.textContent=ta),Qt=a(l),m(Fe.$$.fragment,l),Xt=a(l),m(Ye.$$.fragment,l),At=a(l),ze=U(l,"P",{"data-svelte-h":!0}),u(ze)!=="svelte-1c7qagd"&&(ze.innerHTML=na),Wt=a(l),m(He.$$.fragment,l),Et=a(l),m(Se.$$.fragment,l),vt=a(l),De=U(l,"P",{"data-svelte-h":!0}),u(De)!=="svelte-nqema2"&&(De.textContent=aa),Ft=a(l),m(qe.$$.fragment,l),Yt=a(l),m(Le.$$.fragment,l),zt=a(l),Pe=U(l,"P",{"data-svelte-h":!0}),u(Pe)!=="svelte-j27xgx"&&(Pe.textContent=pa),Ht=a(l),m(Oe.$$.fragment,l),St=a(l),m(Ke.$$.fragment,l),Dt=a(l),El.l(l),Fs=a(l),m(ls.$$.fragment,l),qt=a(l),m(es.$$.fragment,l),Lt=a(l),wl&&wl.l(l),Ys=a(l),ss=U(l,"P",{"data-svelte-h":!0}),u(ss)!=="svelte-bv3cz9"&&(ss.innerHTML=Ma),Pt=a(l),ts=U(l,"P",{"data-svelte-h":!0}),u(ts)!=="svelte-6zqzva"&&(ts.innerHTML=ra),Ot=a(l),m(ns.$$.fragment,l),Kt=a(l),as=U(l,"P",{"data-svelte-h":!0}),u(as)!=="svelte-a4otbu"&&(as.innerHTML=ia),ln=a(l),m(ps.$$.fragment,l),en=a(l),Ms=U(l,"P",{"data-svelte-h":!0}),u(Ms)!=="svelte-sh9i2x"&&(Ms.textContent=ca),sn=a(l),rs=U(l,"P",{"data-svelte-h":!0}),u(rs)!=="svelte-jokngj"&&(rs.textContent=Ja),tn=a(l),m(is.$$.fragment,l),nn=a(l),m(cs.$$.fragment,l),an=a(l),Js=U(l,"P",{"data-svelte-h":!0}),u(Js)!=="svelte-mdpnd2"&&(Js.innerHTML=ya),pn=a(l),m(ys.$$.fragment,l),Mn=a(l),m(Us.$$.fragment,l),rn=a(l),m(Ts.$$.fragment,l),cn=a(l),m(ms.$$.fragment,l),Jn=a(l),os=U(l,"P",{"data-svelte-h":!0}),u(os)!=="svelte-bq8n0g"&&(os.textContent=Ua),yn=a(l),Fl.l(l),zs=a(l),js=U(l,"P",{"data-svelte-h":!0}),u(js)!=="svelte-1b8shi1"&&(js.textContent=Ta),Un=a(l),m(us.$$.fragment,l),Tn=a(l),ds=U(l,"P",{"data-svelte-h":!0}),u(ds)!=="svelte-1yddmca"&&(ds.textContent=ma),mn=a(l),m(ws.$$.fragment,l),on=a(l),fs=U(l,"P",{"data-svelte-h":!0}),u(fs)!=="svelte-ui4fcw"&&(fs.textContent=oa),jn=a(l),bs=U(l,"P",{"data-svelte-h":!0}),u(bs)!=="svelte-15j1x98"&&(bs.textContent=ja),un=a(l),m(hs.$$.fragment,l),dn=a(l),zl.l(l),Hs=a(l),al&&al.l(l),Ss=a(l),m(Cs.$$.fragment,l),wn=a(l),$s=U(l,"P",{"data-svelte-h":!0}),u($s)!=="svelte-1omme7t"&&($s.innerHTML=ua),fn=a(l),m(Is.$$.fragment,l),bn=a(l),m(ks.$$.fragment,l),hn=a(l),_s=U(l,"P",{"data-svelte-h":!0}),u(_s)!=="svelte-8tfwdd"&&(_s.textContent=da),Cn=a(l),m(gs.$$.fragment,l),$n=a(l),m(Zs.$$.fragment,l),In=a(l),xs=U(l,"P",{"data-svelte-h":!0}),u(xs)!=="svelte-rrnwlc"&&(xs.textContent=wa),kn=a(l),m(se.$$.fragment,l),_n=a(l),m(Vs.$$.fragment,l),gn=a(l),Ds=U(l,"P",{}),Xn(Ds).forEach(e),this.h()},h(){Il(p,"name","hf:doc:metadata"),Il(p,"content",dp),Aa(B.src,el="https://course-demos-marian-finetuned-kde4-en-to-fr.hf.space")||Il(B,"src",el),Il(B,"frameborder","0"),Il(B,"height","350"),Il(B,"title","Gradio app"),Il(B,"class","block dark:hidden container p-0 flex-grow space-iframe"),Il(B,"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"),Il(B,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),Il(G,"class","flex justify-center"),Il(G,"href","/huggingface-course/marian-finetuned-kde4-en-to-fr"),Aa(Y.src,Pl="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/language_tags.png")||Il(Y,"src",Pl),Il(Y,"alt","Language available for the KDE4 dataset."),Il(Y,"width","100%")},m(l,t){Da(document.head,p),s(l,b,t),s(l,r,t),s(l,w,t),o(g,l,t),s(l,f,t),o(Z,l,t),s(l,V,t),o(_,l,t),s(l,k,t),Gs[C].m(l,t),s(l,Q,t),s(l,pl,t),s(l,X,t),s(l,A,t),s(l,ml,t),o(W,l,t),s(l,E,t),s(l,v,t),s(l,ol,t),s(l,F,t),s(l,L,t),s(l,N,t),s(l,ll,t),s(l,B,t),s(l,d,t),s(l,G,t),s(l,Jl,t),s(l,sl,t),s(l,fl,t),o(H,l,t),s(l,Hl,t),s(l,yl,t),s(l,bl,t),o(S,l,t),s(l,Zl,t),s(l,tl,t),s(l,hl,t),o(D,l,t),s(l,Sl,t),s(l,Ul,t),s(l,Cl,t),s(l,Y,t),s(l,$l,t),s(l,q,t),s(l,Tl,t),o(cl,l,t),s(l,xl,t),o(nl,l,t),s(l,ul,t),s(l,dl,t),s(l,I,t),o(Vl,l,t),s(l,Gl,t),o(Rl,l,t),s(l,Bl,t),s(l,ql,t),s(l,M,t),o($,l,t),s(l,qs,t),s(l,ae,t),s(l,Ls,t),o(pe,l,t),s(l,Ps,t),o(Me,l,t),s(l,Os,t),s(l,re,t),s(l,Ks,t),o(ie,l,t),s(l,lt,t),o(ce,l,t),s(l,et,t),s(l,Je,t),s(l,st,t),o(ye,l,t),s(l,tt,t),o(Ue,l,t),s(l,nt,t),s(l,Te,t),s(l,at,t),o(me,l,t),s(l,pt,t),o(oe,l,t),s(l,Mt,t),s(l,je,t),s(l,rt,t),o(ue,l,t),s(l,it,t),o(Ol,l,t),s(l,ct,t),o(de,l,t),s(l,Jt,t),o(we,l,t),s(l,yt,t),s(l,fe,t),s(l,Ut,t),o(be,l,t),s(l,Tt,t),s(l,he,t),s(l,mt,t),o(Kl,l,t),s(l,ot,t),s(l,Ce,t),s(l,jt,t),s(l,$e,t),s(l,ut,t),o(Ie,l,t),s(l,dt,t),o(ke,l,t),s(l,wt,t),s(l,_e,t),s(l,ft,t),o(ge,l,t),s(l,bt,t),o(Ze,l,t),s(l,ht,t),s(l,xe,t),s(l,Ct,t),s(l,Ve,t),s(l,$t,t),o(Ge,l,t),s(l,It,t),s(l,Re,t),s(l,kt,t),o(le,l,t),s(l,_t,t),o(ee,l,t),s(l,gt,t),s(l,Be,t),s(l,Zt,t),o(Ne,l,t),s(l,xt,t),s(l,Qe,t),s(l,Vt,t),Rs[Nl].m(l,t),s(l,Es,t),s(l,Xe,t),s(l,Gt,t),o(Ae,l,t),s(l,Rt,t),s(l,We,t),s(l,Bt,t),s(l,Ee,t),s(l,Nt,t),Bs[Xl].m(l,t),s(l,vs,t),s(l,ve,t),s(l,Qt,t),o(Fe,l,t),s(l,Xt,t),o(Ye,l,t),s(l,At,t),s(l,ze,t),s(l,Wt,t),o(He,l,t),s(l,Et,t),o(Se,l,t),s(l,vt,t),s(l,De,t),s(l,Ft,t),o(qe,l,t),s(l,Yt,t),o(Le,l,t),s(l,zt,t),s(l,Pe,t),s(l,Ht,t),o(Oe,l,t),s(l,St,t),o(Ke,l,t),s(l,Dt,t),Ns[Wl].m(l,t),s(l,Fs,t),o(ls,l,t),s(l,qt,t),o(es,l,t),s(l,Lt,t),wl&&wl.m(l,t),s(l,Ys,t),s(l,ss,t),s(l,Pt,t),s(l,ts,t),s(l,Ot,t),o(ns,l,t),s(l,Kt,t),s(l,as,t),s(l,ln,t),o(ps,l,t),s(l,en,t),s(l,Ms,t),s(l,sn,t),s(l,rs,t),s(l,tn,t),o(is,l,t),s(l,nn,t),o(cs,l,t),s(l,an,t),s(l,Js,t),s(l,pn,t),o(ys,l,t),s(l,Mn,t),o(Us,l,t),s(l,rn,t),o(Ts,l,t),s(l,cn,t),o(ms,l,t),s(l,Jn,t),s(l,os,t),s(l,yn,t),Qs[vl].m(l,t),s(l,zs,t),s(l,js,t),s(l,Un,t),o(us,l,t),s(l,Tn,t),s(l,ds,t),s(l,mn,t),o(ws,l,t),s(l,on,t),s(l,fs,t),s(l,jn,t),s(l,bs,t),s(l,un,t),o(hs,l,t),s(l,dn,t),Xs[Yl].m(l,t),s(l,Hs,t),al&&al.m(l,t),s(l,Ss,t),o(Cs,l,t),s(l,wn,t),s(l,$s,t),s(l,fn,t),o(Is,l,t),s(l,bn,t),o(ks,l,t),s(l,hn,t),s(l,_s,t),s(l,Cn,t),o(gs,l,t),s(l,$n,t),o(Zs,l,t),s(l,In,t),s(l,xs,t),s(l,kn,t),o(se,l,t),s(l,_n,t),o(Vs,l,t),s(l,gn,t),s(l,Ds,t),Zn=!0},p(l,[t]){const Ga={};t&1&&(Ga.fw=l[0]),g.$set(Ga);let Vn=C;C=ba(l),C!==Vn&&(Ws(),J(Gs[Vn],1,1,()=>{Gs[Vn]=null}),As(),R=Gs[C],R||(R=Gs[C]=fa[C](l),R.c()),c(R,1),R.m(Q.parentNode,Q));const Ra={};t&2&&(Ra.$$scope={dirty:t,ctx:l}),Ol.$set(Ra);const Ba={};t&2&&(Ba.$$scope={dirty:t,ctx:l}),Kl.$set(Ba);const Na={};t&2&&(Na.$$scope={dirty:t,ctx:l}),le.$set(Na);const Qa={};t&2&&(Qa.$$scope={dirty:t,ctx:l}),ee.$set(Qa);let Gn=Nl;Nl=Ca(l),Nl!==Gn&&(Ws(),J(Rs[Gn],1,1,()=>{Rs[Gn]=null}),As(),Ql=Rs[Nl],Ql||(Ql=Rs[Nl]=ha[Nl](l),Ql.c()),c(Ql,1),Ql.m(Es.parentNode,Es));let Rn=Xl;Xl=Ia(l),Xl!==Rn&&(Ws(),J(Bs[Rn],1,1,()=>{Bs[Rn]=null}),As(),Al=Bs[Xl],Al||(Al=Bs[Xl]=$a[Xl](l),Al.c()),c(Al,1),Al.m(vs.parentNode,vs));let Bn=Wl;Wl=_a(l),Wl!==Bn&&(Ws(),J(Ns[Bn],1,1,()=>{Ns[Bn]=null}),As(),El=Ns[Wl],El||(El=Ns[Wl]=ka[Wl](l),El.c()),c(El,1),El.m(Fs.parentNode,Fs)),l[0]==="pt"?wl||(wl=Wa(),wl.c(),wl.m(Ys.parentNode,Ys)):wl&&(wl.d(1),wl=null);let Nn=vl;vl=Za(l),vl!==Nn&&(Ws(),J(Qs[Nn],1,1,()=>{Qs[Nn]=null}),As(),Fl=Qs[vl],Fl||(Fl=Qs[vl]=ga[vl](l),Fl.c()),c(Fl,1),Fl.m(zs.parentNode,zs));let Qn=Yl;Yl=Va(l),Yl!==Qn&&(Ws(),J(Xs[Qn],1,1,()=>{Xs[Qn]=null}),As(),zl=Xs[Yl],zl||(zl=Xs[Yl]=xa[Yl](l),zl.c()),c(zl,1),zl.m(Hs.parentNode,Hs)),l[0]==="pt"?al?t&1&&c(al,1):(al=Ea(),al.c(),c(al,1),al.m(Ss.parentNode,Ss)):al&&(Ws(),J(al,1,1,()=>{al=null}),As());const Xa={};t&2&&(Xa.$$scope={dirty:t,ctx:l}),se.$set(Xa)},i(l){Zn||(c(g.$$.fragment,l),c(Z.$$.fragment,l),c(_.$$.fragment,l),c(R),c(W.$$.fragment,l),c(H.$$.fragment,l),c(S.$$.fragment,l),c(D.$$.fragment,l),c(cl.$$.fragment,l),c(nl.$$.fragment,l),c(Vl.$$.fragment,l),c(Rl.$$.fragment,l),c($.$$.fragment,l),c(pe.$$.fragment,l),c(Me.$$.fragment,l),c(ie.$$.fragment,l),c(ce.$$.fragment,l),c(ye.$$.fragment,l),c(Ue.$$.fragment,l),c(me.$$.fragment,l),c(oe.$$.fragment,l),c(ue.$$.fragment,l),c(Ol.$$.fragment,l),c(de.$$.fragment,l),c(we.$$.fragment,l),c(be.$$.fragment,l),c(Kl.$$.fragment,l),c(Ie.$$.fragment,l),c(ke.$$.fragment,l),c(ge.$$.fragment,l),c(Ze.$$.fragment,l),c(Ge.$$.fragment,l),c(le.$$.fragment,l),c(ee.$$.fragment,l),c(Ne.$$.fragment,l),c(Ql),c(Ae.$$.fragment,l),c(Al),c(Fe.$$.fragment,l),c(Ye.$$.fragment,l),c(He.$$.fragment,l),c(Se.$$.fragment,l),c(qe.$$.fragment,l),c(Le.$$.fragment,l),c(Oe.$$.fragment,l),c(Ke.$$.fragment,l),c(El),c(ls.$$.fragment,l),c(es.$$.fragment,l),c(ns.$$.fragment,l),c(ps.$$.fragment,l),c(is.$$.fragment,l),c(cs.$$.fragment,l),c(ys.$$.fragment,l),c(Us.$$.fragment,l),c(Ts.$$.fragment,l),c(ms.$$.fragment,l),c(Fl),c(us.$$.fragment,l),c(ws.$$.fragment,l),c(hs.$$.fragment,l),c(zl),c(al),c(Cs.$$.fragment,l),c(Is.$$.fragment,l),c(ks.$$.fragment,l),c(gs.$$.fragment,l),c(Zs.$$.fragment,l),c(se.$$.fragment,l),c(Vs.$$.fragment,l),Zn=!0)},o(l){J(g.$$.fragment,l),J(Z.$$.fragment,l),J(_.$$.fragment,l),J(R),J(W.$$.fragment,l),J(H.$$.fragment,l),J(S.$$.fragment,l),J(D.$$.fragment,l),J(cl.$$.fragment,l),J(nl.$$.fragment,l),J(Vl.$$.fragment,l),J(Rl.$$.fragment,l),J($.$$.fragment,l),J(pe.$$.fragment,l),J(Me.$$.fragment,l),J(ie.$$.fragment,l),J(ce.$$.fragment,l),J(ye.$$.fragment,l),J(Ue.$$.fragment,l),J(me.$$.fragment,l),J(oe.$$.fragment,l),J(ue.$$.fragment,l),J(Ol.$$.fragment,l),J(de.$$.fragment,l),J(we.$$.fragment,l),J(be.$$.fragment,l),J(Kl.$$.fragment,l),J(Ie.$$.fragment,l),J(ke.$$.fragment,l),J(ge.$$.fragment,l),J(Ze.$$.fragment,l),J(Ge.$$.fragment,l),J(le.$$.fragment,l),J(ee.$$.fragment,l),J(Ne.$$.fragment,l),J(Ql),J(Ae.$$.fragment,l),J(Al),J(Fe.$$.fragment,l),J(Ye.$$.fragment,l),J(He.$$.fragment,l),J(Se.$$.fragment,l),J(qe.$$.fragment,l),J(Le.$$.fragment,l),J(Oe.$$.fragment,l),J(Ke.$$.fragment,l),J(El),J(ls.$$.fragment,l),J(es.$$.fragment,l),J(ns.$$.fragment,l),J(ps.$$.fragment,l),J(is.$$.fragment,l),J(cs.$$.fragment,l),J(ys.$$.fragment,l),J(Us.$$.fragment,l),J(Ts.$$.fragment,l),J(ms.$$.fragment,l),J(Fl),J(us.$$.fragment,l),J(ws.$$.fragment,l),J(hs.$$.fragment,l),J(zl),J(al),J(Cs.$$.fragment,l),J(Is.$$.fragment,l),J(ks.$$.fragment,l),J(gs.$$.fragment,l),J(Zs.$$.fragment,l),J(se.$$.fragment,l),J(Vs.$$.fragment,l),Zn=!1},d(l){l&&(e(b),e(r),e(w),e(f),e(V),e(k),e(Q),e(pl),e(X),e(A),e(ml),e(E),e(v),e(ol),e(F),e(L),e(N),e(ll),e(B),e(d),e(G),e(Jl),e(sl),e(fl),e(Hl),e(yl),e(bl),e(Zl),e(tl),e(hl),e(Sl),e(Ul),e(Cl),e(Y),e($l),e(q),e(Tl),e(xl),e(ul),e(dl),e(I),e(Gl),e(Bl),e(ql),e(M),e(qs),e(ae),e(Ls),e(Ps),e(Os),e(re),e(Ks),e(lt),e(et),e(Je),e(st),e(tt),e(nt),e(Te),e(at),e(pt),e(Mt),e(je),e(rt),e(it),e(ct),e(Jt),e(yt),e(fe),e(Ut),e(Tt),e(he),e(mt),e(ot),e(Ce),e(jt),e($e),e(ut),e(dt),e(wt),e(_e),e(ft),e(bt),e(ht),e(xe),e(Ct),e(Ve),e($t),e(It),e(Re),e(kt),e(_t),e(gt),e(Be),e(Zt),e(xt),e(Qe),e(Vt),e(Es),e(Xe),e(Gt),e(Rt),e(We),e(Bt),e(Ee),e(Nt),e(vs),e(ve),e(Qt),e(Xt),e(At),e(ze),e(Wt),e(Et),e(vt),e(De),e(Ft),e(Yt),e(zt),e(Pe),e(Ht),e(St),e(Dt),e(Fs),e(qt),e(Lt),e(Ys),e(ss),e(Pt),e(ts),e(Ot),e(Kt),e(as),e(ln),e(en),e(Ms),e(sn),e(rs),e(tn),e(nn),e(an),e(Js),e(pn),e(Mn),e(rn),e(cn),e(Jn),e(os),e(yn),e(zs),e(js),e(Un),e(Tn),e(ds),e(mn),e(on),e(fs),e(jn),e(bs),e(un),e(dn),e(Hs),e(Ss),e(wn),e($s),e(fn),e(bn),e(hn),e(_s),e(Cn),e($n),e(In),e(xs),e(kn),e(_n),e(gn),e(Ds)),e(p),j(g,l),j(Z,l),j(_,l),Gs[C].d(l),j(W,l),j(H,l),j(S,l),j(D,l),j(cl,l),j(nl,l),j(Vl,l),j(Rl,l),j($,l),j(pe,l),j(Me,l),j(ie,l),j(ce,l),j(ye,l),j(Ue,l),j(me,l),j(oe,l),j(ue,l),j(Ol,l),j(de,l),j(we,l),j(be,l),j(Kl,l),j(Ie,l),j(ke,l),j(ge,l),j(Ze,l),j(Ge,l),j(le,l),j(ee,l),j(Ne,l),Rs[Nl].d(l),j(Ae,l),Bs[Xl].d(l),j(Fe,l),j(Ye,l),j(He,l),j(Se,l),j(qe,l),j(Le,l),j(Oe,l),j(Ke,l),Ns[Wl].d(l),j(ls,l),j(es,l),wl&&wl.d(l),j(ns,l),j(ps,l),j(is,l),j(cs,l),j(ys,l),j(Us,l),j(Ts,l),j(ms,l),Qs[vl].d(l),j(us,l),j(ws,l),j(hs,l),Xs[Yl].d(l),al&&al.d(l),j(Cs,l),j(Is,l),j(ks,l),j(gs,l),j(Zs,l),j(se,l),j(Vs,l)}}}const dp='{"title":"翻译","local":"翻译","sections":[{"title":"准备数据","local":"准备数据","sections":[{"title":"KDE4 数据集","local":"KDE4 数据集","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}],"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 wp(x,p,b){let r="pt";return Ya(()=>{const w=new URLSearchParams(window.location.search);b(0,r=w.get("fw")||"pt")}),[r]}class gp extends za{constructor(p){super(),Ha(this,p,wp,up,Fa,{})}}export{gp as component};
