import{s as pt,o as rt,n as Oe}from"../chunks/scheduler.5c93273d.js";import{S as it,i as mt,g as h,s as p,r as u,A as ot,h as M,f as t,c as r,j as Ce,u as $,x as j,k as xe,y as _e,a as l,v as y,d as g,t as b,w}from"../chunks/index.3944bef6.js";import{T as et}from"../chunks/Tip.9595e28c.js";import{Y as De}from"../chunks/Youtube.0c57e591.js";import{C as I}from"../chunks/CodeBlock.39195187.js";import{D as dt}from"../chunks/DocNotebookDropdown.d64e2194.js";import{F as nt,M as tt}from"../chunks/Markdown.a5917268.js";import{H as Je}from"../chunks/Heading.cee1af11.js";function ct(fe){let i,T="您将会看到一个警告，提到一些预训练权重未被使用，以及一些权重被随机初始化。不用担心，这是完全正常的！BERT 模型的预训练<code>head</code>被丢弃，并替换为一个随机初始化的分类<code>head</code>。您将在您的序列分类任务上微调这个新模型<code>head</code>，将预训练模型的知识转移给它。";return{c(){i=h("p"),i.innerHTML=T},l(m){i=M(m,"P",{"data-svelte-h":!0}),j(i)!=="svelte-g3dpsv"&&(i.innerHTML=T)},m(m,J){l(m,i,J)},p:Oe,d(m){m&&t(i)}}}function ft(fe){let i,T,m,J,Z,ne="🤗 Transformers 提供了一个专为训练 🤗 Transformers 模型而优化的 <code>Trainer</code> 类，使您无需手动编写自己的训练循环步骤而更轻松地开始训练模型。<code>Trainer</code> API 支持各种训练选项和功能，如日志记录、梯度累积和混合精度。",Y,pe,x='首先加载您的模型并指定期望的标签数量。根据 Yelp Review <a href="https://huggingface.co/datasets/yelp_review_full#data-fields" rel="nofollow">数据集卡片</a>，您知道有五个标签：',re,z,E,he,V,U,Me,N,ue='接下来，创建一个 <code>TrainingArguments</code> 类，其中包含您可以调整的所有超参数以及用于激活不同训练选项的标志。对于本教程，您可以从默认的训练<a href="https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments" rel="nofollow">超参数</a>开始，但随时可以尝试不同的设置以找到最佳设置。',B,H,se="指定保存训练检查点的位置：",$e,L,le,X,Te,v,P='<code>Trainer</code> 在训练过程中不会自动评估模型性能。您需要向 <code>Trainer</code> 传递一个函数来计算和展示指标。<a href="https://huggingface.co/docs/evaluate/index" rel="nofollow">🤗 Evaluate</a> 库提供了一个简单的 <a href="https://huggingface.co/spaces/evaluate-metric/accuracy" rel="nofollow"><code>accuracy</code></a> 函数，您可以使用 <code>evaluate.load</code> 函数加载它（有关更多信息，请参阅此<a href="https://huggingface.co/docs/evaluate/a_quick_tour" rel="nofollow">快速入门</a>）：',ie,A,K,_,ye="在 <code>metric</code> 上调用 <code>compute</code> 来计算您的预测的准确性。在将预测传递给 <code>compute</code> 之前，您需要将预测转换为<code>logits</code>（请记住，所有 🤗 Transformers 模型都返回对<code>logits</code>）：",me,Q,D,C,ge="如果您希望在微调过程中监视评估指标，请在您的训练参数中指定 <code>evaluation_strategy</code> 参数，以在每个<code>epoch</code>结束时展示评估指标：",ae,G,be,O,W,we,oe="创建一个包含您的模型、训练参数、训练和测试数据集以及评估函数的 <code>Trainer</code> 对象：",F,ee,R,de,q="然后调用<code>train()</code>以微调模型：",te,k,ce;return i=new De({props:{id:"nvBXf7s7vTI"}}),m=new Je({props:{title:"使用 PyTorch Trainer 进行训练",local:"使用-pytorch-trainer-进行训练",headingTag:"h2"}}),z=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUyMmJlcnQtYmFzZS1jYXNlZCUyMiUyQyUyMG51bV9sYWJlbHMlM0Q1KQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">&quot;bert-base-cased&quot;</span>, num_labels=<span class="hljs-number">5</span>)`,wrap:!1}}),he=new et({props:{$$slots:{default:[ct]},$$scope:{ctx:fe}}}),U=new Je({props:{title:"训练超参数",local:"训练超参数",headingTag:"h3"}}),L=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluaW5nQXJndW1lbnRzJTBBJTBBdHJhaW5pbmdfYXJncyUyMCUzRCUyMFRyYWluaW5nQXJndW1lbnRzKG91dHB1dF9kaXIlM0QlMjJ0ZXN0X3RyYWluZXIlMjIp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments

<span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments(output_dir=<span class="hljs-string">&quot;test_trainer&quot;</span>)`,wrap:!1}}),X=new Je({props:{title:"评估",local:"评估",headingTag:"h3"}}),A=new I({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBaW1wb3J0JTIwZXZhbHVhdGUlMEElMEFtZXRyaWMlMjAlM0QlMjBldmFsdWF0ZS5sb2FkKCUyMmFjY3VyYWN5JTIyKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> evaluate

<span class="hljs-meta">&gt;&gt;&gt; </span>metric = evaluate.load(<span class="hljs-string">&quot;accuracy&quot;</span>)`,wrap:!1}}),Q=new I({props:{code:"ZGVmJTIwY29tcHV0ZV9tZXRyaWNzKGV2YWxfcHJlZCklM0ElMEElMjAlMjAlMjAlMjBsb2dpdHMlMkMlMjBsYWJlbHMlMjAlM0QlMjBldmFsX3ByZWQlMEElMjAlMjAlMjAlMjBwcmVkaWN0aW9ucyUyMCUzRCUyMG5wLmFyZ21heChsb2dpdHMlMkMlMjBheGlzJTNELTEpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwbWV0cmljLmNvbXB1dGUocHJlZGljdGlvbnMlM0RwcmVkaWN0aW9ucyUyQyUyMHJlZmVyZW5jZXMlM0RsYWJlbHMp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>):
<span class="hljs-meta">... </span>    logits, labels = eval_pred
<span class="hljs-meta">... </span>    predictions = np.argmax(logits, axis=-<span class="hljs-number">1</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels)`,wrap:!1}}),G=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluaW5nQXJndW1lbnRzJTJDJTIwVHJhaW5lciUwQSUwQXRyYWluaW5nX2FyZ3MlMjAlM0QlMjBUcmFpbmluZ0FyZ3VtZW50cyhvdXRwdXRfZGlyJTNEJTIydGVzdF90cmFpbmVyJTIyJTJDJTIwZXZhbHVhdGlvbl9zdHJhdGVneSUzRCUyMmVwb2NoJTIyKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments, Trainer

<span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments(output_dir=<span class="hljs-string">&quot;test_trainer&quot;</span>, evaluation_strategy=<span class="hljs-string">&quot;epoch&quot;</span>)`,wrap:!1}}),O=new Je({props:{title:"训练器",local:"训练器",headingTag:"h3"}}),ee=new I({props:{code:"dHJhaW5lciUyMCUzRCUyMFRyYWluZXIoJTBBJTIwJTIwJTIwJTIwbW9kZWwlM0Rtb2RlbCUyQyUwQSUyMCUyMCUyMCUyMGFyZ3MlM0R0cmFpbmluZ19hcmdzJTJDJTBBJTIwJTIwJTIwJTIwdHJhaW5fZGF0YXNldCUzRHNtYWxsX3RyYWluX2RhdGFzZXQlMkMlMEElMjAlMjAlMjAlMjBldmFsX2RhdGFzZXQlM0RzbWFsbF9ldmFsX2RhdGFzZXQlMkMlMEElMjAlMjAlMjAlMjBjb21wdXRlX21ldHJpY3MlM0Rjb21wdXRlX21ldHJpY3MlMkMlMEEp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>trainer = Trainer(
<span class="hljs-meta">... </span>    model=model,
<span class="hljs-meta">... </span>    args=training_args,
<span class="hljs-meta">... </span>    train_dataset=small_train_dataset,
<span class="hljs-meta">... </span>    eval_dataset=small_eval_dataset,
<span class="hljs-meta">... </span>    compute_metrics=compute_metrics,
<span class="hljs-meta">... </span>)`,wrap:!1}}),k=new I({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()',wrap:!1}}),{c(){u(i.$$.fragment),T=p(),u(m.$$.fragment),J=p(),Z=h("p"),Z.innerHTML=ne,Y=p(),pe=h("p"),pe.innerHTML=x,re=p(),u(z.$$.fragment),E=p(),u(he.$$.fragment),V=p(),u(U.$$.fragment),Me=p(),N=h("p"),N.innerHTML=ue,B=p(),H=h("p"),H.textContent=se,$e=p(),u(L.$$.fragment),le=p(),u(X.$$.fragment),Te=p(),v=h("p"),v.innerHTML=P,ie=p(),u(A.$$.fragment),K=p(),_=h("p"),_.innerHTML=ye,me=p(),u(Q.$$.fragment),D=p(),C=h("p"),C.innerHTML=ge,ae=p(),u(G.$$.fragment),be=p(),u(O.$$.fragment),W=p(),we=h("p"),we.innerHTML=oe,F=p(),u(ee.$$.fragment),R=p(),de=h("p"),de.innerHTML=q,te=p(),u(k.$$.fragment)},l(a){$(i.$$.fragment,a),T=r(a),$(m.$$.fragment,a),J=r(a),Z=M(a,"P",{"data-svelte-h":!0}),j(Z)!=="svelte-1oqslc0"&&(Z.innerHTML=ne),Y=r(a),pe=M(a,"P",{"data-svelte-h":!0}),j(pe)!=="svelte-87l9t0"&&(pe.innerHTML=x),re=r(a),$(z.$$.fragment,a),E=r(a),$(he.$$.fragment,a),V=r(a),$(U.$$.fragment,a),Me=r(a),N=M(a,"P",{"data-svelte-h":!0}),j(N)!=="svelte-gfewmt"&&(N.innerHTML=ue),B=r(a),H=M(a,"P",{"data-svelte-h":!0}),j(H)!=="svelte-1rwzshl"&&(H.textContent=se),$e=r(a),$(L.$$.fragment,a),le=r(a),$(X.$$.fragment,a),Te=r(a),v=M(a,"P",{"data-svelte-h":!0}),j(v)!=="svelte-1ulry18"&&(v.innerHTML=P),ie=r(a),$(A.$$.fragment,a),K=r(a),_=M(a,"P",{"data-svelte-h":!0}),j(_)!=="svelte-mvhx78"&&(_.innerHTML=ye),me=r(a),$(Q.$$.fragment,a),D=r(a),C=M(a,"P",{"data-svelte-h":!0}),j(C)!=="svelte-7py9yh"&&(C.innerHTML=ge),ae=r(a),$(G.$$.fragment,a),be=r(a),$(O.$$.fragment,a),W=r(a),we=M(a,"P",{"data-svelte-h":!0}),j(we)!=="svelte-1np8g37"&&(we.innerHTML=oe),F=r(a),$(ee.$$.fragment,a),R=r(a),de=M(a,"P",{"data-svelte-h":!0}),j(de)!=="svelte-1588yvr"&&(de.innerHTML=q),te=r(a),$(k.$$.fragment,a)},m(a,c){y(i,a,c),l(a,T,c),y(m,a,c),l(a,J,c),l(a,Z,c),l(a,Y,c),l(a,pe,c),l(a,re,c),y(z,a,c),l(a,E,c),y(he,a,c),l(a,V,c),y(U,a,c),l(a,Me,c),l(a,N,c),l(a,B,c),l(a,H,c),l(a,$e,c),y(L,a,c),l(a,le,c),y(X,a,c),l(a,Te,c),l(a,v,c),l(a,ie,c),y(A,a,c),l(a,K,c),l(a,_,c),l(a,me,c),y(Q,a,c),l(a,D,c),l(a,C,c),l(a,ae,c),y(G,a,c),l(a,be,c),y(O,a,c),l(a,W,c),l(a,we,c),l(a,F,c),y(ee,a,c),l(a,R,c),l(a,de,c),l(a,te,c),y(k,a,c),ce=!0},p(a,c){const S={};c&2&&(S.$$scope={dirty:c,ctx:a}),he.$set(S)},i(a){ce||(g(i.$$.fragment,a),g(m.$$.fragment,a),g(z.$$.fragment,a),g(he.$$.fragment,a),g(U.$$.fragment,a),g(L.$$.fragment,a),g(X.$$.fragment,a),g(A.$$.fragment,a),g(Q.$$.fragment,a),g(G.$$.fragment,a),g(O.$$.fragment,a),g(ee.$$.fragment,a),g(k.$$.fragment,a),ce=!0)},o(a){b(i.$$.fragment,a),b(m.$$.fragment,a),b(z.$$.fragment,a),b(he.$$.fragment,a),b(U.$$.fragment,a),b(L.$$.fragment,a),b(X.$$.fragment,a),b(A.$$.fragment,a),b(Q.$$.fragment,a),b(G.$$.fragment,a),b(O.$$.fragment,a),b(ee.$$.fragment,a),b(k.$$.fragment,a),ce=!1},d(a){a&&(t(T),t(J),t(Z),t(Y),t(pe),t(re),t(E),t(V),t(Me),t(N),t(B),t(H),t($e),t(le),t(Te),t(v),t(ie),t(K),t(_),t(me),t(D),t(C),t(ae),t(be),t(W),t(we),t(F),t(R),t(de),t(te)),w(i,a),w(m,a),w(z,a),w(he,a),w(U,a),w(L,a),w(X,a),w(A,a),w(Q,a),w(G,a),w(O,a),w(ee,a),w(k,a)}}}function ht(fe){let i,T;return i=new tt({props:{$$slots:{default:[ft]},$$scope:{ctx:fe}}}),{c(){u(i.$$.fragment)},l(m){$(i.$$.fragment,m)},m(m,J){y(i,m,J),T=!0},p(m,J){const Z={};J&2&&(Z.$$scope={dirty:J,ctx:m}),i.$set(Z)},i(m){T||(g(i.$$.fragment,m),T=!0)},o(m){b(i.$$.fragment,m),T=!1},d(m){w(i,m)}}}function Mt(fe){let i,T="当您使用 <code>compile()</code> 编译模型时，无需传递损失参数！如果不指定损失参数，Hugging Face 模型会自动选择适合其任务和模型架构的损失函数。如果需要，您始终可以自己指定损失函数以覆盖默认配置。";return{c(){i=h("p"),i.innerHTML=T},l(m){i=M(m,"P",{"data-svelte-h":!0}),j(i)!=="svelte-1cfr0mt"&&(i.innerHTML=T)},m(m,J){l(m,i,J)},p:Oe,d(m){m&&t(i)}}}function ut(fe){let i,T,m,J,Z,ne,Y,pe="您也可以使用 Keras API 在 TensorFlow 中训练 🤗 Transformers 模型！",x,re,z,E,he="当您希望使用 Keras API 训练 🤗 Transformers 模型时，您需要将您的数据集转换为 Keras 可理解的格式。如果您的数据集很小，您可以将整个数据集转换为NumPy数组并传递给 Keras。在进行更复杂的操作之前，让我们先尝试这种方法。",V,U,Me='首先，加载一个数据集。我们将使用 <a href="https://huggingface.co/datasets/glue" rel="nofollow">GLUE benchmark</a> 中的 CoLA 数据集，因为它是一个简单的二元文本分类任务。现在只使用训练数据集。',N,ue,B,H,se="接下来，加载一个<code>tokenizer</code>并将数据标记为 NumPy 数组。请注意，标签已经是由 0 和 1 组成的<code>list</code>，因此我们可以直接将其转换为 NumPy 数组而无需进行分词处理！",$e,L,le,X,Te='最后，加载、<a href="https://keras.io/api/models/model_training_apis/#compile-method" rel="nofollow"><code>compile</code></a> 和 <a href="https://keras.io/api/models/model_training_apis/#fit-method" rel="nofollow"><code>fit</code></a> 模型。请注意，Transformers 模型都有一个默认的与任务相关的损失函数，因此除非您希望自定义，否则无需指定一个损失函数：',v,P,ie,A,K,_,ye="这种方法对于较小的数据集效果很好，但对于较大的数据集，您可能会发现它开始变得有问题。为什么呢？因为分词后的数组和标签必须完全加载到内存中，而且由于 NumPy 无法处理“不规则”数组，因此每个分词后的样本长度都必须被填充到数据集中最长样本的长度。这将使您的数组变得更大，而所有这些<code>padding tokens</code>也会减慢训练速度！",me,Q,D,C,ge="如果您想避免训练速度减慢，可以将数据加载为 <code>tf.data.Dataset</code>。虽然您可以自己编写自己的 <code>tf.data</code> 流水线，但我们有两种方便的方法来实现这一点：",ae,G,be='<li><a href="/docs/transformers/main/zh/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a>：这是我们在大多数情况下推荐的方法。因为它是模型上的一个方法，它可以检查模型以自动确定哪些列可用作模型输入，并丢弃其他列以创建一个更简单、性能更好的数据集。</li> <li><code>to_tf_dataset</code>：这个方法更低级，但当您希望完全控制数据集的创建方式时非常有用，可以通过指定要包括的确切 <code>columns</code> 和 <code>label_cols</code> 来实现。</li>',O,W,we='在使用 <a href="/docs/transformers/main/zh/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a> 之前，您需要将<code>tokenizer</code>的输出添加到数据集作为列，如下面的代码示例所示：',oe,F,ee,R,de="请记住，默认情况下，Hugging Face 数据集存储在硬盘上，因此这不会增加您的内存使用！一旦列已经添加，您可以从数据集中流式的传输批次数据，并为每个批次添加<code>padding tokens</code>，这与为整个数据集添加<code>padding tokens</code>相比，大大减少了<code>padding tokens</code>的数量。",q,te,k,ce,a='请注意，在上面的代码示例中，您需要将<code>tokenizer</code>传递给<code>prepare_tf_dataset</code>，以便它可以在加载批次时正确填充它们。如果数据集中的所有样本都具有相同的长度而且不需要填充，您可以跳过此参数。如果需要执行比填充样本更复杂的操作（例如，用于掩码语言模型的<code>tokens</code> 替换），则可以使用 <code>collate_fn</code> 参数，而不是传递一个函数来将样本列表转换为批次并应用任何所需的预处理。请查看我们的<a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">示例</a>或<a href="https://huggingface.co/docs/transformers/notebooks" rel="nofollow">笔记</a>以了解此方法的实际操作。',c,S,Ze="一旦创建了 <code>tf.data.Dataset</code>，您可以像以前一样编译和训练模型：",Ue,ke,je;return m=new De({props:{id:"rnTGBy2ax1c"}}),Z=new Je({props:{title:"使用keras训练TensorFlow模型",local:"使用keras训练tensorflow模型",headingTag:"h2"}}),re=new Je({props:{title:"加载用于 Keras 的数据",local:"加载用于-keras-的数据",headingTag:"h3"}}),ue=new I({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBJTBBZGF0YXNldCUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJnbHVlJTIyJTJDJTIwJTIyY29sYSUyMiklMEFkYXRhc2V0JTIwJTNEJTIwZGF0YXNldCU1QiUyMnRyYWluJTIyJTVEJTIwJTIwJTIzJTIwSnVzdCUyMHRha2UlMjB0aGUlMjB0cmFpbmluZyUyMHNwbGl0JTIwZm9yJTIwbm93",highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

dataset = load_dataset(<span class="hljs-string">&quot;glue&quot;</span>, <span class="hljs-string">&quot;cola&quot;</span>)
dataset = dataset[<span class="hljs-string">&quot;train&quot;</span>]  <span class="hljs-comment"># Just take the training split for now</span>`,wrap:!1}}),L=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJiZXJ0LWJhc2UtY2FzZWQlMjIpJTBBdG9rZW5pemVkX2RhdGElMjAlM0QlMjB0b2tlbml6ZXIoZGF0YXNldCU1QiUyMnNlbnRlbmNlJTIyJTVEJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJucCUyMiUyQyUyMHBhZGRpbmclM0RUcnVlKSUwQSUyMyUyMFRva2VuaXplciUyMHJldHVybnMlMjBhJTIwQmF0Y2hFbmNvZGluZyUyQyUyMGJ1dCUyMHdlJTIwY29udmVydCUyMHRoYXQlMjB0byUyMGElMjBkaWN0JTIwZm9yJTIwS2VyYXMlMEF0b2tlbml6ZWRfZGF0YSUyMCUzRCUyMGRpY3QodG9rZW5pemVkX2RhdGEpJTBBJTBBbGFiZWxzJTIwJTNEJTIwbnAuYXJyYXkoZGF0YXNldCU1QiUyMmxhYmVsJTIyJTVEKSUyMCUyMCUyMyUyMExhYmVsJTIwaXMlMjBhbHJlYWR5JTIwYW4lMjBhcnJheSUyMG9mJTIwMCUyMGFuZCUyMDE=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;bert-base-cased&quot;</span>)
tokenized_data = tokenizer(dataset[<span class="hljs-string">&quot;sentence&quot;</span>], return_tensors=<span class="hljs-string">&quot;np&quot;</span>, padding=<span class="hljs-literal">True</span>)
<span class="hljs-comment"># Tokenizer returns a BatchEncoding, but we convert that to a dict for Keras</span>
tokenized_data = <span class="hljs-built_in">dict</span>(tokenized_data)

labels = np.array(dataset[<span class="hljs-string">&quot;label&quot;</span>])  <span class="hljs-comment"># Label is already an array of 0 and 1</span>`,wrap:!1}}),P=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRGQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbiUwQWZyb20lMjB0ZW5zb3JmbG93LmtlcmFzLm9wdGltaXplcnMlMjBpbXBvcnQlMjBBZGFtJTBBJTBBJTIzJTIwTG9hZCUyMGFuZCUyMGNvbXBpbGUlMjBvdXIlMjBtb2RlbCUwQW1vZGVsJTIwJTNEJTIwVEZBdXRvTW9kZWxGb3JTZXF1ZW5jZUNsYXNzaWZpY2F0aW9uLmZyb21fcHJldHJhaW5lZCglMjJiZXJ0LWJhc2UtY2FzZWQlMjIpJTBBJTIzJTIwTG93ZXIlMjBsZWFybmluZyUyMHJhdGVzJTIwYXJlJTIwb2Z0ZW4lMjBiZXR0ZXIlMjBmb3IlMjBmaW5lLXR1bmluZyUyMHRyYW5zZm9ybWVycyUwQW1vZGVsLmNvbXBpbGUob3B0aW1pemVyJTNEQWRhbSgzZS01KSklMjAlMjAlMjMlMjBObyUyMGxvc3MlMjBhcmd1bWVudCElMEElMEFtb2RlbC5maXQodG9rZW5pemVkX2RhdGElMkMlMjBsYWJlbHMp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSequenceClassification
<span class="hljs-keyword">from</span> tensorflow.keras.optimizers <span class="hljs-keyword">import</span> Adam

<span class="hljs-comment"># Load and compile our model</span>
model = TFAutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">&quot;bert-base-cased&quot;</span>)
<span class="hljs-comment"># Lower learning rates are often better for fine-tuning transformers</span>
model.<span class="hljs-built_in">compile</span>(optimizer=Adam(<span class="hljs-number">3e-5</span>))  <span class="hljs-comment"># No loss argument!</span>

model.fit(tokenized_data, labels)`,wrap:!1}}),A=new et({props:{$$slots:{default:[Mt]},$$scope:{ctx:fe}}}),Q=new Je({props:{title:"将数据加载为 tf.data.Dataset",local:"将数据加载为-tfdatadataset",headingTag:"h3"}}),F=new I({props:{code:"ZGVmJTIwdG9rZW5pemVfZGF0YXNldChkYXRhKSUzQSUwQSUyMCUyMCUyMCUyMCUyMyUyMEtleXMlMjBvZiUyMHRoZSUyMHJldHVybmVkJTIwZGljdGlvbmFyeSUyMHdpbGwlMjBiZSUyMGFkZGVkJTIwdG8lMjB0aGUlMjBkYXRhc2V0JTIwYXMlMjBjb2x1bW5zJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwdG9rZW5pemVyKGRhdGElNUIlMjJ0ZXh0JTIyJTVEKSUwQSUwQSUwQWRhdGFzZXQlMjAlM0QlMjBkYXRhc2V0Lm1hcCh0b2tlbml6ZV9kYXRhc2V0KQ==",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_dataset</span>(<span class="hljs-params">data</span>):
    <span class="hljs-comment"># Keys of the returned dictionary will be added to the dataset as columns</span>
    <span class="hljs-keyword">return</span> tokenizer(data[<span class="hljs-string">&quot;text&quot;</span>])


dataset = dataset.<span class="hljs-built_in">map</span>(tokenize_dataset)`,wrap:!1}}),te=new I({props:{code:"dGZfZGF0YXNldCUyMCUzRCUyMG1vZGVsLnByZXBhcmVfdGZfZGF0YXNldChkYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQlMkMlMjBiYXRjaF9zaXplJTNEMTYlMkMlMjBzaHVmZmxlJTNEVHJ1ZSUyQyUyMHRva2VuaXplciUzRHRva2VuaXplcik=",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>tf_dataset = model.prepare_tf_dataset(dataset[<span class="hljs-string">&quot;train&quot;</span>], batch_size=<span class="hljs-number">16</span>, shuffle=<span class="hljs-literal">True</span>, tokenizer=tokenizer)',wrap:!1}}),ke=new I({props:{code:"bW9kZWwuY29tcGlsZShvcHRpbWl6ZXIlM0RBZGFtKDNlLTUpKSUyMCUyMCUyMyUyME5vJTIwbG9zcyUyMGFyZ3VtZW50ISUwQSUwQW1vZGVsLmZpdCh0Zl9kYXRhc2V0KQ==",highlighted:`model.<span class="hljs-built_in">compile</span>(optimizer=Adam(<span class="hljs-number">3e-5</span>))  <span class="hljs-comment"># No loss argument!</span>

model.fit(tf_dataset)`,wrap:!1}}),{c(){i=h("a"),T=p(),u(m.$$.fragment),J=p(),u(Z.$$.fragment),ne=p(),Y=h("p"),Y.textContent=pe,x=p(),u(re.$$.fragment),z=p(),E=h("p"),E.textContent=he,V=p(),U=h("p"),U.innerHTML=Me,N=p(),u(ue.$$.fragment),B=p(),H=h("p"),H.innerHTML=se,$e=p(),u(L.$$.fragment),le=p(),X=h("p"),X.innerHTML=Te,v=p(),u(P.$$.fragment),ie=p(),u(A.$$.fragment),K=p(),_=h("p"),_.innerHTML=ye,me=p(),u(Q.$$.fragment),D=p(),C=h("p"),C.innerHTML=ge,ae=p(),G=h("ul"),G.innerHTML=be,O=p(),W=h("p"),W.innerHTML=we,oe=p(),u(F.$$.fragment),ee=p(),R=h("p"),R.innerHTML=de,q=p(),u(te.$$.fragment),k=p(),ce=h("p"),ce.innerHTML=a,c=p(),S=h("p"),S.innerHTML=Ze,Ue=p(),u(ke.$$.fragment),this.h()},l(n){i=M(n,"A",{id:!0}),Ce(i).forEach(t),T=r(n),$(m.$$.fragment,n),J=r(n),$(Z.$$.fragment,n),ne=r(n),Y=M(n,"P",{"data-svelte-h":!0}),j(Y)!=="svelte-bcxyio"&&(Y.textContent=pe),x=r(n),$(re.$$.fragment,n),z=r(n),E=M(n,"P",{"data-svelte-h":!0}),j(E)!=="svelte-i3cei"&&(E.textContent=he),V=r(n),U=M(n,"P",{"data-svelte-h":!0}),j(U)!=="svelte-1km4tqo"&&(U.innerHTML=Me),N=r(n),$(ue.$$.fragment,n),B=r(n),H=M(n,"P",{"data-svelte-h":!0}),j(H)!=="svelte-7lgu56"&&(H.innerHTML=se),$e=r(n),$(L.$$.fragment,n),le=r(n),X=M(n,"P",{"data-svelte-h":!0}),j(X)!=="svelte-1ggltyx"&&(X.innerHTML=Te),v=r(n),$(P.$$.fragment,n),ie=r(n),$(A.$$.fragment,n),K=r(n),_=M(n,"P",{"data-svelte-h":!0}),j(_)!=="svelte-1f7t43t"&&(_.innerHTML=ye),me=r(n),$(Q.$$.fragment,n),D=r(n),C=M(n,"P",{"data-svelte-h":!0}),j(C)!=="svelte-1wj6cmg"&&(C.innerHTML=ge),ae=r(n),G=M(n,"UL",{"data-svelte-h":!0}),j(G)!=="svelte-13siqzd"&&(G.innerHTML=be),O=r(n),W=M(n,"P",{"data-svelte-h":!0}),j(W)!=="svelte-vpnvoi"&&(W.innerHTML=we),oe=r(n),$(F.$$.fragment,n),ee=r(n),R=M(n,"P",{"data-svelte-h":!0}),j(R)!=="svelte-hz86ti"&&(R.innerHTML=de),q=r(n),$(te.$$.fragment,n),k=r(n),ce=M(n,"P",{"data-svelte-h":!0}),j(ce)!=="svelte-5a7b6l"&&(ce.innerHTML=a),c=r(n),S=M(n,"P",{"data-svelte-h":!0}),j(S)!=="svelte-10v3zfh"&&(S.innerHTML=Ze),Ue=r(n),$(ke.$$.fragment,n),this.h()},h(){xe(i,"id","keras")},m(n,f){l(n,i,f),l(n,T,f),y(m,n,f),l(n,J,f),y(Z,n,f),l(n,ne,f),l(n,Y,f),l(n,x,f),y(re,n,f),l(n,z,f),l(n,E,f),l(n,V,f),l(n,U,f),l(n,N,f),y(ue,n,f),l(n,B,f),l(n,H,f),l(n,$e,f),y(L,n,f),l(n,le,f),l(n,X,f),l(n,v,f),y(P,n,f),l(n,ie,f),y(A,n,f),l(n,K,f),l(n,_,f),l(n,me,f),y(Q,n,f),l(n,D,f),l(n,C,f),l(n,ae,f),l(n,G,f),l(n,O,f),l(n,W,f),l(n,oe,f),y(F,n,f),l(n,ee,f),l(n,R,f),l(n,q,f),y(te,n,f),l(n,k,f),l(n,ce,f),l(n,c,f),l(n,S,f),l(n,Ue,f),y(ke,n,f),je=!0},p(n,f){const Ge={};f&2&&(Ge.$$scope={dirty:f,ctx:n}),A.$set(Ge)},i(n){je||(g(m.$$.fragment,n),g(Z.$$.fragment,n),g(re.$$.fragment,n),g(ue.$$.fragment,n),g(L.$$.fragment,n),g(P.$$.fragment,n),g(A.$$.fragment,n),g(Q.$$.fragment,n),g(F.$$.fragment,n),g(te.$$.fragment,n),g(ke.$$.fragment,n),je=!0)},o(n){b(m.$$.fragment,n),b(Z.$$.fragment,n),b(re.$$.fragment,n),b(ue.$$.fragment,n),b(L.$$.fragment,n),b(P.$$.fragment,n),b(A.$$.fragment,n),b(Q.$$.fragment,n),b(F.$$.fragment,n),b(te.$$.fragment,n),b(ke.$$.fragment,n),je=!1},d(n){n&&(t(i),t(T),t(J),t(ne),t(Y),t(x),t(z),t(E),t(V),t(U),t(N),t(B),t(H),t($e),t(le),t(X),t(v),t(ie),t(K),t(_),t(me),t(D),t(C),t(ae),t(G),t(O),t(W),t(oe),t(ee),t(R),t(q),t(k),t(ce),t(c),t(S),t(Ue)),w(m,n),w(Z,n),w(re,n),w(ue,n),w(L,n),w(P,n),w(A,n),w(Q,n),w(F,n),w(te,n),w(ke,n)}}}function $t(fe){let i,T;return i=new tt({props:{$$slots:{default:[ut]},$$scope:{ctx:fe}}}),{c(){u(i.$$.fragment)},l(m){$(i.$$.fragment,m)},m(m,J){y(i,m,J),T=!0},p(m,J){const Z={};J&2&&(Z.$$scope={dirty:J,ctx:m}),i.$set(Z)},i(m){T||(g(i.$$.fragment,m),T=!0)},o(m){b(i.$$.fragment,m),T=!1},d(m){w(i,m)}}}function yt(fe){let i,T='如果没有 GPU，可以通过notebook平台如 <a href="https://colab.research.google.com/" rel="nofollow">Colaboratory</a> 或 <a href="https://studiolab.sagemaker.aws/" rel="nofollow">SageMaker StudioLab</a> 来免费获得云端GPU使用。';return{c(){i=h("p"),i.innerHTML=T},l(m){i=M(m,"P",{"data-svelte-h":!0}),j(i)!=="svelte-mcuqp7"&&(i.innerHTML=T)},m(m,J){l(m,i,J)},p:Oe,d(m){m&&t(i)}}}function gt(fe){let i,T,m,J="<code>Trainer</code> 负责训练循环，允许您在一行代码中微调模型。对于喜欢编写自己训练循环的用户，您也可以在原生 PyTorch 中微调 🤗 Transformers 模型。",Z,ne,Y="现在，您可能需要重新启动您的<code>notebook</code>，或执行以下代码以释放一些内存：",pe,x,re,z,E="接下来，手动处理 <code>tokenized_dataset</code> 以准备进行训练。",he,V,U,Me,N="移除 text 列，因为模型不接受原始文本作为输入：",ue,B,H,se,$e,L="将 label 列重命名为 labels，因为模型期望参数的名称为 labels：",le,X,Te,v,P,ie="设置数据集的格式以返回 PyTorch 张量而不是<code>lists</code>：",A,K,_,ye,me="接着，创建一个先前展示的数据集的较小子集，以加速微调过程",Q,D,C,ge,ae,G,be="您的训练和测试数据集创建一个<code>DataLoader</code>类，以便可以迭代处理数据批次",O,W,we,oe,F="加载您的模型，并指定期望的标签数量：",ee,R,de,q,te,k,ce='创建一个<code>optimizer</code>和<code>learning rate scheduler</code>以进行模型微调。让我们使用 PyTorch 中的 <a href="https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html" rel="nofollow">AdamW</a> 优化器：',a,c,S,Ze,Ue="创建来自 <code>Trainer</code> 的默认<code>learning rate scheduler</code>：",ke,je,n,f,Ge="最后，指定 <code>device</code> 以使用 GPU（如果有的话）。否则，使用 CPU 进行训练可能需要几个小时，而不是几分钟。",We,ve,s,o,Re,Ie,st="现在您已经准备好训练了！🥳",Ee,Xe,Ne,ze,lt='为了跟踪训练进度，使用 <a href="https://tqdm.github.io/" rel="nofollow">tqdm</a> 库来添加一个进度条，显示训练步数的进展：',Le,Ve,Ae,Be,Qe,He,at="就像您在 <code>Trainer</code> 中添加了一个评估函数一样，当您编写自己的训练循环时，您需要做同样的事情。但与在每个<code>epoch</code>结束时计算和展示指标不同，这一次您将使用 <code>add_batch</code> 累积所有批次，并在最后计算指标。",qe,Fe,Se;return i=new De({props:{id:"Dh9CL8fyG80"}}),x=new I({props:{code:"ZGVsJTIwbW9kZWwlMEFkZWwlMjB0cmFpbmVyJTBBdG9yY2guY3VkYS5lbXB0eV9jYWNoZSgp",highlighted:`<span class="hljs-keyword">del</span> model
<span class="hljs-keyword">del</span> trainer
torch.cuda.empty_cache()`,wrap:!1}}),B=new I({props:{code:"dG9rZW5pemVkX2RhdGFzZXRzJTIwJTNEJTIwdG9rZW5pemVkX2RhdGFzZXRzLnJlbW92ZV9jb2x1bW5zKCU1QiUyMnRleHQlMjIlNUQp",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets = tokenized_datasets.remove_columns([<span class="hljs-string">&quot;text&quot;</span>])',wrap:!1}}),X=new I({props:{code:"dG9rZW5pemVkX2RhdGFzZXRzJTIwJTNEJTIwdG9rZW5pemVkX2RhdGFzZXRzLnJlbmFtZV9jb2x1bW4oJTIybGFiZWwlMjIlMkMlMjAlMjJsYWJlbHMlMjIp",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets = tokenized_datasets.rename_column(<span class="hljs-string">&quot;label&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>)',wrap:!1}}),K=new I({props:{code:"dG9rZW5pemVkX2RhdGFzZXRzLnNldF9mb3JtYXQoJTIydG9yY2glMjIp",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets.set_format(<span class="hljs-string">&quot;torch&quot;</span>)',wrap:!1}}),D=new I({props:{code:"c21hbGxfdHJhaW5fZGF0YXNldCUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVELnNodWZmbGUoc2VlZCUzRDQyKS5zZWxlY3QocmFuZ2UoMTAwMCkpJTBBc21hbGxfZXZhbF9kYXRhc2V0JTIwJTNEJTIwdG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydGVzdCUyMiU1RC5zaHVmZmxlKHNlZWQlM0Q0Mikuc2VsZWN0KHJhbmdlKDEwMDApKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>small_train_dataset = tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>small_eval_dataset = tokenized_datasets[<span class="hljs-string">&quot;test&quot;</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>))`,wrap:!1}}),ge=new Je({props:{title:"DataLoader",local:"dataloader",headingTag:"h3"}}),W=new I({props:{code:"ZnJvbSUyMHRvcmNoLnV0aWxzLmRhdGElMjBpbXBvcnQlMjBEYXRhTG9hZGVyJTBBJTBBdHJhaW5fZGF0YWxvYWRlciUyMCUzRCUyMERhdGFMb2FkZXIoc21hbGxfdHJhaW5fZGF0YXNldCUyQyUyMHNodWZmbGUlM0RUcnVlJTJDJTIwYmF0Y2hfc2l6ZSUzRDgpJTBBZXZhbF9kYXRhbG9hZGVyJTIwJTNEJTIwRGF0YUxvYWRlcihzbWFsbF9ldmFsX2RhdGFzZXQlMkMlMjBiYXRjaF9zaXplJTNEOCk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader

<span class="hljs-meta">&gt;&gt;&gt; </span>train_dataloader = DataLoader(small_train_dataset, shuffle=<span class="hljs-literal">True</span>, batch_size=<span class="hljs-number">8</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>eval_dataloader = DataLoader(small_eval_dataset, batch_size=<span class="hljs-number">8</span>)`,wrap:!1}}),R=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUyMmJlcnQtYmFzZS1jYXNlZCUyMiUyQyUyMG51bV9sYWJlbHMlM0Q1KQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">&quot;bert-base-cased&quot;</span>, num_labels=<span class="hljs-number">5</span>)`,wrap:!1}}),q=new Je({props:{title:"Optimizer and learning rate scheduler",local:"optimizer-and-learning-rate-scheduler",headingTag:"h3"}}),c=new I({props:{code:"ZnJvbSUyMHRvcmNoLm9wdGltJTIwaW1wb3J0JTIwQWRhbVclMEElMEFvcHRpbWl6ZXIlMjAlM0QlMjBBZGFtVyhtb2RlbC5wYXJhbWV0ZXJzKCklMkMlMjBsciUzRDVlLTUp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW

<span class="hljs-meta">&gt;&gt;&gt; </span>optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">5e-5</span>)`,wrap:!1}}),je=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMGdldF9zY2hlZHVsZXIlMEElMEFudW1fZXBvY2hzJTIwJTNEJTIwMyUwQW51bV90cmFpbmluZ19zdGVwcyUyMCUzRCUyMG51bV9lcG9jaHMlMjAqJTIwbGVuKHRyYWluX2RhdGFsb2FkZXIpJTBBbHJfc2NoZWR1bGVyJTIwJTNEJTIwZ2V0X3NjaGVkdWxlciglMEElMjAlMjAlMjAlMjBuYW1lJTNEJTIybGluZWFyJTIyJTJDJTIwb3B0aW1pemVyJTNEb3B0aW1pemVyJTJDJTIwbnVtX3dhcm11cF9zdGVwcyUzRDAlMkMlMjBudW1fdHJhaW5pbmdfc3RlcHMlM0RudW1fdHJhaW5pbmdfc3RlcHMlMEEp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler

<span class="hljs-meta">&gt;&gt;&gt; </span>num_epochs = <span class="hljs-number">3</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dataloader)
<span class="hljs-meta">&gt;&gt;&gt; </span>lr_scheduler = get_scheduler(
<span class="hljs-meta">... </span>    name=<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
<span class="hljs-meta">... </span>)`,wrap:!1}}),ve=new I({props:{code:"aW1wb3J0JTIwdG9yY2glMEElMEFkZXZpY2UlMjAlM0QlMjB0b3JjaC5kZXZpY2UoJTIyY3VkYSUyMiklMjBpZiUyMHRvcmNoLmN1ZGEuaXNfYXZhaWxhYmxlKCklMjBlbHNlJTIwdG9yY2guZGV2aWNlKCUyMmNwdSUyMiklMEFtb2RlbC50byhkZXZpY2Up",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch

<span class="hljs-meta">&gt;&gt;&gt; </span>device = torch.device(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">&quot;cpu&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model.to(device)`,wrap:!1}}),o=new et({props:{$$slots:{default:[yt]},$$scope:{ctx:fe}}}),Xe=new Je({props:{title:"训练循环",local:"训练循环",headingTag:"h3"}}),Ve=new I({props:{code:"ZnJvbSUyMHRxZG0uYXV0byUyMGltcG9ydCUyMHRxZG0lMEElMEFwcm9ncmVzc19iYXIlMjAlM0QlMjB0cWRtKHJhbmdlKG51bV90cmFpbmluZ19zdGVwcykpJTBBJTBBbW9kZWwudHJhaW4oKSUwQWZvciUyMGVwb2NoJTIwaW4lMjByYW5nZShudW1fZXBvY2hzKSUzQSUwQSUyMCUyMCUyMCUyMGZvciUyMGJhdGNoJTIwaW4lMjB0cmFpbl9kYXRhbG9hZGVyJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYmF0Y2glMjAlM0QlMjAlN0JrJTNBJTIwdi50byhkZXZpY2UpJTIwZm9yJTIwayUyQyUyMHYlMjBpbiUyMGJhdGNoLml0ZW1zKCklN0QlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvdXRwdXRzJTIwJTNEJTIwbW9kZWwoKipiYXRjaCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwb3V0cHV0cy5sb3NzJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbG9zcy5iYWNrd2FyZCgpJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwb3B0aW1pemVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxyX3NjaGVkdWxlci5zdGVwKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuemVyb19ncmFkKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9ncmVzc19iYXIudXBkYXRlKDEp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm

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

<span class="hljs-meta">&gt;&gt;&gt; </span>model.train()
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs):
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
<span class="hljs-meta">... </span>        batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}
<span class="hljs-meta">... </span>        outputs = model(**batch)
<span class="hljs-meta">... </span>        loss = outputs.loss
<span class="hljs-meta">... </span>        loss.backward()

<span class="hljs-meta">... </span>        optimizer.step()
<span class="hljs-meta">... </span>        lr_scheduler.step()
<span class="hljs-meta">... </span>        optimizer.zero_grad()
<span class="hljs-meta">... </span>        progress_bar.update(<span class="hljs-number">1</span>)`,wrap:!1}}),Be=new Je({props:{title:"评估",local:"评估",headingTag:"h3"}}),Fe=new I({props:{code:"aW1wb3J0JTIwZXZhbHVhdGUlMEElMEFtZXRyaWMlMjAlM0QlMjBldmFsdWF0ZS5sb2FkKCUyMmFjY3VyYWN5JTIyKSUwQW1vZGVsLmV2YWwoKSUwQWZvciUyMGJhdGNoJTIwaW4lMjBldmFsX2RhdGFsb2FkZXIlM0ElMEElMjAlMjAlMjAlMjBiYXRjaCUyMCUzRCUyMCU3QmslM0ElMjB2LnRvKGRldmljZSklMjBmb3IlMjBrJTJDJTIwdiUyMGluJTIwYmF0Y2guaXRlbXMoKSU3RCUwQSUyMCUyMCUyMCUyMHdpdGglMjB0b3JjaC5ub19ncmFkKCklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvdXRwdXRzJTIwJTNEJTIwbW9kZWwoKipiYXRjaCklMEElMEElMjAlMjAlMjAlMjBsb2dpdHMlMjAlM0QlMjBvdXRwdXRzLmxvZ2l0cyUwQSUyMCUyMCUyMCUyMHByZWRpY3Rpb25zJTIwJTNEJTIwdG9yY2guYXJnbWF4KGxvZ2l0cyUyQyUyMGRpbSUzRC0xKSUwQSUyMCUyMCUyMCUyMG1ldHJpYy5hZGRfYmF0Y2gocHJlZGljdGlvbnMlM0RwcmVkaWN0aW9ucyUyQyUyMHJlZmVyZW5jZXMlM0RiYXRjaCU1QiUyMmxhYmVscyUyMiU1RCklMEElMEFtZXRyaWMuY29tcHV0ZSgp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> evaluate

<span class="hljs-meta">&gt;&gt;&gt; </span>metric = evaluate.load(<span class="hljs-string">&quot;accuracy&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model.<span class="hljs-built_in">eval</span>()
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> eval_dataloader:
<span class="hljs-meta">... </span>    batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}
<span class="hljs-meta">... </span>    <span class="hljs-keyword">with</span> torch.no_grad():
<span class="hljs-meta">... </span>        outputs = model(**batch)

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

<span class="hljs-meta">&gt;&gt;&gt; </span>metric.compute()`,wrap:!1}}),{c(){u(i.$$.fragment),T=p(),m=h("p"),m.innerHTML=J,Z=p(),ne=h("p"),ne.innerHTML=Y,pe=p(),u(x.$$.fragment),re=p(),z=h("p"),z.innerHTML=E,he=p(),V=h("ol"),U=h("li"),Me=h("p"),Me.textContent=N,ue=p(),u(B.$$.fragment),H=p(),se=h("li"),$e=h("p"),$e.textContent=L,le=p(),u(X.$$.fragment),Te=p(),v=h("li"),P=h("p"),P.innerHTML=ie,A=p(),u(K.$$.fragment),_=p(),ye=h("p"),ye.textContent=me,Q=p(),u(D.$$.fragment),C=p(),u(ge.$$.fragment),ae=p(),G=h("p"),G.innerHTML=be,O=p(),u(W.$$.fragment),we=p(),oe=h("p"),oe.textContent=F,ee=p(),u(R.$$.fragment),de=p(),u(q.$$.fragment),te=p(),k=h("p"),k.innerHTML=ce,a=p(),u(c.$$.fragment),S=p(),Ze=h("p"),Ze.innerHTML=Ue,ke=p(),u(je.$$.fragment),n=p(),f=h("p"),f.innerHTML=Ge,We=p(),u(ve.$$.fragment),s=p(),u(o.$$.fragment),Re=p(),Ie=h("p"),Ie.textContent=st,Ee=p(),u(Xe.$$.fragment),Ne=p(),ze=h("p"),ze.innerHTML=lt,Le=p(),u(Ve.$$.fragment),Ae=p(),u(Be.$$.fragment),Qe=p(),He=h("p"),He.innerHTML=at,qe=p(),u(Fe.$$.fragment)},l(e){$(i.$$.fragment,e),T=r(e),m=M(e,"P",{"data-svelte-h":!0}),j(m)!=="svelte-2zvenq"&&(m.innerHTML=J),Z=r(e),ne=M(e,"P",{"data-svelte-h":!0}),j(ne)!=="svelte-8pwpf3"&&(ne.innerHTML=Y),pe=r(e),$(x.$$.fragment,e),re=r(e),z=M(e,"P",{"data-svelte-h":!0}),j(z)!=="svelte-9st6qy"&&(z.innerHTML=E),he=r(e),V=M(e,"OL",{});var d=Ce(V);U=M(d,"LI",{});var Ye=Ce(U);Me=M(Ye,"P",{"data-svelte-h":!0}),j(Me)!=="svelte-15k6rlf"&&(Me.textContent=N),ue=r(Ye),$(B.$$.fragment,Ye),Ye.forEach(t),H=r(d),se=M(d,"LI",{});var Pe=Ce(se);$e=M(Pe,"P",{"data-svelte-h":!0}),j($e)!=="svelte-neb5xl"&&($e.textContent=L),le=r(Pe),$(X.$$.fragment,Pe),Pe.forEach(t),Te=r(d),v=M(d,"LI",{});var Ke=Ce(v);P=M(Ke,"P",{"data-svelte-h":!0}),j(P)!=="svelte-1wyw554"&&(P.innerHTML=ie),A=r(Ke),$(K.$$.fragment,Ke),Ke.forEach(t),d.forEach(t),_=r(e),ye=M(e,"P",{"data-svelte-h":!0}),j(ye)!=="svelte-yjo7t9"&&(ye.textContent=me),Q=r(e),$(D.$$.fragment,e),C=r(e),$(ge.$$.fragment,e),ae=r(e),G=M(e,"P",{"data-svelte-h":!0}),j(G)!=="svelte-9fh0v0"&&(G.innerHTML=be),O=r(e),$(W.$$.fragment,e),we=r(e),oe=M(e,"P",{"data-svelte-h":!0}),j(oe)!=="svelte-o91ors"&&(oe.textContent=F),ee=r(e),$(R.$$.fragment,e),de=r(e),$(q.$$.fragment,e),te=r(e),k=M(e,"P",{"data-svelte-h":!0}),j(k)!=="svelte-17epabp"&&(k.innerHTML=ce),a=r(e),$(c.$$.fragment,e),S=r(e),Ze=M(e,"P",{"data-svelte-h":!0}),j(Ze)!=="svelte-11uhvs"&&(Ze.innerHTML=Ue),ke=r(e),$(je.$$.fragment,e),n=r(e),f=M(e,"P",{"data-svelte-h":!0}),j(f)!=="svelte-vdsbfp"&&(f.innerHTML=Ge),We=r(e),$(ve.$$.fragment,e),s=r(e),$(o.$$.fragment,e),Re=r(e),Ie=M(e,"P",{"data-svelte-h":!0}),j(Ie)!=="svelte-1vahmcn"&&(Ie.textContent=st),Ee=r(e),$(Xe.$$.fragment,e),Ne=r(e),ze=M(e,"P",{"data-svelte-h":!0}),j(ze)!=="svelte-5vfufh"&&(ze.innerHTML=lt),Le=r(e),$(Ve.$$.fragment,e),Ae=r(e),$(Be.$$.fragment,e),Qe=r(e),He=M(e,"P",{"data-svelte-h":!0}),j(He)!=="svelte-yb6192"&&(He.innerHTML=at),qe=r(e),$(Fe.$$.fragment,e)},m(e,d){y(i,e,d),l(e,T,d),l(e,m,d),l(e,Z,d),l(e,ne,d),l(e,pe,d),y(x,e,d),l(e,re,d),l(e,z,d),l(e,he,d),l(e,V,d),_e(V,U),_e(U,Me),_e(U,ue),y(B,U,null),_e(V,H),_e(V,se),_e(se,$e),_e(se,le),y(X,se,null),_e(V,Te),_e(V,v),_e(v,P),_e(v,A),y(K,v,null),l(e,_,d),l(e,ye,d),l(e,Q,d),y(D,e,d),l(e,C,d),y(ge,e,d),l(e,ae,d),l(e,G,d),l(e,O,d),y(W,e,d),l(e,we,d),l(e,oe,d),l(e,ee,d),y(R,e,d),l(e,de,d),y(q,e,d),l(e,te,d),l(e,k,d),l(e,a,d),y(c,e,d),l(e,S,d),l(e,Ze,d),l(e,ke,d),y(je,e,d),l(e,n,d),l(e,f,d),l(e,We,d),y(ve,e,d),l(e,s,d),y(o,e,d),l(e,Re,d),l(e,Ie,d),l(e,Ee,d),y(Xe,e,d),l(e,Ne,d),l(e,ze,d),l(e,Le,d),y(Ve,e,d),l(e,Ae,d),y(Be,e,d),l(e,Qe,d),l(e,He,d),l(e,qe,d),y(Fe,e,d),Se=!0},p(e,d){const Ye={};d&2&&(Ye.$$scope={dirty:d,ctx:e}),o.$set(Ye)},i(e){Se||(g(i.$$.fragment,e),g(x.$$.fragment,e),g(B.$$.fragment,e),g(X.$$.fragment,e),g(K.$$.fragment,e),g(D.$$.fragment,e),g(ge.$$.fragment,e),g(W.$$.fragment,e),g(R.$$.fragment,e),g(q.$$.fragment,e),g(c.$$.fragment,e),g(je.$$.fragment,e),g(ve.$$.fragment,e),g(o.$$.fragment,e),g(Xe.$$.fragment,e),g(Ve.$$.fragment,e),g(Be.$$.fragment,e),g(Fe.$$.fragment,e),Se=!0)},o(e){b(i.$$.fragment,e),b(x.$$.fragment,e),b(B.$$.fragment,e),b(X.$$.fragment,e),b(K.$$.fragment,e),b(D.$$.fragment,e),b(ge.$$.fragment,e),b(W.$$.fragment,e),b(R.$$.fragment,e),b(q.$$.fragment,e),b(c.$$.fragment,e),b(je.$$.fragment,e),b(ve.$$.fragment,e),b(o.$$.fragment,e),b(Xe.$$.fragment,e),b(Ve.$$.fragment,e),b(Be.$$.fragment,e),b(Fe.$$.fragment,e),Se=!1},d(e){e&&(t(T),t(m),t(Z),t(ne),t(pe),t(re),t(z),t(he),t(V),t(_),t(ye),t(Q),t(C),t(ae),t(G),t(O),t(we),t(oe),t(ee),t(de),t(te),t(k),t(a),t(S),t(Ze),t(ke),t(n),t(f),t(We),t(s),t(Re),t(Ie),t(Ee),t(Ne),t(ze),t(Le),t(Ae),t(Qe),t(He),t(qe)),w(i,e),w(x,e),w(B),w(X),w(K),w(D,e),w(ge,e),w(W,e),w(R,e),w(q,e),w(c,e),w(je,e),w(ve,e),w(o,e),w(Xe,e),w(Ve,e),w(Be,e),w(Fe,e)}}}function bt(fe){let i,T;return i=new tt({props:{$$slots:{default:[gt]},$$scope:{ctx:fe}}}),{c(){u(i.$$.fragment)},l(m){$(i.$$.fragment,m)},m(m,J){y(i,m,J),T=!0},p(m,J){const Z={};J&2&&(Z.$$scope={dirty:J,ctx:m}),i.$set(Z)},i(m){T||(g(i.$$.fragment,m),T=!0)},o(m){b(i.$$.fragment,m),T=!1},d(m){w(i,m)}}}function wt(fe){let i,T,m,J,Z,ne,Y,pe,x,re="使用预训练模型有许多显著的好处。它降低了计算成本，减少了碳排放，同时允许您使用最先进的模型，而无需从头开始训练一个。🤗 Transformers 提供了涉及各种任务的成千上万的预训练模型。当您使用预训练模型时，您需要在与任务相关的数据集上训练该模型。这种操作被称为微调，是一种非常强大的训练技术。在本教程中，您将使用您选择的深度学习框架来微调一个预训练模型：",z,E,he="<li>使用 🤗 Transformers 的 <code>Trainer</code> 来微调预训练模型。</li> <li>在 TensorFlow 中使用 Keras 来微调预训练模型。</li> <li>在原生 PyTorch 中微调预训练模型。</li>",V,U,Me,N,ue,B,H,se,$e="在您进行预训练模型微调之前，需要下载一个数据集并为训练做好准备。之前的教程向您展示了如何处理训练数据，现在您有机会将这些技能付诸实践！",L,le,X='首先，加载<a href="https://huggingface.co/datasets/yelp_review_full" rel="nofollow">Yelp评论</a>数据集：',Te,v,P,ie,A='正如您现在所知，您需要一个<code>tokenizer</code>来处理文本，包括填充和截断操作以处理可变的序列长度。如果要一次性处理您的数据集，可以使用 🤗 Datasets 的 <a href="https://huggingface.co/docs/datasets/process#map" rel="nofollow"><code>map</code></a> 方法，将预处理函数应用于整个数据集：',K,_,ye,me,Q="如果愿意的话，您可以从完整数据集提取一个较小子集来进行微调，以减少训练所需的时间：",D,C,ge,ae,G,be,O,W,we="此时，您应该根据您训练所用的框架来选择对应的教程章节。您可以使用右侧的链接跳转到您想要的章节 - 如果您想隐藏某个框架对应的所有教程内容，只需使用右上角的按钮！",oe,F,ee,R,de,q,te,k,ce,a,c,S,Ze,Ue,ke="更多微调例子可参考如下链接：",je,n,f='<li><p><a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">🤗 Transformers 示例</a> 包含用于在 PyTorch 和 TensorFlow 中训练常见自然语言处理任务的脚本。</p></li> <li><p><a href="notebooks">🤗 Transformers 笔记</a> 包含针对特定任务在 PyTorch 和 TensorFlow 中微调模型的各种<code>notebook</code>。</p></li>',Ge,We,ve;return Z=new Je({props:{title:"微调预训练模型",local:"微调预训练模型",headingTag:"h1"}}),Y=new dt({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/zh/training.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/zh/pytorch/training.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/zh/tensorflow/training.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/zh/training.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/zh/pytorch/training.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/zh/tensorflow/training.ipynb"}]}}),N=new Je({props:{title:"准备数据集",local:"准备数据集",headingTag:"h2"}}),B=new De({props:{id:"_BZearw7f0w"}}),v=new I({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBJTBBZGF0YXNldCUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJ5ZWxwX3Jldmlld19mdWxsJTIyKSUwQWRhdGFzZXQlNUIlMjJ0cmFpbiUyMiU1RCU1QjEwMCU1RA==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

<span class="hljs-meta">&gt;&gt;&gt; </span>dataset = load_dataset(<span class="hljs-string">&quot;yelp_review_full&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">100</span>]
{<span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-number">0</span>,
 <span class="hljs-string">&#x27;text&#x27;</span>: <span class="hljs-string">&#x27;My expectations for McDonalds are t rarely high. But for one to still fail so spectacularly...that takes something special!\\\\nThe cashier took my friends\\&#x27;s order, then promptly ignored me. I had to force myself in front of a cashier who opened his register to wait on the person BEHIND me. I waited over five minutes for a gigantic order that included precisely one kid\\&#x27;s meal. After watching two people who ordered after me be handed their food, I asked where mine was. The manager started yelling at the cashiers for \\\\&quot;serving off their orders\\\\&quot; when they didn\\&#x27;t have their food. But neither cashier was anywhere near those controls, and the manager was the one serving food to customers and clearing the boards.\\\\nThe manager was rude when giving me my order. She didn\\&#x27;t make sure that I had everything ON MY RECEIPT, and never even had the decency to apologize that I felt I was getting poor service.\\\\nI\\&#x27;ve eaten at various McDonalds restaurants for over 30 years. I\\&#x27;ve worked at more than one location. I expect bad days, bad moods, and the occasional mistake. But I have yet to have a decent experience at this store. It will remain a place I avoid unless someone in my party needs to avoid illness from low blood sugar. Perhaps I should go back to the racially biased service of Steak n Shake instead!&#x27;</span>}`,wrap:!1}}),_=new I({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJiZXJ0LWJhc2UtY2FzZWQlMjIpJTBBJTBBJTBBZGVmJTIwdG9rZW5pemVfZnVuY3Rpb24oZXhhbXBsZXMpJTNBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwdG9rZW5pemVyKGV4YW1wbGVzJTVCJTIydGV4dCUyMiU1RCUyQyUyMHBhZGRpbmclM0QlMjJtYXhfbGVuZ3RoJTIyJTJDJTIwdHJ1bmNhdGlvbiUzRFRydWUpJTBBJTBBJTBBdG9rZW5pemVkX2RhdGFzZXRzJTIwJTNEJTIwZGF0YXNldC5tYXAodG9rZW5pemVfZnVuY3Rpb24lMkMlMjBiYXRjaGVkJTNEVHJ1ZSk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;bert-base-cased&quot;</span>)


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">examples</span>):
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">&quot;text&quot;</span>], padding=<span class="hljs-string">&quot;max_length&quot;</span>, truncation=<span class="hljs-literal">True</span>)


<span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets = dataset.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>)`,wrap:!1}}),C=new I({props:{code:"c21hbGxfdHJhaW5fZGF0YXNldCUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVELnNodWZmbGUoc2VlZCUzRDQyKS5zZWxlY3QocmFuZ2UoMTAwMCkpJTBBc21hbGxfZXZhbF9kYXRhc2V0JTIwJTNEJTIwdG9rZW5pemVkX2RhdGFzZXRzJTVCJTIydGVzdCUyMiU1RC5zaHVmZmxlKHNlZWQlM0Q0Mikuc2VsZWN0KHJhbmdlKDEwMDApKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>small_train_dataset = tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>small_eval_dataset = tokenized_datasets[<span class="hljs-string">&quot;test&quot;</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>))`,wrap:!1}}),be=new Je({props:{title:"训练",local:"训练",headingTag:"h2"}}),F=new nt({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[$t],pytorch:[ht]},$$scope:{ctx:fe}}}),q=new Je({props:{title:"在原生 PyTorch 中训练",local:"在原生-pytorch-中训练",headingTag:"h2"}}),k=new nt({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[bt]},$$scope:{ctx:fe}}}),S=new Je({props:{title:"附加资源",local:"附加资源",headingTag:"h2"}}),{c(){i=h("meta"),T=p(),m=h("p"),J=p(),u(Z.$$.fragment),ne=p(),u(Y.$$.fragment),pe=p(),x=h("p"),x.textContent=re,z=p(),E=h("ul"),E.innerHTML=he,V=p(),U=h("a"),Me=p(),u(N.$$.fragment),ue=p(),u(B.$$.fragment),H=p(),se=h("p"),se.textContent=$e,L=p(),le=h("p"),le.innerHTML=X,Te=p(),u(v.$$.fragment),P=p(),ie=h("p"),ie.innerHTML=A,K=p(),u(_.$$.fragment),ye=p(),me=h("p"),me.textContent=Q,D=p(),u(C.$$.fragment),ge=p(),ae=h("a"),G=p(),u(be.$$.fragment),O=p(),W=h("p"),W.textContent=we,oe=p(),u(F.$$.fragment),ee=p(),R=h("a"),de=p(),u(q.$$.fragment),te=p(),u(k.$$.fragment),ce=p(),a=h("a"),c=p(),u(S.$$.fragment),Ze=p(),Ue=h("p"),Ue.textContent=ke,je=p(),n=h("ul"),n.innerHTML=f,Ge=p(),We=h("p"),this.h()},l(s){const o=ot("svelte-u9bgzb",document.head);i=M(o,"META",{name:!0,content:!0}),o.forEach(t),T=r(s),m=M(s,"P",{}),Ce(m).forEach(t),J=r(s),$(Z.$$.fragment,s),ne=r(s),$(Y.$$.fragment,s),pe=r(s),x=M(s,"P",{"data-svelte-h":!0}),j(x)!=="svelte-1x1dtyg"&&(x.textContent=re),z=r(s),E=M(s,"UL",{"data-svelte-h":!0}),j(E)!=="svelte-ftqcej"&&(E.innerHTML=he),V=r(s),U=M(s,"A",{id:!0}),Ce(U).forEach(t),Me=r(s),$(N.$$.fragment,s),ue=r(s),$(B.$$.fragment,s),H=r(s),se=M(s,"P",{"data-svelte-h":!0}),j(se)!=="svelte-15rycle"&&(se.textContent=$e),L=r(s),le=M(s,"P",{"data-svelte-h":!0}),j(le)!=="svelte-im3f9x"&&(le.innerHTML=X),Te=r(s),$(v.$$.fragment,s),P=r(s),ie=M(s,"P",{"data-svelte-h":!0}),j(ie)!=="svelte-n07g06"&&(ie.innerHTML=A),K=r(s),$(_.$$.fragment,s),ye=r(s),me=M(s,"P",{"data-svelte-h":!0}),j(me)!=="svelte-bdwtar"&&(me.textContent=Q),D=r(s),$(C.$$.fragment,s),ge=r(s),ae=M(s,"A",{id:!0}),Ce(ae).forEach(t),G=r(s),$(be.$$.fragment,s),O=r(s),W=M(s,"P",{"data-svelte-h":!0}),j(W)!=="svelte-1rp8czm"&&(W.textContent=we),oe=r(s),$(F.$$.fragment,s),ee=r(s),R=M(s,"A",{id:!0}),Ce(R).forEach(t),de=r(s),$(q.$$.fragment,s),te=r(s),$(k.$$.fragment,s),ce=r(s),a=M(s,"A",{id:!0}),Ce(a).forEach(t),c=r(s),$(S.$$.fragment,s),Ze=r(s),Ue=M(s,"P",{"data-svelte-h":!0}),j(Ue)!=="svelte-q8amck"&&(Ue.textContent=ke),je=r(s),n=M(s,"UL",{"data-svelte-h":!0}),j(n)!=="svelte-1h2cd17"&&(n.innerHTML=f),Ge=r(s),We=M(s,"P",{}),Ce(We).forEach(t),this.h()},h(){xe(i,"name","hf:doc:metadata"),xe(i,"content",jt),xe(U,"id","data-processing"),xe(ae,"id","trainer"),xe(R,"id","pytorch_native"),xe(a,"id","additional-resources")},m(s,o){_e(document.head,i),l(s,T,o),l(s,m,o),l(s,J,o),y(Z,s,o),l(s,ne,o),y(Y,s,o),l(s,pe,o),l(s,x,o),l(s,z,o),l(s,E,o),l(s,V,o),l(s,U,o),l(s,Me,o),y(N,s,o),l(s,ue,o),y(B,s,o),l(s,H,o),l(s,se,o),l(s,L,o),l(s,le,o),l(s,Te,o),y(v,s,o),l(s,P,o),l(s,ie,o),l(s,K,o),y(_,s,o),l(s,ye,o),l(s,me,o),l(s,D,o),y(C,s,o),l(s,ge,o),l(s,ae,o),l(s,G,o),y(be,s,o),l(s,O,o),l(s,W,o),l(s,oe,o),y(F,s,o),l(s,ee,o),l(s,R,o),l(s,de,o),y(q,s,o),l(s,te,o),y(k,s,o),l(s,ce,o),l(s,a,o),l(s,c,o),y(S,s,o),l(s,Ze,o),l(s,Ue,o),l(s,je,o),l(s,n,o),l(s,Ge,o),l(s,We,o),ve=!0},p(s,[o]){const Re={};o&2&&(Re.$$scope={dirty:o,ctx:s}),F.$set(Re);const Ie={};o&2&&(Ie.$$scope={dirty:o,ctx:s}),k.$set(Ie)},i(s){ve||(g(Z.$$.fragment,s),g(Y.$$.fragment,s),g(N.$$.fragment,s),g(B.$$.fragment,s),g(v.$$.fragment,s),g(_.$$.fragment,s),g(C.$$.fragment,s),g(be.$$.fragment,s),g(F.$$.fragment,s),g(q.$$.fragment,s),g(k.$$.fragment,s),g(S.$$.fragment,s),ve=!0)},o(s){b(Z.$$.fragment,s),b(Y.$$.fragment,s),b(N.$$.fragment,s),b(B.$$.fragment,s),b(v.$$.fragment,s),b(_.$$.fragment,s),b(C.$$.fragment,s),b(be.$$.fragment,s),b(F.$$.fragment,s),b(q.$$.fragment,s),b(k.$$.fragment,s),b(S.$$.fragment,s),ve=!1},d(s){s&&(t(T),t(m),t(J),t(ne),t(pe),t(x),t(z),t(E),t(V),t(U),t(Me),t(ue),t(H),t(se),t(L),t(le),t(Te),t(P),t(ie),t(K),t(ye),t(me),t(D),t(ge),t(ae),t(G),t(O),t(W),t(oe),t(ee),t(R),t(de),t(te),t(ce),t(a),t(c),t(Ze),t(Ue),t(je),t(n),t(Ge),t(We)),t(i),w(Z,s),w(Y,s),w(N,s),w(B,s),w(v,s),w(_,s),w(C,s),w(be,s),w(F,s),w(q,s),w(k,s),w(S,s)}}}const jt='{"title":"微调预训练模型","local":"微调预训练模型","sections":[{"title":"准备数据集","local":"准备数据集","sections":[],"depth":2},{"title":"训练","local":"训练","sections":[],"depth":2},{"title":"使用 PyTorch Trainer 进行训练","local":"使用-pytorch-trainer-进行训练","sections":[{"title":"训练超参数","local":"训练超参数","sections":[],"depth":3},{"title":"评估","local":"评估","sections":[],"depth":3},{"title":"训练器","local":"训练器","sections":[],"depth":3}],"depth":2},{"title":"使用keras训练TensorFlow模型","local":"使用keras训练tensorflow模型","sections":[{"title":"加载用于 Keras 的数据","local":"加载用于-keras-的数据","sections":[],"depth":3},{"title":"将数据加载为 tf.data.Dataset","local":"将数据加载为-tfdatadataset","sections":[],"depth":3}],"depth":2},{"title":"在原生 PyTorch 中训练","local":"在原生-pytorch-中训练","sections":[{"title":"DataLoader","local":"dataloader","sections":[],"depth":3},{"title":"Optimizer and learning rate scheduler","local":"optimizer-and-learning-rate-scheduler","sections":[],"depth":3},{"title":"训练循环","local":"训练循环","sections":[],"depth":3},{"title":"评估","local":"评估","sections":[],"depth":3}],"depth":2},{"title":"附加资源","local":"附加资源","sections":[],"depth":2}],"depth":1}';function Tt(fe){return rt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Wt extends it{constructor(i){super(),mt(this,i,Tt,wt,pt,{})}}export{Wt as component};
