import{s as pt,o as rt,n as Oe}from"../chunks/scheduler.9991993c.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 g,d as y,t as b,w}from"../chunks/index.7fc9a5e7.js";import{T as et}from"../chunks/Tip.9de92fc6.js";import{Y as De}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 tt}from"../chunks/Markdown.87f31c7e.js";import{H as Je}from"../chunks/Heading.e3de321f.js";function dt(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,k,ne="🤗 Transformers 提供了一个专为训练 🤗 Transformers 模型而优化的 <code>Trainer</code> 类，使您无需手动编写自己的训练循环步骤而更轻松地开始训练模型。<code>Trainer</code> API 支持各种训练选项和功能，如日志记录、梯度累积和混合精度。",B,pe,x='首先加载您的模型并指定期望的标签数量。根据 Yelp Review <a href="https://huggingface.co/datasets/yelp_review_full#data-fields" rel="nofollow">数据集卡片</a>，您知道有五个标签：',re,V,E,he,z,Z,Me,N,ue='接下来，创建一个 <code>TrainingArguments</code> 类，其中包含您可以调整的所有超参数以及用于激活不同训练选项的标志。对于本教程，您可以从默认的训练<a href="https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments" rel="nofollow">超参数</a>开始，但随时可以尝试不同的设置以找到最佳设置。',F,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,_,ge="在 <code>metric</code> 上调用 <code>compute</code> 来计算您的预测的准确性。在将预测传递给 <code>compute</code> 之前，您需要将预测转换为<code>logits</code>（请记住，所有 🤗 Transformers 模型都返回对<code>logits</code>）：",me,Q,D,C,ye="如果您希望在微调过程中监视评估指标，请在您的训练参数中指定 <code>evaluation_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 De({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 et({props:{$$slots:{default:[dt]},$$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:"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 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(){u(i.$$.fragment),T=p(),u(m.$$.fragment),J=p(),k=h("p"),k.innerHTML=ne,B=p(),pe=h("p"),pe.innerHTML=x,re=p(),u(V.$$.fragment),E=p(),u(he.$$.fragment),z=p(),u(Z.$$.fragment),Me=p(),N=h("p"),N.innerHTML=ue,F=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=ge,me=p(),u(Q.$$.fragment),D=p(),C=h("p"),C.innerHTML=ye,ae=p(),u(G.$$.fragment),be=p(),u(O.$$.fragment),I=p(),we=h("p"),we.innerHTML=oe,Y=p(),u(ee.$$.fragment),W=p(),ce=h("p"),ce.innerHTML=q,te=p(),u(U.$$.fragment)},l(a){$(i.$$.fragment,a),T=r(a),$(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=x),re=r(a),$(V.$$.fragment,a),E=r(a),$(he.$$.fragment,a),z=r(a),$(Z.$$.fragment,a),Me=r(a),N=M(a,"P",{"data-svelte-h":!0}),j(N)!=="svelte-gfewmt"&&(N.innerHTML=ue),F=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=ge),me=r(a),$(Q.$$.fragment,a),D=r(a),C=M(a,"P",{"data-svelte-h":!0}),j(C)!=="svelte-7py9yh"&&(C.innerHTML=ye),ae=r(a),$(G.$$.fragment,a),be=r(a),$(O.$$.fragment,a),I=r(a),we=M(a,"P",{"data-svelte-h":!0}),j(we)!=="svelte-1np8g37"&&(we.innerHTML=oe),Y=r(a),$(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.$$.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,E,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,$e,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(E),t(z),t(Me),t(N),t(F),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(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 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){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 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,k,ne,B,pe="您也可以使用 Keras API 在 TensorFlow 中训练 🤗 Transformers 模型！",x,re,V,E,he="当您希望使用 Keras API 训练 🤗 Transformers 模型时，您需要将您的数据集转换为 Keras 可理解的格式。如果您的数据集很小，您可以将整个数据集转换为NumPy数组并传递给 Keras。在进行更复杂的操作之前，让我们先尝试这种方法。",z,Z,Me='首先，加载一个数据集。我们将使用 <a href="https://huggingface.co/datasets/glue" rel="nofollow">GLUE benchmark</a> 中的 CoLA 数据集，因为它是一个简单的二元文本分类任务。现在只使用训练数据集。',N,ue,F,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,_,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/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,I,we='在使用 <a href="/docs/transformers/main/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 De({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"}}),ue=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 et({props:{$$slots:{default:[Mt]},$$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(),u(m.$$.fragment),J=p(),u(k.$$.fragment),ne=p(),B=h("p"),B.textContent=pe,x=p(),u(re.$$.fragment),V=p(),E=h("p"),E.textContent=he,z=p(),Z=h("p"),Z.innerHTML=Me,N=p(),u(ue.$$.fragment),F=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=ge,me=p(),u(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(),u(Y.$$.fragment),ee=p(),W=h("p"),W.innerHTML=ce,q=p(),u(te.$$.fragment),U=p(),de=h("p"),de.innerHTML=a,d=p(),S=h("p"),S.innerHTML=ke,Ze=p(),u(Ue.$$.fragment),this.h()},l(n){i=M(n,"A",{id:!0}),Ce(i).forEach(t),T=r(n),$(m.$$.fragment,n),J=r(n),$(k.$$.fragment,n),ne=r(n),B=M(n,"P",{"data-svelte-h":!0}),j(B)!=="svelte-bcxyio"&&(B.textContent=pe),x=r(n),$(re.$$.fragment,n),V=r(n),E=M(n,"P",{"data-svelte-h":!0}),j(E)!=="svelte-i3cei"&&(E.textContent=he),z=r(n),Z=M(n,"P",{"data-svelte-h":!0}),j(Z)!=="svelte-1km4tqo"&&(Z.innerHTML=Me),N=r(n),$(ue.$$.fragment,n),F=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=ge),me=r(n),$(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-13siqzd"&&(G.innerHTML=be),O=r(n),I=M(n,"P",{"data-svelte-h":!0}),j(I)!=="svelte-vpnvoi"&&(I.innerHTML=we),oe=r(n),$(Y.$$.fragment,n),ee=r(n),W=M(n,"P",{"data-svelte-h":!0}),j(W)!=="svelte-hz86ti"&&(W.innerHTML=ce),q=r(n),$(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),$(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,x,f),g(re,n,f),l(n,V,f),l(n,E,f),l(n,z,f),l(n,Z,f),l(n,N,f),g(ue,n,f),l(n,F,f),l(n,H,f),l(n,$e,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 Ge={};f&2&&(Ge.$$scope={dirty:f,ctx:n}),A.$set(Ge)},i(n){je||(y(m.$$.fragment,n),y(k.$$.fragment,n),y(re.$$.fragment,n),y(ue.$$.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(ue.$$.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(x),t(V),t(E),t(z),t(Z),t(N),t(F),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(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(ue,n),w(L,n),w(P,n),w(A,n),w(Q,n),w(Y,n),w(te,n),w(Ue,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){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 gt(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 yt(fe){let i,T,m,J="<code>Trainer</code> 负责训练循环，允许您在一行代码中微调模型。对于喜欢编写自己训练循环的用户，您也可以在原生 PyTorch 中微调 🤗 Transformers 模型。",k,ne,B="现在，您可能需要重新启动您的<code>notebook</code>，或执行以下代码以释放一些内存：",pe,x,re,V,E="接下来，手动处理 <code>tokenized_dataset</code> 以准备进行训练。",he,z,Z,Me,N="移除 text 列，因为模型不接受原始文本作为输入：",ue,F,H,se,$e,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,Ge="最后，指定 <code>device</code> 以使用 GPU（如果有的话）。否则，使用 CPU 进行训练可能需要几个小时，而不是几分钟。",Ie,ve,s,o,We,Re,st="现在您已经准备好训练了！🥳",Ee,Xe,Ne,Ve,lt='为了跟踪训练进度，使用 <a href="https://tqdm.github.io/" rel="nofollow">tqdm</a> 库来添加一个进度条，显示训练步数的进展：',Le,ze,Ae,Fe,Qe,He,at="就像您在 <code>Trainer</code> 中添加了一个评估函数一样，当您编写自己的训练循环时，您需要做同样的事情。但与在每个<code>epoch</code>结束时计算和展示指标不同，这一次您将使用 <code>add_batch</code> 累积所有批次，并在最后计算指标。",qe,Ye,Se;return i=new De({props:{id:"Dh9CL8fyG80"}}),x=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}}),ve=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}}),o=new et({props:{$$slots:{default:[gt]},$$scope:{ctx:fe}}}),Xe=new Je({props:{title:"训练循环",local:"训练循环",headingTag:"h3"}}),ze=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}}),Fe=new Je({props:{title:"评估",local:"评估",headingTag:"h3"}}),Ye=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(){u(i.$$.fragment),T=p(),m=h("p"),m.innerHTML=J,k=p(),ne=h("p"),ne.innerHTML=B,pe=p(),u(x.$$.fragment),re=p(),V=h("p"),V.innerHTML=E,he=p(),z=h("ol"),Z=h("li"),Me=h("p"),Me.textContent=N,ue=p(),u(F.$$.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(),ge=h("p"),ge.textContent=me,Q=p(),u(D.$$.fragment),C=p(),u(ye.$$.fragment),ae=p(),G=h("p"),G.innerHTML=be,O=p(),u(I.$$.fragment),we=p(),oe=h("p"),oe.textContent=Y,ee=p(),u(W.$$.fragment),ce=p(),u(q.$$.fragment),te=p(),U=h("p"),U.innerHTML=de,a=p(),u(d.$$.fragment),S=p(),ke=h("p"),ke.innerHTML=Ze,Ue=p(),u(je.$$.fragment),n=p(),f=h("p"),f.innerHTML=Ge,Ie=p(),u(ve.$$.fragment),s=p(),u(o.$$.fragment),We=p(),Re=h("p"),Re.textContent=st,Ee=p(),u(Xe.$$.fragment),Ne=p(),Ve=h("p"),Ve.innerHTML=lt,Le=p(),u(ze.$$.fragment),Ae=p(),u(Fe.$$.fragment),Qe=p(),He=h("p"),He.innerHTML=at,qe=p(),u(Ye.$$.fragment)},l(e){$(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),$(x.$$.fragment,e),re=r(e),V=M(e,"P",{"data-svelte-h":!0}),j(V)!=="svelte-9st6qy"&&(V.innerHTML=E),he=r(e),z=M(e,"OL",{});var c=Ce(z);Z=M(c,"LI",{});var Be=Ce(Z);Me=M(Be,"P",{"data-svelte-h":!0}),j(Me)!=="svelte-15k6rlf"&&(Me.textContent=N),ue=r(Be),$(F.$$.fragment,Be),Be.forEach(t),H=r(c),se=M(c,"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(c),v=M(c,"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),c.forEach(t),_=r(e),ge=M(e,"P",{"data-svelte-h":!0}),j(ge)!=="svelte-yjo7t9"&&(ge.textContent=me),Q=r(e),$(D.$$.fragment,e),C=r(e),$(ye.$$.fragment,e),ae=r(e),G=M(e,"P",{"data-svelte-h":!0}),j(G)!=="svelte-9fh0v0"&&(G.innerHTML=be),O=r(e),$(I.$$.fragment,e),we=r(e),oe=M(e,"P",{"data-svelte-h":!0}),j(oe)!=="svelte-o91ors"&&(oe.textContent=Y),ee=r(e),$(W.$$.fragment,e),ce=r(e),$(q.$$.fragment,e),te=r(e),U=M(e,"P",{"data-svelte-h":!0}),j(U)!=="svelte-17epabp"&&(U.innerHTML=de),a=r(e),$(d.$$.fragment,e),S=r(e),ke=M(e,"P",{"data-svelte-h":!0}),j(ke)!=="svelte-11uhvs"&&(ke.innerHTML=Ze),Ue=r(e),$(je.$$.fragment,e),n=r(e),f=M(e,"P",{"data-svelte-h":!0}),j(f)!=="svelte-vdsbfp"&&(f.innerHTML=Ge),Ie=r(e),$(ve.$$.fragment,e),s=r(e),$(o.$$.fragment,e),We=r(e),Re=M(e,"P",{"data-svelte-h":!0}),j(Re)!=="svelte-1vahmcn"&&(Re.textContent=st),Ee=r(e),$(Xe.$$.fragment,e),Ne=r(e),Ve=M(e,"P",{"data-svelte-h":!0}),j(Ve)!=="svelte-5vfufh"&&(Ve.innerHTML=lt),Le=r(e),$(ze.$$.fragment,e),Ae=r(e),$(Fe.$$.fragment,e),Qe=r(e),He=M(e,"P",{"data-svelte-h":!0}),j(He)!=="svelte-yb6192"&&(He.innerHTML=at),qe=r(e),$(Ye.$$.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(x,e,c),l(e,re,c),l(e,V,c),l(e,he,c),l(e,z,c),_e(z,Z),_e(Z,Me),_e(Z,ue),g(F,Z,null),_e(z,H),_e(z,se),_e(se,$e),_e(se,le),g(X,se,null),_e(z,Te),_e(z,v),_e(v,P),_e(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,Ie,c),g(ve,e,c),l(e,s,c),g(o,e,c),l(e,We,c),l(e,Re,c),l(e,Ee,c),g(Xe,e,c),l(e,Ne,c),l(e,Ve,c),l(e,Le,c),g(ze,e,c),l(e,Ae,c),g(Fe,e,c),l(e,Qe,c),l(e,He,c),l(e,qe,c),g(Ye,e,c),Se=!0},p(e,c){const Be={};c&2&&(Be.$$scope={dirty:c,ctx:e}),o.$set(Be)},i(e){Se||(y(i.$$.fragment,e),y(x.$$.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(ve.$$.fragment,e),y(o.$$.fragment,e),y(Xe.$$.fragment,e),y(ze.$$.fragment,e),y(Fe.$$.fragment,e),y(Ye.$$.fragment,e),Se=!0)},o(e){b(i.$$.fragment,e),b(x.$$.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(ve.$$.fragment,e),b(o.$$.fragment,e),b(Xe.$$.fragment,e),b(ze.$$.fragment,e),b(Fe.$$.fragment,e),b(Ye.$$.fragment,e),Se=!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(Ie),t(s),t(We),t(Re),t(Ee),t(Ne),t(Ve),t(Le),t(Ae),t(Qe),t(He),t(qe)),w(i,e),w(x,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(ve,e),w(o,e),w(Xe,e),w(ze,e),w(Fe,e),w(Ye,e)}}}function bt(fe){let i,T;return i=new tt({props:{$$slots:{default:[yt]},$$scope:{ctx:fe}}}),{c(){u(i.$$.fragment)},l(m){$(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 wt(fe){let i,T,m,J,k,ne,B,pe,x,re="使用预训练模型有许多显著的好处。它降低了计算成本，减少了碳排放，同时允许您使用最先进的模型，而无需从头开始训练一个。🤗 Transformers 提供了涉及各种任务的成千上万的预训练模型。当您使用预训练模型时，您需要在与任务相关的数据集上训练该模型。这种操作被称为微调，是一种非常强大的训练技术。在本教程中，您将使用您选择的深度学习框架来微调一个预训练模型：",V,E,he="<li>使用 🤗 Transformers 的 <code>Trainer</code> 来微调预训练模型。</li> <li>在 TensorFlow 中使用 Keras 来微调预训练模型。</li> <li>在原生 PyTorch 中微调预训练模型。</li>",z,Z,Me,N,ue,F,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,_,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>',Ge,Ie,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 De({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:[$t],pytorch:[ht]},$$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:[bt]},$$scope:{ctx:fe}}}),S=new Je({props:{title:"附加资源",local:"附加资源",headingTag:"h2"}}),{c(){i=h("meta"),T=p(),m=h("p"),J=p(),u(k.$$.fragment),ne=p(),u(B.$$.fragment),pe=p(),x=h("p"),x.textContent=re,V=p(),E=h("ul"),E.innerHTML=he,z=p(),Z=h("a"),Me=p(),u(N.$$.fragment),ue=p(),u(F.$$.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),ge=p(),me=h("p"),me.textContent=Q,D=p(),u(C.$$.fragment),ye=p(),ae=h("a"),G=p(),u(be.$$.fragment),O=p(),I=h("p"),I.textContent=we,oe=p(),u(Y.$$.fragment),ee=p(),W=h("a"),ce=p(),u(q.$$.fragment),te=p(),u(U.$$.fragment),de=p(),a=h("a"),d=p(),u(S.$$.fragment),ke=p(),Ze=h("p"),Ze.textContent=Ue,je=p(),n=h("ul"),n.innerHTML=f,Ge=p(),Ie=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),$(k.$$.fragment,s),ne=r(s),$(B.$$.fragment,s),pe=r(s),x=M(s,"P",{"data-svelte-h":!0}),j(x)!=="svelte-1x1dtyg"&&(x.textContent=re),V=r(s),E=M(s,"UL",{"data-svelte-h":!0}),j(E)!=="svelte-ftqcej"&&(E.innerHTML=he),z=r(s),Z=M(s,"A",{id:!0}),Ce(Z).forEach(t),Me=r(s),$(N.$$.fragment,s),ue=r(s),$(F.$$.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),ge=r(s),me=M(s,"P",{"data-svelte-h":!0}),j(me)!=="svelte-bdwtar"&&(me.textContent=Q),D=r(s),$(C.$$.fragment,s),ye=r(s),ae=M(s,"A",{id:!0}),Ce(ae).forEach(t),G=r(s),$(be.$$.fragment,s),O=r(s),I=M(s,"P",{"data-svelte-h":!0}),j(I)!=="svelte-1rp8czm"&&(I.textContent=we),oe=r(s),$(Y.$$.fragment,s),ee=r(s),W=M(s,"A",{id:!0}),Ce(W).forEach(t),ce=r(s),$(q.$$.fragment,s),te=r(s),$(U.$$.fragment,s),de=r(s),a=M(s,"A",{id:!0}),Ce(a).forEach(t),d=r(s),$(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),Ge=r(s),Ie=M(s,"P",{}),Ce(Ie).forEach(t),this.h()},h(){xe(i,"name","hf:doc:metadata"),xe(i,"content",jt),xe(Z,"id","data-processing"),xe(ae,"id","trainer"),xe(W,"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),g(k,s,o),l(s,ne,o),g(B,s,o),l(s,pe,o),l(s,x,o),l(s,V,o),l(s,E,o),l(s,z,o),l(s,Z,o),l(s,Me,o),g(N,s,o),l(s,ue,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,Ge,o),l(s,Ie,o),ve=!0},p(s,[o]){const We={};o&2&&(We.$$scope={dirty:o,ctx:s}),Y.$set(We);const Re={};o&2&&(Re.$$scope={dirty:o,ctx:s}),U.$set(Re)},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),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),ve=!1},d(s){s&&(t(T),t(m),t(J),t(ne),t(pe),t(x),t(V),t(E),t(z),t(Z),t(Me),t(ue),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(Ge),t(Ie)),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)}}}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 It extends it{constructor(i){super(),mt(this,i,Tt,wt,pt,{})}}export{It as component};
