import{s as rt,o as ot,n as Oe}from"../chunks/scheduler.9bc65507.js";import{S as it,i as pt,g as h,s as r,r as y,A as mt,h as u,f as t,c as o,j as Ce,u as g,x as j,k as He,y as _e,a as s,v as M,d as w,t as $,w as b}from"../chunks/index.707bf1b6.js";import{T as et}from"../chunks/Tip.c2ecdbf4.js";import{Y as De}from"../chunks/Youtube.e1129c6f.js";import{C as W}from"../chunks/CodeBlock.54a9f38d.js";import{D as dt}from"../chunks/DocNotebookDropdown.41f65cb5.js";import{F as nt,M as tt}from"../chunks/Markdown.fef84341.js";import{H as Je}from"../chunks/Heading.342b1fa6.js";function ct(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:Oe,d(p){p&&t(i)}}}function ft(fe){let i,T,p,J,k,ne='🤗 Transformers provides a <a href="/docs/transformers/main/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/main/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.',Y,re,H='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,B,E,he,x,v,ue,N,ye='Next, create a <a href="/docs/transformers/main/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.',z,V,ae="Specify where to save the checkpoints from your training:",ge,A,se,X,Te,U,P='<a href="/docs/transformers/main/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/main/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>evaluation_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/main/en/main_classes/trainer#transformers.Trainer">Trainer</a> object with your model, training arguments, training and test datasets, and evaluation function:',F,ee,R,de,q='Then fine-tune your model by calling <a href="/docs/transformers/main/en/main_classes/trainer#transformers.Trainer.train">train()</a>:',te,Z,ce;return i=new De({props:{id:"nvBXf7s7vTI"}}),p=new Je({props:{title:"Train with PyTorch Trainer",local:"train-with-pytorch-trainer",headingTag:"h2"}}),B=new W({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUyMmJlcnQtYmFzZS1jYXNlZCUyMiUyQyUyMG51bV9sYWJlbHMlM0Q1KQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">&quot;bert-base-cased&quot;</span>, num_labels=<span class="hljs-number">5</span>)`,wrap:!1}}),he=new et({props:{$$slots:{default:[ct]},$$scope:{ctx:fe}}}),v=new Je({props:{title:"Training hyperparameters",local:"training-hyperparameters",headingTag:"h3"}}),A=new W({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 W({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 W({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 W({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:"Trainer",local:"trainer",headingTag:"h3"}}),ee=new W({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 W({props:{code:"dHJhaW5lci50cmFpbigp",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()',wrap:!1}}),{c(){y(i.$$.fragment),T=r(),y(p.$$.fragment),J=r(),k=h("p"),k.innerHTML=ne,Y=r(),re=h("p"),re.innerHTML=H,oe=r(),y(B.$$.fragment),E=r(),y(he.$$.fragment),x=r(),y(v.$$.fragment),ue=r(),N=h("p"),N.innerHTML=ye,z=r(),V=h("p"),V.textContent=ae,ge=r(),y(A.$$.fragment),se=r(),y(X.$$.fragment),Te=r(),U=h("p"),U.innerHTML=P,ie=r(),y(L.$$.fragment),K=r(),_=h("p"),_.innerHTML=Me,pe=r(),y(Q.$$.fragment),D=r(),C=h("p"),C.innerHTML=we,le=r(),y(G.$$.fragment),$e=r(),y(O.$$.fragment),I=r(),be=h("p"),be.innerHTML=me,F=r(),y(ee.$$.fragment),R=r(),de=h("p"),de.innerHTML=q,te=r(),y(Z.$$.fragment)},l(l){g(i.$$.fragment,l),T=o(l),g(p.$$.fragment,l),J=o(l),k=u(l,"P",{"data-svelte-h":!0}),j(k)!=="svelte-12q4od1"&&(k.innerHTML=ne),Y=o(l),re=u(l,"P",{"data-svelte-h":!0}),j(re)!=="svelte-3dzt1s"&&(re.innerHTML=H),oe=o(l),g(B.$$.fragment,l),E=o(l),g(he.$$.fragment,l),x=o(l),g(v.$$.fragment,l),ue=o(l),N=u(l,"P",{"data-svelte-h":!0}),j(N)!=="svelte-82ke4l"&&(N.innerHTML=ye),z=o(l),V=u(l,"P",{"data-svelte-h":!0}),j(V)!=="svelte-1v0ty1x"&&(V.textContent=ae),ge=o(l),g(A.$$.fragment,l),se=o(l),g(X.$$.fragment,l),Te=o(l),U=u(l,"P",{"data-svelte-h":!0}),j(U)!=="svelte-8s65hs"&&(U.innerHTML=P),ie=o(l),g(L.$$.fragment,l),K=o(l),_=u(l,"P",{"data-svelte-h":!0}),j(_)!=="svelte-1j1ti7k"&&(_.innerHTML=Me),pe=o(l),g(Q.$$.fragment,l),D=o(l),C=u(l,"P",{"data-svelte-h":!0}),j(C)!=="svelte-v41bk4"&&(C.innerHTML=we),le=o(l),g(G.$$.fragment,l),$e=o(l),g(O.$$.fragment,l),I=o(l),be=u(l,"P",{"data-svelte-h":!0}),j(be)!=="svelte-dwmjb3"&&(be.innerHTML=me),F=o(l),g(ee.$$.fragment,l),R=o(l),de=u(l,"P",{"data-svelte-h":!0}),j(de)!=="svelte-6i42t1"&&(de.innerHTML=q),te=o(l),g(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,Y,c),s(l,re,c),s(l,oe,c),M(B,l,c),s(l,E,c),M(he,l,c),s(l,x,c),M(v,l,c),s(l,ue,c),s(l,N,c),s(l,z,c),s(l,V,c),s(l,ge,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,F,c),M(ee,l,c),s(l,R,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(B.$$.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),$(B.$$.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(Y),t(re),t(oe),t(E),t(x),t(ue),t(N),t(z),t(V),t(ge),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(F),t(R),t(de),t(te)),b(i,l),b(p,l),b(B,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 ht(fe){let i,T;return i=new tt({props:{$$slots:{default:[ft]},$$scope:{ctx:fe}}}),{c(){y(i.$$.fragment)},l(p){g(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 ut(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:Oe,d(p){p&&t(i)}}}function yt(fe){let i,T,p,J,k,ne,Y,re="You can also train 🤗 Transformers models in TensorFlow with the Keras API!",H,oe,B,E,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,ye,z,V,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!`,ge,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/main/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/main/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,F,ee,R,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 De({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"}}),ye=new W({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 W({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJiZXJ0LWJhc2UtY2FzZWQlMjIpJTBBdG9rZW5pemVkX2RhdGElMjAlM0QlMjB0b2tlbml6ZXIoZGF0YXNldCU1QiUyMnNlbnRlbmNlJTIyJTVEJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJucCUyMiUyQyUyMHBhZGRpbmclM0RUcnVlKSUwQSUyMyUyMFRva2VuaXplciUyMHJldHVybnMlMjBhJTIwQmF0Y2hFbmNvZGluZyUyQyUyMGJ1dCUyMHdlJTIwY29udmVydCUyMHRoYXQlMjB0byUyMGElMjBkaWN0JTIwZm9yJTIwS2VyYXMlMEF0b2tlbml6ZWRfZGF0YSUyMCUzRCUyMGRpY3QodG9rZW5pemVkX2RhdGEpJTBBJTBBbGFiZWxzJTIwJTNEJTIwbnAuYXJyYXkoZGF0YXNldCU1QiUyMmxhYmVsJTIyJTVEKSUyMCUyMCUyMyUyMExhYmVsJTIwaXMlMjBhbHJlYWR5JTIwYW4lMjBhcnJheSUyMG9mJTIwMCUyMGFuZCUyMDE=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

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

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

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

model.fit(tokenized_data, labels)`,wrap:!1}}),L=new et({props:{$$slots:{default:[ut]},$$scope:{ctx:fe}}}),Q=new Je({props:{title:"Loading data as a tf.data.Dataset",local:"loading-data-as-a-tfdatadataset",headingTag:"h3"}}),F=new W({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 W({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 W({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(),y(p.$$.fragment),J=r(),y(k.$$.fragment),ne=r(),Y=h("p"),Y.textContent=re,H=r(),y(oe.$$.fragment),B=r(),E=h("p"),E.textContent=he,x=r(),v=h("p"),v.innerHTML=ue,N=r(),y(ye.$$.fragment),z=r(),V=h("p"),V.textContent=ae,ge=r(),y(A.$$.fragment),se=r(),X=h("p"),X.innerHTML=Te,U=r(),y(P.$$.fragment),ie=r(),y(L.$$.fragment),K=r(),_=h("p"),_.textContent=Me,pe=r(),y(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(),y(F.$$.fragment),ee=r(),R=h("p"),R.textContent=de,q=r(),y(te.$$.fragment),Z=r(),ce=h("p"),ce.innerHTML=l,c=r(),S=h("p"),S.innerHTML=ke,ve=r(),y(Ze.$$.fragment),this.h()},l(n){i=u(n,"A",{id:!0}),Ce(i).forEach(t),T=o(n),g(p.$$.fragment,n),J=o(n),g(k.$$.fragment,n),ne=o(n),Y=u(n,"P",{"data-svelte-h":!0}),j(Y)!=="svelte-pfehij"&&(Y.textContent=re),H=o(n),g(oe.$$.fragment,n),B=o(n),E=u(n,"P",{"data-svelte-h":!0}),j(E)!=="svelte-1koz3e5"&&(E.textContent=he),x=o(n),v=u(n,"P",{"data-svelte-h":!0}),j(v)!=="svelte-12jkaap"&&(v.innerHTML=ue),N=o(n),g(ye.$$.fragment,n),z=o(n),V=u(n,"P",{"data-svelte-h":!0}),j(V)!=="svelte-1t3pc5r"&&(V.textContent=ae),ge=o(n),g(A.$$.fragment,n),se=o(n),X=u(n,"P",{"data-svelte-h":!0}),j(X)!=="svelte-t3abgj"&&(X.innerHTML=Te),U=o(n),g(P.$$.fragment,n),ie=o(n),g(L.$$.fragment,n),K=o(n),_=u(n,"P",{"data-svelte-h":!0}),j(_)!=="svelte-janu8e"&&(_.textContent=Me),pe=o(n),g(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-1rdbjwk"&&(G.innerHTML=$e),O=o(n),I=u(n,"P",{"data-svelte-h":!0}),j(I)!=="svelte-14ngvpu"&&(I.innerHTML=be),me=o(n),g(F.$$.fragment,n),ee=o(n),R=u(n,"P",{"data-svelte-h":!0}),j(R)!=="svelte-1n8nrqv"&&(R.textContent=de),q=o(n),g(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),g(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,Y,f),s(n,H,f),M(oe,n,f),s(n,B,f),s(n,E,f),s(n,x,f),s(n,v,f),s(n,N,f),M(ye,n,f),s(n,z,f),s(n,V,f),s(n,ge,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(F,n,f),s(n,ee,f),s(n,R,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 Ge={};f&2&&(Ge.$$scope={dirty:f,ctx:n}),L.$set(Ge)},i(n){je||(w(p.$$.fragment,n),w(k.$$.fragment,n),w(oe.$$.fragment,n),w(ye.$$.fragment,n),w(A.$$.fragment,n),w(P.$$.fragment,n),w(L.$$.fragment,n),w(Q.$$.fragment,n),w(F.$$.fragment,n),w(te.$$.fragment,n),w(Ze.$$.fragment,n),je=!0)},o(n){$(p.$$.fragment,n),$(k.$$.fragment,n),$(oe.$$.fragment,n),$(ye.$$.fragment,n),$(A.$$.fragment,n),$(P.$$.fragment,n),$(L.$$.fragment,n),$(Q.$$.fragment,n),$(F.$$.fragment,n),$(te.$$.fragment,n),$(Ze.$$.fragment,n),je=!1},d(n){n&&(t(i),t(T),t(J),t(ne),t(Y),t(H),t(B),t(E),t(x),t(v),t(N),t(z),t(V),t(ge),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(R),t(q),t(Z),t(ce),t(c),t(S),t(ve)),b(p,n),b(k,n),b(oe,n),b(ye,n),b(A,n),b(P,n),b(L,n),b(Q,n),b(F,n),b(te,n),b(Ze,n)}}}function gt(fe){let i,T;return i=new tt({props:{$$slots:{default:[yt]},$$scope:{ctx:fe}}}),{c(){y(i.$$.fragment)},l(p){g(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 Mt(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:Oe,d(p){p&&t(i)}}}function wt(fe){let i,T,p,J='<a href="/docs/transformers/main/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,Y="At this point, you may need to restart your notebook or execute the following code to free some memory:",re,H,oe,B,E="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:",ye,z,V,ae,ge,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,F="Load your model with the number of expected labels:",ee,R,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/main/en/main_classes/trainer#transformers.Trainer">Trainer</a>:',Ze,je,n,f,Ge="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.",Ie,Ue,a,m,Re,We,at="Great, now you are ready to train! 🥳",Ee,Xe,Ne,Be,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:',Ae,xe,Le,ze,Qe,Ve,lt='Just like how you added an evaluation function to <a href="/docs/transformers/main/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.',qe,Fe,Se;return i=new De({props:{id:"Dh9CL8fyG80"}}),H=new W({props:{code:"ZGVsJTIwbW9kZWwlMEFkZWwlMjB0cmFpbmVyJTBBdG9yY2guY3VkYS5lbXB0eV9jYWNoZSgp",highlighted:`<span class="hljs-keyword">del</span> model
<span class="hljs-keyword">del</span> trainer
torch.cuda.empty_cache()`,wrap:!1}}),z=new W({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 W({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 W({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 W({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 W({props:{code:"ZnJvbSUyMHRvcmNoLnV0aWxzLmRhdGElMjBpbXBvcnQlMjBEYXRhTG9hZGVyJTBBJTBBdHJhaW5fZGF0YWxvYWRlciUyMCUzRCUyMERhdGFMb2FkZXIoc21hbGxfdHJhaW5fZGF0YXNldCUyQyUyMHNodWZmbGUlM0RUcnVlJTJDJTIwYmF0Y2hfc2l6ZSUzRDgpJTBBZXZhbF9kYXRhbG9hZGVyJTIwJTNEJTIwRGF0YUxvYWRlcihzbWFsbF9ldmFsX2RhdGFzZXQlMkMlMjBiYXRjaF9zaXplJTNEOCk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader

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

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">&quot;bert-base-cased&quot;</span>, num_labels=<span class="hljs-number">5</span>)`,wrap:!1}}),q=new Je({props:{title:"Optimizer and learning rate scheduler",local:"optimizer-and-learning-rate-scheduler",headingTag:"h3"}}),c=new W({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 W({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}}),Ue=new W({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}}),m=new et({props:{$$slots:{default:[Mt]},$$scope:{ctx:fe}}}),Xe=new Je({props:{title:"Training loop",local:"training-loop",headingTag:"h3"}}),xe=new W({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"}}),Fe=new W({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(){y(i.$$.fragment),T=r(),p=h("p"),p.innerHTML=J,k=r(),ne=h("p"),ne.textContent=Y,re=r(),y(H.$$.fragment),oe=r(),B=h("p"),B.innerHTML=E,he=r(),x=h("ol"),v=h("li"),ue=h("p"),ue.innerHTML=N,ye=r(),y(z.$$.fragment),V=r(),ae=h("li"),ge=h("p"),ge.innerHTML=A,se=r(),y(X.$$.fragment),Te=r(),U=h("li"),P=h("p"),P.textContent=ie,L=r(),y(K.$$.fragment),_=r(),Me=h("p"),Me.textContent=pe,Q=r(),y(D.$$.fragment),C=r(),y(we.$$.fragment),le=r(),G=h("p"),G.innerHTML=$e,O=r(),y(I.$$.fragment),be=r(),me=h("p"),me.textContent=F,ee=r(),y(R.$$.fragment),de=r(),y(q.$$.fragment),te=r(),Z=h("p"),Z.innerHTML=ce,l=r(),y(c.$$.fragment),S=r(),ke=h("p"),ke.innerHTML=ve,Ze=r(),y(je.$$.fragment),n=r(),f=h("p"),f.innerHTML=Ge,Ie=r(),y(Ue.$$.fragment),a=r(),y(m.$$.fragment),Re=r(),We=h("p"),We.textContent=at,Ee=r(),y(Xe.$$.fragment),Ne=r(),Be=h("p"),Be.innerHTML=st,Ae=r(),y(xe.$$.fragment),Le=r(),y(ze.$$.fragment),Qe=r(),Ve=h("p"),Ve.innerHTML=lt,qe=r(),y(Fe.$$.fragment)},l(e){g(i.$$.fragment,e),T=o(e),p=u(e,"P",{"data-svelte-h":!0}),j(p)!=="svelte-1cjy5np"&&(p.innerHTML=J),k=o(e),ne=u(e,"P",{"data-svelte-h":!0}),j(ne)!=="svelte-1fj5odk"&&(ne.textContent=Y),re=o(e),g(H.$$.fragment,e),oe=o(e),B=u(e,"P",{"data-svelte-h":!0}),j(B)!=="svelte-x8g0ya"&&(B.innerHTML=E),he=o(e),x=u(e,"OL",{});var d=Ce(x);v=u(d,"LI",{});var Ye=Ce(v);ue=u(Ye,"P",{"data-svelte-h":!0}),j(ue)!=="svelte-1rn013d"&&(ue.innerHTML=N),ye=o(Ye),g(z.$$.fragment,Ye),Ye.forEach(t),V=o(d),ae=u(d,"LI",{});var Pe=Ce(ae);ge=u(Pe,"P",{"data-svelte-h":!0}),j(ge)!=="svelte-14b1mbw"&&(ge.innerHTML=A),se=o(Pe),g(X.$$.fragment,Pe),Pe.forEach(t),Te=o(d),U=u(d,"LI",{});var Ke=Ce(U);P=u(Ke,"P",{"data-svelte-h":!0}),j(P)!=="svelte-1pkf4ls"&&(P.textContent=ie),L=o(Ke),g(K.$$.fragment,Ke),Ke.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),g(D.$$.fragment,e),C=o(e),g(we.$$.fragment,e),le=o(e),G=u(e,"P",{"data-svelte-h":!0}),j(G)!=="svelte-ukozu9"&&(G.innerHTML=$e),O=o(e),g(I.$$.fragment,e),be=o(e),me=u(e,"P",{"data-svelte-h":!0}),j(me)!=="svelte-i6qo3i"&&(me.textContent=F),ee=o(e),g(R.$$.fragment,e),de=o(e),g(q.$$.fragment,e),te=o(e),Z=u(e,"P",{"data-svelte-h":!0}),j(Z)!=="svelte-co458k"&&(Z.innerHTML=ce),l=o(e),g(c.$$.fragment,e),S=o(e),ke=u(e,"P",{"data-svelte-h":!0}),j(ke)!=="svelte-fnk0uv"&&(ke.innerHTML=ve),Ze=o(e),g(je.$$.fragment,e),n=o(e),f=u(e,"P",{"data-svelte-h":!0}),j(f)!=="svelte-3xf3g0"&&(f.innerHTML=Ge),Ie=o(e),g(Ue.$$.fragment,e),a=o(e),g(m.$$.fragment,e),Re=o(e),We=u(e,"P",{"data-svelte-h":!0}),j(We)!=="svelte-a8vh6y"&&(We.textContent=at),Ee=o(e),g(Xe.$$.fragment,e),Ne=o(e),Be=u(e,"P",{"data-svelte-h":!0}),j(Be)!=="svelte-s0vw3u"&&(Be.innerHTML=st),Ae=o(e),g(xe.$$.fragment,e),Le=o(e),g(ze.$$.fragment,e),Qe=o(e),Ve=u(e,"P",{"data-svelte-h":!0}),j(Ve)!=="svelte-2fm41l"&&(Ve.innerHTML=lt),qe=o(e),g(Fe.$$.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(H,e,d),s(e,oe,d),s(e,B,d),s(e,he,d),s(e,x,d),_e(x,v),_e(v,ue),_e(v,ye),M(z,v,null),_e(x,V),_e(x,ae),_e(ae,ge),_e(ae,se),M(X,ae,null),_e(x,Te),_e(x,U),_e(U,P),_e(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(R,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,Ie,d),M(Ue,e,d),s(e,a,d),M(m,e,d),s(e,Re,d),s(e,We,d),s(e,Ee,d),M(Xe,e,d),s(e,Ne,d),s(e,Be,d),s(e,Ae,d),M(xe,e,d),s(e,Le,d),M(ze,e,d),s(e,Qe,d),s(e,Ve,d),s(e,qe,d),M(Fe,e,d),Se=!0},p(e,d){const Ye={};d&2&&(Ye.$$scope={dirty:d,ctx:e}),m.$set(Ye)},i(e){Se||(w(i.$$.fragment,e),w(H.$$.fragment,e),w(z.$$.fragment,e),w(X.$$.fragment,e),w(K.$$.fragment,e),w(D.$$.fragment,e),w(we.$$.fragment,e),w(I.$$.fragment,e),w(R.$$.fragment,e),w(q.$$.fragment,e),w(c.$$.fragment,e),w(je.$$.fragment,e),w(Ue.$$.fragment,e),w(m.$$.fragment,e),w(Xe.$$.fragment,e),w(xe.$$.fragment,e),w(ze.$$.fragment,e),w(Fe.$$.fragment,e),Se=!0)},o(e){$(i.$$.fragment,e),$(H.$$.fragment,e),$(z.$$.fragment,e),$(X.$$.fragment,e),$(K.$$.fragment,e),$(D.$$.fragment,e),$(we.$$.fragment,e),$(I.$$.fragment,e),$(R.$$.fragment,e),$(q.$$.fragment,e),$(c.$$.fragment,e),$(je.$$.fragment,e),$(Ue.$$.fragment,e),$(m.$$.fragment,e),$(Xe.$$.fragment,e),$(xe.$$.fragment,e),$(ze.$$.fragment,e),$(Fe.$$.fragment,e),Se=!1},d(e){e&&(t(T),t(p),t(k),t(ne),t(re),t(oe),t(B),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(Ie),t(a),t(Re),t(We),t(Ee),t(Ne),t(Be),t(Ae),t(Le),t(Qe),t(Ve),t(qe)),b(i,e),b(H,e),b(z),b(X),b(K),b(D,e),b(we,e),b(I,e),b(R,e),b(q,e),b(c,e),b(je,e),b(Ue,e),b(m,e),b(Xe,e),b(xe,e),b(ze,e),b(Fe,e)}}}function $t(fe){let i,T;return i=new tt({props:{$$slots:{default:[wt]},$$scope:{ctx:fe}}}),{c(){y(i.$$.fragment)},l(p){g(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 bt(fe){let i,T,p,J,k,ne,Y,re,H,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:",B,E,he='<li>Fine-tune a pretrained model with 🤗 Transformers <a href="/docs/transformers/main/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,ye,z,V,ae,ge="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,F,ee,R,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>`,Ge,Ie,Ue;return k=new Je({props:{title:"Fine-tune a pretrained model",local:"fine-tune-a-pretrained-model",headingTag:"h1"}}),Y=new dt({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/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"}}),z=new De({props:{id:"_BZearw7f0w"}}),U=new W({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 W({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJiZXJ0LWJhc2UtY2FzZWQlMjIpJTBBJTBBJTBBZGVmJTIwdG9rZW5pemVfZnVuY3Rpb24oZXhhbXBsZXMpJTNBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwdG9rZW5pemVyKGV4YW1wbGVzJTVCJTIydGV4dCUyMiU1RCUyQyUyMHBhZGRpbmclM0QlMjJtYXhfbGVuZ3RoJTIyJTJDJTIwdHJ1bmNhdGlvbiUzRFRydWUpJTBBJTBBJTBBdG9rZW5pemVkX2RhdGFzZXRzJTIwJTNEJTIwZGF0YXNldC5tYXAodG9rZW5pemVfZnVuY3Rpb24lMkMlMjBiYXRjaGVkJTNEVHJ1ZSk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

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


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


<span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets = dataset.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>)`,wrap:!1}}),C=new W({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"}}),F=new nt({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[gt],pytorch:[ht]},$$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:[$t]},$$scope:{ctx:fe}}}),S=new Je({props:{title:"Additional resources",local:"additional-resources",headingTag:"h2"}}),{c(){i=h("meta"),T=r(),p=h("p"),J=r(),y(k.$$.fragment),ne=r(),y(Y.$$.fragment),re=r(),H=h("p"),H.textContent=oe,B=r(),E=h("ul"),E.innerHTML=he,x=r(),v=h("a"),ue=r(),y(N.$$.fragment),ye=r(),y(z.$$.fragment),V=r(),ae=h("p"),ae.textContent=ge,A=r(),se=h("p"),se.innerHTML=X,Te=r(),y(U.$$.fragment),P=r(),ie=h("p"),ie.innerHTML=L,K=r(),y(_.$$.fragment),Me=r(),pe=h("p"),pe.textContent=Q,D=r(),y(C.$$.fragment),we=r(),le=h("a"),G=r(),y($e.$$.fragment),O=r(),I=h("p"),I.textContent=be,me=r(),y(F.$$.fragment),ee=r(),R=h("a"),de=r(),y(q.$$.fragment),te=r(),y(Z.$$.fragment),ce=r(),l=h("a"),c=r(),y(S.$$.fragment),ke=r(),ve=h("p"),ve.textContent=Ze,je=r(),n=h("ul"),n.innerHTML=f,Ge=r(),Ie=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",{}),Ce(p).forEach(t),J=o(a),g(k.$$.fragment,a),ne=o(a),g(Y.$$.fragment,a),re=o(a),H=u(a,"P",{"data-svelte-h":!0}),j(H)!=="svelte-12akn4f"&&(H.textContent=oe),B=o(a),E=u(a,"UL",{"data-svelte-h":!0}),j(E)!=="svelte-1g53p56"&&(E.innerHTML=he),x=o(a),v=u(a,"A",{id:!0}),Ce(v).forEach(t),ue=o(a),g(N.$$.fragment,a),ye=o(a),g(z.$$.fragment,a),V=o(a),ae=u(a,"P",{"data-svelte-h":!0}),j(ae)!=="svelte-1tgiufu"&&(ae.textContent=ge),A=o(a),se=u(a,"P",{"data-svelte-h":!0}),j(se)!=="svelte-1vrtere"&&(se.innerHTML=X),Te=o(a),g(U.$$.fragment,a),P=o(a),ie=u(a,"P",{"data-svelte-h":!0}),j(ie)!=="svelte-apcbq6"&&(ie.innerHTML=L),K=o(a),g(_.$$.fragment,a),Me=o(a),pe=u(a,"P",{"data-svelte-h":!0}),j(pe)!=="svelte-1wp411h"&&(pe.textContent=Q),D=o(a),g(C.$$.fragment,a),we=o(a),le=u(a,"A",{id:!0}),Ce(le).forEach(t),G=o(a),g($e.$$.fragment,a),O=o(a),I=u(a,"P",{"data-svelte-h":!0}),j(I)!=="svelte-4poo92"&&(I.textContent=be),me=o(a),g(F.$$.fragment,a),ee=o(a),R=u(a,"A",{id:!0}),Ce(R).forEach(t),de=o(a),g(q.$$.fragment,a),te=o(a),g(Z.$$.fragment,a),ce=o(a),l=u(a,"A",{id:!0}),Ce(l).forEach(t),c=o(a),g(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),Ge=o(a),Ie=u(a,"P",{}),Ce(Ie).forEach(t),this.h()},h(){He(i,"name","hf:doc:metadata"),He(i,"content",jt),He(v,"id","data-processing"),He(le,"id","trainer"),He(R,"id","pytorch_native"),He(l,"id","additional-resources")},m(a,m){_e(document.head,i),s(a,T,m),s(a,p,m),s(a,J,m),M(k,a,m),s(a,ne,m),M(Y,a,m),s(a,re,m),s(a,H,m),s(a,B,m),s(a,E,m),s(a,x,m),s(a,v,m),s(a,ue,m),M(N,a,m),s(a,ye,m),M(z,a,m),s(a,V,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(F,a,m),s(a,ee,m),s(a,R,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,Ge,m),s(a,Ie,m),Ue=!0},p(a,[m]){const Re={};m&2&&(Re.$$scope={dirty:m,ctx:a}),F.$set(Re);const We={};m&2&&(We.$$scope={dirty:m,ctx:a}),Z.$set(We)},i(a){Ue||(w(k.$$.fragment,a),w(Y.$$.fragment,a),w(N.$$.fragment,a),w(z.$$.fragment,a),w(U.$$.fragment,a),w(_.$$.fragment,a),w(C.$$.fragment,a),w($e.$$.fragment,a),w(F.$$.fragment,a),w(q.$$.fragment,a),w(Z.$$.fragment,a),w(S.$$.fragment,a),Ue=!0)},o(a){$(k.$$.fragment,a),$(Y.$$.fragment,a),$(N.$$.fragment,a),$(z.$$.fragment,a),$(U.$$.fragment,a),$(_.$$.fragment,a),$(C.$$.fragment,a),$($e.$$.fragment,a),$(F.$$.fragment,a),$(q.$$.fragment,a),$(Z.$$.fragment,a),$(S.$$.fragment,a),Ue=!1},d(a){a&&(t(T),t(p),t(J),t(ne),t(re),t(H),t(B),t(E),t(x),t(v),t(ue),t(ye),t(V),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(R),t(de),t(te),t(ce),t(l),t(c),t(ke),t(ve),t(je),t(n),t(Ge),t(Ie)),t(i),b(k,a),b(Y,a),b(N,a),b(z,a),b(U,a),b(_,a),b(C,a),b($e,a),b(F,a),b(q,a),b(Z,a),b(S,a)}}}const jt='{"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 Tt(fe){return ot(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class It extends it{constructor(i){super(),pt(this,i,Tt,bt,rt,{})}}export{It as component};
