import{s as pt,o as rt,n as et}from"../chunks/scheduler.9991993c.js";import{S as it,i as mt,g as h,s as p,r as $,A as ot,h as M,f as t,c as r,j as Ie,u,x as j,k as xe,y as Ge,a as l,v as g,d as y,t as b,w}from"../chunks/index.7fc9a5e7.js";import{T as tt}from"../chunks/Tip.9de92fc6.js";import{Y as Oe}from"../chunks/Youtube.7934cf81.js";import{C as R}from"../chunks/CodeBlock.e11cba92.js";import{D as ct}from"../chunks/DocNotebookDropdown.a0cb4c0f.js";import{F as nt,M as st}from"../chunks/Markdown.87f31c7e.js";import{H as Je,E as dt}from"../chunks/EditOnGithub.84ab7f0e.js";function ft(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:et,d(m){m&&t(i)}}}function ht(fe){let i,T,m,J,k,ne="🤗 Transformers 提供了一个专为训练 🤗 Transformers 模型而优化的 <code>Trainer</code> 类，使您无需手动编写自己的训练循环步骤而更轻松地开始训练模型。<code>Trainer</code> API 支持各种训练选项和功能，如日志记录、梯度累积和混合精度。",B,pe,E='首先加载您的模型并指定期望的标签数量。根据 Yelp Review <a href="https://huggingface.co/datasets/yelp_review_full#data-fields" rel="nofollow">数据集卡片</a>，您知道有五个标签：',re,V,x,he,z,Z,Me,N,$e='接下来，创建一个 <code>TrainingArguments</code> 类，其中包含您可以调整的所有超参数以及用于激活不同训练选项的标志。对于本教程，您可以从默认的训练<a href="https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments" rel="nofollow">超参数</a>开始，但随时可以尝试不同的设置以找到最佳设置。',F,H,se="指定保存训练检查点的位置：",ue,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,_,ge="在 <code>metric</code> 上调用 <code>compute</code> 来计算您的预测的准确性。在将预测传递给 <code>compute</code> 之前，您需要将预测转换为<code>logits</code>（请记住，所有 🤗 Transformers 模型都返回对<code>logits</code>）：",me,Q,D,C,ye="如果您希望在微调过程中监视评估指标，请在您的训练参数中指定 <code>eval_strategy</code> 参数，以在每个<code>epoch</code>结束时展示评估指标：",ae,G,be,O,I,we,oe="创建一个包含您的模型、训练参数、训练和测试数据集以及评估函数的 <code>Trainer</code> 对象：",Y,ee,W,ce,q="然后调用<code>train()</code>以微调模型：",te,U,de;return i=new Oe({props:{id:"nvBXf7s7vTI"}}),m=new Je({props:{title:"使用 PyTorch Trainer 进行训练",local:"使用-pytorch-trainer-进行训练",headingTag:"h2"}}),V=new R({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZS1iZXJ0JTJGYmVydC1iYXNlLWNhc2VkJTIyJTJDJTIwbnVtX2xhYmVscyUzRDUp",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;google-bert/bert-base-cased&quot;</span>, num_labels=<span class="hljs-number">5</span>)`,wrap:!1}}),he=new tt({props:{$$slots:{default:[ft]},$$scope:{ctx:fe}}}),Z=new Je({props:{title:"训练超参数",local:"训练超参数",headingTag:"h3"}}),L=new R({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 R({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 R({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 R({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRyYWluaW5nQXJndW1lbnRzJTJDJTIwVHJhaW5lciUwQSUwQXRyYWluaW5nX2FyZ3MlMjAlM0QlMjBUcmFpbmluZ0FyZ3VtZW50cyhvdXRwdXRfZGlyJTNEJTIydGVzdF90cmFpbmVyJTIyJTJDJTIwZXZhbF9zdHJhdGVneSUzRCUyMmVwb2NoJTIyKQ==",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>, eval_strategy=<span class="hljs-string">&quot;epoch&quot;</span>)`,wrap:!1}}),O=new Je({props:{title:"训练器",local:"训练器",headingTag:"h3"}}),ee=new R({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}}),U=new R({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()',wrap:!1}}),{c(){$(i.$$.fragment),T=p(),$(m.$$.fragment),J=p(),k=h("p"),k.innerHTML=ne,B=p(),pe=h("p"),pe.innerHTML=E,re=p(),$(V.$$.fragment),x=p(),$(he.$$.fragment),z=p(),$(Z.$$.fragment),Me=p(),N=h("p"),N.innerHTML=$e,F=p(),H=h("p"),H.textContent=se,ue=p(),$(L.$$.fragment),le=p(),$(X.$$.fragment),Te=p(),v=h("p"),v.innerHTML=P,ie=p(),$(A.$$.fragment),K=p(),_=h("p"),_.innerHTML=ge,me=p(),$(Q.$$.fragment),D=p(),C=h("p"),C.innerHTML=ye,ae=p(),$(G.$$.fragment),be=p(),$(O.$$.fragment),I=p(),we=h("p"),we.innerHTML=oe,Y=p(),$(ee.$$.fragment),W=p(),ce=h("p"),ce.innerHTML=q,te=p(),$(U.$$.fragment)},l(a){u(i.$$.fragment,a),T=r(a),u(m.$$.fragment,a),J=r(a),k=M(a,"P",{"data-svelte-h":!0}),j(k)!=="svelte-1oqslc0"&&(k.innerHTML=ne),B=r(a),pe=M(a,"P",{"data-svelte-h":!0}),j(pe)!=="svelte-87l9t0"&&(pe.innerHTML=E),re=r(a),u(V.$$.fragment,a),x=r(a),u(he.$$.fragment,a),z=r(a),u(Z.$$.fragment,a),Me=r(a),N=M(a,"P",{"data-svelte-h":!0}),j(N)!=="svelte-gfewmt"&&(N.innerHTML=$e),F=r(a),H=M(a,"P",{"data-svelte-h":!0}),j(H)!=="svelte-1rwzshl"&&(H.textContent=se),ue=r(a),u(L.$$.fragment,a),le=r(a),u(X.$$.fragment,a),Te=r(a),v=M(a,"P",{"data-svelte-h":!0}),j(v)!=="svelte-1ulry18"&&(v.innerHTML=P),ie=r(a),u(A.$$.fragment,a),K=r(a),_=M(a,"P",{"data-svelte-h":!0}),j(_)!=="svelte-mvhx78"&&(_.innerHTML=ge),me=r(a),u(Q.$$.fragment,a),D=r(a),C=M(a,"P",{"data-svelte-h":!0}),j(C)!=="svelte-fhsy41"&&(C.innerHTML=ye),ae=r(a),u(G.$$.fragment,a),be=r(a),u(O.$$.fragment,a),I=r(a),we=M(a,"P",{"data-svelte-h":!0}),j(we)!=="svelte-1np8g37"&&(we.innerHTML=oe),Y=r(a),u(ee.$$.fragment,a),W=r(a),ce=M(a,"P",{"data-svelte-h":!0}),j(ce)!=="svelte-1588yvr"&&(ce.innerHTML=q),te=r(a),u(U.$$.fragment,a)},m(a,d){g(i,a,d),l(a,T,d),g(m,a,d),l(a,J,d),l(a,k,d),l(a,B,d),l(a,pe,d),l(a,re,d),g(V,a,d),l(a,x,d),g(he,a,d),l(a,z,d),g(Z,a,d),l(a,Me,d),l(a,N,d),l(a,F,d),l(a,H,d),l(a,ue,d),g(L,a,d),l(a,le,d),g(X,a,d),l(a,Te,d),l(a,v,d),l(a,ie,d),g(A,a,d),l(a,K,d),l(a,_,d),l(a,me,d),g(Q,a,d),l(a,D,d),l(a,C,d),l(a,ae,d),g(G,a,d),l(a,be,d),g(O,a,d),l(a,I,d),l(a,we,d),l(a,Y,d),g(ee,a,d),l(a,W,d),l(a,ce,d),l(a,te,d),g(U,a,d),de=!0},p(a,d){const S={};d&2&&(S.$$scope={dirty:d,ctx:a}),he.$set(S)},i(a){de||(y(i.$$.fragment,a),y(m.$$.fragment,a),y(V.$$.fragment,a),y(he.$$.fragment,a),y(Z.$$.fragment,a),y(L.$$.fragment,a),y(X.$$.fragment,a),y(A.$$.fragment,a),y(Q.$$.fragment,a),y(G.$$.fragment,a),y(O.$$.fragment,a),y(ee.$$.fragment,a),y(U.$$.fragment,a),de=!0)},o(a){b(i.$$.fragment,a),b(m.$$.fragment,a),b(V.$$.fragment,a),b(he.$$.fragment,a),b(Z.$$.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(U.$$.fragment,a),de=!1},d(a){a&&(t(T),t(J),t(k),t(B),t(pe),t(re),t(x),t(z),t(Me),t(N),t(F),t(H),t(ue),t(le),t(Te),t(v),t(ie),t(K),t(_),t(me),t(D),t(C),t(ae),t(be),t(I),t(we),t(Y),t(W),t(ce),t(te)),w(i,a),w(m,a),w(V,a),w(he,a),w(Z,a),w(L,a),w(X,a),w(A,a),w(Q,a),w(G,a),w(O,a),w(ee,a),w(U,a)}}}function Mt(fe){let i,T;return i=new st({props:{$$slots:{default:[ht]},$$scope:{ctx:fe}}}),{c(){$(i.$$.fragment)},l(m){u(i.$$.fragment,m)},m(m,J){g(i,m,J),T=!0},p(m,J){const k={};J&2&&(k.$$scope={dirty:J,ctx:m}),i.$set(k)},i(m){T||(y(i.$$.fragment,m),T=!0)},o(m){b(i.$$.fragment,m),T=!1},d(m){w(i,m)}}}function $t(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:et,d(m){m&&t(i)}}}function ut(fe){let i,T,m,J,k,ne,B,pe="您也可以使用 Keras API 在 TensorFlow 中训练 🤗 Transformers 模型！",E,re,V,x,he="当您希望使用 Keras API 训练 🤗 Transformers 模型时，您需要将您的数据集转换为 Keras 可理解的格式。如果您的数据集很小，您可以将整个数据集转换为NumPy数组并传递给 Keras。在进行更复杂的操作之前，让我们先尝试这种方法。",z,Z,Me='首先，加载一个数据集。我们将使用 <a href="https://huggingface.co/datasets/glue" rel="nofollow">GLUE benchmark</a> 中的 CoLA 数据集，因为它是一个简单的二元文本分类任务。现在只使用训练数据集。',N,$e,F,H,se="接下来，加载一个<code>tokenizer</code>并将数据标记为 NumPy 数组。请注意，标签已经是由 0 和 1 组成的<code>list</code>，因此我们可以直接将其转换为 NumPy 数组而无需进行分词处理！",ue,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,_,ge="这种方法对于较小的数据集效果很好，但对于较大的数据集，您可能会发现它开始变得有问题。为什么呢？因为分词后的数组和标签必须完全加载到内存中，而且由于 NumPy 无法处理“不规则”数组，因此每个分词后的样本长度都必须被填充到数据集中最长样本的长度。这将使您的数组变得更大，而所有这些<code>padding tokens</code>也会减慢训练速度！",me,Q,D,C,ye="如果您想避免训练速度减慢，可以将数据加载为 <code>tf.data.Dataset</code>。虽然您可以自己编写自己的 <code>tf.data</code> 流水线，但我们有两种方便的方法来实现这一点：",ae,G,be='<li><a href="/docs/transformers/v4.41.0/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,I,we='在使用 <a href="/docs/transformers/v4.41.0/zh/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a> 之前，您需要将<code>tokenizer</code>的输出添加到数据集作为列，如下面的代码示例所示：',oe,Y,ee,W,ce="请记住，默认情况下，Hugging Face 数据集存储在硬盘上，因此这不会增加您的内存使用！一旦列已经添加，您可以从数据集中流式的传输批次数据，并为每个批次添加<code>padding tokens</code>，这与为整个数据集添加<code>padding tokens</code>相比，大大减少了<code>padding tokens</code>的数量。",q,te,U,de,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>以了解此方法的实际操作。',d,S,ke="一旦创建了 <code>tf.data.Dataset</code>，您可以像以前一样编译和训练模型：",Ze,Ue,je;return m=new Oe({props:{id:"rnTGBy2ax1c"}}),k=new Je({props:{title:"使用keras训练TensorFlow模型",local:"使用keras训练tensorflow模型",headingTag:"h2"}}),re=new Je({props:{title:"加载用于 Keras 的数据",local:"加载用于-keras-的数据",headingTag:"h3"}}),$e=new R({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 R({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJnb29nbGUtYmVydCUyRmJlcnQtYmFzZS1jYXNlZCUyMiklMEF0b2tlbml6ZWRfZGF0YSUyMCUzRCUyMHRva2VuaXplcihkYXRhc2V0JTVCJTIyc2VudGVuY2UlMjIlNUQlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMm5wJTIyJTJDJTIwcGFkZGluZyUzRFRydWUpJTBBJTIzJTIwVG9rZW5pemVyJTIwcmV0dXJucyUyMGElMjBCYXRjaEVuY29kaW5nJTJDJTIwYnV0JTIwd2UlMjBjb252ZXJ0JTIwdGhhdCUyMHRvJTIwYSUyMGRpY3QlMjBmb3IlMjBLZXJhcyUwQXRva2VuaXplZF9kYXRhJTIwJTNEJTIwZGljdCh0b2tlbml6ZWRfZGF0YSklMEElMEFsYWJlbHMlMjAlM0QlMjBucC5hcnJheShkYXRhc2V0JTVCJTIybGFiZWwlMjIlNUQpJTIwJTIwJTIzJTIwTGFiZWwlMjBpcyUyMGFscmVhZHklMjBhbiUyMGFycmF5JTIwb2YlMjAwJTIwYW5kJTIwMQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;google-bert/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 R({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFRGQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbiUwQWZyb20lMjB0ZW5zb3JmbG93LmtlcmFzLm9wdGltaXplcnMlMjBpbXBvcnQlMjBBZGFtJTBBJTBBJTIzJTIwTG9hZCUyMGFuZCUyMGNvbXBpbGUlMjBvdXIlMjBtb2RlbCUwQW1vZGVsJTIwJTNEJTIwVEZBdXRvTW9kZWxGb3JTZXF1ZW5jZUNsYXNzaWZpY2F0aW9uLmZyb21fcHJldHJhaW5lZCglMjJnb29nbGUtYmVydCUyRmJlcnQtYmFzZS1jYXNlZCUyMiklMEElMjMlMjBMb3dlciUyMGxlYXJuaW5nJTIwcmF0ZXMlMjBhcmUlMjBvZnRlbiUyMGJldHRlciUyMGZvciUyMGZpbmUtdHVuaW5nJTIwdHJhbnNmb3JtZXJzJTBBbW9kZWwuY29tcGlsZShvcHRpbWl6ZXIlM0RBZGFtKDNlLTUpKSUyMCUyMCUyMyUyME5vJTIwbG9zcyUyMGFyZ3VtZW50ISUwQSUwQW1vZGVsLmZpdCh0b2tlbml6ZWRfZGF0YSUyQyUyMGxhYmVscyk=",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;google-bert/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 tt({props:{$$slots:{default:[$t]},$$scope:{ctx:fe}}}),Q=new Je({props:{title:"将数据加载为 tf.data.Dataset",local:"将数据加载为-tfdatadataset",headingTag:"h3"}}),Y=new R({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 R({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}}),Ue=new R({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(),$(m.$$.fragment),J=p(),$(k.$$.fragment),ne=p(),B=h("p"),B.textContent=pe,E=p(),$(re.$$.fragment),V=p(),x=h("p"),x.textContent=he,z=p(),Z=h("p"),Z.innerHTML=Me,N=p(),$($e.$$.fragment),F=p(),H=h("p"),H.innerHTML=se,ue=p(),$(L.$$.fragment),le=p(),X=h("p"),X.innerHTML=Te,v=p(),$(P.$$.fragment),ie=p(),$(A.$$.fragment),K=p(),_=h("p"),_.innerHTML=ge,me=p(),$(Q.$$.fragment),D=p(),C=h("p"),C.innerHTML=ye,ae=p(),G=h("ul"),G.innerHTML=be,O=p(),I=h("p"),I.innerHTML=we,oe=p(),$(Y.$$.fragment),ee=p(),W=h("p"),W.innerHTML=ce,q=p(),$(te.$$.fragment),U=p(),de=h("p"),de.innerHTML=a,d=p(),S=h("p"),S.innerHTML=ke,Ze=p(),$(Ue.$$.fragment),this.h()},l(n){i=M(n,"A",{id:!0}),Ie(i).forEach(t),T=r(n),u(m.$$.fragment,n),J=r(n),u(k.$$.fragment,n),ne=r(n),B=M(n,"P",{"data-svelte-h":!0}),j(B)!=="svelte-bcxyio"&&(B.textContent=pe),E=r(n),u(re.$$.fragment,n),V=r(n),x=M(n,"P",{"data-svelte-h":!0}),j(x)!=="svelte-i3cei"&&(x.textContent=he),z=r(n),Z=M(n,"P",{"data-svelte-h":!0}),j(Z)!=="svelte-1km4tqo"&&(Z.innerHTML=Me),N=r(n),u($e.$$.fragment,n),F=r(n),H=M(n,"P",{"data-svelte-h":!0}),j(H)!=="svelte-7lgu56"&&(H.innerHTML=se),ue=r(n),u(L.$$.fragment,n),le=r(n),X=M(n,"P",{"data-svelte-h":!0}),j(X)!=="svelte-1ggltyx"&&(X.innerHTML=Te),v=r(n),u(P.$$.fragment,n),ie=r(n),u(A.$$.fragment,n),K=r(n),_=M(n,"P",{"data-svelte-h":!0}),j(_)!=="svelte-1f7t43t"&&(_.innerHTML=ge),me=r(n),u(Q.$$.fragment,n),D=r(n),C=M(n,"P",{"data-svelte-h":!0}),j(C)!=="svelte-1wj6cmg"&&(C.innerHTML=ye),ae=r(n),G=M(n,"UL",{"data-svelte-h":!0}),j(G)!=="svelte-lw2685"&&(G.innerHTML=be),O=r(n),I=M(n,"P",{"data-svelte-h":!0}),j(I)!=="svelte-15pl0da"&&(I.innerHTML=we),oe=r(n),u(Y.$$.fragment,n),ee=r(n),W=M(n,"P",{"data-svelte-h":!0}),j(W)!=="svelte-hz86ti"&&(W.innerHTML=ce),q=r(n),u(te.$$.fragment,n),U=r(n),de=M(n,"P",{"data-svelte-h":!0}),j(de)!=="svelte-5a7b6l"&&(de.innerHTML=a),d=r(n),S=M(n,"P",{"data-svelte-h":!0}),j(S)!=="svelte-10v3zfh"&&(S.innerHTML=ke),Ze=r(n),u(Ue.$$.fragment,n),this.h()},h(){xe(i,"id","keras")},m(n,f){l(n,i,f),l(n,T,f),g(m,n,f),l(n,J,f),g(k,n,f),l(n,ne,f),l(n,B,f),l(n,E,f),g(re,n,f),l(n,V,f),l(n,x,f),l(n,z,f),l(n,Z,f),l(n,N,f),g($e,n,f),l(n,F,f),l(n,H,f),l(n,ue,f),g(L,n,f),l(n,le,f),l(n,X,f),l(n,v,f),g(P,n,f),l(n,ie,f),g(A,n,f),l(n,K,f),l(n,_,f),l(n,me,f),g(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,I,f),l(n,oe,f),g(Y,n,f),l(n,ee,f),l(n,W,f),l(n,q,f),g(te,n,f),l(n,U,f),l(n,de,f),l(n,d,f),l(n,S,f),l(n,Ze,f),g(Ue,n,f),je=!0},p(n,f){const Re={};f&2&&(Re.$$scope={dirty:f,ctx:n}),A.$set(Re)},i(n){je||(y(m.$$.fragment,n),y(k.$$.fragment,n),y(re.$$.fragment,n),y($e.$$.fragment,n),y(L.$$.fragment,n),y(P.$$.fragment,n),y(A.$$.fragment,n),y(Q.$$.fragment,n),y(Y.$$.fragment,n),y(te.$$.fragment,n),y(Ue.$$.fragment,n),je=!0)},o(n){b(m.$$.fragment,n),b(k.$$.fragment,n),b(re.$$.fragment,n),b($e.$$.fragment,n),b(L.$$.fragment,n),b(P.$$.fragment,n),b(A.$$.fragment,n),b(Q.$$.fragment,n),b(Y.$$.fragment,n),b(te.$$.fragment,n),b(Ue.$$.fragment,n),je=!1},d(n){n&&(t(i),t(T),t(J),t(ne),t(B),t(E),t(V),t(x),t(z),t(Z),t(N),t(F),t(H),t(ue),t(le),t(X),t(v),t(ie),t(K),t(_),t(me),t(D),t(C),t(ae),t(G),t(O),t(I),t(oe),t(ee),t(W),t(q),t(U),t(de),t(d),t(S),t(Ze)),w(m,n),w(k,n),w(re,n),w($e,n),w(L,n),w(P,n),w(A,n),w(Q,n),w(Y,n),w(te,n),w(Ue,n)}}}function gt(fe){let i,T;return i=new st({props:{$$slots:{default:[ut]},$$scope:{ctx:fe}}}),{c(){$(i.$$.fragment)},l(m){u(i.$$.fragment,m)},m(m,J){g(i,m,J),T=!0},p(m,J){const k={};J&2&&(k.$$scope={dirty:J,ctx:m}),i.$set(k)},i(m){T||(y(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:et,d(m){m&&t(i)}}}function bt(fe){let i,T,m,J="<code>Trainer</code> 负责训练循环，允许您在一行代码中微调模型。对于喜欢编写自己训练循环的用户，您也可以在原生 PyTorch 中微调 🤗 Transformers 模型。",k,ne,B="现在，您可能需要重新启动您的<code>notebook</code>，或执行以下代码以释放一些内存：",pe,E,re,V,x="接下来，手动处理 <code>tokenized_dataset</code> 以准备进行训练。",he,z,Z,Me,N="移除 text 列，因为模型不接受原始文本作为输入：",$e,F,H,se,ue,L="将 label 列重命名为 labels，因为模型期望参数的名称为 labels：",le,X,Te,v,P,ie="设置数据集的格式以返回 PyTorch 张量而不是<code>lists</code>：",A,K,_,ge,me="接着，创建一个先前展示的数据集的较小子集，以加速微调过程",Q,D,C,ye,ae,G,be="您的训练和测试数据集创建一个<code>DataLoader</code>类，以便可以迭代处理数据批次",O,I,we,oe,Y="加载您的模型，并指定期望的标签数量：",ee,W,ce,q,te,U,de='创建一个<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,d,S,ke,Ze="创建来自 <code>Trainer</code> 的默认<code>learning rate scheduler</code>：",Ue,je,n,f,Re="最后，指定 <code>device</code> 以使用 GPU（如果有的话）。否则，使用 CPU 进行训练可能需要几个小时，而不是几分钟。",_e,Ce,We,ve,s,o,Ne="现在您已经准备好训练了！🥳",Xe,Ve,Le,ze,lt='为了跟踪训练进度，使用 <a href="https://tqdm.github.io/" rel="nofollow">tqdm</a> 库来添加一个进度条，显示训练步数的进展：',Ae,Fe,Qe,He,qe,Ye,at="就像您在 <code>Trainer</code> 中添加了一个评估函数一样，当您编写自己的训练循环时，您需要做同样的事情。但与在每个<code>epoch</code>结束时计算和展示指标不同，这一次您将使用 <code>add_batch</code> 累积所有批次，并在最后计算指标。",Se,Be,Pe;return i=new Oe({props:{id:"Dh9CL8fyG80"}}),E=new R({props:{code:"ZGVsJTIwbW9kZWwlMEFkZWwlMjB0cmFpbmVyJTBBdG9yY2guY3VkYS5lbXB0eV9jYWNoZSgp",highlighted:`<span class="hljs-keyword">del</span> model
<span class="hljs-keyword">del</span> trainer
torch.cuda.empty_cache()`,wrap:!1}}),F=new R({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 R({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 R({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 R({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}}),ye=new Je({props:{title:"DataLoader",local:"dataloader",headingTag:"h3"}}),I=new R({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}}),W=new R({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZS1iZXJ0JTJGYmVydC1iYXNlLWNhc2VkJTIyJTJDJTIwbnVtX2xhYmVscyUzRDUp",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;google-bert/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"}}),d=new R({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 R({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}}),Ce=new R({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}}),ve=new tt({props:{$$slots:{default:[yt]},$$scope:{ctx:fe}}}),Ve=new Je({props:{title:"训练循环",local:"训练循环",headingTag:"h3"}}),Fe=new R({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}}),He=new Je({props:{title:"评估",local:"评估",headingTag:"h3"}}),Be=new R({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(){$(i.$$.fragment),T=p(),m=h("p"),m.innerHTML=J,k=p(),ne=h("p"),ne.innerHTML=B,pe=p(),$(E.$$.fragment),re=p(),V=h("p"),V.innerHTML=x,he=p(),z=h("ol"),Z=h("li"),Me=h("p"),Me.textContent=N,$e=p(),$(F.$$.fragment),H=p(),se=h("li"),ue=h("p"),ue.textContent=L,le=p(),$(X.$$.fragment),Te=p(),v=h("li"),P=h("p"),P.innerHTML=ie,A=p(),$(K.$$.fragment),_=p(),ge=h("p"),ge.textContent=me,Q=p(),$(D.$$.fragment),C=p(),$(ye.$$.fragment),ae=p(),G=h("p"),G.innerHTML=be,O=p(),$(I.$$.fragment),we=p(),oe=h("p"),oe.textContent=Y,ee=p(),$(W.$$.fragment),ce=p(),$(q.$$.fragment),te=p(),U=h("p"),U.innerHTML=de,a=p(),$(d.$$.fragment),S=p(),ke=h("p"),ke.innerHTML=Ze,Ue=p(),$(je.$$.fragment),n=p(),f=h("p"),f.innerHTML=Re,_e=p(),$(Ce.$$.fragment),We=p(),$(ve.$$.fragment),s=p(),o=h("p"),o.textContent=Ne,Xe=p(),$(Ve.$$.fragment),Le=p(),ze=h("p"),ze.innerHTML=lt,Ae=p(),$(Fe.$$.fragment),Qe=p(),$(He.$$.fragment),qe=p(),Ye=h("p"),Ye.innerHTML=at,Se=p(),$(Be.$$.fragment)},l(e){u(i.$$.fragment,e),T=r(e),m=M(e,"P",{"data-svelte-h":!0}),j(m)!=="svelte-2zvenq"&&(m.innerHTML=J),k=r(e),ne=M(e,"P",{"data-svelte-h":!0}),j(ne)!=="svelte-8pwpf3"&&(ne.innerHTML=B),pe=r(e),u(E.$$.fragment,e),re=r(e),V=M(e,"P",{"data-svelte-h":!0}),j(V)!=="svelte-9st6qy"&&(V.innerHTML=x),he=r(e),z=M(e,"OL",{});var c=Ie(z);Z=M(c,"LI",{});var Ee=Ie(Z);Me=M(Ee,"P",{"data-svelte-h":!0}),j(Me)!=="svelte-15k6rlf"&&(Me.textContent=N),$e=r(Ee),u(F.$$.fragment,Ee),Ee.forEach(t),H=r(c),se=M(c,"LI",{});var Ke=Ie(se);ue=M(Ke,"P",{"data-svelte-h":!0}),j(ue)!=="svelte-neb5xl"&&(ue.textContent=L),le=r(Ke),u(X.$$.fragment,Ke),Ke.forEach(t),Te=r(c),v=M(c,"LI",{});var De=Ie(v);P=M(De,"P",{"data-svelte-h":!0}),j(P)!=="svelte-1wyw554"&&(P.innerHTML=ie),A=r(De),u(K.$$.fragment,De),De.forEach(t),c.forEach(t),_=r(e),ge=M(e,"P",{"data-svelte-h":!0}),j(ge)!=="svelte-yjo7t9"&&(ge.textContent=me),Q=r(e),u(D.$$.fragment,e),C=r(e),u(ye.$$.fragment,e),ae=r(e),G=M(e,"P",{"data-svelte-h":!0}),j(G)!=="svelte-9fh0v0"&&(G.innerHTML=be),O=r(e),u(I.$$.fragment,e),we=r(e),oe=M(e,"P",{"data-svelte-h":!0}),j(oe)!=="svelte-o91ors"&&(oe.textContent=Y),ee=r(e),u(W.$$.fragment,e),ce=r(e),u(q.$$.fragment,e),te=r(e),U=M(e,"P",{"data-svelte-h":!0}),j(U)!=="svelte-17epabp"&&(U.innerHTML=de),a=r(e),u(d.$$.fragment,e),S=r(e),ke=M(e,"P",{"data-svelte-h":!0}),j(ke)!=="svelte-11uhvs"&&(ke.innerHTML=Ze),Ue=r(e),u(je.$$.fragment,e),n=r(e),f=M(e,"P",{"data-svelte-h":!0}),j(f)!=="svelte-vdsbfp"&&(f.innerHTML=Re),_e=r(e),u(Ce.$$.fragment,e),We=r(e),u(ve.$$.fragment,e),s=r(e),o=M(e,"P",{"data-svelte-h":!0}),j(o)!=="svelte-1vahmcn"&&(o.textContent=Ne),Xe=r(e),u(Ve.$$.fragment,e),Le=r(e),ze=M(e,"P",{"data-svelte-h":!0}),j(ze)!=="svelte-5vfufh"&&(ze.innerHTML=lt),Ae=r(e),u(Fe.$$.fragment,e),Qe=r(e),u(He.$$.fragment,e),qe=r(e),Ye=M(e,"P",{"data-svelte-h":!0}),j(Ye)!=="svelte-yb6192"&&(Ye.innerHTML=at),Se=r(e),u(Be.$$.fragment,e)},m(e,c){g(i,e,c),l(e,T,c),l(e,m,c),l(e,k,c),l(e,ne,c),l(e,pe,c),g(E,e,c),l(e,re,c),l(e,V,c),l(e,he,c),l(e,z,c),Ge(z,Z),Ge(Z,Me),Ge(Z,$e),g(F,Z,null),Ge(z,H),Ge(z,se),Ge(se,ue),Ge(se,le),g(X,se,null),Ge(z,Te),Ge(z,v),Ge(v,P),Ge(v,A),g(K,v,null),l(e,_,c),l(e,ge,c),l(e,Q,c),g(D,e,c),l(e,C,c),g(ye,e,c),l(e,ae,c),l(e,G,c),l(e,O,c),g(I,e,c),l(e,we,c),l(e,oe,c),l(e,ee,c),g(W,e,c),l(e,ce,c),g(q,e,c),l(e,te,c),l(e,U,c),l(e,a,c),g(d,e,c),l(e,S,c),l(e,ke,c),l(e,Ue,c),g(je,e,c),l(e,n,c),l(e,f,c),l(e,_e,c),g(Ce,e,c),l(e,We,c),g(ve,e,c),l(e,s,c),l(e,o,c),l(e,Xe,c),g(Ve,e,c),l(e,Le,c),l(e,ze,c),l(e,Ae,c),g(Fe,e,c),l(e,Qe,c),g(He,e,c),l(e,qe,c),l(e,Ye,c),l(e,Se,c),g(Be,e,c),Pe=!0},p(e,c){const Ee={};c&2&&(Ee.$$scope={dirty:c,ctx:e}),ve.$set(Ee)},i(e){Pe||(y(i.$$.fragment,e),y(E.$$.fragment,e),y(F.$$.fragment,e),y(X.$$.fragment,e),y(K.$$.fragment,e),y(D.$$.fragment,e),y(ye.$$.fragment,e),y(I.$$.fragment,e),y(W.$$.fragment,e),y(q.$$.fragment,e),y(d.$$.fragment,e),y(je.$$.fragment,e),y(Ce.$$.fragment,e),y(ve.$$.fragment,e),y(Ve.$$.fragment,e),y(Fe.$$.fragment,e),y(He.$$.fragment,e),y(Be.$$.fragment,e),Pe=!0)},o(e){b(i.$$.fragment,e),b(E.$$.fragment,e),b(F.$$.fragment,e),b(X.$$.fragment,e),b(K.$$.fragment,e),b(D.$$.fragment,e),b(ye.$$.fragment,e),b(I.$$.fragment,e),b(W.$$.fragment,e),b(q.$$.fragment,e),b(d.$$.fragment,e),b(je.$$.fragment,e),b(Ce.$$.fragment,e),b(ve.$$.fragment,e),b(Ve.$$.fragment,e),b(Fe.$$.fragment,e),b(He.$$.fragment,e),b(Be.$$.fragment,e),Pe=!1},d(e){e&&(t(T),t(m),t(k),t(ne),t(pe),t(re),t(V),t(he),t(z),t(_),t(ge),t(Q),t(C),t(ae),t(G),t(O),t(we),t(oe),t(ee),t(ce),t(te),t(U),t(a),t(S),t(ke),t(Ue),t(n),t(f),t(_e),t(We),t(s),t(o),t(Xe),t(Le),t(ze),t(Ae),t(Qe),t(qe),t(Ye),t(Se)),w(i,e),w(E,e),w(F),w(X),w(K),w(D,e),w(ye,e),w(I,e),w(W,e),w(q,e),w(d,e),w(je,e),w(Ce,e),w(ve,e),w(Ve,e),w(Fe,e),w(He,e),w(Be,e)}}}function wt(fe){let i,T;return i=new st({props:{$$slots:{default:[bt]},$$scope:{ctx:fe}}}),{c(){$(i.$$.fragment)},l(m){u(i.$$.fragment,m)},m(m,J){g(i,m,J),T=!0},p(m,J){const k={};J&2&&(k.$$scope={dirty:J,ctx:m}),i.$set(k)},i(m){T||(y(i.$$.fragment,m),T=!0)},o(m){b(i.$$.fragment,m),T=!1},d(m){w(i,m)}}}function jt(fe){let i,T,m,J,k,ne,B,pe,E,re="使用预训练模型有许多显著的好处。它降低了计算成本，减少了碳排放，同时允许您使用最先进的模型，而无需从头开始训练一个。🤗 Transformers 提供了涉及各种任务的成千上万的预训练模型。当您使用预训练模型时，您需要在与任务相关的数据集上训练该模型。这种操作被称为微调，是一种非常强大的训练技术。在本教程中，您将使用您选择的深度学习框架来微调一个预训练模型：",V,x,he="<li>使用 🤗 Transformers 的 <code>Trainer</code> 来微调预训练模型。</li> <li>在 TensorFlow 中使用 Keras 来微调预训练模型。</li> <li>在原生 PyTorch 中微调预训练模型。</li>",z,Z,Me,N,$e,F,H,se,ue="在您进行预训练模型微调之前，需要下载一个数据集并为训练做好准备。之前的教程向您展示了如何处理训练数据，现在您有机会将这些技能付诸实践！",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,_,ge,me,Q="如果愿意的话，您可以从完整数据集提取一个较小子集来进行微调，以减少训练所需的时间：",D,C,ye,ae,G,be,O,I,we="此时，您应该根据您训练所用的框架来选择对应的教程章节。您可以使用右侧的链接跳转到您想要的章节 - 如果您想隐藏某个框架对应的所有教程内容，只需使用右上角的按钮！",oe,Y,ee,W,ce,q,te,U,de,a,d,S,ke,Ze,Ue="更多微调例子可参考如下链接：",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>',Re,_e,Ce,We,ve;return k=new Je({props:{title:"微调预训练模型",local:"微调预训练模型",headingTag:"h1"}}),B=new ct({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"}}),F=new Oe({props:{id:"_BZearw7f0w"}}),v=new R({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 R({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJnb29nbGUtYmVydCUyRmJlcnQtYmFzZS1jYXNlZCUyMiklMEElMEElMEFkZWYlMjB0b2tlbml6ZV9mdW5jdGlvbihleGFtcGxlcyklM0ElMEElMjAlMjAlMjAlMjByZXR1cm4lMjB0b2tlbml6ZXIoZXhhbXBsZXMlNUIlMjJ0ZXh0JTIyJTVEJTJDJTIwcGFkZGluZyUzRCUyMm1heF9sZW5ndGglMjIlMkMlMjB0cnVuY2F0aW9uJTNEVHJ1ZSklMEElMEElMEF0b2tlbml6ZWRfZGF0YXNldHMlMjAlM0QlMjBkYXRhc2V0Lm1hcCh0b2tlbml6ZV9mdW5jdGlvbiUyQyUyMGJhdGNoZWQlM0RUcnVlKQ==",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;google-bert/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 R({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"}}),Y=new nt({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[gt],pytorch:[Mt]},$$scope:{ctx:fe}}}),q=new Je({props:{title:"在原生 PyTorch 中训练",local:"在原生-pytorch-中训练",headingTag:"h2"}}),U=new nt({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[wt]},$$scope:{ctx:fe}}}),S=new Je({props:{title:"附加资源",local:"附加资源",headingTag:"h2"}}),_e=new dt({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/zh/training.md"}}),{c(){i=h("meta"),T=p(),m=h("p"),J=p(),$(k.$$.fragment),ne=p(),$(B.$$.fragment),pe=p(),E=h("p"),E.textContent=re,V=p(),x=h("ul"),x.innerHTML=he,z=p(),Z=h("a"),Me=p(),$(N.$$.fragment),$e=p(),$(F.$$.fragment),H=p(),se=h("p"),se.textContent=ue,L=p(),le=h("p"),le.innerHTML=X,Te=p(),$(v.$$.fragment),P=p(),ie=h("p"),ie.innerHTML=A,K=p(),$(_.$$.fragment),ge=p(),me=h("p"),me.textContent=Q,D=p(),$(C.$$.fragment),ye=p(),ae=h("a"),G=p(),$(be.$$.fragment),O=p(),I=h("p"),I.textContent=we,oe=p(),$(Y.$$.fragment),ee=p(),W=h("a"),ce=p(),$(q.$$.fragment),te=p(),$(U.$$.fragment),de=p(),a=h("a"),d=p(),$(S.$$.fragment),ke=p(),Ze=h("p"),Ze.textContent=Ue,je=p(),n=h("ul"),n.innerHTML=f,Re=p(),$(_e.$$.fragment),Ce=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",{}),Ie(m).forEach(t),J=r(s),u(k.$$.fragment,s),ne=r(s),u(B.$$.fragment,s),pe=r(s),E=M(s,"P",{"data-svelte-h":!0}),j(E)!=="svelte-1x1dtyg"&&(E.textContent=re),V=r(s),x=M(s,"UL",{"data-svelte-h":!0}),j(x)!=="svelte-ftqcej"&&(x.innerHTML=he),z=r(s),Z=M(s,"A",{id:!0}),Ie(Z).forEach(t),Me=r(s),u(N.$$.fragment,s),$e=r(s),u(F.$$.fragment,s),H=r(s),se=M(s,"P",{"data-svelte-h":!0}),j(se)!=="svelte-15rycle"&&(se.textContent=ue),L=r(s),le=M(s,"P",{"data-svelte-h":!0}),j(le)!=="svelte-im3f9x"&&(le.innerHTML=X),Te=r(s),u(v.$$.fragment,s),P=r(s),ie=M(s,"P",{"data-svelte-h":!0}),j(ie)!=="svelte-n07g06"&&(ie.innerHTML=A),K=r(s),u(_.$$.fragment,s),ge=r(s),me=M(s,"P",{"data-svelte-h":!0}),j(me)!=="svelte-bdwtar"&&(me.textContent=Q),D=r(s),u(C.$$.fragment,s),ye=r(s),ae=M(s,"A",{id:!0}),Ie(ae).forEach(t),G=r(s),u(be.$$.fragment,s),O=r(s),I=M(s,"P",{"data-svelte-h":!0}),j(I)!=="svelte-1rp8czm"&&(I.textContent=we),oe=r(s),u(Y.$$.fragment,s),ee=r(s),W=M(s,"A",{id:!0}),Ie(W).forEach(t),ce=r(s),u(q.$$.fragment,s),te=r(s),u(U.$$.fragment,s),de=r(s),a=M(s,"A",{id:!0}),Ie(a).forEach(t),d=r(s),u(S.$$.fragment,s),ke=r(s),Ze=M(s,"P",{"data-svelte-h":!0}),j(Ze)!=="svelte-q8amck"&&(Ze.textContent=Ue),je=r(s),n=M(s,"UL",{"data-svelte-h":!0}),j(n)!=="svelte-1h2cd17"&&(n.innerHTML=f),Re=r(s),u(_e.$$.fragment,s),Ce=r(s),We=M(s,"P",{}),Ie(We).forEach(t),this.h()},h(){xe(i,"name","hf:doc:metadata"),xe(i,"content",Tt),xe(Z,"id","data-processing"),xe(ae,"id","trainer"),xe(W,"id","pytorch_native"),xe(a,"id","additional-resources")},m(s,o){Ge(document.head,i),l(s,T,o),l(s,m,o),l(s,J,o),g(k,s,o),l(s,ne,o),g(B,s,o),l(s,pe,o),l(s,E,o),l(s,V,o),l(s,x,o),l(s,z,o),l(s,Z,o),l(s,Me,o),g(N,s,o),l(s,$e,o),g(F,s,o),l(s,H,o),l(s,se,o),l(s,L,o),l(s,le,o),l(s,Te,o),g(v,s,o),l(s,P,o),l(s,ie,o),l(s,K,o),g(_,s,o),l(s,ge,o),l(s,me,o),l(s,D,o),g(C,s,o),l(s,ye,o),l(s,ae,o),l(s,G,o),g(be,s,o),l(s,O,o),l(s,I,o),l(s,oe,o),g(Y,s,o),l(s,ee,o),l(s,W,o),l(s,ce,o),g(q,s,o),l(s,te,o),g(U,s,o),l(s,de,o),l(s,a,o),l(s,d,o),g(S,s,o),l(s,ke,o),l(s,Ze,o),l(s,je,o),l(s,n,o),l(s,Re,o),g(_e,s,o),l(s,Ce,o),l(s,We,o),ve=!0},p(s,[o]){const Ne={};o&2&&(Ne.$$scope={dirty:o,ctx:s}),Y.$set(Ne);const Xe={};o&2&&(Xe.$$scope={dirty:o,ctx:s}),U.$set(Xe)},i(s){ve||(y(k.$$.fragment,s),y(B.$$.fragment,s),y(N.$$.fragment,s),y(F.$$.fragment,s),y(v.$$.fragment,s),y(_.$$.fragment,s),y(C.$$.fragment,s),y(be.$$.fragment,s),y(Y.$$.fragment,s),y(q.$$.fragment,s),y(U.$$.fragment,s),y(S.$$.fragment,s),y(_e.$$.fragment,s),ve=!0)},o(s){b(k.$$.fragment,s),b(B.$$.fragment,s),b(N.$$.fragment,s),b(F.$$.fragment,s),b(v.$$.fragment,s),b(_.$$.fragment,s),b(C.$$.fragment,s),b(be.$$.fragment,s),b(Y.$$.fragment,s),b(q.$$.fragment,s),b(U.$$.fragment,s),b(S.$$.fragment,s),b(_e.$$.fragment,s),ve=!1},d(s){s&&(t(T),t(m),t(J),t(ne),t(pe),t(E),t(V),t(x),t(z),t(Z),t(Me),t($e),t(H),t(se),t(L),t(le),t(Te),t(P),t(ie),t(K),t(ge),t(me),t(D),t(ye),t(ae),t(G),t(O),t(I),t(oe),t(ee),t(W),t(ce),t(te),t(de),t(a),t(d),t(ke),t(Ze),t(je),t(n),t(Re),t(Ce),t(We)),t(i),w(k,s),w(B,s),w(N,s),w(F,s),w(v,s),w(_,s),w(C,s),w(be,s),w(Y,s),w(q,s),w(U,s),w(S,s),w(_e,s)}}}const Tt='{"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 Jt(fe){return rt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Rt extends it{constructor(i){super(),mt(this,i,Jt,jt,pt,{})}}export{Rt as component};
