import{s as rt,o as ot,n as et}from"../chunks/scheduler.25b97de1.js";import{S as it,i as pt,g as h,s as r,r as g,A as mt,h as u,f as t,c as o,j as Ie,u as y,x as j,k as He,y as Ge,a as s,v as M,d as w,t as $,w as b}from"../chunks/index.d9030fc9.js";import{T as tt}from"../chunks/Tip.baa67368.js";import{Y as Oe}from"../chunks/Youtube.eaf1a617.js";import{C as R}from"../chunks/CodeBlock.e6cd0d95.js";import{D as dt}from"../chunks/DocNotebookDropdown.5ea6cb78.js";import{F as nt,M as at}from"../chunks/Markdown.7217f838.js";import{H as Je,E as ct}from"../chunks/EditOnGithub.91d95064.js";function ft(fe){let i,T=`You will see a warning about some of the pretrained weights not being used and some weights being randomly
initialized. Don’t worry, this is completely normal! The pretrained head of the BERT model is discarded, and replaced with a randomly initialized classification head. You will fine-tune this new model head on your sequence classification task, transferring the knowledge of the pretrained model to it.`;return{c(){i=h("p"),i.textContent=T},l(p){i=u(p,"P",{"data-svelte-h":!0}),j(i)!=="svelte-19yjdqu"&&(i.textContent=T)},m(p,J){s(p,i,J)},p:et,d(p){p&&t(i)}}}function ht(fe){let i,T,p,J,k,ne='🤗 Transformers provides a <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a> class optimized for training 🤗 Transformers models, making it easier to start training without manually writing your own training loop. The <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a> API supports a wide range of training options and features such as logging, gradient accumulation, and mixed precision.',B,re,E='Start by loading your model and specify the number of expected labels. From the Yelp Review <a href="https://huggingface.co/datasets/yelp_review_full#data-fields" rel="nofollow">dataset card</a>, you know there are five labels:',oe,F,H,he,x,v,ue,N,ge='Next, create a <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> class which contains all the hyperparameters you can tune as well as flags for activating different training options. For this tutorial you can start with the default training <a href="https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments" rel="nofollow">hyperparameters</a>, but feel free to experiment with these to find your optimal settings.',V,z,ae="Specify where to save the checkpoints from your training:",ye,A,se,X,Te,U,P='<a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a> does not automatically evaluate model performance during training. You’ll need to pass <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a> a function to compute and report metrics. The <a href="https://huggingface.co/docs/evaluate/index" rel="nofollow">🤗 Evaluate</a> library provides a simple <a href="https://huggingface.co/spaces/evaluate-metric/accuracy" rel="nofollow"><code>accuracy</code></a> function you can load with the <a href="https://huggingface.co/docs/evaluate/main/en/package_reference/loading_methods#evaluate.load" rel="nofollow">evaluate.load</a> (see this <a href="https://huggingface.co/docs/evaluate/a_quick_tour" rel="nofollow">quicktour</a> for more information) function:',ie,L,K,_,Me="Call <code>compute</code> on <code>metric</code> to calculate the accuracy of your predictions. Before passing your predictions to <code>compute</code>, you need to convert the logits to predictions (remember all 🤗 Transformers models return logits):",pe,Q,D,C,we="If you’d like to monitor your evaluation metrics during fine-tuning, specify the <code>eval_strategy</code> parameter in your training arguments to report the evaluation metric at the end of each epoch:",le,G,$e,O,I,be,me='Create a <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a> object with your model, training arguments, training and test datasets, and evaluation function:',Y,ee,W,de,q='Then fine-tune your model by calling <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer.train">train()</a>:',te,Z,ce;return i=new Oe({props:{id:"nvBXf7s7vTI"}}),p=new Je({props:{title:"Train with PyTorch Trainer",local:"train-with-pytorch-trainer",headingTag:"h2"}}),F=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}}}),v=new Je({props:{title:"Training hyperparameters",local:"training-hyperparameters",headingTag:"h3"}}),A=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:"Evaluate",local:"evaluate",headingTag:"h3"}}),L=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:"Trainer",local:"trainer",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}}),Z=new R({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()',wrap:!1}}),{c(){g(i.$$.fragment),T=r(),g(p.$$.fragment),J=r(),k=h("p"),k.innerHTML=ne,B=r(),re=h("p"),re.innerHTML=E,oe=r(),g(F.$$.fragment),H=r(),g(he.$$.fragment),x=r(),g(v.$$.fragment),ue=r(),N=h("p"),N.innerHTML=ge,V=r(),z=h("p"),z.textContent=ae,ye=r(),g(A.$$.fragment),se=r(),g(X.$$.fragment),Te=r(),U=h("p"),U.innerHTML=P,ie=r(),g(L.$$.fragment),K=r(),_=h("p"),_.innerHTML=Me,pe=r(),g(Q.$$.fragment),D=r(),C=h("p"),C.innerHTML=we,le=r(),g(G.$$.fragment),$e=r(),g(O.$$.fragment),I=r(),be=h("p"),be.innerHTML=me,Y=r(),g(ee.$$.fragment),W=r(),de=h("p"),de.innerHTML=q,te=r(),g(Z.$$.fragment)},l(l){y(i.$$.fragment,l),T=o(l),y(p.$$.fragment,l),J=o(l),k=u(l,"P",{"data-svelte-h":!0}),j(k)!=="svelte-60sa53"&&(k.innerHTML=ne),B=o(l),re=u(l,"P",{"data-svelte-h":!0}),j(re)!=="svelte-3dzt1s"&&(re.innerHTML=E),oe=o(l),y(F.$$.fragment,l),H=o(l),y(he.$$.fragment,l),x=o(l),y(v.$$.fragment,l),ue=o(l),N=u(l,"P",{"data-svelte-h":!0}),j(N)!=="svelte-6ieqr4"&&(N.innerHTML=ge),V=o(l),z=u(l,"P",{"data-svelte-h":!0}),j(z)!=="svelte-1v0ty1x"&&(z.textContent=ae),ye=o(l),y(A.$$.fragment,l),se=o(l),y(X.$$.fragment,l),Te=o(l),U=u(l,"P",{"data-svelte-h":!0}),j(U)!=="svelte-172s06i"&&(U.innerHTML=P),ie=o(l),y(L.$$.fragment,l),K=o(l),_=u(l,"P",{"data-svelte-h":!0}),j(_)!=="svelte-1j1ti7k"&&(_.innerHTML=Me),pe=o(l),y(Q.$$.fragment,l),D=o(l),C=u(l,"P",{"data-svelte-h":!0}),j(C)!=="svelte-1lmipg"&&(C.innerHTML=we),le=o(l),y(G.$$.fragment,l),$e=o(l),y(O.$$.fragment,l),I=o(l),be=u(l,"P",{"data-svelte-h":!0}),j(be)!=="svelte-tg6f66"&&(be.innerHTML=me),Y=o(l),y(ee.$$.fragment,l),W=o(l),de=u(l,"P",{"data-svelte-h":!0}),j(de)!=="svelte-17mi8bo"&&(de.innerHTML=q),te=o(l),y(Z.$$.fragment,l)},m(l,c){M(i,l,c),s(l,T,c),M(p,l,c),s(l,J,c),s(l,k,c),s(l,B,c),s(l,re,c),s(l,oe,c),M(F,l,c),s(l,H,c),M(he,l,c),s(l,x,c),M(v,l,c),s(l,ue,c),s(l,N,c),s(l,V,c),s(l,z,c),s(l,ye,c),M(A,l,c),s(l,se,c),M(X,l,c),s(l,Te,c),s(l,U,c),s(l,ie,c),M(L,l,c),s(l,K,c),s(l,_,c),s(l,pe,c),M(Q,l,c),s(l,D,c),s(l,C,c),s(l,le,c),M(G,l,c),s(l,$e,c),M(O,l,c),s(l,I,c),s(l,be,c),s(l,Y,c),M(ee,l,c),s(l,W,c),s(l,de,c),s(l,te,c),M(Z,l,c),ce=!0},p(l,c){const S={};c&2&&(S.$$scope={dirty:c,ctx:l}),he.$set(S)},i(l){ce||(w(i.$$.fragment,l),w(p.$$.fragment,l),w(F.$$.fragment,l),w(he.$$.fragment,l),w(v.$$.fragment,l),w(A.$$.fragment,l),w(X.$$.fragment,l),w(L.$$.fragment,l),w(Q.$$.fragment,l),w(G.$$.fragment,l),w(O.$$.fragment,l),w(ee.$$.fragment,l),w(Z.$$.fragment,l),ce=!0)},o(l){$(i.$$.fragment,l),$(p.$$.fragment,l),$(F.$$.fragment,l),$(he.$$.fragment,l),$(v.$$.fragment,l),$(A.$$.fragment,l),$(X.$$.fragment,l),$(L.$$.fragment,l),$(Q.$$.fragment,l),$(G.$$.fragment,l),$(O.$$.fragment,l),$(ee.$$.fragment,l),$(Z.$$.fragment,l),ce=!1},d(l){l&&(t(T),t(J),t(k),t(B),t(re),t(oe),t(H),t(x),t(ue),t(N),t(V),t(z),t(ye),t(se),t(Te),t(U),t(ie),t(K),t(_),t(pe),t(D),t(C),t(le),t($e),t(I),t(be),t(Y),t(W),t(de),t(te)),b(i,l),b(p,l),b(F,l),b(he,l),b(v,l),b(A,l),b(X,l),b(L,l),b(Q,l),b(G,l),b(O,l),b(ee,l),b(Z,l)}}}function ut(fe){let i,T;return i=new at({props:{$$slots:{default:[ht]},$$scope:{ctx:fe}}}),{c(){g(i.$$.fragment)},l(p){y(i.$$.fragment,p)},m(p,J){M(i,p,J),T=!0},p(p,J){const k={};J&2&&(k.$$scope={dirty:J,ctx:p}),i.$set(k)},i(p){T||(w(i.$$.fragment,p),T=!0)},o(p){$(i.$$.fragment,p),T=!1},d(p){b(i,p)}}}function gt(fe){let i,T=`You don’t have to pass a loss argument to your models when you <code>compile()</code> them! Hugging Face models automatically
choose a loss that is appropriate for their task and model architecture if this argument is left blank. You can always
override this by specifying a loss yourself if you want to!`;return{c(){i=h("p"),i.innerHTML=T},l(p){i=u(p,"P",{"data-svelte-h":!0}),j(i)!=="svelte-z0w0oe"&&(i.innerHTML=T)},m(p,J){s(p,i,J)},p:et,d(p){p&&t(i)}}}function yt(fe){let i,T,p,J,k,ne,B,re="You can also train 🤗 Transformers models in TensorFlow with the Keras API!",E,oe,F,H,he=`When you want to train a 🤗 Transformers model with the Keras API, you need to convert your dataset to a format that
Keras understands. If your dataset is small, you can just convert the whole thing to NumPy arrays and pass it to Keras.
Let’s try that first before we do anything more complicated.`,x,v,ue=`First, load a dataset. We’ll use the CoLA dataset from the <a href="https://huggingface.co/datasets/glue" rel="nofollow">GLUE benchmark</a>,
since it’s a simple binary text classification task, and just take the training split for now.`,N,ge,V,z,ae=`Next, load a tokenizer and tokenize the data as NumPy arrays. Note that the labels are already a list of 0 and 1s,
so we can just convert that directly to a NumPy array without tokenization!`,ye,A,se,X,Te='Finally, load, <a href="https://keras.io/api/models/model_training_apis/#compile-method" rel="nofollow"><code>compile</code></a>, and <a href="https://keras.io/api/models/model_training_apis/#fit-method" rel="nofollow"><code>fit</code></a> the model. Note that Transformers models all have a default task-relevant loss function, so you don’t need to specify one unless you want to:',U,P,ie,L,K,_,Me=`This approach works great for smaller datasets, but for larger datasets, you might find it starts to become a problem. Why?
Because the tokenized array and labels would have to be fully loaded into memory, and because NumPy doesn’t handle
“jagged” arrays, so every tokenized sample would have to be padded to the length of the longest sample in the whole
dataset. That’s going to make your array even bigger, and all those padding tokens will slow down training too!`,pe,Q,D,C,we=`If you want to avoid slowing down training, you can load your data as a <code>tf.data.Dataset</code> instead. Although you can write your own
<code>tf.data</code> pipeline if you want, we have two convenience methods for doing this:`,le,G,$e=`<li><a href="/docs/transformers/pr_29969/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a>: This is the method we recommend in most cases. Because it is a method
on your model, it can inspect the model to automatically figure out which columns are usable as model inputs, and
discard the others to make a simpler, more performant dataset.</li> <li><a href="https://huggingface.co/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset.to_tf_dataset" rel="nofollow">to_tf_dataset</a>: This method is more low-level, and is useful when you want to exactly control how
your dataset is created, by specifying exactly which <code>columns</code> and <code>label_cols</code> to include.</li>`,O,I,be=`Before you can use <a href="/docs/transformers/pr_29969/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a>, you will need to add the tokenizer outputs to your dataset as columns, as shown in
the following code sample:`,me,Y,ee,W,de=`Remember that Hugging Face datasets are stored on disk by default, so this will not inflate your memory usage! Once the
columns have been added, you can stream batches from the dataset and add padding to each batch, which greatly
reduces the number of padding tokens compared to padding the entire dataset.`,q,te,Z,ce,l=`Note that in the code sample above, you need to pass the tokenizer to <code>prepare_tf_dataset</code> so it can correctly pad batches as they’re loaded.
If all the samples in your dataset are the same length and no padding is necessary, you can skip this argument.
If you need to do something more complex than just padding samples (e.g. corrupting tokens for masked language
modelling), you can use the <code>collate_fn</code> argument instead to pass a function that will be called to transform the
list of samples into a batch and apply any preprocessing you want. See our
<a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">examples</a> or
<a href="https://huggingface.co/docs/transformers/notebooks" rel="nofollow">notebooks</a> to see this approach in action.`,c,S,ke="Once you’ve created a <code>tf.data.Dataset</code>, you can compile and fit the model as before:",ve,Ze,je;return p=new Oe({props:{id:"rnTGBy2ax1c"}}),k=new Je({props:{title:"Train a TensorFlow model with Keras",local:"train-a-tensorflow-model-with-keras",headingTag:"h2"}}),oe=new Je({props:{title:"Loading data for Keras",local:"loading-data-for-keras",headingTag:"h3"}}),ge=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}}),A=new R({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEFpbXBvcnQlMjBudW1weSUyMGFzJTIwbnAlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJnb29nbGUtYmVydCUyRmJlcnQtYmFzZS1jYXNlZCUyMiklMEF0b2tlbml6ZWRfZGF0YSUyMCUzRCUyMHRva2VuaXplcihkYXRhc2V0JTVCJTIyc2VudGVuY2UlMjIlNUQlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMm5wJTIyJTJDJTIwcGFkZGluZyUzRFRydWUpJTBBJTIzJTIwVG9rZW5pemVyJTIwcmV0dXJucyUyMGElMjBCYXRjaEVuY29kaW5nJTJDJTIwYnV0JTIwd2UlMjBjb252ZXJ0JTIwdGhhdCUyMHRvJTIwYSUyMGRpY3QlMjBmb3IlMjBLZXJhcyUwQXRva2VuaXplZF9kYXRhJTIwJTNEJTIwZGljdCh0b2tlbml6ZWRfZGF0YSklMEElMEFsYWJlbHMlMjAlM0QlMjBucC5hcnJheShkYXRhc2V0JTVCJTIybGFiZWwlMjIlNUQpJTIwJTIwJTIzJTIwTGFiZWwlMjBpcyUyMGFscmVhZHklMjBhbiUyMGFycmF5JTIwb2YlMjAwJTIwYW5kJTIwMQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

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}}),L=new tt({props:{$$slots:{default:[gt]},$$scope:{ctx:fe}}}),Q=new Je({props:{title:"Loading data as a tf.data.Dataset",local:"loading-data-as-a-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}}),Ze=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=r(),g(p.$$.fragment),J=r(),g(k.$$.fragment),ne=r(),B=h("p"),B.textContent=re,E=r(),g(oe.$$.fragment),F=r(),H=h("p"),H.textContent=he,x=r(),v=h("p"),v.innerHTML=ue,N=r(),g(ge.$$.fragment),V=r(),z=h("p"),z.textContent=ae,ye=r(),g(A.$$.fragment),se=r(),X=h("p"),X.innerHTML=Te,U=r(),g(P.$$.fragment),ie=r(),g(L.$$.fragment),K=r(),_=h("p"),_.textContent=Me,pe=r(),g(Q.$$.fragment),D=r(),C=h("p"),C.innerHTML=we,le=r(),G=h("ul"),G.innerHTML=$e,O=r(),I=h("p"),I.innerHTML=be,me=r(),g(Y.$$.fragment),ee=r(),W=h("p"),W.textContent=de,q=r(),g(te.$$.fragment),Z=r(),ce=h("p"),ce.innerHTML=l,c=r(),S=h("p"),S.innerHTML=ke,ve=r(),g(Ze.$$.fragment),this.h()},l(n){i=u(n,"A",{id:!0}),Ie(i).forEach(t),T=o(n),y(p.$$.fragment,n),J=o(n),y(k.$$.fragment,n),ne=o(n),B=u(n,"P",{"data-svelte-h":!0}),j(B)!=="svelte-pfehij"&&(B.textContent=re),E=o(n),y(oe.$$.fragment,n),F=o(n),H=u(n,"P",{"data-svelte-h":!0}),j(H)!=="svelte-1koz3e5"&&(H.textContent=he),x=o(n),v=u(n,"P",{"data-svelte-h":!0}),j(v)!=="svelte-12jkaap"&&(v.innerHTML=ue),N=o(n),y(ge.$$.fragment,n),V=o(n),z=u(n,"P",{"data-svelte-h":!0}),j(z)!=="svelte-1t3pc5r"&&(z.textContent=ae),ye=o(n),y(A.$$.fragment,n),se=o(n),X=u(n,"P",{"data-svelte-h":!0}),j(X)!=="svelte-t3abgj"&&(X.innerHTML=Te),U=o(n),y(P.$$.fragment,n),ie=o(n),y(L.$$.fragment,n),K=o(n),_=u(n,"P",{"data-svelte-h":!0}),j(_)!=="svelte-janu8e"&&(_.textContent=Me),pe=o(n),y(Q.$$.fragment,n),D=o(n),C=u(n,"P",{"data-svelte-h":!0}),j(C)!=="svelte-1ar86wm"&&(C.innerHTML=we),le=o(n),G=u(n,"UL",{"data-svelte-h":!0}),j(G)!=="svelte-a21hvn"&&(G.innerHTML=$e),O=o(n),I=u(n,"P",{"data-svelte-h":!0}),j(I)!=="svelte-g4b67n"&&(I.innerHTML=be),me=o(n),y(Y.$$.fragment,n),ee=o(n),W=u(n,"P",{"data-svelte-h":!0}),j(W)!=="svelte-1n8nrqv"&&(W.textContent=de),q=o(n),y(te.$$.fragment,n),Z=o(n),ce=u(n,"P",{"data-svelte-h":!0}),j(ce)!=="svelte-1hn9om"&&(ce.innerHTML=l),c=o(n),S=u(n,"P",{"data-svelte-h":!0}),j(S)!=="svelte-4nybjx"&&(S.innerHTML=ke),ve=o(n),y(Ze.$$.fragment,n),this.h()},h(){He(i,"id","keras")},m(n,f){s(n,i,f),s(n,T,f),M(p,n,f),s(n,J,f),M(k,n,f),s(n,ne,f),s(n,B,f),s(n,E,f),M(oe,n,f),s(n,F,f),s(n,H,f),s(n,x,f),s(n,v,f),s(n,N,f),M(ge,n,f),s(n,V,f),s(n,z,f),s(n,ye,f),M(A,n,f),s(n,se,f),s(n,X,f),s(n,U,f),M(P,n,f),s(n,ie,f),M(L,n,f),s(n,K,f),s(n,_,f),s(n,pe,f),M(Q,n,f),s(n,D,f),s(n,C,f),s(n,le,f),s(n,G,f),s(n,O,f),s(n,I,f),s(n,me,f),M(Y,n,f),s(n,ee,f),s(n,W,f),s(n,q,f),M(te,n,f),s(n,Z,f),s(n,ce,f),s(n,c,f),s(n,S,f),s(n,ve,f),M(Ze,n,f),je=!0},p(n,f){const Re={};f&2&&(Re.$$scope={dirty:f,ctx:n}),L.$set(Re)},i(n){je||(w(p.$$.fragment,n),w(k.$$.fragment,n),w(oe.$$.fragment,n),w(ge.$$.fragment,n),w(A.$$.fragment,n),w(P.$$.fragment,n),w(L.$$.fragment,n),w(Q.$$.fragment,n),w(Y.$$.fragment,n),w(te.$$.fragment,n),w(Ze.$$.fragment,n),je=!0)},o(n){$(p.$$.fragment,n),$(k.$$.fragment,n),$(oe.$$.fragment,n),$(ge.$$.fragment,n),$(A.$$.fragment,n),$(P.$$.fragment,n),$(L.$$.fragment,n),$(Q.$$.fragment,n),$(Y.$$.fragment,n),$(te.$$.fragment,n),$(Ze.$$.fragment,n),je=!1},d(n){n&&(t(i),t(T),t(J),t(ne),t(B),t(E),t(F),t(H),t(x),t(v),t(N),t(V),t(z),t(ye),t(se),t(X),t(U),t(ie),t(K),t(_),t(pe),t(D),t(C),t(le),t(G),t(O),t(I),t(me),t(ee),t(W),t(q),t(Z),t(ce),t(c),t(S),t(ve)),b(p,n),b(k,n),b(oe,n),b(ge,n),b(A,n),b(P,n),b(L,n),b(Q,n),b(Y,n),b(te,n),b(Ze,n)}}}function Mt(fe){let i,T;return i=new at({props:{$$slots:{default:[yt]},$$scope:{ctx:fe}}}),{c(){g(i.$$.fragment)},l(p){y(i.$$.fragment,p)},m(p,J){M(i,p,J),T=!0},p(p,J){const k={};J&2&&(k.$$scope={dirty:J,ctx:p}),i.$set(k)},i(p){T||(w(i.$$.fragment,p),T=!0)},o(p){$(i.$$.fragment,p),T=!1},d(p){b(i,p)}}}function wt(fe){let i,T='Get free access to a cloud GPU if you don’t have one with a hosted notebook like <a href="https://colab.research.google.com/" rel="nofollow">Colaboratory</a> or <a href="https://studiolab.sagemaker.aws/" rel="nofollow">SageMaker StudioLab</a>.';return{c(){i=h("p"),i.innerHTML=T},l(p){i=u(p,"P",{"data-svelte-h":!0}),j(i)!=="svelte-19nlkqe"&&(i.innerHTML=T)},m(p,J){s(p,i,J)},p:et,d(p){p&&t(i)}}}function $t(fe){let i,T,p,J='<a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a> takes care of the training loop and allows you to fine-tune a model in a single line of code. For users who prefer to write their own training loop, you can also fine-tune a 🤗 Transformers model in native PyTorch.',k,ne,B="At this point, you may need to restart your notebook or execute the following code to free some memory:",re,E,oe,F,H="Next, manually postprocess <code>tokenized_dataset</code> to prepare it for training.",he,x,v,ue,N="Remove the <code>text</code> column because the model does not accept raw text as an input:",ge,V,z,ae,ye,A="Rename the <code>label</code> column to <code>labels</code> because the model expects the argument to be named <code>labels</code>:",se,X,Te,U,P,ie="Set the format of the dataset to return PyTorch tensors instead of lists:",L,K,_,Me,pe="Then create a smaller subset of the dataset as previously shown to speed up the fine-tuning:",Q,D,C,we,le,G,$e="Create a <code>DataLoader</code> for your training and test datasets so you can iterate over batches of data:",O,I,be,me,Y="Load your model with the number of expected labels:",ee,W,de,q,te,Z,ce='Create an optimizer and learning rate scheduler to fine-tune the model. Let’s use the <a href="https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html" rel="nofollow"><code>AdamW</code></a> optimizer from PyTorch:',l,c,S,ke,ve='Create the default learning rate scheduler from <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a>:',Ze,je,n,f,Re="Lastly, specify <code>device</code> to use a GPU if you have access to one. Otherwise, training on a CPU may take several hours instead of a couple of minutes.",_e,Ce,We,Ue,a,m,Ne="Great, now you are ready to train! 🥳",Xe,Fe,Ae,xe,st='To keep track of your training progress, use the <a href="https://tqdm.github.io/" rel="nofollow">tqdm</a> library to add a progress bar over the number of training steps:',Le,Ve,Qe,ze,qe,Ye,lt='Just like how you added an evaluation function to <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a>, you need to do the same when you write your own training loop. But instead of calculating and reporting the metric at the end of each epoch, this time you’ll accumulate all the batches with <code>add_batch</code> and calculate the metric at the very end.',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}}),V=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}}),we=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"}}),c=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}}),Ue=new tt({props:{$$slots:{default:[wt]},$$scope:{ctx:fe}}}),Fe=new Je({props:{title:"Training loop",local:"training-loop",headingTag:"h3"}}),Ve=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}}),ze=new Je({props:{title:"Evaluate",local:"evaluate",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(){g(i.$$.fragment),T=r(),p=h("p"),p.innerHTML=J,k=r(),ne=h("p"),ne.textContent=B,re=r(),g(E.$$.fragment),oe=r(),F=h("p"),F.innerHTML=H,he=r(),x=h("ol"),v=h("li"),ue=h("p"),ue.innerHTML=N,ge=r(),g(V.$$.fragment),z=r(),ae=h("li"),ye=h("p"),ye.innerHTML=A,se=r(),g(X.$$.fragment),Te=r(),U=h("li"),P=h("p"),P.textContent=ie,L=r(),g(K.$$.fragment),_=r(),Me=h("p"),Me.textContent=pe,Q=r(),g(D.$$.fragment),C=r(),g(we.$$.fragment),le=r(),G=h("p"),G.innerHTML=$e,O=r(),g(I.$$.fragment),be=r(),me=h("p"),me.textContent=Y,ee=r(),g(W.$$.fragment),de=r(),g(q.$$.fragment),te=r(),Z=h("p"),Z.innerHTML=ce,l=r(),g(c.$$.fragment),S=r(),ke=h("p"),ke.innerHTML=ve,Ze=r(),g(je.$$.fragment),n=r(),f=h("p"),f.innerHTML=Re,_e=r(),g(Ce.$$.fragment),We=r(),g(Ue.$$.fragment),a=r(),m=h("p"),m.textContent=Ne,Xe=r(),g(Fe.$$.fragment),Ae=r(),xe=h("p"),xe.innerHTML=st,Le=r(),g(Ve.$$.fragment),Qe=r(),g(ze.$$.fragment),qe=r(),Ye=h("p"),Ye.innerHTML=lt,Se=r(),g(Be.$$.fragment)},l(e){y(i.$$.fragment,e),T=o(e),p=u(e,"P",{"data-svelte-h":!0}),j(p)!=="svelte-1ts4vwu"&&(p.innerHTML=J),k=o(e),ne=u(e,"P",{"data-svelte-h":!0}),j(ne)!=="svelte-1fj5odk"&&(ne.textContent=B),re=o(e),y(E.$$.fragment,e),oe=o(e),F=u(e,"P",{"data-svelte-h":!0}),j(F)!=="svelte-x8g0ya"&&(F.innerHTML=H),he=o(e),x=u(e,"OL",{});var d=Ie(x);v=u(d,"LI",{});var Ee=Ie(v);ue=u(Ee,"P",{"data-svelte-h":!0}),j(ue)!=="svelte-1rn013d"&&(ue.innerHTML=N),ge=o(Ee),y(V.$$.fragment,Ee),Ee.forEach(t),z=o(d),ae=u(d,"LI",{});var Ke=Ie(ae);ye=u(Ke,"P",{"data-svelte-h":!0}),j(ye)!=="svelte-14b1mbw"&&(ye.innerHTML=A),se=o(Ke),y(X.$$.fragment,Ke),Ke.forEach(t),Te=o(d),U=u(d,"LI",{});var De=Ie(U);P=u(De,"P",{"data-svelte-h":!0}),j(P)!=="svelte-1pkf4ls"&&(P.textContent=ie),L=o(De),y(K.$$.fragment,De),De.forEach(t),d.forEach(t),_=o(e),Me=u(e,"P",{"data-svelte-h":!0}),j(Me)!=="svelte-suyard"&&(Me.textContent=pe),Q=o(e),y(D.$$.fragment,e),C=o(e),y(we.$$.fragment,e),le=o(e),G=u(e,"P",{"data-svelte-h":!0}),j(G)!=="svelte-ukozu9"&&(G.innerHTML=$e),O=o(e),y(I.$$.fragment,e),be=o(e),me=u(e,"P",{"data-svelte-h":!0}),j(me)!=="svelte-i6qo3i"&&(me.textContent=Y),ee=o(e),y(W.$$.fragment,e),de=o(e),y(q.$$.fragment,e),te=o(e),Z=u(e,"P",{"data-svelte-h":!0}),j(Z)!=="svelte-co458k"&&(Z.innerHTML=ce),l=o(e),y(c.$$.fragment,e),S=o(e),ke=u(e,"P",{"data-svelte-h":!0}),j(ke)!=="svelte-1zl710"&&(ke.innerHTML=ve),Ze=o(e),y(je.$$.fragment,e),n=o(e),f=u(e,"P",{"data-svelte-h":!0}),j(f)!=="svelte-3xf3g0"&&(f.innerHTML=Re),_e=o(e),y(Ce.$$.fragment,e),We=o(e),y(Ue.$$.fragment,e),a=o(e),m=u(e,"P",{"data-svelte-h":!0}),j(m)!=="svelte-a8vh6y"&&(m.textContent=Ne),Xe=o(e),y(Fe.$$.fragment,e),Ae=o(e),xe=u(e,"P",{"data-svelte-h":!0}),j(xe)!=="svelte-s0vw3u"&&(xe.innerHTML=st),Le=o(e),y(Ve.$$.fragment,e),Qe=o(e),y(ze.$$.fragment,e),qe=o(e),Ye=u(e,"P",{"data-svelte-h":!0}),j(Ye)!=="svelte-h8cpa6"&&(Ye.innerHTML=lt),Se=o(e),y(Be.$$.fragment,e)},m(e,d){M(i,e,d),s(e,T,d),s(e,p,d),s(e,k,d),s(e,ne,d),s(e,re,d),M(E,e,d),s(e,oe,d),s(e,F,d),s(e,he,d),s(e,x,d),Ge(x,v),Ge(v,ue),Ge(v,ge),M(V,v,null),Ge(x,z),Ge(x,ae),Ge(ae,ye),Ge(ae,se),M(X,ae,null),Ge(x,Te),Ge(x,U),Ge(U,P),Ge(U,L),M(K,U,null),s(e,_,d),s(e,Me,d),s(e,Q,d),M(D,e,d),s(e,C,d),M(we,e,d),s(e,le,d),s(e,G,d),s(e,O,d),M(I,e,d),s(e,be,d),s(e,me,d),s(e,ee,d),M(W,e,d),s(e,de,d),M(q,e,d),s(e,te,d),s(e,Z,d),s(e,l,d),M(c,e,d),s(e,S,d),s(e,ke,d),s(e,Ze,d),M(je,e,d),s(e,n,d),s(e,f,d),s(e,_e,d),M(Ce,e,d),s(e,We,d),M(Ue,e,d),s(e,a,d),s(e,m,d),s(e,Xe,d),M(Fe,e,d),s(e,Ae,d),s(e,xe,d),s(e,Le,d),M(Ve,e,d),s(e,Qe,d),M(ze,e,d),s(e,qe,d),s(e,Ye,d),s(e,Se,d),M(Be,e,d),Pe=!0},p(e,d){const Ee={};d&2&&(Ee.$$scope={dirty:d,ctx:e}),Ue.$set(Ee)},i(e){Pe||(w(i.$$.fragment,e),w(E.$$.fragment,e),w(V.$$.fragment,e),w(X.$$.fragment,e),w(K.$$.fragment,e),w(D.$$.fragment,e),w(we.$$.fragment,e),w(I.$$.fragment,e),w(W.$$.fragment,e),w(q.$$.fragment,e),w(c.$$.fragment,e),w(je.$$.fragment,e),w(Ce.$$.fragment,e),w(Ue.$$.fragment,e),w(Fe.$$.fragment,e),w(Ve.$$.fragment,e),w(ze.$$.fragment,e),w(Be.$$.fragment,e),Pe=!0)},o(e){$(i.$$.fragment,e),$(E.$$.fragment,e),$(V.$$.fragment,e),$(X.$$.fragment,e),$(K.$$.fragment,e),$(D.$$.fragment,e),$(we.$$.fragment,e),$(I.$$.fragment,e),$(W.$$.fragment,e),$(q.$$.fragment,e),$(c.$$.fragment,e),$(je.$$.fragment,e),$(Ce.$$.fragment,e),$(Ue.$$.fragment,e),$(Fe.$$.fragment,e),$(Ve.$$.fragment,e),$(ze.$$.fragment,e),$(Be.$$.fragment,e),Pe=!1},d(e){e&&(t(T),t(p),t(k),t(ne),t(re),t(oe),t(F),t(he),t(x),t(_),t(Me),t(Q),t(C),t(le),t(G),t(O),t(be),t(me),t(ee),t(de),t(te),t(Z),t(l),t(S),t(ke),t(Ze),t(n),t(f),t(_e),t(We),t(a),t(m),t(Xe),t(Ae),t(xe),t(Le),t(Qe),t(qe),t(Ye),t(Se)),b(i,e),b(E,e),b(V),b(X),b(K),b(D,e),b(we,e),b(I,e),b(W,e),b(q,e),b(c,e),b(je,e),b(Ce,e),b(Ue,e),b(Fe,e),b(Ve,e),b(ze,e),b(Be,e)}}}function bt(fe){let i,T;return i=new at({props:{$$slots:{default:[$t]},$$scope:{ctx:fe}}}),{c(){g(i.$$.fragment)},l(p){y(i.$$.fragment,p)},m(p,J){M(i,p,J),T=!0},p(p,J){const k={};J&2&&(k.$$scope={dirty:J,ctx:p}),i.$set(k)},i(p){T||(w(i.$$.fragment,p),T=!0)},o(p){$(i.$$.fragment,p),T=!1},d(p){b(i,p)}}}function jt(fe){let i,T,p,J,k,ne,B,re,E,oe="There are significant benefits to using a pretrained model. It reduces computation costs, your carbon footprint, and allows you to use state-of-the-art models without having to train one from scratch. 🤗 Transformers provides access to thousands of pretrained models for a wide range of tasks. When you use a pretrained model, you train it on a dataset specific to your task. This is known as fine-tuning, an incredibly powerful training technique. In this tutorial, you will fine-tune a pretrained model with a deep learning framework of your choice:",F,H,he='<li>Fine-tune a pretrained model with 🤗 Transformers <a href="/docs/transformers/pr_29969/en/main_classes/trainer#transformers.Trainer">Trainer</a>.</li> <li>Fine-tune a pretrained model in TensorFlow with Keras.</li> <li>Fine-tune a pretrained model in native PyTorch.</li>',x,v,ue,N,ge,V,z,ae,ye="Before you can fine-tune a pretrained model, download a dataset and prepare it for training. The previous tutorial showed you how to process data for training, and now you get an opportunity to put those skills to the test!",A,se,X='Begin by loading the <a href="https://huggingface.co/datasets/yelp_review_full" rel="nofollow">Yelp Reviews</a> dataset:',Te,U,P,ie,L='As you now know, you need a tokenizer to process the text and include a padding and truncation strategy to handle any variable sequence lengths. To process your dataset in one step, use 🤗 Datasets <a href="https://huggingface.co/docs/datasets/process#map" rel="nofollow"><code>map</code></a> method to apply a preprocessing function over the entire dataset:',K,_,Me,pe,Q="If you like, you can create a smaller subset of the full dataset to fine-tune on to reduce the time it takes:",D,C,we,le,G,$e,O,I,be=`At this point, you should follow the section corresponding to the framework you want to use. You can use the links
in the right sidebar to jump to the one you want - and if you want to hide all of the content for a given framework,
just use the button at the top-right of that framework’s block!`,me,Y,ee,W,de,q,te,Z,ce,l,c,S,ke,ve,Ze="For more fine-tuning examples, refer to:",je,n,f=`<li><p><a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">🤗 Transformers Examples</a> includes scripts
to train common NLP tasks in PyTorch and TensorFlow.</p></li> <li><p><a href="notebooks">🤗 Transformers Notebooks</a> contains various notebooks on how to fine-tune a model for specific tasks in PyTorch and TensorFlow.</p></li>`,Re,_e,Ce,We,Ue;return k=new Je({props:{title:"Fine-tune a pretrained model",local:"fine-tune-a-pretrained-model",headingTag:"h1"}}),B=new dt({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/training.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/training.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/training.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/training.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/training.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/training.ipynb"}]}}),N=new Je({props:{title:"Prepare a dataset",local:"prepare-a-dataset",headingTag:"h2"}}),V=new Oe({props:{id:"_BZearw7f0w"}}),U=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}}),$e=new Je({props:{title:"Train",local:"train",headingTag:"h2"}}),Y=new nt({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Mt],pytorch:[ut]},$$scope:{ctx:fe}}}),q=new Je({props:{title:"Train in native PyTorch",local:"train-in-native-pytorch",headingTag:"h2"}}),Z=new nt({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[bt]},$$scope:{ctx:fe}}}),S=new Je({props:{title:"Additional resources",local:"additional-resources",headingTag:"h2"}}),_e=new ct({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/en/training.md"}}),{c(){i=h("meta"),T=r(),p=h("p"),J=r(),g(k.$$.fragment),ne=r(),g(B.$$.fragment),re=r(),E=h("p"),E.textContent=oe,F=r(),H=h("ul"),H.innerHTML=he,x=r(),v=h("a"),ue=r(),g(N.$$.fragment),ge=r(),g(V.$$.fragment),z=r(),ae=h("p"),ae.textContent=ye,A=r(),se=h("p"),se.innerHTML=X,Te=r(),g(U.$$.fragment),P=r(),ie=h("p"),ie.innerHTML=L,K=r(),g(_.$$.fragment),Me=r(),pe=h("p"),pe.textContent=Q,D=r(),g(C.$$.fragment),we=r(),le=h("a"),G=r(),g($e.$$.fragment),O=r(),I=h("p"),I.textContent=be,me=r(),g(Y.$$.fragment),ee=r(),W=h("a"),de=r(),g(q.$$.fragment),te=r(),g(Z.$$.fragment),ce=r(),l=h("a"),c=r(),g(S.$$.fragment),ke=r(),ve=h("p"),ve.textContent=Ze,je=r(),n=h("ul"),n.innerHTML=f,Re=r(),g(_e.$$.fragment),Ce=r(),We=h("p"),this.h()},l(a){const m=mt("svelte-u9bgzb",document.head);i=u(m,"META",{name:!0,content:!0}),m.forEach(t),T=o(a),p=u(a,"P",{}),Ie(p).forEach(t),J=o(a),y(k.$$.fragment,a),ne=o(a),y(B.$$.fragment,a),re=o(a),E=u(a,"P",{"data-svelte-h":!0}),j(E)!=="svelte-12akn4f"&&(E.textContent=oe),F=o(a),H=u(a,"UL",{"data-svelte-h":!0}),j(H)!=="svelte-1as6dnx"&&(H.innerHTML=he),x=o(a),v=u(a,"A",{id:!0}),Ie(v).forEach(t),ue=o(a),y(N.$$.fragment,a),ge=o(a),y(V.$$.fragment,a),z=o(a),ae=u(a,"P",{"data-svelte-h":!0}),j(ae)!=="svelte-1tgiufu"&&(ae.textContent=ye),A=o(a),se=u(a,"P",{"data-svelte-h":!0}),j(se)!=="svelte-1vrtere"&&(se.innerHTML=X),Te=o(a),y(U.$$.fragment,a),P=o(a),ie=u(a,"P",{"data-svelte-h":!0}),j(ie)!=="svelte-apcbq6"&&(ie.innerHTML=L),K=o(a),y(_.$$.fragment,a),Me=o(a),pe=u(a,"P",{"data-svelte-h":!0}),j(pe)!=="svelte-1wp411h"&&(pe.textContent=Q),D=o(a),y(C.$$.fragment,a),we=o(a),le=u(a,"A",{id:!0}),Ie(le).forEach(t),G=o(a),y($e.$$.fragment,a),O=o(a),I=u(a,"P",{"data-svelte-h":!0}),j(I)!=="svelte-4poo92"&&(I.textContent=be),me=o(a),y(Y.$$.fragment,a),ee=o(a),W=u(a,"A",{id:!0}),Ie(W).forEach(t),de=o(a),y(q.$$.fragment,a),te=o(a),y(Z.$$.fragment,a),ce=o(a),l=u(a,"A",{id:!0}),Ie(l).forEach(t),c=o(a),y(S.$$.fragment,a),ke=o(a),ve=u(a,"P",{"data-svelte-h":!0}),j(ve)!=="svelte-1keox1g"&&(ve.textContent=Ze),je=o(a),n=u(a,"UL",{"data-svelte-h":!0}),j(n)!=="svelte-4e1ihs"&&(n.innerHTML=f),Re=o(a),y(_e.$$.fragment,a),Ce=o(a),We=u(a,"P",{}),Ie(We).forEach(t),this.h()},h(){He(i,"name","hf:doc:metadata"),He(i,"content",Tt),He(v,"id","data-processing"),He(le,"id","trainer"),He(W,"id","pytorch_native"),He(l,"id","additional-resources")},m(a,m){Ge(document.head,i),s(a,T,m),s(a,p,m),s(a,J,m),M(k,a,m),s(a,ne,m),M(B,a,m),s(a,re,m),s(a,E,m),s(a,F,m),s(a,H,m),s(a,x,m),s(a,v,m),s(a,ue,m),M(N,a,m),s(a,ge,m),M(V,a,m),s(a,z,m),s(a,ae,m),s(a,A,m),s(a,se,m),s(a,Te,m),M(U,a,m),s(a,P,m),s(a,ie,m),s(a,K,m),M(_,a,m),s(a,Me,m),s(a,pe,m),s(a,D,m),M(C,a,m),s(a,we,m),s(a,le,m),s(a,G,m),M($e,a,m),s(a,O,m),s(a,I,m),s(a,me,m),M(Y,a,m),s(a,ee,m),s(a,W,m),s(a,de,m),M(q,a,m),s(a,te,m),M(Z,a,m),s(a,ce,m),s(a,l,m),s(a,c,m),M(S,a,m),s(a,ke,m),s(a,ve,m),s(a,je,m),s(a,n,m),s(a,Re,m),M(_e,a,m),s(a,Ce,m),s(a,We,m),Ue=!0},p(a,[m]){const Ne={};m&2&&(Ne.$$scope={dirty:m,ctx:a}),Y.$set(Ne);const Xe={};m&2&&(Xe.$$scope={dirty:m,ctx:a}),Z.$set(Xe)},i(a){Ue||(w(k.$$.fragment,a),w(B.$$.fragment,a),w(N.$$.fragment,a),w(V.$$.fragment,a),w(U.$$.fragment,a),w(_.$$.fragment,a),w(C.$$.fragment,a),w($e.$$.fragment,a),w(Y.$$.fragment,a),w(q.$$.fragment,a),w(Z.$$.fragment,a),w(S.$$.fragment,a),w(_e.$$.fragment,a),Ue=!0)},o(a){$(k.$$.fragment,a),$(B.$$.fragment,a),$(N.$$.fragment,a),$(V.$$.fragment,a),$(U.$$.fragment,a),$(_.$$.fragment,a),$(C.$$.fragment,a),$($e.$$.fragment,a),$(Y.$$.fragment,a),$(q.$$.fragment,a),$(Z.$$.fragment,a),$(S.$$.fragment,a),$(_e.$$.fragment,a),Ue=!1},d(a){a&&(t(T),t(p),t(J),t(ne),t(re),t(E),t(F),t(H),t(x),t(v),t(ue),t(ge),t(z),t(ae),t(A),t(se),t(Te),t(P),t(ie),t(K),t(Me),t(pe),t(D),t(we),t(le),t(G),t(O),t(I),t(me),t(ee),t(W),t(de),t(te),t(ce),t(l),t(c),t(ke),t(ve),t(je),t(n),t(Re),t(Ce),t(We)),t(i),b(k,a),b(B,a),b(N,a),b(V,a),b(U,a),b(_,a),b(C,a),b($e,a),b(Y,a),b(q,a),b(Z,a),b(S,a),b(_e,a)}}}const Tt='{"title":"Fine-tune a pretrained model","local":"fine-tune-a-pretrained-model","sections":[{"title":"Prepare a dataset","local":"prepare-a-dataset","sections":[],"depth":2},{"title":"Train","local":"train","sections":[],"depth":2},{"title":"Train with PyTorch Trainer","local":"train-with-pytorch-trainer","sections":[{"title":"Training hyperparameters","local":"training-hyperparameters","sections":[],"depth":3},{"title":"Evaluate","local":"evaluate","sections":[],"depth":3},{"title":"Trainer","local":"trainer","sections":[],"depth":3}],"depth":2},{"title":"Train a TensorFlow model with Keras","local":"train-a-tensorflow-model-with-keras","sections":[{"title":"Loading data for Keras","local":"loading-data-for-keras","sections":[],"depth":3},{"title":"Loading data as a tf.data.Dataset","local":"loading-data-as-a-tfdatadataset","sections":[],"depth":3}],"depth":2},{"title":"Train in native PyTorch","local":"train-in-native-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":"Training loop","local":"training-loop","sections":[],"depth":3},{"title":"Evaluate","local":"evaluate","sections":[],"depth":3}],"depth":2},{"title":"Additional resources","local":"additional-resources","sections":[],"depth":2}],"depth":1}';function Jt(fe){return ot(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Rt extends it{constructor(i){super(),pt(this,i,Jt,jt,rt,{})}}export{Rt as component};
