import{s as Ea,f as Qa,o as va,n as ql}from"../chunks/scheduler.37c15a92.js";import{S as Fa,i as Ya,g as y,s as n,r as T,A as za,h as U,f as e,c as a,j as Nn,u as m,x as u,k as Cl,y as Ha,a as s,v as o,t as J,b as Xs,d as i,w as j,p as As}from"../chunks/index.2bf4358c.js";import{T as te}from"../chunks/Tip.363c041f.js";import{Y as Zn}from"../chunks/Youtube.1e50a667.js";import{C as h}from"../chunks/CodeBlock.4e987730.js";import{D as Wa}from"../chunks/DocNotebookDropdown.efc1fb7c.js";import{F as Sa}from"../chunks/FrameworkSwitchCourse.8d4d4ab6.js";import{H as _l,E as Da}from"../chunks/getInferenceSnippets.7bb25a7f.js";function qa(x){let p,f;return p=new Wa({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),f=!0},i(r){f||(i(p.$$.fragment,r),f=!0)},o(r){J(p.$$.fragment,r),f=!1},d(r){j(p,r)}}}function La(x){let p,f;return p=new Wa({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),f=!0},i(r){f||(i(p.$$.fragment,r),f=!0)},o(r){J(p.$$.fragment,r),f=!1},d(r){j(p,r)}}}function Pa(x){let p,f="✏️ <strong>轮到你了！</strong> 另一个在法语中经常使用的英语单词是“email”。在训练数据集中找到使用这个词的第一个样本。在数据集中它是如何翻译的？预训练模型如何翻译同一个英文句子？";return{c(){p=y("p"),p.innerHTML=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-l4ry1q"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},p:ql,d(r){r&&e(p)}}}function Oa(x){let p,f="💡 如果你在使用一个多语言的 tokenizer，比如 mBART，mBART-50，或者 M2M100，你需要通过设置 <code>tokenizer.src_lang</code> 和 <code>tokenizer.tgt_lang</code> 来在 tokenizer 中指定输入和目标的语言代码。";return{c(){p=y("p"),p.innerHTML=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-bd9swy"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},p:ql,d(r){r&&e(p)}}}function Ka(x){let p,f="💡 如果你正在使用 T5 模型（更具体地说，一个 <code>t5-xxx</code> checkpoint ），模型会期望文本输入有一个前缀指示目前的任务，比如 <code>translate: English to French:</code> 。";return{c(){p=y("p"),p.innerHTML=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-so9xz0"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},p:ql,d(r){r&&e(p)}}}function lp(x){let p,f="⚠️ 我们不需要对待遇测的目标设置注意力掩码，因为模型序列到序列的不会需要它。不过，我们应该将填充（padding） token 对应的标签设置为 <code>-100</code> ，以便在 loss 计算中忽略它们。由于我们正在使用动态填充，这将在稍后由我们的数据整理器完成，但是如果你在此处就打算进行填充，你应该调整预处理函数，将所有填充（padding） token 对应的标签设置为 <code>-100</code> 。";return{c(){p=y("p"),p.innerHTML=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-1orli4b"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},p:ql,d(r){r&&e(p)}}}function ep(x){let p,f,r,w="首先，我们需要一个模型来进行微调。我们将使用常用的 <code>AutoModel</code> API：",Z,b,g,V,_;return p=new _l({props:{title:"使用 Keras 微调模型",local:"使用 Keras 微调模型",headingTag:"h2"}}),b=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:[tp]},$$scope:{ctx:x}}}),{c(){T(p.$$.fragment),f=n(),r=y("p"),r.innerHTML=w,Z=n(),T(b.$$.fragment),g=n(),T(V.$$.fragment)},l(I){m(p.$$.fragment,I),f=a(I),r=U(I,"P",{"data-svelte-h":!0}),u(r)!=="svelte-itbeyk"&&(r.innerHTML=w),Z=a(I),m(b.$$.fragment,I),g=a(I),m(V.$$.fragment,I)},m(I,k){o(p,I,k),s(I,f,k),s(I,r,k),s(I,Z,k),o(b,I,k),s(I,g,k),o(V,I,k),_=!0},i(I){_||(i(p.$$.fragment,I),i(b.$$.fragment,I),i(V.$$.fragment,I),_=!0)},o(I){J(p.$$.fragment,I),J(b.$$.fragment,I),J(V.$$.fragment,I),_=!1},d(I){I&&(e(f),e(r),e(Z),e(g)),j(p,I),j(b,I),j(V,I)}}}function sp(x){let p,f,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> 方法来预测输入的输出结果，并且可以正确处理这种序列到序列的评估。当我们讨论评估指标时，我们将更详细地探讨这一点。',Z,b,g="首先，我们需要一个模型来进行微调。我们将使用常用的 <code>AutoModel</code> API：",V,_,I;return p=new _l({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),f=n(),r=y("p"),r.innerHTML=w,Z=n(),b=y("p"),b.innerHTML=g,V=n(),T(_.$$.fragment)},l(k){m(p.$$.fragment,k),f=a(k),r=U(k,"P",{"data-svelte-h":!0}),u(r)!=="svelte-1v395ca"&&(r.innerHTML=w),Z=a(k),b=U(k,"P",{"data-svelte-h":!0}),u(b)!=="svelte-itbeyk"&&(b.innerHTML=g),V=a(k),m(_.$$.fragment,k)},m(k,R){o(p,k,R),s(k,f,R),s(k,r,R),s(k,Z,R),s(k,b,R),s(k,V,R),o(_,k,R),I=!0},i(k){I||(i(p.$$.fragment,k),i(_.$$.fragment,k),I=!0)},o(k){J(p.$$.fragment,k),J(_.$$.fragment,k),I=!1},d(k){k&&(e(f),e(r),e(Z),e(b),e(V)),j(p,k),j(_,k)}}}function tp(x){let p,f="💡 <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=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-101ggn6"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},p:ql,d(r){r&&e(p)}}}function np(x){let p,f;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),f=!0},i(r){f||(i(p.$$.fragment,r),f=!0)},o(r){J(p.$$.fragment,r),f=!1},d(r){j(p,r)}}}function ap(x){let p,f;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),f=!0},i(r){f||(i(p.$$.fragment,r),f=!0)},o(r){J(p.$$.fragment,r),f=!1},d(r){j(p,r)}}}function pp(x){let p,f="我们现在可以使用 <code>data_collator</code> 将我们的每个数据集转换为 <code>tf.data.Dataset</code> ，这样就完成了数据整理：",r,w,Z;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=f,r=n(),T(w.$$.fragment)},l(b){p=U(b,"P",{"data-svelte-h":!0}),u(p)!=="svelte-11wa54c"&&(p.innerHTML=f),r=a(b),m(w.$$.fragment,b)},m(b,g){s(b,p,g),s(b,r,g),o(w,b,g),Z=!0},i(b){Z||(i(w.$$.fragment,b),Z=!0)},o(b){J(w.$$.fragment,b),Z=!1},d(b){b&&(e(p),e(r)),j(w,b)}}}function Mp(x){let p,f="把 <code>data_collator</code> 传递给 <code>Seq2SeqTrainer</code> 后就完成了数据整理。接下来，让我们看一下评估指标。";return{c(){p=y("p"),p.innerHTML=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-bytd29"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},i:ql,o:ql,d(r){r&&e(p)}}}function Xa(x){let p,f="<code>Seq2SeqTrainer</code> 是 <code>Trainer</code> 类的一个子类，它的主要增强特性是在评估或预测时使用 <code>generate()</code> 方法。在训练过程中，模型会利用 <code>decoder_input_ids</code> 和一个特殊的注意力掩码来加速训练。这种方法允许模型在预测下一个token时看到部分目标序列，但确保它不会使用预测token之后的信息。这种优化策略显著提高了训练效率。然而，在实际的推理过程中，我们没有真实的标签值，因此无法生成 <code>decoder_input_ids</code> 和相应的注意力掩码。这意味着我们无法在推理时使用这种训练时的优化方法。",r,w,Z='为了确保评估结果能够准确反映模型在实际使用中的表现，我们应该在评估阶段模拟真实推理的条件。这意味着我们需要使用在 <a href="/course/chapter1/6">第一章</a> 中介绍的 🤗 Transformers 库中的 <code>generate()</code> 方法。该方法能够逐个生成token，真实地模拟推理过程，而不是依赖于训练时的优化技巧。要启用这个功能，我们需要在训练时添加 <code>predict_with_generate=True</code> 参数。这样做可以确保我们的评估结果更加接近模型在实际应用中的表现。';return{c(){p=y("p"),p.innerHTML=f,r=n(),w=y("p"),w.innerHTML=Z},l(b){p=U(b,"P",{"data-svelte-h":!0}),u(p)!=="svelte-1y7422"&&(p.innerHTML=f),r=a(b),w=U(b,"P",{"data-svelte-h":!0}),u(w)!=="svelte-5149qw"&&(w.innerHTML=Z)},m(b,g){s(b,p,g),s(b,r,g),s(b,w,g)},d(b){b&&(e(p),e(r),e(w))}}}function rp(x){let p,f="为了将模型的输出转化为评估指标可以使用的文本，我们将使用 <code>tokenizer.batch_decode()</code> 方法。因为 tokenizer 会自动处理填充的 tokens，所以我们只需要清理标签中的所有 <code>-100</code> token：",r,w,Z;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=f,r=n(),T(w.$$.fragment)},l(b){p=U(b,"P",{"data-svelte-h":!0}),u(p)!=="svelte-gr0ukh"&&(p.innerHTML=f),r=a(b),m(w.$$.fragment,b)},m(b,g){s(b,p,g),s(b,r,g),o(w,b,g),Z=!0},i(b){Z||(i(w.$$.fragment,b),Z=!0)},o(b){J(w.$$.fragment,b),Z=!1},d(b){b&&(e(p),e(r)),j(w,b)}}}function cp(x){let p,f="为了将模型的输出转化为评估指标可以使用的文本，我们将利用 <code>tokenizer.batch_decode()</code> 方法。因为 tokenizer 会自动处理填充 <code>tokens</code>，所以我们只需要清理所有标签中的 填充的<code>-100</code> 。让我们定义一个函数，这个函数会接收一个模型和一个数据集，并在其上计算 BLEU 指标。",r,w,Z='我们还将使用一个显著提升性能的技巧 - 使用 <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 编译。',b,g,V;return g=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=f,r=n(),w=y("p"),w.innerHTML=Z,b=n(),T(g.$$.fragment)},l(_){p=U(_,"P",{"data-svelte-h":!0}),u(p)!=="svelte-18d2huy"&&(p.innerHTML=f),r=a(_),w=U(_,"P",{"data-svelte-h":!0}),u(w)!=="svelte-t5bv7v"&&(w.innerHTML=Z),b=a(_),m(g.$$.fragment,_)},m(_,I){s(_,p,I),s(_,r,I),s(_,w,I),s(_,b,I),o(g,_,I),V=!0},i(_){V||(i(g.$$.fragment,_),V=!0)},o(_){J(g.$$.fragment,_),V=!1},d(_){_&&(e(p),e(r),e(w),e(b)),j(g,_)}}}function ip(x){let p,f="完成这些步骤之后，我们就可以定义我们的 <code>Seq2SeqTrainingArguments</code> 了。与 <code>Trainer</code> 一样，它是 <code>TrainingArguments</code> 的子类，其中包含更多可以设置的字段：",r,w,Z,b,g="除了通常的超参数（如学习率、训练轮数、批次大小和一些权重衰减）之外，这里的部分参数与我们在前面章节看到的有一些不同：",V,_,I="<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>",k,R,K='请注意，你可以使用 <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> 。',Tl,W,Y,L,X="最后，我们将所有内容传递给 <code>Seq2SeqTrainer</code> ：",ml,E,z,P,A="在开始训练之前，我们先查看一下我们的模型目前的 BLEU 分数，以确保我们的微调并未使情况变得更糟。这个命令需要一些时间，所以你可以在执行期间去喝杯咖啡：",Il,O,N,ll,v,B,Ml="BLEU 得分为 39 并不算太差，这反映了我们的模型已经擅长将英语句子翻译成法语句子。",el,Q,d="接下来是训练，这也需要一些时间：",G,sl,ol,Jl,rl="请注意，在训练过程中，每当保存模型时（这里是每个 epoch），它都会在后台将模型上传到 Hub。这样，如有必要，你将能够在另一台机器上继续你的训练。",dl,H,Ll="训练完成后，我们再次评估我们的模型——希望我们会看到 BLEU 分数有所提高！",wl,S,bl,D,Sl,yl,cl="可以看到近 14 点的改进，这很棒！",fl,q,Pl="最后，我们使用 <code>push_to_hub()</code> 方法来确保我们上传了模型最新的版本。 <code>Trainer</code> 还创建了一张包含所有评估结果的模型卡片并上传到 Hub 。这个模型卡片包含了可以帮助 Hub 为推理演示选择小部件的元数据，通常情况下我们不需要做额外的更改，因为它可以从模型类中推断出正确的小部件，但在这个示例中，它只能通过模型类推断这是一个序列到序列的问题，所以我们补充一下具体的模型类别。",hl,F,Zl,tl,$l="如果你想检查命令执行的结果，此命令将返回它刚刚执行的提交的 URL，你可以打开 url 进行检查：",gl,nl,jl,Ul,il="在此阶段，你可以在 Model Hub 上使用推理小部件来测试你的模型，并与你的朋友分享。你已经成功地在翻译任务上进行了模型的微调，恭喜你！",xl,al,kl="如果你想更深入地了解训练循环，我们现在将向你展示如何使用 🤗 Accelerate 做同样的事情。",Vl;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}}),W=new te({props:{$$slots:{default:[yp]},$$scope:{ctx:x}}}),E=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}}),O=new h({props:{code:"dHJhaW5lci5ldmFsdWF0ZShtYXhfbGVuZ3RoJTNEbWF4X2xlbmd0aCk=",highlighted:"trainer.evaluate(max_length=max_length)",wrap:!1}}),ll=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}}),sl=new h({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:"trainer.train()",wrap:!1}}),S=new h({props:{code:"dHJhaW5lci5ldmFsdWF0ZShtYXhfbGVuZ3RoJTNEbWF4X2xlbmd0aCk=",highlighted:"trainer.evaluate(max_length=max_length)",wrap:!1}}),D=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}}),F=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}}),nl=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=f,r=n(),T(w.$$.fragment),Z=n(),b=y("p"),b.textContent=g,V=n(),_=y("ul"),_.innerHTML=I,k=n(),R=y("p"),R.innerHTML=K,Tl=n(),T(W.$$.fragment),Y=n(),L=y("p"),L.innerHTML=X,ml=n(),T(E.$$.fragment),z=n(),P=y("p"),P.textContent=A,Il=n(),T(O.$$.fragment),N=n(),T(ll.$$.fragment),v=n(),B=y("p"),B.textContent=Ml,el=n(),Q=y("p"),Q.textContent=d,G=n(),T(sl.$$.fragment),ol=n(),Jl=y("p"),Jl.textContent=rl,dl=n(),H=y("p"),H.textContent=Ll,wl=n(),T(S.$$.fragment),bl=n(),T(D.$$.fragment),Sl=n(),yl=y("p"),yl.textContent=cl,fl=n(),q=y("p"),q.innerHTML=Pl,hl=n(),T(F.$$.fragment),Zl=n(),tl=y("p"),tl.textContent=$l,gl=n(),T(nl.$$.fragment),jl=n(),Ul=y("p"),Ul.textContent=il,xl=n(),al=y("p"),al.textContent=kl},l(c){p=U(c,"P",{"data-svelte-h":!0}),u(p)!=="svelte-1qmt64b"&&(p.innerHTML=f),r=a(c),m(w.$$.fragment,c),Z=a(c),b=U(c,"P",{"data-svelte-h":!0}),u(b)!=="svelte-p1srp5"&&(b.textContent=g),V=a(c),_=U(c,"UL",{"data-svelte-h":!0}),u(_)!=="svelte-1bsmz7p"&&(_.innerHTML=I),k=a(c),R=U(c,"P",{"data-svelte-h":!0}),u(R)!=="svelte-1chdo7e"&&(R.innerHTML=K),Tl=a(c),m(W.$$.fragment,c),Y=a(c),L=U(c,"P",{"data-svelte-h":!0}),u(L)!=="svelte-jj26i9"&&(L.innerHTML=X),ml=a(c),m(E.$$.fragment,c),z=a(c),P=U(c,"P",{"data-svelte-h":!0}),u(P)!=="svelte-1v7m6nv"&&(P.textContent=A),Il=a(c),m(O.$$.fragment,c),N=a(c),m(ll.$$.fragment,c),v=a(c),B=U(c,"P",{"data-svelte-h":!0}),u(B)!=="svelte-xajbnv"&&(B.textContent=Ml),el=a(c),Q=U(c,"P",{"data-svelte-h":!0}),u(Q)!=="svelte-mtrcqq"&&(Q.textContent=d),G=a(c),m(sl.$$.fragment,c),ol=a(c),Jl=U(c,"P",{"data-svelte-h":!0}),u(Jl)!=="svelte-bm11br"&&(Jl.textContent=rl),dl=a(c),H=U(c,"P",{"data-svelte-h":!0}),u(H)!=="svelte-143pqop"&&(H.textContent=Ll),wl=a(c),m(S.$$.fragment,c),bl=a(c),m(D.$$.fragment,c),Sl=a(c),yl=U(c,"P",{"data-svelte-h":!0}),u(yl)!=="svelte-31z6tc"&&(yl.textContent=cl),fl=a(c),q=U(c,"P",{"data-svelte-h":!0}),u(q)!=="svelte-2jw0f7"&&(q.innerHTML=Pl),hl=a(c),m(F.$$.fragment,c),Zl=a(c),tl=U(c,"P",{"data-svelte-h":!0}),u(tl)!=="svelte-hof6xv"&&(tl.textContent=$l),gl=a(c),m(nl.$$.fragment,c),jl=a(c),Ul=U(c,"P",{"data-svelte-h":!0}),u(Ul)!=="svelte-1hcxxrd"&&(Ul.textContent=il),xl=a(c),al=U(c,"P",{"data-svelte-h":!0}),u(al)!=="svelte-5myn0z"&&(al.textContent=kl)},m(c,$){s(c,p,$),s(c,r,$),o(w,c,$),s(c,Z,$),s(c,b,$),s(c,V,$),s(c,_,$),s(c,k,$),s(c,R,$),s(c,Tl,$),o(W,c,$),s(c,Y,$),s(c,L,$),s(c,ml,$),o(E,c,$),s(c,z,$),s(c,P,$),s(c,Il,$),o(O,c,$),s(c,N,$),o(ll,c,$),s(c,v,$),s(c,B,$),s(c,el,$),s(c,Q,$),s(c,G,$),o(sl,c,$),s(c,ol,$),s(c,Jl,$),s(c,dl,$),s(c,H,$),s(c,wl,$),o(S,c,$),s(c,bl,$),o(D,c,$),s(c,Sl,$),s(c,yl,$),s(c,fl,$),s(c,q,$),s(c,hl,$),o(F,c,$),s(c,Zl,$),s(c,tl,$),s(c,gl,$),o(nl,c,$),s(c,jl,$),s(c,Ul,$),s(c,xl,$),s(c,al,$),Vl=!0},i(c){Vl||(i(w.$$.fragment,c),i(W.$$.fragment,c),i(E.$$.fragment,c),i(O.$$.fragment,c),i(ll.$$.fragment,c),i(sl.$$.fragment,c),i(S.$$.fragment,c),i(D.$$.fragment,c),i(F.$$.fragment,c),i(nl.$$.fragment,c),Vl=!0)},o(c){J(w.$$.fragment,c),J(W.$$.fragment,c),J(E.$$.fragment,c),J(O.$$.fragment,c),J(ll.$$.fragment,c),J(sl.$$.fragment,c),J(S.$$.fragment,c),J(D.$$.fragment,c),J(F.$$.fragment,c),J(nl.$$.fragment,c),Vl=!1},d(c){c&&(e(p),e(r),e(Z),e(b),e(V),e(_),e(k),e(R),e(Tl),e(Y),e(L),e(ml),e(z),e(P),e(Il),e(N),e(v),e(B),e(el),e(Q),e(G),e(ol),e(Jl),e(dl),e(H),e(wl),e(bl),e(Sl),e(yl),e(fl),e(q),e(hl),e(Zl),e(tl),e(gl),e(jl),e(Ul),e(xl),e(al)),j(w,c),j(W,c),j(E,c),j(O,c),j(ll,c),j(sl,c),j(S,c),j(D,c),j(F,c),j(nl,c)}}}function Jp(x){let p,f="在我们开始之前，让我们看看我们在没有任何训练的情况下我们的模型效果怎么样。",r,w,Z,b,g,V,_="现在可以准备编译和训练模型了。运行 <code>tf.keras.mixed_precision.set_global_policy(&quot;mixed_ﬂoat16&quot;)</code> 后， Keras 将使用 ﬂoat16 精度进行训练，这样可以显著提高支持半精度 GPU（Nvidia 20xx/V100 或更高版本）的训练速度",I,k,R,K,Tl='接下来就像我们在 <a href="/course/chapter7/2">第 2 节</a> 中学到的，我们将定义一个 <code>PushToHubCallback</code> 回调函数，并拟合模型的时候添加该回调函数，这样就可以训练期间将我们的模型上传到 Hub。',W,Y,L,X,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> 。',E,z,P,A,Il="最后，让我们看看训练结束后我们的模型的 BLEU 的分数：",O,N,ll,v,B,Ml,el="在这个阶段，你可以使用模型中心上的推理小部件来测试你的模型并与你的朋友分享。恭喜你，你已经成功地在翻译任务上微调了一个模型！",Q;return w=new h({props:{code:"cHJpbnQoY29tcHV0ZV9tZXRyaWNzKCkp",highlighted:'<span class="hljs-built_in">print</span>(compute_metrics())',wrap:!1}}),b=new h({props:{code:"JTdCJ2JsZXUnJTNBJTIwMzMuMjY5ODM3MDE0NTQ3MzMlN0Q=",highlighted:'{&#x27;bleu&#x27;: <span class="hljs-number">33.26983701454733</span>}',wrap:!1}}),k=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}}),Y=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}}),z=new te({props:{$$slots:{default:[Up]},$$scope:{ctx:x}}}),N=new h({props:{code:"cHJpbnQoY29tcHV0ZV9tZXRyaWNzKCkp",highlighted:'<span class="hljs-built_in">print</span>(compute_metrics())',wrap:!1}}),v=new h({props:{code:"JTdCJ2JsZXUnJTNBJTIwNTcuMzM0MDY2MjcxNTQ1ODY1JTdE",highlighted:'{&#x27;bleu&#x27;: <span class="hljs-number">57.334066271545865</span>}',wrap:!1}}),{c(){p=y("p"),p.textContent=f,r=n(),T(w.$$.fragment),Z=n(),T(b.$$.fragment),g=n(),V=y("p"),V.innerHTML=_,I=n(),T(k.$$.fragment),R=n(),K=y("p"),K.innerHTML=Tl,W=n(),T(Y.$$.fragment),L=n(),X=y("p"),X.innerHTML=ml,E=n(),T(z.$$.fragment),P=n(),A=y("p"),A.textContent=Il,O=n(),T(N.$$.fragment),ll=n(),T(v.$$.fragment),B=n(),Ml=y("p"),Ml.textContent=el},l(d){p=U(d,"P",{"data-svelte-h":!0}),u(p)!=="svelte-lgd8u2"&&(p.textContent=f),r=a(d),m(w.$$.fragment,d),Z=a(d),m(b.$$.fragment,d),g=a(d),V=U(d,"P",{"data-svelte-h":!0}),u(V)!=="svelte-m497wj"&&(V.innerHTML=_),I=a(d),m(k.$$.fragment,d),R=a(d),K=U(d,"P",{"data-svelte-h":!0}),u(K)!=="svelte-cj4fke"&&(K.innerHTML=Tl),W=a(d),m(Y.$$.fragment,d),L=a(d),X=U(d,"P",{"data-svelte-h":!0}),u(X)!=="svelte-6k5dz8"&&(X.innerHTML=ml),E=a(d),m(z.$$.fragment,d),P=a(d),A=U(d,"P",{"data-svelte-h":!0}),u(A)!=="svelte-1c01lu3"&&(A.textContent=Il),O=a(d),m(N.$$.fragment,d),ll=a(d),m(v.$$.fragment,d),B=a(d),Ml=U(d,"P",{"data-svelte-h":!0}),u(Ml)!=="svelte-8h8zkn"&&(Ml.textContent=el)},m(d,G){s(d,p,G),s(d,r,G),o(w,d,G),s(d,Z,G),o(b,d,G),s(d,g,G),s(d,V,G),s(d,I,G),o(k,d,G),s(d,R,G),s(d,K,G),s(d,W,G),o(Y,d,G),s(d,L,G),s(d,X,G),s(d,E,G),o(z,d,G),s(d,P,G),s(d,A,G),s(d,O,G),o(N,d,G),s(d,ll,G),o(v,d,G),s(d,B,G),s(d,Ml,G),Q=!0},i(d){Q||(i(w.$$.fragment,d),i(b.$$.fragment,d),i(k.$$.fragment,d),i(Y.$$.fragment,d),i(z.$$.fragment,d),i(N.$$.fragment,d),i(v.$$.fragment,d),Q=!0)},o(d){J(w.$$.fragment,d),J(b.$$.fragment,d),J(k.$$.fragment,d),J(Y.$$.fragment,d),J(z.$$.fragment,d),J(N.$$.fragment,d),J(v.$$.fragment,d),Q=!1},d(d){d&&(e(p),e(r),e(Z),e(g),e(V),e(I),e(R),e(K),e(W),e(L),e(X),e(E),e(P),e(A),e(O),e(ll),e(B),e(Ml)),j(w,d),j(b,d),j(k,d),j(Y,d),j(z,d),j(N,d),j(v,d)}}}function yp(x){let p,f="💡如果你使用的输出目录已经存在一个同名的文件夹，则它应该是推送的仓库克隆在本地的版本。如果不是，你将在定义你的 <code>Seq2SeqTrainer</code> 名称时会遇到错误，并且需要设置一个新名称。";return{c(){p=y("p"),p.innerHTML=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-19lswvy"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},p:ql,d(r){r&&e(p)}}}function Up(x){let p,f="💡如果正在使用的输出目录已经存在一个同名的文件夹，则它应该是目标推送仓库的在本地克隆在本地的版本。如果不是，当调用 <code>model.fit()</code> 时会收到错误，并需要设置一个新的路径。";return{c(){p=y("p"),p.innerHTML=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-2lusqh"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},p:ql,d(r){r&&e(p)}}}function Aa(x){let p,f,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> 中做的非常相似。',Z,b,g,V,_="由于这里的步骤在之前的章节已经出现过很多次，因此这里只做简略说明。首先，我们将数据集设置为 <code>torch</code> 格式，这样可以将数据集的格式转换为 <code>PyTorch</code> 张量，然后我们用数据集构建 <code>DataLoader</code> ：",I,k,R,K,Tl="接下来我们重新实例化我们的模型，以确保我们不会继续上一节的微调，而是再次从预训练模型开始重新训练：",W,Y,L,X,ml="然后我们需要一个优化器：",E,z,P,A,Il="准备好这些对象，我们就可以将它们发送到 <code>accelerator.prepare()</code> 方法中。请记住，如果你想在 Colab Notebook 上使用 TPU 进行训练，你需要将所有这些代码移动到一个训练函数中，并且这个训练函数不应该包含实例化 <code>Accelerator</code> <code>的代码。换句话说，Accelerator</code> 的实例化应该在这个函数之外进行。这么做的原因是，TPU 在 Colab 中的工作方式有些特殊。TPU 运行时会重新执行整个单元格的代码，因此如果 <code>Accelerator</code> 的实例化在训练函数内部，它可能会被多次实例化，导致错误。",O,N,ll,v,B="现在我们已经将我们的 <code>train_dataloader</code> 发送到 <code>accelerator.prepare()</code> 方法中了，现在我们可以使用它的长度来计算训练步骤的数量。请记住，我们应该始终在准备好数据加载器后再执行此操作，因为更改数据加载器会改变 <code>DataLoader</code> 的长度。然后，我们使用学习率衰减到 0 的经典线性学习率调度：",Ml,el,Q,d,G="最后，为了将我们的模型推送到 Hugging Face Hub，我们需要在一个工作文件夹中创建一个 <code>Repository</code> 对象。如果你尚未登录 Hugging Face，请先进行登录。我们将根据模型 ID 来确定仓库名称。你可以使用自己选择的名称替换 <code>repo_name</code>，但请确保包含你的用户名。如果你不确定当前的用户名，可以使用<code>get_full_repo_name()</code> 函数来查看：",sl,ol,Jl,rl,dl,H,Ll="然后我们可以在本地文件夹中克隆该存储库。如果已经存在一个同名的文件夹，这个本地文件夹应该是我们正在使用的存储库克隆到本地的版本：",wl,S,bl,D,Sl="现在，我们可以通过调用 <code>repo.push_to_hub()</code> 方法上传我们在 <code>output_dir</code> 中保存的所有文件。这将帮助我们在每个 epoch 结束时上传中间模型。",yl,cl,fl,q,Pl="我们现在准备编写完整的训练循环。为了简化其评估部分，我们定义了这个 <code>postprocess()</code> 函数用于接收预测值和参考翻译对于的标签值，并将其转换为 <code>metric</code> 对象所需要的字符串列表。：",hl,F,Zl,tl,$l='训练循环看起来和本章 <a href="/course/chapter7/2">第 2 节</a> 与 <a href="/course/chapter3">第三章</a> 中代码很相似，只是在评估部分有一些不同 —— 所以让我们重点关注一下这一点！',gl,nl,jl="首先要注意的是，我们使用 <code>generate()</code> 方法来计算预测，但这是我们基础模型上的一个方法，而不是🤗 Accelerate 在 <code>prepare()</code> 方法中创建的封装模型。这就是为什么我们首先 <code>unwrap_model</code> ，然后调用此方法。",Ul,il,xl="首先要注意的是，我们用来计算预测的 <code>generate()</code> 函数是基础模型上的一个方法，而不是🤗 Accelerate 在 <code>prepare()</code> 函数中创建的封装模型。这就是在调用此函数之前先调用<code>unwrap_model</code>，的原因。",al,kl,Vl='第二个要注意的是，就像 <a href="https://chat.openai.com/course/chapter7/2" rel="nofollow">token 分类</a> 一样，在训练和评估这两个过程可能以不同的形状对输入和标签进行了填充，所以我们在调用 <code>gather()</code> 函数之前使用 <code>accelerator.pad_across_processes()</code> 方法，使预测和标签具有相同的形状。如果我们不这么做，那么评估的过程将会出错或被永远挂起。',c,$,Gl,Rl,Bl,Dl,ne='训练完成之后，你就有了一个模型，最终的 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> 上查看我们使用此代码训练的模型。如果你想测试对训练循环的任何调整，你可以直接通过编辑上面的代码来实现！',Nl;return p=new _l({props:{title:"自定义训练循环",local:"自定义训练循环",headingTag:"h2"}}),b=new _l({props:{title:"准备训练所需的一切",local:"准备训练所需的一切",headingTag:"h3"}}),k=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}}),Y=new h({props:{code:"bW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JTZXEyU2VxTE0uZnJvbV9wcmV0cmFpbmVkKG1vZGVsX2NoZWNrcG9pbnQp",highlighted:"model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)",wrap:!1}}),z=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}}),N=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}}),el=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}}),ol=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}}),rl=new h({props:{code:"J3NndWdnZXIlMkZtYXJpYW4tZmluZXR1bmVkLWtkZTQtZW4tdG8tZnItYWNjZWxlcmF0ZSc=",highlighted:'<span class="hljs-string">&#x27;sgugger/marian-finetuned-kde4-en-to-fr-accelerate&#x27;</span>',wrap:!1}}),S=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}}),cl=new _l({props:{title:"训练循环",local:"训练循环",headingTag:"h3"}}),F=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}}),$=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}}),Rl=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),f=n(),r=y("p"),r.innerHTML=w,Z=n(),T(b.$$.fragment),g=n(),V=y("p"),V.innerHTML=_,I=n(),T(k.$$.fragment),R=n(),K=y("p"),K.textContent=Tl,W=n(),T(Y.$$.fragment),L=n(),X=y("p"),X.textContent=ml,E=n(),T(z.$$.fragment),P=n(),A=y("p"),A.innerHTML=Il,O=n(),T(N.$$.fragment),ll=n(),v=y("p"),v.innerHTML=B,Ml=n(),T(el.$$.fragment),Q=n(),d=y("p"),d.innerHTML=G,sl=n(),T(ol.$$.fragment),Jl=n(),T(rl.$$.fragment),dl=n(),H=y("p"),H.textContent=Ll,wl=n(),T(S.$$.fragment),bl=n(),D=y("p"),D.innerHTML=Sl,yl=n(),T(cl.$$.fragment),fl=n(),q=y("p"),q.innerHTML=Pl,hl=n(),T(F.$$.fragment),Zl=n(),tl=y("p"),tl.innerHTML=$l,gl=n(),nl=y("p"),nl.innerHTML=jl,Ul=n(),il=y("p"),il.innerHTML=xl,al=n(),kl=y("p"),kl.innerHTML=Vl,c=n(),T($.$$.fragment),Gl=n(),T(Rl.$$.fragment),Bl=n(),Dl=y("p"),Dl.innerHTML=ne},l(M){m(p.$$.fragment,M),f=a(M),r=U(M,"P",{"data-svelte-h":!0}),u(r)!=="svelte-16nlo74"&&(r.innerHTML=w),Z=a(M),m(b.$$.fragment,M),g=a(M),V=U(M,"P",{"data-svelte-h":!0}),u(V)!=="svelte-gj0900"&&(V.innerHTML=_),I=a(M),m(k.$$.fragment,M),R=a(M),K=U(M,"P",{"data-svelte-h":!0}),u(K)!=="svelte-3cw9vf"&&(K.textContent=Tl),W=a(M),m(Y.$$.fragment,M),L=a(M),X=U(M,"P",{"data-svelte-h":!0}),u(X)!=="svelte-boavug"&&(X.textContent=ml),E=a(M),m(z.$$.fragment,M),P=a(M),A=U(M,"P",{"data-svelte-h":!0}),u(A)!=="svelte-hnego"&&(A.innerHTML=Il),O=a(M),m(N.$$.fragment,M),ll=a(M),v=U(M,"P",{"data-svelte-h":!0}),u(v)!=="svelte-19v4qb8"&&(v.innerHTML=B),Ml=a(M),m(el.$$.fragment,M),Q=a(M),d=U(M,"P",{"data-svelte-h":!0}),u(d)!=="svelte-320na4"&&(d.innerHTML=G),sl=a(M),m(ol.$$.fragment,M),Jl=a(M),m(rl.$$.fragment,M),dl=a(M),H=U(M,"P",{"data-svelte-h":!0}),u(H)!=="svelte-9w674v"&&(H.textContent=Ll),wl=a(M),m(S.$$.fragment,M),bl=a(M),D=U(M,"P",{"data-svelte-h":!0}),u(D)!=="svelte-1ka4pss"&&(D.innerHTML=Sl),yl=a(M),m(cl.$$.fragment,M),fl=a(M),q=U(M,"P",{"data-svelte-h":!0}),u(q)!=="svelte-1m015ho"&&(q.innerHTML=Pl),hl=a(M),m(F.$$.fragment,M),Zl=a(M),tl=U(M,"P",{"data-svelte-h":!0}),u(tl)!=="svelte-1s9jf3x"&&(tl.innerHTML=$l),gl=a(M),nl=U(M,"P",{"data-svelte-h":!0}),u(nl)!=="svelte-1b6hsb7"&&(nl.innerHTML=jl),Ul=a(M),il=U(M,"P",{"data-svelte-h":!0}),u(il)!=="svelte-1025olm"&&(il.innerHTML=xl),al=a(M),kl=U(M,"P",{"data-svelte-h":!0}),u(kl)!=="svelte-3nape9"&&(kl.innerHTML=Vl),c=a(M),m($.$$.fragment,M),Gl=a(M),m(Rl.$$.fragment,M),Bl=a(M),Dl=U(M,"P",{"data-svelte-h":!0}),u(Dl)!=="svelte-n3cu4c"&&(Dl.innerHTML=ne)},m(M,C){o(p,M,C),s(M,f,C),s(M,r,C),s(M,Z,C),o(b,M,C),s(M,g,C),s(M,V,C),s(M,I,C),o(k,M,C),s(M,R,C),s(M,K,C),s(M,W,C),o(Y,M,C),s(M,L,C),s(M,X,C),s(M,E,C),o(z,M,C),s(M,P,C),s(M,A,C),s(M,O,C),o(N,M,C),s(M,ll,C),s(M,v,C),s(M,Ml,C),o(el,M,C),s(M,Q,C),s(M,d,C),s(M,sl,C),o(ol,M,C),s(M,Jl,C),o(rl,M,C),s(M,dl,C),s(M,H,C),s(M,wl,C),o(S,M,C),s(M,bl,C),s(M,D,C),s(M,yl,C),o(cl,M,C),s(M,fl,C),s(M,q,C),s(M,hl,C),o(F,M,C),s(M,Zl,C),s(M,tl,C),s(M,gl,C),s(M,nl,C),s(M,Ul,C),s(M,il,C),s(M,al,C),s(M,kl,C),s(M,c,C),o($,M,C),s(M,Gl,C),o(Rl,M,C),s(M,Bl,C),s(M,Dl,C),Nl=!0},i(M){Nl||(i(p.$$.fragment,M),i(b.$$.fragment,M),i(k.$$.fragment,M),i(Y.$$.fragment,M),i(z.$$.fragment,M),i(N.$$.fragment,M),i(el.$$.fragment,M),i(ol.$$.fragment,M),i(rl.$$.fragment,M),i(S.$$.fragment,M),i(cl.$$.fragment,M),i(F.$$.fragment,M),i($.$$.fragment,M),i(Rl.$$.fragment,M),Nl=!0)},o(M){J(p.$$.fragment,M),J(b.$$.fragment,M),J(k.$$.fragment,M),J(Y.$$.fragment,M),J(z.$$.fragment,M),J(N.$$.fragment,M),J(el.$$.fragment,M),J(ol.$$.fragment,M),J(rl.$$.fragment,M),J(S.$$.fragment,M),J(cl.$$.fragment,M),J(F.$$.fragment,M),J($.$$.fragment,M),J(Rl.$$.fragment,M),Nl=!1},d(M){M&&(e(f),e(r),e(Z),e(g),e(V),e(I),e(R),e(K),e(W),e(L),e(X),e(E),e(P),e(A),e(O),e(ll),e(v),e(Ml),e(Q),e(d),e(sl),e(Jl),e(dl),e(H),e(wl),e(bl),e(D),e(yl),e(fl),e(q),e(hl),e(Zl),e(tl),e(gl),e(nl),e(Ul),e(il),e(al),e(kl),e(c),e(Gl),e(Bl),e(Dl)),j(p,M),j(b,M),j(k,M),j(Y,M),j(z,M),j(N,M),j(el,M),j(ol,M),j(rl,M),j(S,M),j(cl,M),j(F,M),j($,M),j(Rl,M)}}}function Tp(x){let p,f="✏️ <strong>轮到你了！</strong> 把之前找到的包含单词“email”样本输入模型，会返回什么结果？";return{c(){p=y("p"),p.innerHTML=f},l(r){p=U(r,"P",{"data-svelte-h":!0}),u(p)!=="svelte-10t97dq"&&(p.innerHTML=f)},m(r,w){s(r,p,w)},p:ql,d(r){r&&e(p)}}}function mp(x){let p,f,r,w,Z,b,g,V,_,I,k,R,K='现在让我们深入研究翻译。这是另一个 <a href="/course/chapter1/7">sequence-to-sequence 任务</a> ，着这是一个可以表述为输入是一个序列输出另一个序列的问题。从这个意义上说，这个问题非常类似 <a href="/course/chapter7/6">文本摘要</a> ，并且你可以将我们将在此处学习到的一些技巧迁移到其他的序列到序列问题，例如：',Tl,W,Y="<li><strong>风格迁移</strong> 创建一个模型将某种风格迁移到一段文本（例如，正式的风格迁移到休闲的风格，或从莎士比亚英语迁移到现代英语）。</li> <li><strong>生成问题的回答</strong> 创建一个模型，在给定上下文的情况下生成问题的答案。</li>",L,X,ml,E,z='如果你有足够大的两种（或更多）语言的文本语料库，你可以从头开始训练一个新的翻译模型，就像我们在 <a href="/course/chapter7/6">因果语言建模</a> 部分中所做的那样。然而，微调现有的翻译模型会更快，无论是从像 mT5 或 mBART 这样的多语言模型微调到特定的语言对，还是从特定语料库的一种语言到另一种语言的专用翻译模型。',P,A,Il='在这一节中，我们将在 <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 数据集）中提取的法语和英语文本的大型语料库上进行了预先训练。不过，即使我们使用的预训练模型在其预训练期间使用了这部分数据集，我们也会看到，经过微调后，我们可以得到一个更好的版本。',O,N,ll="完成后，我们将拥有一个模型，可以进行这样的翻译：",v,B,Ml,el,Q,d='<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."/>',G,sl,ol='与前面几节一样，你可以使用以下代码找到我们将训练并上传到 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> 查看模型输出的结果。',Jl,rl,dl,H,Ll='为了从头开始微调或训练翻译模型，我们需要一个适合该任务的数据集。如前所述，我们将使用 <a href="https://huggingface.co/datasets/kde4" rel="nofollow">KDE4 数据集</a> 。只要数据集中有互译的两种语言的句子对，就可以很容易地调整本节的代码以使用自己的数据集进行微调。如果你需要复习如何将自定义数据加载到 <code>Dataset</code> ，可以复习一下 <a href="/course/chapter5">第五章</a> 。',wl,S,bl,D,Sl="像往常一样，我们使用 <code>load_dataset()</code> 函数下载数据集：",yl,cl,fl,q,Pl='如果你想使用其他的语言对，你可以使用语言代码来设置你想使用的语言对。该数据集共有 92 种语言可用；你可以通过展开 <a href="https://huggingface.co/datasets/kde4" rel="nofollow">数据集卡片</a> 上的语言标签来查看数据集支持的语言标签。',hl,F,Zl,tl,$l,gl="我们来看看数据集：",nl,jl,Ul,il,xl,al,kl='我们下载的数据集有 210,173 对句子，在一次训练过程中，除了训练集，我们也需要创建自己的验证集。正如我们在 <a href="/course/chapter5">第五章</a> 学的的那样， <code>Dataset</code> 有一个 <code>train_test_split()</code> 方法可以帮助我们。我们将设置一个固定的随机数种子以保证结果可以复现：',Vl,c,$,Gl,Rl,Bl,Dl="我们可以像下面这样将 <code>test</code> 键重命名为 <code>validation</code>：",ne,Nl,M,C,Qn="现在让我们看一下数据集的一个元素：",Ds,ae,qs,pe,Ls,Me,Xn="我们得到一个包含我们选择的两种语言的两个句子的字典。这个充满技术计算机科学术语的数据集的一个特殊之处在于它们都完全用法语翻译。然而现实中，法国工程师在交谈时，大多数计算机科学专用词汇都用英语表述。例如，“threads”这个词很可能出现在法语句子中，尤其是在技术对话中；但在这个数据集中，它被翻译成更准确的“fils de Discussion”。我们使用的预训练模型已经在一个更大的法语和英语句子语料库上进行了预训练，所以输出的是原始的英语表达：",Ps,re,Os,ce,Ks,ie,An="这种情况的另一个例子可以在“plugin”这个词上看到，它并非正式的法语词汇，但大多数母语是法语的人都能够看懂并且不会去翻译它。不过，在 KDE4 数据集中，这个词被翻译成了更正式的法语词汇“module d’extension”：",lt,Je,et,ye,st,Ue,Wn="然而，我们的预训练模型坚持使用简练而熟悉的英文单词：",tt,Te,nt,me,at,oe,En="看看我们的微调模型是否能学习到数据集的这些特殊特性。（剧透警告：它能）。",pt,je,Mt,Ol,rt,ue,ct,de,it,we,vn='你现在应该可以预测我们的下一步该做些什么了：将所有文本转换为 token IDs 的集合，这样模型才可以理解它们。对于这个任务，我们需要同时对原始文本和翻译后的文本同时进行 tokenize。首先，我们需要创建 <code>tokenizer</code> 对象。如前所述，我们将使用 Marian 英语到法语的预训练模型。如果你使用下面的代码微调另一对语言，请记得更改下面代码中的 checkpoint。 <a href="https://huggingface.co/Helsinki-NLP" rel="nofollow">Helsinki-NLP</a> 组织提供了超过一千个多语言模型。',Jt,be,yt,fe,Fn='你也可以将 <code>model_checkpoint</code> 替换为你从 <a href="https://huggingface.co/models" rel="nofollow">Hub</a> 中选择的其他模型，或者一个保存了预训练模型和 tokenizer 的本地文件夹。',Ut,Kl,Tt,he,Yn="我们的数据准备相当简单。只有一点要记住；你需要确保 tokenizer 处理的目标是输出语言（在这里是法语）。你可以通过将目标语言传递给 tokenizer 的 <code>__call__</code> 方法的 <code>text_targets</code> 参数来完成此操作。",mt,Ce,zn="为了演示设置的方法，让我们处理训练集中的一个样本：",ot,Ie,jt,$e,ut,ke,Hn="我们可以看到，输出包含了与英语句子的 <code>inputs IDs</code>，而与法语句子的 IDs 存储在 <code>labels</code> 字段中。如果你忘记设置 labels 的 <code>tokenizer</code>，默认情况下 <code>labels</code> 将由输入的 <code>tokenizer</code>（语言类型不一样） 进行 <code>tokenize</code>，对于 Marian 模型来说，效果不会很好。",dt,_e,wt,Ze,bt,ge,Sn="如你所见，如果用英语的 tokenizer 来预处理法语句子，会产生更多的 tokens，因为这个 tokenizer 不认识任何法语单词（除了那些在英语里也出现的，比如“discussion”）。",ft,xe,Dn="最后一步是定义我们数据集的预处理函数：",ht,Ve,Ct,Ge,qn="请注意，上述代码也为输入和输出设置了相同的最大长度。由于要处理的文本看起来很短，因此在这里将最大长度设置为 128。",It,le,$t,ee,kt,Re,Ln="我们现在可以一次性使用上述预处理处理数据集的所有数据。",_t,Be,Zt,Ne,Pn="现在数据已经过预处理，我们准备好微调我们的预训练模型了！",gt,Ql,Xl,Ws,Qe,On="注意，这次我们使用的是一个已经在翻译任务上进行过训练的模型，实际上已经可以直接使用了，所以没有收到关于缺少权重或重新初始化的权重的警告。",xt,Xe,Vt,Ae,Kn='在这个任务中，我们需要一个数据整理器来动态批处理填充。因此，我们不能像 <a href="/course/chapter3">第三章</a> 那样直接使用 <code>DataCollatorWithPadding</code> ，因为它只填充输入的部分（inputs ID、注意掩码和 token 类型 ID）。我们的标签也应该被填充到所有标签中最大的长度。而且，如前所述，用于填充标签的填充值应为 <code>-100</code> ，而不是 tokenizer 默认的的填充 token，这样才可以在确保在损失计算中忽略这些填充值。',Gt,We,la='上述的这些需求都可以由 <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> 对象：',Rt,Al,Wl,Es,Ee,ea="为了在几个样本上进行测试，我们在已经完成 tokenize 的训练集中的部分数据上调用它，测试一下其功能：",Bt,ve,Nt,Fe,Qt,Ye,sa="我们可以检查我们的标签是否已经用 <code>-100</code> 填充到 batch 的最大长度：",Xt,ze,At,He,Wt,Se,ta="我们还可以查看解码器的 inputs ID，可以看到它们是标签经过偏移后的结果：",Et,De,vt,qe,Ft,Le,na="以下是我们数据集中第一个和第二个元素的标签：",Yt,Pe,zt,Oe,Ht,El,vl,vs,Ke,St,ls,Dt,Fs,es,aa='用于翻译的传统指标是 <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”这样的句子）都会被惩罚。',qt,ss,pa='BLEU 的一个缺点是的输入是已分词的文本，这使得比较使用不同分词器的模型之间的分数变得困难。因此，当今用于评估翻译模型的最常用指标是 <a href="https://github.com/mjpost/sacrebleu" rel="nofollow">SacreBLEU</a> ，它通过标准化的分词步骤解决了这个缺点（和其他的一些缺点）。要使用此指标，我们首先需要安装 SacreBLEU 库：',Lt,ts,Pt,ns,Ma='然后我们可以就像在 <a href="/course/chapter3">第三章</a> 那样通过 <code>evaluate.load()</code> 加载它',Ot,as,Kt,ps,ra="SacreBLEU 指标中待评估的预测和参考的目标译文输入的格式都是文本。它的设计是为了支持多个参考翻译，因为同一句话通常有多种可接受的翻译——虽然我们使用的数据集只提供一个，但在 NLP 中找到将多个句子作为标签的数据集是很常见的。因此，预测结果应该是一个句子列表，而参考应该是一个句子列表的列表。",ln,Ms,ca="让我们尝试一个例子：",en,rs,sn,cs,tn,is,ia='达到了 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 分数：',nn,Js,an,ys,pn,Us,Mn,Ts,rn,ms,Ja="分数可以从 0 到 100，越高越好。",cn,Fl,Yl,Ys,os,ya="现在这已经完成了，我们已经准备好微调我们的模型了！",Jn,js,yn,us,Ua="第一步是登录 Hugging Face，这样你就可以在训练过程中将结果上传到 Hub中。有一个方便的功能可以帮助你在 notebook 中完成登陆：",Un,ds,Tn,ws,Ta="这将显示一个小部件，你可以在其中输入你的 Hugging Face 登录凭据。",mn,bs,ma="如果你不是在 notebook 上运行代码，可以在终端中输入以下命令：",on,fs,jn,zl,Hl,zs,Hs,hs,un,Cs,oa="我们已经向你展示了如何在模型 Hub 上使用我们微调的模型。要在本地的 <code>pipeline</code> 中使用它，我们只需要指定正确的模型标识符：",dn,Is,wn,$s,bn,ks,ja="和预想的一样，我们的预训练模型适应了我们微调它的语料库，没有保留英语单词“threads”，而是将它翻译成官方的法语版本。对于“plugin”也是如此：",fn,_s,hn,Zs,Cn,gs,ua="这是另一个领域适应的好例子！",In,se,$n,xs,kn,Ss,_n;Z=new Sa({props:{fw:x[0]}}),g=new _l({props:{title:"翻译",local:"翻译",headingTag:"h1"}});const da=[La,qa],Vs=[];function wa(l,t){return l[0]==="pt"?0:1}_=wa(x),I=Vs[_]=da[_](x),X=new Zn({props:{id:"1JvfrvZgi6c"}}),rl=new _l({props:{title:"准备数据",local:"准备数据",headingTag:"h2"}}),S=new _l({props:{title:"KDE4 数据集",local:"KDE4 数据集",headingTag:"h3"}}),cl=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}}),jl=new h({props:{code:"cmF3X2RhdGFzZXRz",highlighted:"raw_datasets",wrap:!1}}),il=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}}),c=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}}),Gl=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}}),Nl=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}}),ae=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}}),pe=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}}),re=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}}),Je=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}}),ye=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}}),Te=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}}),me=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}}),je=new Zn({props:{id:"0Oxphw4Q9fo"}}),Ol=new te({props:{$$slots:{default:[Pa]},$$scope:{ctx:x}}}),ue=new _l({props:{title:"处理数据",local:"处理数据",headingTag:"h3"}}),de=new Zn({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:[Oa]},$$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}}),$e=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}}),_e=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}}),Ve=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:[Ka]},$$scope:{ctx:x}}}),ee=new te({props:{warning:!0,$$slots:{default:[lp]},$$scope:{ctx:x}}}),Be=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 ba=[sp,ep],Gs=[];function fa(l,t){return l[0]==="pt"?0:1}Ql=fa(x),Xl=Gs[Ql]=ba[Ql](x),Xe=new _l({props:{title:"数据整理",local:"数据整理",headingTag:"h3"}});const ha=[ap,np],Rs=[];function Ca(l,t){return l[0]==="pt"?0:1}Al=Ca(x),Wl=Rs[Al]=ha[Al](x),ve=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}}),Fe=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}}),ze=new h({props:{code:"YmF0Y2glNUIlMjJsYWJlbHMlMjIlNUQ=",highlighted:'batch[<span class="hljs-string">&quot;labels&quot;</span>]',wrap:!1}}),He=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}}),De=new h({props:{code:"YmF0Y2glNUIlMjJkZWNvZGVyX2lucHV0X2lkcyUyMiU1RA==",highlighted:'batch[<span class="hljs-string">&quot;decoder_input_ids&quot;</span>]',wrap:!1}}),qe=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}}),Pe=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}}),Oe=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 Ia=[Mp,pp],Bs=[];function $a(l,t){return l[0]==="pt"?0:1}El=$a(x),vl=Bs[El]=Ia[El](x),Ke=new _l({props:{title:"评估指标",local:"评估指标",headingTag:"h3"}}),ls=new Zn({props:{id:"M05L1DhFqcw"}});let ul=x[0]==="pt"&&Xa();ts=new h({props:{code:"IXBpcCUyMGluc3RhbGwlMjBzYWNyZWJsZXU=",highlighted:"!pip install sacrebleu",wrap:!1}}),as=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}}),rs=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}}),Js=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}}),ys=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}}),Us=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}}),Ts=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 ka=[cp,rp],Ns=[];function _a(l,t){return l[0]==="tf"?0:1}Fl=_a(x),Yl=Ns[Fl]=ka[Fl](x),js=new _l({props:{title:"微调模型",local:"微调模型",headingTag:"h3"}}),ds=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}}),fs=new h({props:{code:"aHVnZ2luZ2ZhY2UtY2xpJTIwbG9naW4=",highlighted:"huggingface-cli login",wrap:!1}});const Za=[Jp,ip],Qs=[];function ga(l,t){return l[0]==="tf"?0:1}zl=ga(x),Hl=Qs[zl]=Za[zl](x);let pl=x[0]==="pt"&&Aa();return hs=new _l({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}}),$s=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}}),_s=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:[Tp]},$$scope:{ctx:x}}}),xs=new Da({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter7/4.mdx"}}),{c(){p=y("meta"),f=n(),r=y("p"),w=n(),T(Z.$$.fragment),b=n(),T(g.$$.fragment),V=n(),I.c(),k=n(),R=y("p"),R.innerHTML=K,Tl=n(),W=y("ul"),W.innerHTML=Y,L=n(),T(X.$$.fragment),ml=n(),E=y("p"),E.innerHTML=z,P=n(),A=y("p"),A.innerHTML=Il,O=n(),N=y("p"),N.textContent=ll,v=n(),B=y("iframe"),el=n(),Q=y("a"),Q.innerHTML=d,G=n(),sl=y("p"),sl.innerHTML=ol,Jl=n(),T(rl.$$.fragment),dl=n(),H=y("p"),H.innerHTML=Ll,wl=n(),T(S.$$.fragment),bl=n(),D=y("p"),D.innerHTML=Sl,yl=n(),T(cl.$$.fragment),fl=n(),q=y("p"),q.innerHTML=Pl,hl=n(),F=y("img"),tl=n(),$l=y("p"),$l.textContent=gl,nl=n(),T(jl.$$.fragment),Ul=n(),T(il.$$.fragment),xl=n(),al=y("p"),al.innerHTML=kl,Vl=n(),T(c.$$.fragment),$=n(),T(Gl.$$.fragment),Rl=n(),Bl=y("p"),Bl.innerHTML=Dl,ne=n(),T(Nl.$$.fragment),M=n(),C=y("p"),C.textContent=Qn,Ds=n(),T(ae.$$.fragment),qs=n(),T(pe.$$.fragment),Ls=n(),Me=y("p"),Me.textContent=Xn,Ps=n(),T(re.$$.fragment),Os=n(),T(ce.$$.fragment),Ks=n(),ie=y("p"),ie.textContent=An,lt=n(),T(Je.$$.fragment),et=n(),T(ye.$$.fragment),st=n(),Ue=y("p"),Ue.textContent=Wn,tt=n(),T(Te.$$.fragment),nt=n(),T(me.$$.fragment),at=n(),oe=y("p"),oe.textContent=En,pt=n(),T(je.$$.fragment),Mt=n(),T(Ol.$$.fragment),rt=n(),T(ue.$$.fragment),ct=n(),T(de.$$.fragment),it=n(),we=y("p"),we.innerHTML=vn,Jt=n(),T(be.$$.fragment),yt=n(),fe=y("p"),fe.innerHTML=Fn,Ut=n(),T(Kl.$$.fragment),Tt=n(),he=y("p"),he.innerHTML=Yn,mt=n(),Ce=y("p"),Ce.textContent=zn,ot=n(),T(Ie.$$.fragment),jt=n(),T($e.$$.fragment),ut=n(),ke=y("p"),ke.innerHTML=Hn,dt=n(),T(_e.$$.fragment),wt=n(),T(Ze.$$.fragment),bt=n(),ge=y("p"),ge.textContent=Sn,ft=n(),xe=y("p"),xe.textContent=Dn,ht=n(),T(Ve.$$.fragment),Ct=n(),Ge=y("p"),Ge.textContent=qn,It=n(),T(le.$$.fragment),$t=n(),T(ee.$$.fragment),kt=n(),Re=y("p"),Re.textContent=Ln,_t=n(),T(Be.$$.fragment),Zt=n(),Ne=y("p"),Ne.textContent=Pn,gt=n(),Xl.c(),Ws=n(),Qe=y("p"),Qe.textContent=On,xt=n(),T(Xe.$$.fragment),Vt=n(),Ae=y("p"),Ae.innerHTML=Kn,Gt=n(),We=y("p"),We.innerHTML=la,Rt=n(),Wl.c(),Es=n(),Ee=y("p"),Ee.textContent=ea,Bt=n(),T(ve.$$.fragment),Nt=n(),T(Fe.$$.fragment),Qt=n(),Ye=y("p"),Ye.innerHTML=sa,Xt=n(),T(ze.$$.fragment),At=n(),T(He.$$.fragment),Wt=n(),Se=y("p"),Se.textContent=ta,Et=n(),T(De.$$.fragment),vt=n(),T(qe.$$.fragment),Ft=n(),Le=y("p"),Le.textContent=na,Yt=n(),T(Pe.$$.fragment),zt=n(),T(Oe.$$.fragment),Ht=n(),vl.c(),vs=n(),T(Ke.$$.fragment),St=n(),T(ls.$$.fragment),Dt=n(),ul&&ul.c(),Fs=n(),es=y("p"),es.innerHTML=aa,qt=n(),ss=y("p"),ss.innerHTML=pa,Lt=n(),T(ts.$$.fragment),Pt=n(),ns=y("p"),ns.innerHTML=Ma,Ot=n(),T(as.$$.fragment),Kt=n(),ps=y("p"),ps.textContent=ra,ln=n(),Ms=y("p"),Ms.textContent=ca,en=n(),T(rs.$$.fragment),sn=n(),T(cs.$$.fragment),tn=n(),is=y("p"),is.innerHTML=ia,nn=n(),T(Js.$$.fragment),an=n(),T(ys.$$.fragment),pn=n(),T(Us.$$.fragment),Mn=n(),T(Ts.$$.fragment),rn=n(),ms=y("p"),ms.textContent=Ja,cn=n(),Yl.c(),Ys=n(),os=y("p"),os.textContent=ya,Jn=n(),T(js.$$.fragment),yn=n(),us=y("p"),us.textContent=Ua,Un=n(),T(ds.$$.fragment),Tn=n(),ws=y("p"),ws.textContent=Ta,mn=n(),bs=y("p"),bs.textContent=ma,on=n(),T(fs.$$.fragment),jn=n(),Hl.c(),zs=n(),pl&&pl.c(),Hs=n(),T(hs.$$.fragment),un=n(),Cs=y("p"),Cs.innerHTML=oa,dn=n(),T(Is.$$.fragment),wn=n(),T($s.$$.fragment),bn=n(),ks=y("p"),ks.textContent=ja,fn=n(),T(_s.$$.fragment),hn=n(),T(Zs.$$.fragment),Cn=n(),gs=y("p"),gs.textContent=ua,In=n(),T(se.$$.fragment),$n=n(),T(xs.$$.fragment),kn=n(),Ss=y("p"),this.h()},l(l){const t=za("svelte-u9bgzb",document.head);p=U(t,"META",{name:!0,content:!0}),t.forEach(e),f=a(l),r=U(l,"P",{}),Nn(r).forEach(e),w=a(l),m(Z.$$.fragment,l),b=a(l),m(g.$$.fragment,l),V=a(l),I.l(l),k=a(l),R=U(l,"P",{"data-svelte-h":!0}),u(R)!=="svelte-18zyinn"&&(R.innerHTML=K),Tl=a(l),W=U(l,"UL",{"data-svelte-h":!0}),u(W)!=="svelte-j95dky"&&(W.innerHTML=Y),L=a(l),m(X.$$.fragment,l),ml=a(l),E=U(l,"P",{"data-svelte-h":!0}),u(E)!=="svelte-1bvu257"&&(E.innerHTML=z),P=a(l),A=U(l,"P",{"data-svelte-h":!0}),u(A)!=="svelte-1psbmv4"&&(A.innerHTML=Il),O=a(l),N=U(l,"P",{"data-svelte-h":!0}),u(N)!=="svelte-1unz1qk"&&(N.textContent=ll),v=a(l),B=U(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),Nn(B).forEach(e),el=a(l),Q=U(l,"A",{class:!0,href:!0,"data-svelte-h":!0}),u(Q)!=="svelte-1ifl6xg"&&(Q.innerHTML=d),G=a(l),sl=U(l,"P",{"data-svelte-h":!0}),u(sl)!=="svelte-137tcdv"&&(sl.innerHTML=ol),Jl=a(l),m(rl.$$.fragment,l),dl=a(l),H=U(l,"P",{"data-svelte-h":!0}),u(H)!=="svelte-ay6bgq"&&(H.innerHTML=Ll),wl=a(l),m(S.$$.fragment,l),bl=a(l),D=U(l,"P",{"data-svelte-h":!0}),u(D)!=="svelte-18daai"&&(D.innerHTML=Sl),yl=a(l),m(cl.$$.fragment,l),fl=a(l),q=U(l,"P",{"data-svelte-h":!0}),u(q)!=="svelte-11b1ocr"&&(q.innerHTML=Pl),hl=a(l),F=U(l,"IMG",{src:!0,alt:!0,width:!0}),tl=a(l),$l=U(l,"P",{"data-svelte-h":!0}),u($l)!=="svelte-vvikr8"&&($l.textContent=gl),nl=a(l),m(jl.$$.fragment,l),Ul=a(l),m(il.$$.fragment,l),xl=a(l),al=U(l,"P",{"data-svelte-h":!0}),u(al)!=="svelte-13mqn0"&&(al.innerHTML=kl),Vl=a(l),m(c.$$.fragment,l),$=a(l),m(Gl.$$.fragment,l),Rl=a(l),Bl=U(l,"P",{"data-svelte-h":!0}),u(Bl)!=="svelte-jkjlop"&&(Bl.innerHTML=Dl),ne=a(l),m(Nl.$$.fragment,l),M=a(l),C=U(l,"P",{"data-svelte-h":!0}),u(C)!=="svelte-17klq8b"&&(C.textContent=Qn),Ds=a(l),m(ae.$$.fragment,l),qs=a(l),m(pe.$$.fragment,l),Ls=a(l),Me=U(l,"P",{"data-svelte-h":!0}),u(Me)!=="svelte-6ttj3c"&&(Me.textContent=Xn),Ps=a(l),m(re.$$.fragment,l),Os=a(l),m(ce.$$.fragment,l),Ks=a(l),ie=U(l,"P",{"data-svelte-h":!0}),u(ie)!=="svelte-a94pa5"&&(ie.textContent=An),lt=a(l),m(Je.$$.fragment,l),et=a(l),m(ye.$$.fragment,l),st=a(l),Ue=U(l,"P",{"data-svelte-h":!0}),u(Ue)!=="svelte-ipwn4g"&&(Ue.textContent=Wn),tt=a(l),m(Te.$$.fragment,l),nt=a(l),m(me.$$.fragment,l),at=a(l),oe=U(l,"P",{"data-svelte-h":!0}),u(oe)!=="svelte-n6sjbk"&&(oe.textContent=En),pt=a(l),m(je.$$.fragment,l),Mt=a(l),m(Ol.$$.fragment,l),rt=a(l),m(ue.$$.fragment,l),ct=a(l),m(de.$$.fragment,l),it=a(l),we=U(l,"P",{"data-svelte-h":!0}),u(we)!=="svelte-1mokqbf"&&(we.innerHTML=vn),Jt=a(l),m(be.$$.fragment,l),yt=a(l),fe=U(l,"P",{"data-svelte-h":!0}),u(fe)!=="svelte-sct5i7"&&(fe.innerHTML=Fn),Ut=a(l),m(Kl.$$.fragment,l),Tt=a(l),he=U(l,"P",{"data-svelte-h":!0}),u(he)!=="svelte-ld1hou"&&(he.innerHTML=Yn),mt=a(l),Ce=U(l,"P",{"data-svelte-h":!0}),u(Ce)!=="svelte-11p5ywo"&&(Ce.textContent=zn),ot=a(l),m(Ie.$$.fragment,l),jt=a(l),m($e.$$.fragment,l),ut=a(l),ke=U(l,"P",{"data-svelte-h":!0}),u(ke)!=="svelte-z2on5m"&&(ke.innerHTML=Hn),dt=a(l),m(_e.$$.fragment,l),wt=a(l),m(Ze.$$.fragment,l),bt=a(l),ge=U(l,"P",{"data-svelte-h":!0}),u(ge)!=="svelte-1r2tov4"&&(ge.textContent=Sn),ft=a(l),xe=U(l,"P",{"data-svelte-h":!0}),u(xe)!=="svelte-1qyv0at"&&(xe.textContent=Dn),ht=a(l),m(Ve.$$.fragment,l),Ct=a(l),Ge=U(l,"P",{"data-svelte-h":!0}),u(Ge)!=="svelte-durau1"&&(Ge.textContent=qn),It=a(l),m(le.$$.fragment,l),$t=a(l),m(ee.$$.fragment,l),kt=a(l),Re=U(l,"P",{"data-svelte-h":!0}),u(Re)!=="svelte-1f2d1pj"&&(Re.textContent=Ln),_t=a(l),m(Be.$$.fragment,l),Zt=a(l),Ne=U(l,"P",{"data-svelte-h":!0}),u(Ne)!=="svelte-aijya6"&&(Ne.textContent=Pn),gt=a(l),Xl.l(l),Ws=a(l),Qe=U(l,"P",{"data-svelte-h":!0}),u(Qe)!=="svelte-1mvoyq7"&&(Qe.textContent=On),xt=a(l),m(Xe.$$.fragment,l),Vt=a(l),Ae=U(l,"P",{"data-svelte-h":!0}),u(Ae)!=="svelte-venz08"&&(Ae.innerHTML=Kn),Gt=a(l),We=U(l,"P",{"data-svelte-h":!0}),u(We)!=="svelte-6qluvz"&&(We.innerHTML=la),Rt=a(l),Wl.l(l),Es=a(l),Ee=U(l,"P",{"data-svelte-h":!0}),u(Ee)!=="svelte-a4euy9"&&(Ee.textContent=ea),Bt=a(l),m(ve.$$.fragment,l),Nt=a(l),m(Fe.$$.fragment,l),Qt=a(l),Ye=U(l,"P",{"data-svelte-h":!0}),u(Ye)!=="svelte-1c7qagd"&&(Ye.innerHTML=sa),Xt=a(l),m(ze.$$.fragment,l),At=a(l),m(He.$$.fragment,l),Wt=a(l),Se=U(l,"P",{"data-svelte-h":!0}),u(Se)!=="svelte-nqema2"&&(Se.textContent=ta),Et=a(l),m(De.$$.fragment,l),vt=a(l),m(qe.$$.fragment,l),Ft=a(l),Le=U(l,"P",{"data-svelte-h":!0}),u(Le)!=="svelte-j27xgx"&&(Le.textContent=na),Yt=a(l),m(Pe.$$.fragment,l),zt=a(l),m(Oe.$$.fragment,l),Ht=a(l),vl.l(l),vs=a(l),m(Ke.$$.fragment,l),St=a(l),m(ls.$$.fragment,l),Dt=a(l),ul&&ul.l(l),Fs=a(l),es=U(l,"P",{"data-svelte-h":!0}),u(es)!=="svelte-bv3cz9"&&(es.innerHTML=aa),qt=a(l),ss=U(l,"P",{"data-svelte-h":!0}),u(ss)!=="svelte-6zqzva"&&(ss.innerHTML=pa),Lt=a(l),m(ts.$$.fragment,l),Pt=a(l),ns=U(l,"P",{"data-svelte-h":!0}),u(ns)!=="svelte-a4otbu"&&(ns.innerHTML=Ma),Ot=a(l),m(as.$$.fragment,l),Kt=a(l),ps=U(l,"P",{"data-svelte-h":!0}),u(ps)!=="svelte-sh9i2x"&&(ps.textContent=ra),ln=a(l),Ms=U(l,"P",{"data-svelte-h":!0}),u(Ms)!=="svelte-jokngj"&&(Ms.textContent=ca),en=a(l),m(rs.$$.fragment,l),sn=a(l),m(cs.$$.fragment,l),tn=a(l),is=U(l,"P",{"data-svelte-h":!0}),u(is)!=="svelte-mdpnd2"&&(is.innerHTML=ia),nn=a(l),m(Js.$$.fragment,l),an=a(l),m(ys.$$.fragment,l),pn=a(l),m(Us.$$.fragment,l),Mn=a(l),m(Ts.$$.fragment,l),rn=a(l),ms=U(l,"P",{"data-svelte-h":!0}),u(ms)!=="svelte-bq8n0g"&&(ms.textContent=Ja),cn=a(l),Yl.l(l),Ys=a(l),os=U(l,"P",{"data-svelte-h":!0}),u(os)!=="svelte-1b8shi1"&&(os.textContent=ya),Jn=a(l),m(js.$$.fragment,l),yn=a(l),us=U(l,"P",{"data-svelte-h":!0}),u(us)!=="svelte-1yddmca"&&(us.textContent=Ua),Un=a(l),m(ds.$$.fragment,l),Tn=a(l),ws=U(l,"P",{"data-svelte-h":!0}),u(ws)!=="svelte-ui4fcw"&&(ws.textContent=Ta),mn=a(l),bs=U(l,"P",{"data-svelte-h":!0}),u(bs)!=="svelte-15j1x98"&&(bs.textContent=ma),on=a(l),m(fs.$$.fragment,l),jn=a(l),Hl.l(l),zs=a(l),pl&&pl.l(l),Hs=a(l),m(hs.$$.fragment,l),un=a(l),Cs=U(l,"P",{"data-svelte-h":!0}),u(Cs)!=="svelte-1omme7t"&&(Cs.innerHTML=oa),dn=a(l),m(Is.$$.fragment,l),wn=a(l),m($s.$$.fragment,l),bn=a(l),ks=U(l,"P",{"data-svelte-h":!0}),u(ks)!=="svelte-8tfwdd"&&(ks.textContent=ja),fn=a(l),m(_s.$$.fragment,l),hn=a(l),m(Zs.$$.fragment,l),Cn=a(l),gs=U(l,"P",{"data-svelte-h":!0}),u(gs)!=="svelte-rrnwlc"&&(gs.textContent=ua),In=a(l),m(se.$$.fragment,l),$n=a(l),m(xs.$$.fragment,l),kn=a(l),Ss=U(l,"P",{}),Nn(Ss).forEach(e),this.h()},h(){Cl(p,"name","hf:doc:metadata"),Cl(p,"content",op),Qa(B.src,Ml="https://course-demos-marian-finetuned-kde4-en-to-fr.hf.space")||Cl(B,"src",Ml),Cl(B,"frameborder","0"),Cl(B,"height","350"),Cl(B,"title","Gradio app"),Cl(B,"class","block dark:hidden container p-0 flex-grow space-iframe"),Cl(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"),Cl(B,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),Cl(Q,"class","flex justify-center"),Cl(Q,"href","/huggingface-course/marian-finetuned-kde4-en-to-fr"),Qa(F.src,Zl="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/language_tags.png")||Cl(F,"src",Zl),Cl(F,"alt","Language available for the KDE4 dataset."),Cl(F,"width","100%")},m(l,t){Ha(document.head,p),s(l,f,t),s(l,r,t),s(l,w,t),o(Z,l,t),s(l,b,t),o(g,l,t),s(l,V,t),Vs[_].m(l,t),s(l,k,t),s(l,R,t),s(l,Tl,t),s(l,W,t),s(l,L,t),o(X,l,t),s(l,ml,t),s(l,E,t),s(l,P,t),s(l,A,t),s(l,O,t),s(l,N,t),s(l,v,t),s(l,B,t),s(l,el,t),s(l,Q,t),s(l,G,t),s(l,sl,t),s(l,Jl,t),o(rl,l,t),s(l,dl,t),s(l,H,t),s(l,wl,t),o(S,l,t),s(l,bl,t),s(l,D,t),s(l,yl,t),o(cl,l,t),s(l,fl,t),s(l,q,t),s(l,hl,t),s(l,F,t),s(l,tl,t),s(l,$l,t),s(l,nl,t),o(jl,l,t),s(l,Ul,t),o(il,l,t),s(l,xl,t),s(l,al,t),s(l,Vl,t),o(c,l,t),s(l,$,t),o(Gl,l,t),s(l,Rl,t),s(l,Bl,t),s(l,ne,t),o(Nl,l,t),s(l,M,t),s(l,C,t),s(l,Ds,t),o(ae,l,t),s(l,qs,t),o(pe,l,t),s(l,Ls,t),s(l,Me,t),s(l,Ps,t),o(re,l,t),s(l,Os,t),o(ce,l,t),s(l,Ks,t),s(l,ie,t),s(l,lt,t),o(Je,l,t),s(l,et,t),o(ye,l,t),s(l,st,t),s(l,Ue,t),s(l,tt,t),o(Te,l,t),s(l,nt,t),o(me,l,t),s(l,at,t),s(l,oe,t),s(l,pt,t),o(je,l,t),s(l,Mt,t),o(Ol,l,t),s(l,rt,t),o(ue,l,t),s(l,ct,t),o(de,l,t),s(l,it,t),s(l,we,t),s(l,Jt,t),o(be,l,t),s(l,yt,t),s(l,fe,t),s(l,Ut,t),o(Kl,l,t),s(l,Tt,t),s(l,he,t),s(l,mt,t),s(l,Ce,t),s(l,ot,t),o(Ie,l,t),s(l,jt,t),o($e,l,t),s(l,ut,t),s(l,ke,t),s(l,dt,t),o(_e,l,t),s(l,wt,t),o(Ze,l,t),s(l,bt,t),s(l,ge,t),s(l,ft,t),s(l,xe,t),s(l,ht,t),o(Ve,l,t),s(l,Ct,t),s(l,Ge,t),s(l,It,t),o(le,l,t),s(l,$t,t),o(ee,l,t),s(l,kt,t),s(l,Re,t),s(l,_t,t),o(Be,l,t),s(l,Zt,t),s(l,Ne,t),s(l,gt,t),Gs[Ql].m(l,t),s(l,Ws,t),s(l,Qe,t),s(l,xt,t),o(Xe,l,t),s(l,Vt,t),s(l,Ae,t),s(l,Gt,t),s(l,We,t),s(l,Rt,t),Rs[Al].m(l,t),s(l,Es,t),s(l,Ee,t),s(l,Bt,t),o(ve,l,t),s(l,Nt,t),o(Fe,l,t),s(l,Qt,t),s(l,Ye,t),s(l,Xt,t),o(ze,l,t),s(l,At,t),o(He,l,t),s(l,Wt,t),s(l,Se,t),s(l,Et,t),o(De,l,t),s(l,vt,t),o(qe,l,t),s(l,Ft,t),s(l,Le,t),s(l,Yt,t),o(Pe,l,t),s(l,zt,t),o(Oe,l,t),s(l,Ht,t),Bs[El].m(l,t),s(l,vs,t),o(Ke,l,t),s(l,St,t),o(ls,l,t),s(l,Dt,t),ul&&ul.m(l,t),s(l,Fs,t),s(l,es,t),s(l,qt,t),s(l,ss,t),s(l,Lt,t),o(ts,l,t),s(l,Pt,t),s(l,ns,t),s(l,Ot,t),o(as,l,t),s(l,Kt,t),s(l,ps,t),s(l,ln,t),s(l,Ms,t),s(l,en,t),o(rs,l,t),s(l,sn,t),o(cs,l,t),s(l,tn,t),s(l,is,t),s(l,nn,t),o(Js,l,t),s(l,an,t),o(ys,l,t),s(l,pn,t),o(Us,l,t),s(l,Mn,t),o(Ts,l,t),s(l,rn,t),s(l,ms,t),s(l,cn,t),Ns[Fl].m(l,t),s(l,Ys,t),s(l,os,t),s(l,Jn,t),o(js,l,t),s(l,yn,t),s(l,us,t),s(l,Un,t),o(ds,l,t),s(l,Tn,t),s(l,ws,t),s(l,mn,t),s(l,bs,t),s(l,on,t),o(fs,l,t),s(l,jn,t),Qs[zl].m(l,t),s(l,zs,t),pl&&pl.m(l,t),s(l,Hs,t),o(hs,l,t),s(l,un,t),s(l,Cs,t),s(l,dn,t),o(Is,l,t),s(l,wn,t),o($s,l,t),s(l,bn,t),s(l,ks,t),s(l,fn,t),o(_s,l,t),s(l,hn,t),o(Zs,l,t),s(l,Cn,t),s(l,gs,t),s(l,In,t),o(se,l,t),s(l,$n,t),o(xs,l,t),s(l,kn,t),s(l,Ss,t),_n=!0},p(l,[t]){const xa={};t&1&&(xa.fw=l[0]),Z.$set(xa);let gn=_;_=wa(l),_!==gn&&(As(),J(Vs[gn],1,1,()=>{Vs[gn]=null}),Xs(),I=Vs[_],I||(I=Vs[_]=da[_](l),I.c()),i(I,1),I.m(k.parentNode,k));const Va={};t&2&&(Va.$$scope={dirty:t,ctx:l}),Ol.$set(Va);const Ga={};t&2&&(Ga.$$scope={dirty:t,ctx:l}),Kl.$set(Ga);const Ra={};t&2&&(Ra.$$scope={dirty:t,ctx:l}),le.$set(Ra);const Ba={};t&2&&(Ba.$$scope={dirty:t,ctx:l}),ee.$set(Ba);let xn=Ql;Ql=fa(l),Ql!==xn&&(As(),J(Gs[xn],1,1,()=>{Gs[xn]=null}),Xs(),Xl=Gs[Ql],Xl||(Xl=Gs[Ql]=ba[Ql](l),Xl.c()),i(Xl,1),Xl.m(Ws.parentNode,Ws));let Vn=Al;Al=Ca(l),Al!==Vn&&(As(),J(Rs[Vn],1,1,()=>{Rs[Vn]=null}),Xs(),Wl=Rs[Al],Wl||(Wl=Rs[Al]=ha[Al](l),Wl.c()),i(Wl,1),Wl.m(Es.parentNode,Es));let Gn=El;El=$a(l),El!==Gn&&(As(),J(Bs[Gn],1,1,()=>{Bs[Gn]=null}),Xs(),vl=Bs[El],vl||(vl=Bs[El]=Ia[El](l),vl.c()),i(vl,1),vl.m(vs.parentNode,vs)),l[0]==="pt"?ul||(ul=Xa(),ul.c(),ul.m(Fs.parentNode,Fs)):ul&&(ul.d(1),ul=null);let Rn=Fl;Fl=_a(l),Fl!==Rn&&(As(),J(Ns[Rn],1,1,()=>{Ns[Rn]=null}),Xs(),Yl=Ns[Fl],Yl||(Yl=Ns[Fl]=ka[Fl](l),Yl.c()),i(Yl,1),Yl.m(Ys.parentNode,Ys));let Bn=zl;zl=ga(l),zl!==Bn&&(As(),J(Qs[Bn],1,1,()=>{Qs[Bn]=null}),Xs(),Hl=Qs[zl],Hl||(Hl=Qs[zl]=Za[zl](l),Hl.c()),i(Hl,1),Hl.m(zs.parentNode,zs)),l[0]==="pt"?pl?t&1&&i(pl,1):(pl=Aa(),pl.c(),i(pl,1),pl.m(Hs.parentNode,Hs)):pl&&(As(),J(pl,1,1,()=>{pl=null}),Xs());const Na={};t&2&&(Na.$$scope={dirty:t,ctx:l}),se.$set(Na)},i(l){_n||(i(Z.$$.fragment,l),i(g.$$.fragment,l),i(I),i(X.$$.fragment,l),i(rl.$$.fragment,l),i(S.$$.fragment,l),i(cl.$$.fragment,l),i(jl.$$.fragment,l),i(il.$$.fragment,l),i(c.$$.fragment,l),i(Gl.$$.fragment,l),i(Nl.$$.fragment,l),i(ae.$$.fragment,l),i(pe.$$.fragment,l),i(re.$$.fragment,l),i(ce.$$.fragment,l),i(Je.$$.fragment,l),i(ye.$$.fragment,l),i(Te.$$.fragment,l),i(me.$$.fragment,l),i(je.$$.fragment,l),i(Ol.$$.fragment,l),i(ue.$$.fragment,l),i(de.$$.fragment,l),i(be.$$.fragment,l),i(Kl.$$.fragment,l),i(Ie.$$.fragment,l),i($e.$$.fragment,l),i(_e.$$.fragment,l),i(Ze.$$.fragment,l),i(Ve.$$.fragment,l),i(le.$$.fragment,l),i(ee.$$.fragment,l),i(Be.$$.fragment,l),i(Xl),i(Xe.$$.fragment,l),i(Wl),i(ve.$$.fragment,l),i(Fe.$$.fragment,l),i(ze.$$.fragment,l),i(He.$$.fragment,l),i(De.$$.fragment,l),i(qe.$$.fragment,l),i(Pe.$$.fragment,l),i(Oe.$$.fragment,l),i(vl),i(Ke.$$.fragment,l),i(ls.$$.fragment,l),i(ts.$$.fragment,l),i(as.$$.fragment,l),i(rs.$$.fragment,l),i(cs.$$.fragment,l),i(Js.$$.fragment,l),i(ys.$$.fragment,l),i(Us.$$.fragment,l),i(Ts.$$.fragment,l),i(Yl),i(js.$$.fragment,l),i(ds.$$.fragment,l),i(fs.$$.fragment,l),i(Hl),i(pl),i(hs.$$.fragment,l),i(Is.$$.fragment,l),i($s.$$.fragment,l),i(_s.$$.fragment,l),i(Zs.$$.fragment,l),i(se.$$.fragment,l),i(xs.$$.fragment,l),_n=!0)},o(l){J(Z.$$.fragment,l),J(g.$$.fragment,l),J(I),J(X.$$.fragment,l),J(rl.$$.fragment,l),J(S.$$.fragment,l),J(cl.$$.fragment,l),J(jl.$$.fragment,l),J(il.$$.fragment,l),J(c.$$.fragment,l),J(Gl.$$.fragment,l),J(Nl.$$.fragment,l),J(ae.$$.fragment,l),J(pe.$$.fragment,l),J(re.$$.fragment,l),J(ce.$$.fragment,l),J(Je.$$.fragment,l),J(ye.$$.fragment,l),J(Te.$$.fragment,l),J(me.$$.fragment,l),J(je.$$.fragment,l),J(Ol.$$.fragment,l),J(ue.$$.fragment,l),J(de.$$.fragment,l),J(be.$$.fragment,l),J(Kl.$$.fragment,l),J(Ie.$$.fragment,l),J($e.$$.fragment,l),J(_e.$$.fragment,l),J(Ze.$$.fragment,l),J(Ve.$$.fragment,l),J(le.$$.fragment,l),J(ee.$$.fragment,l),J(Be.$$.fragment,l),J(Xl),J(Xe.$$.fragment,l),J(Wl),J(ve.$$.fragment,l),J(Fe.$$.fragment,l),J(ze.$$.fragment,l),J(He.$$.fragment,l),J(De.$$.fragment,l),J(qe.$$.fragment,l),J(Pe.$$.fragment,l),J(Oe.$$.fragment,l),J(vl),J(Ke.$$.fragment,l),J(ls.$$.fragment,l),J(ts.$$.fragment,l),J(as.$$.fragment,l),J(rs.$$.fragment,l),J(cs.$$.fragment,l),J(Js.$$.fragment,l),J(ys.$$.fragment,l),J(Us.$$.fragment,l),J(Ts.$$.fragment,l),J(Yl),J(js.$$.fragment,l),J(ds.$$.fragment,l),J(fs.$$.fragment,l),J(Hl),J(pl),J(hs.$$.fragment,l),J(Is.$$.fragment,l),J($s.$$.fragment,l),J(_s.$$.fragment,l),J(Zs.$$.fragment,l),J(se.$$.fragment,l),J(xs.$$.fragment,l),_n=!1},d(l){l&&(e(f),e(r),e(w),e(b),e(V),e(k),e(R),e(Tl),e(W),e(L),e(ml),e(E),e(P),e(A),e(O),e(N),e(v),e(B),e(el),e(Q),e(G),e(sl),e(Jl),e(dl),e(H),e(wl),e(bl),e(D),e(yl),e(fl),e(q),e(hl),e(F),e(tl),e($l),e(nl),e(Ul),e(xl),e(al),e(Vl),e($),e(Rl),e(Bl),e(ne),e(M),e(C),e(Ds),e(qs),e(Ls),e(Me),e(Ps),e(Os),e(Ks),e(ie),e(lt),e(et),e(st),e(Ue),e(tt),e(nt),e(at),e(oe),e(pt),e(Mt),e(rt),e(ct),e(it),e(we),e(Jt),e(yt),e(fe),e(Ut),e(Tt),e(he),e(mt),e(Ce),e(ot),e(jt),e(ut),e(ke),e(dt),e(wt),e(bt),e(ge),e(ft),e(xe),e(ht),e(Ct),e(Ge),e(It),e($t),e(kt),e(Re),e(_t),e(Zt),e(Ne),e(gt),e(Ws),e(Qe),e(xt),e(Vt),e(Ae),e(Gt),e(We),e(Rt),e(Es),e(Ee),e(Bt),e(Nt),e(Qt),e(Ye),e(Xt),e(At),e(Wt),e(Se),e(Et),e(vt),e(Ft),e(Le),e(Yt),e(zt),e(Ht),e(vs),e(St),e(Dt),e(Fs),e(es),e(qt),e(ss),e(Lt),e(Pt),e(ns),e(Ot),e(Kt),e(ps),e(ln),e(Ms),e(en),e(sn),e(tn),e(is),e(nn),e(an),e(pn),e(Mn),e(rn),e(ms),e(cn),e(Ys),e(os),e(Jn),e(yn),e(us),e(Un),e(Tn),e(ws),e(mn),e(bs),e(on),e(jn),e(zs),e(Hs),e(un),e(Cs),e(dn),e(wn),e(bn),e(ks),e(fn),e(hn),e(Cn),e(gs),e(In),e($n),e(kn),e(Ss)),e(p),j(Z,l),j(g,l),Vs[_].d(l),j(X,l),j(rl,l),j(S,l),j(cl,l),j(jl,l),j(il,l),j(c,l),j(Gl,l),j(Nl,l),j(ae,l),j(pe,l),j(re,l),j(ce,l),j(Je,l),j(ye,l),j(Te,l),j(me,l),j(je,l),j(Ol,l),j(ue,l),j(de,l),j(be,l),j(Kl,l),j(Ie,l),j($e,l),j(_e,l),j(Ze,l),j(Ve,l),j(le,l),j(ee,l),j(Be,l),Gs[Ql].d(l),j(Xe,l),Rs[Al].d(l),j(ve,l),j(Fe,l),j(ze,l),j(He,l),j(De,l),j(qe,l),j(Pe,l),j(Oe,l),Bs[El].d(l),j(Ke,l),j(ls,l),ul&&ul.d(l),j(ts,l),j(as,l),j(rs,l),j(cs,l),j(Js,l),j(ys,l),j(Us,l),j(Ts,l),Ns[Fl].d(l),j(js,l),j(ds,l),j(fs,l),Qs[zl].d(l),pl&&pl.d(l),j(hs,l),j(Is,l),j($s,l),j(_s,l),j(Zs,l),j(se,l),j(xs,l)}}}const op='{"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 jp(x,p,f){let r="pt";return va(()=>{const w=new URLSearchParams(window.location.search);f(0,r=w.get("fw")||"pt")}),[r]}class $p extends Fa{constructor(p){super(),Ya(this,p,jp,mp,Ea,{})}}export{$p as component};
