import{s as rt,o as ot,n as et}from"../chunks/scheduler.25b97de1.js";import{S as it,i as pt,g as f,s as r,r as y,A as mt,h as u,f as t,c as o,j as Ie,u as g,x as j,k as Ee,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 G}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 ke,E as ct}from"../chunks/EditOnGithub.91d95064.js";function ht(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=f("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 ft(fe){let i,T,p,J,k,re='🤗 Transformers provides a <a href="/docs/transformers/pr_30530/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_30530/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.',L,oe,Q='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.',ie,W,se="By default, the weights are loaded in full precision (torch.float32) regardless of the actual data type the weights are stored in such as torch.float16. Set <code>torch_dtype=&quot;auto&quot;</code> to load the weights in the data type defined in a model’s <code>config.json</code> file to automatically load the most memory-optimal data type.",ve,Z,I,S,pe,E,X,N,le='Next, create a <a href="/docs/transformers/pr_30530/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.',ue,q,ye="Specify where to save the checkpoints from your training:",P,ge,F,R,me,K,de='<a href="/docs/transformers/pr_30530/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_30530/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:',V,D,ce,O,he="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):",x,ee,ne,_,Je="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:",Me,v,Ue,z,A,te,B='Create a <a href="/docs/transformers/pr_30530/en/main_classes/trainer#transformers.Trainer">Trainer</a> object with your model, training arguments, training and test datasets, and evaluation function:',je,C,ae,U,$e='Then fine-tune your model by calling <a href="/docs/transformers/pr_30530/en/main_classes/trainer#transformers.Trainer.train">train()</a>:',we,Y,H;return i=new Oe({props:{id:"nvBXf7s7vTI"}}),p=new ke({props:{title:"Train with PyTorch Trainer",local:"train-with-pytorch-trainer",headingTag:"h2"}}),Z=new G({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZS1iZXJ0JTJGYmVydC1iYXNlLWNhc2VkJTIyJTJDJTIwbnVtX2xhYmVscyUzRDUlMkMlMjB0b3JjaF9kdHlwZSUzRCUyMmF1dG8lMjIp",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>, torch_dtype=<span class="hljs-string">&quot;auto&quot;</span>)`,wrap:!1}}),S=new tt({props:{$$slots:{default:[ht]},$$scope:{ctx:fe}}}),E=new ke({props:{title:"Training hyperparameters",local:"training-hyperparameters",headingTag:"h3"}}),ge=new G({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}}),R=new ke({props:{title:"Evaluate",local:"evaluate",headingTag:"h3"}}),D=new G({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}}),ee=new G({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}}),v=new G({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}}),z=new ke({props:{title:"Trainer",local:"trainer",headingTag:"h3"}}),C=new G({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}}),Y=new G({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=f("p"),k.innerHTML=re,L=r(),oe=f("p"),oe.innerHTML=Q,ie=r(),W=f("p"),W.innerHTML=se,ve=r(),y(Z.$$.fragment),I=r(),y(S.$$.fragment),pe=r(),y(E.$$.fragment),X=r(),N=f("p"),N.innerHTML=le,ue=r(),q=f("p"),q.textContent=ye,P=r(),y(ge.$$.fragment),F=r(),y(R.$$.fragment),me=r(),K=f("p"),K.innerHTML=de,V=r(),y(D.$$.fragment),ce=r(),O=f("p"),O.innerHTML=he,x=r(),y(ee.$$.fragment),ne=r(),_=f("p"),_.innerHTML=Je,Me=r(),y(v.$$.fragment),Ue=r(),y(z.$$.fragment),A=r(),te=f("p"),te.innerHTML=B,je=r(),y(C.$$.fragment),ae=r(),U=f("p"),U.innerHTML=$e,we=r(),y(Y.$$.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-qo6nbj"&&(k.innerHTML=re),L=o(l),oe=u(l,"P",{"data-svelte-h":!0}),j(oe)!=="svelte-olagb0"&&(oe.innerHTML=Q),ie=o(l),W=u(l,"P",{"data-svelte-h":!0}),j(W)!=="svelte-9fdmcv"&&(W.innerHTML=se),ve=o(l),g(Z.$$.fragment,l),I=o(l),g(S.$$.fragment,l),pe=o(l),g(E.$$.fragment,l),X=o(l),N=u(l,"P",{"data-svelte-h":!0}),j(N)!=="svelte-1pqf1ho"&&(N.innerHTML=le),ue=o(l),q=u(l,"P",{"data-svelte-h":!0}),j(q)!=="svelte-1v0ty1x"&&(q.textContent=ye),P=o(l),g(ge.$$.fragment,l),F=o(l),g(R.$$.fragment,l),me=o(l),K=u(l,"P",{"data-svelte-h":!0}),j(K)!=="svelte-1ngvgga"&&(K.innerHTML=de),V=o(l),g(D.$$.fragment,l),ce=o(l),O=u(l,"P",{"data-svelte-h":!0}),j(O)!=="svelte-1j1ti7k"&&(O.innerHTML=he),x=o(l),g(ee.$$.fragment,l),ne=o(l),_=u(l,"P",{"data-svelte-h":!0}),j(_)!=="svelte-1lmipg"&&(_.innerHTML=Je),Me=o(l),g(v.$$.fragment,l),Ue=o(l),g(z.$$.fragment,l),A=o(l),te=u(l,"P",{"data-svelte-h":!0}),j(te)!=="svelte-16r31kq"&&(te.innerHTML=B),je=o(l),g(C.$$.fragment,l),ae=o(l),U=u(l,"P",{"data-svelte-h":!0}),j(U)!=="svelte-1a8l5k8"&&(U.innerHTML=$e),we=o(l),g(Y.$$.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,L,c),s(l,oe,c),s(l,ie,c),s(l,W,c),s(l,ve,c),M(Z,l,c),s(l,I,c),M(S,l,c),s(l,pe,c),M(E,l,c),s(l,X,c),s(l,N,c),s(l,ue,c),s(l,q,c),s(l,P,c),M(ge,l,c),s(l,F,c),M(R,l,c),s(l,me,c),s(l,K,c),s(l,V,c),M(D,l,c),s(l,ce,c),s(l,O,c),s(l,x,c),M(ee,l,c),s(l,ne,c),s(l,_,c),s(l,Me,c),M(v,l,c),s(l,Ue,c),M(z,l,c),s(l,A,c),s(l,te,c),s(l,je,c),M(C,l,c),s(l,ae,c),s(l,U,c),s(l,we,c),M(Y,l,c),H=!0},p(l,c){const be={};c&2&&(be.$$scope={dirty:c,ctx:l}),S.$set(be)},i(l){H||(w(i.$$.fragment,l),w(p.$$.fragment,l),w(Z.$$.fragment,l),w(S.$$.fragment,l),w(E.$$.fragment,l),w(ge.$$.fragment,l),w(R.$$.fragment,l),w(D.$$.fragment,l),w(ee.$$.fragment,l),w(v.$$.fragment,l),w(z.$$.fragment,l),w(C.$$.fragment,l),w(Y.$$.fragment,l),H=!0)},o(l){$(i.$$.fragment,l),$(p.$$.fragment,l),$(Z.$$.fragment,l),$(S.$$.fragment,l),$(E.$$.fragment,l),$(ge.$$.fragment,l),$(R.$$.fragment,l),$(D.$$.fragment,l),$(ee.$$.fragment,l),$(v.$$.fragment,l),$(z.$$.fragment,l),$(C.$$.fragment,l),$(Y.$$.fragment,l),H=!1},d(l){l&&(t(T),t(J),t(k),t(L),t(oe),t(ie),t(W),t(ve),t(I),t(pe),t(X),t(N),t(ue),t(q),t(P),t(F),t(me),t(K),t(V),t(ce),t(O),t(x),t(ne),t(_),t(Me),t(Ue),t(A),t(te),t(je),t(ae),t(U),t(we)),b(i,l),b(p,l),b(Z,l),b(S,l),b(E,l),b(ge,l),b(R,l),b(D,l),b(ee,l),b(v,l),b(z,l),b(C,l),b(Y,l)}}}function ut(fe){let i,T;return i=new at({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 yt(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=f("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 gt(fe){let i,T,p,J,k,re,L,oe="You can also train 🤗 Transformers models in TensorFlow with the Keras API!",Q,ie,W,se,ve=`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.`,Z,I,S=`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.`,pe,E,X,N,le=`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!`,ue,q,ye,P,ge='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:',F,R,me,K,de,V,D=`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!`,ce,O,he,x,ee=`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:`,ne,_,Je=`<li><a href="/docs/transformers/pr_30530/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>`,Me,v,Ue=`Before you can use <a href="/docs/transformers/pr_30530/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:`,z,A,te,B,je=`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.`,C,ae,U,$e,we=`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.`,Y,H,l="Once you’ve created a <code>tf.data.Dataset</code>, you can compile and fit the model as before:",c,be,Te;return p=new Oe({props:{id:"rnTGBy2ax1c"}}),k=new ke({props:{title:"Train a TensorFlow model with Keras",local:"train-a-tensorflow-model-with-keras",headingTag:"h2"}}),ie=new ke({props:{title:"Loading data for Keras",local:"loading-data-for-keras",headingTag:"h3"}}),E=new G({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}}),q=new G({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}}),R=new G({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}}),K=new tt({props:{$$slots:{default:[yt]},$$scope:{ctx:fe}}}),O=new ke({props:{title:"Loading data as a tf.data.Dataset",local:"loading-data-as-a-tfdatadataset",headingTag:"h3"}}),A=new G({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}}),ae=new G({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}}),be=new G({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=f("a"),T=r(),y(p.$$.fragment),J=r(),y(k.$$.fragment),re=r(),L=f("p"),L.textContent=oe,Q=r(),y(ie.$$.fragment),W=r(),se=f("p"),se.textContent=ve,Z=r(),I=f("p"),I.innerHTML=S,pe=r(),y(E.$$.fragment),X=r(),N=f("p"),N.textContent=le,ue=r(),y(q.$$.fragment),ye=r(),P=f("p"),P.innerHTML=ge,F=r(),y(R.$$.fragment),me=r(),y(K.$$.fragment),de=r(),V=f("p"),V.textContent=D,ce=r(),y(O.$$.fragment),he=r(),x=f("p"),x.innerHTML=ee,ne=r(),_=f("ul"),_.innerHTML=Je,Me=r(),v=f("p"),v.innerHTML=Ue,z=r(),y(A.$$.fragment),te=r(),B=f("p"),B.textContent=je,C=r(),y(ae.$$.fragment),U=r(),$e=f("p"),$e.innerHTML=we,Y=r(),H=f("p"),H.innerHTML=l,c=r(),y(be.$$.fragment),this.h()},l(n){i=u(n,"A",{id:!0}),Ie(i).forEach(t),T=o(n),g(p.$$.fragment,n),J=o(n),g(k.$$.fragment,n),re=o(n),L=u(n,"P",{"data-svelte-h":!0}),j(L)!=="svelte-pfehij"&&(L.textContent=oe),Q=o(n),g(ie.$$.fragment,n),W=o(n),se=u(n,"P",{"data-svelte-h":!0}),j(se)!=="svelte-1koz3e5"&&(se.textContent=ve),Z=o(n),I=u(n,"P",{"data-svelte-h":!0}),j(I)!=="svelte-12jkaap"&&(I.innerHTML=S),pe=o(n),g(E.$$.fragment,n),X=o(n),N=u(n,"P",{"data-svelte-h":!0}),j(N)!=="svelte-1t3pc5r"&&(N.textContent=le),ue=o(n),g(q.$$.fragment,n),ye=o(n),P=u(n,"P",{"data-svelte-h":!0}),j(P)!=="svelte-t3abgj"&&(P.innerHTML=ge),F=o(n),g(R.$$.fragment,n),me=o(n),g(K.$$.fragment,n),de=o(n),V=u(n,"P",{"data-svelte-h":!0}),j(V)!=="svelte-janu8e"&&(V.textContent=D),ce=o(n),g(O.$$.fragment,n),he=o(n),x=u(n,"P",{"data-svelte-h":!0}),j(x)!=="svelte-1ar86wm"&&(x.innerHTML=ee),ne=o(n),_=u(n,"UL",{"data-svelte-h":!0}),j(_)!=="svelte-iok0zb"&&(_.innerHTML=Je),Me=o(n),v=u(n,"P",{"data-svelte-h":!0}),j(v)!=="svelte-qv6fyf"&&(v.innerHTML=Ue),z=o(n),g(A.$$.fragment,n),te=o(n),B=u(n,"P",{"data-svelte-h":!0}),j(B)!=="svelte-1n8nrqv"&&(B.textContent=je),C=o(n),g(ae.$$.fragment,n),U=o(n),$e=u(n,"P",{"data-svelte-h":!0}),j($e)!=="svelte-1hn9om"&&($e.innerHTML=we),Y=o(n),H=u(n,"P",{"data-svelte-h":!0}),j(H)!=="svelte-4nybjx"&&(H.innerHTML=l),c=o(n),g(be.$$.fragment,n),this.h()},h(){Ee(i,"id","keras")},m(n,h){s(n,i,h),s(n,T,h),M(p,n,h),s(n,J,h),M(k,n,h),s(n,re,h),s(n,L,h),s(n,Q,h),M(ie,n,h),s(n,W,h),s(n,se,h),s(n,Z,h),s(n,I,h),s(n,pe,h),M(E,n,h),s(n,X,h),s(n,N,h),s(n,ue,h),M(q,n,h),s(n,ye,h),s(n,P,h),s(n,F,h),M(R,n,h),s(n,me,h),M(K,n,h),s(n,de,h),s(n,V,h),s(n,ce,h),M(O,n,h),s(n,he,h),s(n,x,h),s(n,ne,h),s(n,_,h),s(n,Me,h),s(n,v,h),s(n,z,h),M(A,n,h),s(n,te,h),s(n,B,h),s(n,C,h),M(ae,n,h),s(n,U,h),s(n,$e,h),s(n,Y,h),s(n,H,h),s(n,c,h),M(be,n,h),Te=!0},p(n,h){const Re={};h&2&&(Re.$$scope={dirty:h,ctx:n}),K.$set(Re)},i(n){Te||(w(p.$$.fragment,n),w(k.$$.fragment,n),w(ie.$$.fragment,n),w(E.$$.fragment,n),w(q.$$.fragment,n),w(R.$$.fragment,n),w(K.$$.fragment,n),w(O.$$.fragment,n),w(A.$$.fragment,n),w(ae.$$.fragment,n),w(be.$$.fragment,n),Te=!0)},o(n){$(p.$$.fragment,n),$(k.$$.fragment,n),$(ie.$$.fragment,n),$(E.$$.fragment,n),$(q.$$.fragment,n),$(R.$$.fragment,n),$(K.$$.fragment,n),$(O.$$.fragment,n),$(A.$$.fragment,n),$(ae.$$.fragment,n),$(be.$$.fragment,n),Te=!1},d(n){n&&(t(i),t(T),t(J),t(re),t(L),t(Q),t(W),t(se),t(Z),t(I),t(pe),t(X),t(N),t(ue),t(ye),t(P),t(F),t(me),t(de),t(V),t(ce),t(he),t(x),t(ne),t(_),t(Me),t(v),t(z),t(te),t(B),t(C),t(U),t($e),t(Y),t(H),t(c)),b(p,n),b(k,n),b(ie,n),b(E,n),b(q,n),b(R,n),b(K,n),b(O,n),b(A,n),b(ae,n),b(be,n)}}}function Mt(fe){let i,T;return i=new at({props:{$$slots:{default:[gt]},$$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 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=f("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_30530/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,re,L="At this point, you may need to restart your notebook or execute the following code to free some memory:",oe,Q,ie,W,se="Next, manually postprocess <code>tokenized_dataset</code> to prepare it for training.",ve,Z,I,S,pe="Remove the <code>text</code> column because the model does not accept raw text as an input:",E,X,N,le,ue,q="Rename the <code>label</code> column to <code>labels</code> because the model expects the argument to be named <code>labels</code>:",ye,P,ge,F,R,me="Set the format of the dataset to return PyTorch tensors instead of lists:",K,de,V,D,ce="Then create a smaller subset of the dataset as previously shown to speed up the fine-tuning:",O,he,x,ee,ne,_,Je="Create a <code>DataLoader</code> for your training and test datasets so you can iterate over batches of data:",Me,v,Ue,z,A="Load your model with the number of expected labels:",te,B,je,C,ae,U,$e='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:',we,Y,H,l,c='Create the default learning rate scheduler from <a href="/docs/transformers/pr_30530/en/main_classes/trainer#transformers.Trainer">Trainer</a>:',be,Te,n,h,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,Ze,a,m,Ne="Great, now you are ready to train! 🥳",Xe,Fe,Ae,Ve,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,xe,Qe,ze,Se,Be,lt='Just like how you added an evaluation function to <a href="/docs/transformers/pr_30530/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,Ye,Pe;return i=new Oe({props:{id:"Dh9CL8fyG80"}}),Q=new G({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUudXRpbHMubWVtb3J5JTIwaW1wb3J0JTIwY2xlYXJfZGV2aWNlX2NhY2hlJTBBZGVsJTIwbW9kZWwlMEFkZWwlMjB0cmFpbmVyJTBBY2xlYXJfZGV2aWNlX2NhY2hlKCk=",highlighted:`<span class="hljs-keyword">from</span> accelerate.utils.memory <span class="hljs-keyword">import</span> clear_device_cache
<span class="hljs-keyword">del</span> model
<span class="hljs-keyword">del</span> trainer
clear_device_cache()`,wrap:!1}}),X=new G({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}}),P=new G({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}}),de=new G({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}}),he=new G({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}}),ee=new ke({props:{title:"DataLoader",local:"dataloader",headingTag:"h3"}}),v=new G({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}}),B=new G({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}}),C=new ke({props:{title:"Optimizer and learning rate scheduler",local:"optimizer-and-learning-rate-scheduler",headingTag:"h3"}}),Y=new G({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}}),Te=new G({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 G({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwYWNjZWxlcmF0ZS50ZXN0X3V0aWxzLnRlc3RpbmclMjBpbXBvcnQlMjBnZXRfYmFja2VuZCUwQSUwQWRldmljZSUyQyUyMF8lMkMlMjBfJTIwJTNEJTIwZ2V0X2JhY2tlbmQoKSUyMCUyMyUyMGF1dG9tYXRpY2FsbHklMjBkZXRlY3RzJTIwdGhlJTIwdW5kZXJseWluZyUyMGRldmljZSUyMHR5cGUlMjAoQ1VEQSUyQyUyMENQVSUyQyUyMFhQVSUyQyUyME1QUyUyQyUyMGV0Yy4pJTBBbW9kZWwudG8oZGV2aWNlKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> accelerate.test_utils.testing <span class="hljs-keyword">import</span> get_backend

<span class="hljs-meta">&gt;&gt;&gt; </span>device, _, _ = get_backend() <span class="hljs-comment"># automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>model.to(device)`,wrap:!1}}),Ze=new tt({props:{$$slots:{default:[wt]},$$scope:{ctx:fe}}}),Fe=new ke({props:{title:"Training loop",local:"training-loop",headingTag:"h3"}}),xe=new G({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 ke({props:{title:"Evaluate",local:"evaluate",headingTag:"h3"}}),Ye=new G({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=f("p"),p.innerHTML=J,k=r(),re=f("p"),re.textContent=L,oe=r(),y(Q.$$.fragment),ie=r(),W=f("p"),W.innerHTML=se,ve=r(),Z=f("ol"),I=f("li"),S=f("p"),S.innerHTML=pe,E=r(),y(X.$$.fragment),N=r(),le=f("li"),ue=f("p"),ue.innerHTML=q,ye=r(),y(P.$$.fragment),ge=r(),F=f("li"),R=f("p"),R.textContent=me,K=r(),y(de.$$.fragment),V=r(),D=f("p"),D.textContent=ce,O=r(),y(he.$$.fragment),x=r(),y(ee.$$.fragment),ne=r(),_=f("p"),_.innerHTML=Je,Me=r(),y(v.$$.fragment),Ue=r(),z=f("p"),z.textContent=A,te=r(),y(B.$$.fragment),je=r(),y(C.$$.fragment),ae=r(),U=f("p"),U.innerHTML=$e,we=r(),y(Y.$$.fragment),H=r(),l=f("p"),l.innerHTML=c,be=r(),y(Te.$$.fragment),n=r(),h=f("p"),h.innerHTML=Re,_e=r(),y(Ce.$$.fragment),We=r(),y(Ze.$$.fragment),a=r(),m=f("p"),m.textContent=Ne,Xe=r(),y(Fe.$$.fragment),Ae=r(),Ve=f("p"),Ve.innerHTML=st,Le=r(),y(xe.$$.fragment),Qe=r(),y(ze.$$.fragment),Se=r(),Be=f("p"),Be.innerHTML=lt,qe=r(),y(Ye.$$.fragment)},l(e){g(i.$$.fragment,e),T=o(e),p=u(e,"P",{"data-svelte-h":!0}),j(p)!=="svelte-1vaxvu"&&(p.innerHTML=J),k=o(e),re=u(e,"P",{"data-svelte-h":!0}),j(re)!=="svelte-1fj5odk"&&(re.textContent=L),oe=o(e),g(Q.$$.fragment,e),ie=o(e),W=u(e,"P",{"data-svelte-h":!0}),j(W)!=="svelte-x8g0ya"&&(W.innerHTML=se),ve=o(e),Z=u(e,"OL",{});var d=Ie(Z);I=u(d,"LI",{});var He=Ie(I);S=u(He,"P",{"data-svelte-h":!0}),j(S)!=="svelte-1rn013d"&&(S.innerHTML=pe),E=o(He),g(X.$$.fragment,He),He.forEach(t),N=o(d),le=u(d,"LI",{});var Ke=Ie(le);ue=u(Ke,"P",{"data-svelte-h":!0}),j(ue)!=="svelte-14b1mbw"&&(ue.innerHTML=q),ye=o(Ke),g(P.$$.fragment,Ke),Ke.forEach(t),ge=o(d),F=u(d,"LI",{});var De=Ie(F);R=u(De,"P",{"data-svelte-h":!0}),j(R)!=="svelte-1pkf4ls"&&(R.textContent=me),K=o(De),g(de.$$.fragment,De),De.forEach(t),d.forEach(t),V=o(e),D=u(e,"P",{"data-svelte-h":!0}),j(D)!=="svelte-suyard"&&(D.textContent=ce),O=o(e),g(he.$$.fragment,e),x=o(e),g(ee.$$.fragment,e),ne=o(e),_=u(e,"P",{"data-svelte-h":!0}),j(_)!=="svelte-ukozu9"&&(_.innerHTML=Je),Me=o(e),g(v.$$.fragment,e),Ue=o(e),z=u(e,"P",{"data-svelte-h":!0}),j(z)!=="svelte-i6qo3i"&&(z.textContent=A),te=o(e),g(B.$$.fragment,e),je=o(e),g(C.$$.fragment,e),ae=o(e),U=u(e,"P",{"data-svelte-h":!0}),j(U)!=="svelte-co458k"&&(U.innerHTML=$e),we=o(e),g(Y.$$.fragment,e),H=o(e),l=u(e,"P",{"data-svelte-h":!0}),j(l)!=="svelte-1t437k"&&(l.innerHTML=c),be=o(e),g(Te.$$.fragment,e),n=o(e),h=u(e,"P",{"data-svelte-h":!0}),j(h)!=="svelte-3xf3g0"&&(h.innerHTML=Re),_e=o(e),g(Ce.$$.fragment,e),We=o(e),g(Ze.$$.fragment,e),a=o(e),m=u(e,"P",{"data-svelte-h":!0}),j(m)!=="svelte-a8vh6y"&&(m.textContent=Ne),Xe=o(e),g(Fe.$$.fragment,e),Ae=o(e),Ve=u(e,"P",{"data-svelte-h":!0}),j(Ve)!=="svelte-s0vw3u"&&(Ve.innerHTML=st),Le=o(e),g(xe.$$.fragment,e),Qe=o(e),g(ze.$$.fragment,e),Se=o(e),Be=u(e,"P",{"data-svelte-h":!0}),j(Be)!=="svelte-1q3e3be"&&(Be.innerHTML=lt),qe=o(e),g(Ye.$$.fragment,e)},m(e,d){M(i,e,d),s(e,T,d),s(e,p,d),s(e,k,d),s(e,re,d),s(e,oe,d),M(Q,e,d),s(e,ie,d),s(e,W,d),s(e,ve,d),s(e,Z,d),Ge(Z,I),Ge(I,S),Ge(I,E),M(X,I,null),Ge(Z,N),Ge(Z,le),Ge(le,ue),Ge(le,ye),M(P,le,null),Ge(Z,ge),Ge(Z,F),Ge(F,R),Ge(F,K),M(de,F,null),s(e,V,d),s(e,D,d),s(e,O,d),M(he,e,d),s(e,x,d),M(ee,e,d),s(e,ne,d),s(e,_,d),s(e,Me,d),M(v,e,d),s(e,Ue,d),s(e,z,d),s(e,te,d),M(B,e,d),s(e,je,d),M(C,e,d),s(e,ae,d),s(e,U,d),s(e,we,d),M(Y,e,d),s(e,H,d),s(e,l,d),s(e,be,d),M(Te,e,d),s(e,n,d),s(e,h,d),s(e,_e,d),M(Ce,e,d),s(e,We,d),M(Ze,e,d),s(e,a,d),s(e,m,d),s(e,Xe,d),M(Fe,e,d),s(e,Ae,d),s(e,Ve,d),s(e,Le,d),M(xe,e,d),s(e,Qe,d),M(ze,e,d),s(e,Se,d),s(e,Be,d),s(e,qe,d),M(Ye,e,d),Pe=!0},p(e,d){const He={};d&2&&(He.$$scope={dirty:d,ctx:e}),Ze.$set(He)},i(e){Pe||(w(i.$$.fragment,e),w(Q.$$.fragment,e),w(X.$$.fragment,e),w(P.$$.fragment,e),w(de.$$.fragment,e),w(he.$$.fragment,e),w(ee.$$.fragment,e),w(v.$$.fragment,e),w(B.$$.fragment,e),w(C.$$.fragment,e),w(Y.$$.fragment,e),w(Te.$$.fragment,e),w(Ce.$$.fragment,e),w(Ze.$$.fragment,e),w(Fe.$$.fragment,e),w(xe.$$.fragment,e),w(ze.$$.fragment,e),w(Ye.$$.fragment,e),Pe=!0)},o(e){$(i.$$.fragment,e),$(Q.$$.fragment,e),$(X.$$.fragment,e),$(P.$$.fragment,e),$(de.$$.fragment,e),$(he.$$.fragment,e),$(ee.$$.fragment,e),$(v.$$.fragment,e),$(B.$$.fragment,e),$(C.$$.fragment,e),$(Y.$$.fragment,e),$(Te.$$.fragment,e),$(Ce.$$.fragment,e),$(Ze.$$.fragment,e),$(Fe.$$.fragment,e),$(xe.$$.fragment,e),$(ze.$$.fragment,e),$(Ye.$$.fragment,e),Pe=!1},d(e){e&&(t(T),t(p),t(k),t(re),t(oe),t(ie),t(W),t(ve),t(Z),t(V),t(D),t(O),t(x),t(ne),t(_),t(Me),t(Ue),t(z),t(te),t(je),t(ae),t(U),t(we),t(H),t(l),t(be),t(n),t(h),t(_e),t(We),t(a),t(m),t(Xe),t(Ae),t(Ve),t(Le),t(Qe),t(Se),t(Be),t(qe)),b(i,e),b(Q,e),b(X),b(P),b(de),b(he,e),b(ee,e),b(v,e),b(B,e),b(C,e),b(Y,e),b(Te,e),b(Ce,e),b(Ze,e),b(Fe,e),b(xe,e),b(ze,e),b(Ye,e)}}}function bt(fe){let i,T;return i=new at({props:{$$slots:{default:[$t]},$$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 jt(fe){let i,T,p,J,k,re,L,oe,Q,ie="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:",W,se,ve='<li>Fine-tune a pretrained model with 🤗 Transformers <a href="/docs/transformers/pr_30530/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>',Z,I,S,pe,E,X,N,le,ue="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!",q,ye,P='Begin by loading the <a href="https://huggingface.co/datasets/yelp_review_full" rel="nofollow">Yelp Reviews</a> dataset:',ge,F,R,me,K='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:',de,V,D,ce,O="If you like, you can create a smaller subset of the full dataset to fine-tune on to reduce the time it takes:",he,x,ee,ne,_,Je,Me,v,Ue=`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!`,z,A,te,B,je,C,ae,U,$e,we,Y,H,l,c,be="For more fine-tuning examples, refer to:",Te,n,h=`<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,Ze;return k=new ke({props:{title:"Fine-tune a pretrained model",local:"fine-tune-a-pretrained-model",headingTag:"h1"}}),L=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"}]}}),pe=new ke({props:{title:"Prepare a dataset",local:"prepare-a-dataset",headingTag:"h2"}}),X=new Oe({props:{id:"_BZearw7f0w"}}),F=new G({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}}),V=new G({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}}),x=new G({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}}),Je=new ke({props:{title:"Train",local:"train",headingTag:"h2"}}),A=new nt({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Mt],pytorch:[ut]},$$scope:{ctx:fe}}}),C=new ke({props:{title:"Train in native PyTorch",local:"train-in-native-pytorch",headingTag:"h2"}}),U=new nt({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[bt]},$$scope:{ctx:fe}}}),H=new ke({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=f("meta"),T=r(),p=f("p"),J=r(),y(k.$$.fragment),re=r(),y(L.$$.fragment),oe=r(),Q=f("p"),Q.textContent=ie,W=r(),se=f("ul"),se.innerHTML=ve,Z=r(),I=f("a"),S=r(),y(pe.$$.fragment),E=r(),y(X.$$.fragment),N=r(),le=f("p"),le.textContent=ue,q=r(),ye=f("p"),ye.innerHTML=P,ge=r(),y(F.$$.fragment),R=r(),me=f("p"),me.innerHTML=K,de=r(),y(V.$$.fragment),D=r(),ce=f("p"),ce.textContent=O,he=r(),y(x.$$.fragment),ee=r(),ne=f("a"),_=r(),y(Je.$$.fragment),Me=r(),v=f("p"),v.textContent=Ue,z=r(),y(A.$$.fragment),te=r(),B=f("a"),je=r(),y(C.$$.fragment),ae=r(),y(U.$$.fragment),$e=r(),we=f("a"),Y=r(),y(H.$$.fragment),l=r(),c=f("p"),c.textContent=be,Te=r(),n=f("ul"),n.innerHTML=h,Re=r(),y(_e.$$.fragment),Ce=r(),We=f("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),g(k.$$.fragment,a),re=o(a),g(L.$$.fragment,a),oe=o(a),Q=u(a,"P",{"data-svelte-h":!0}),j(Q)!=="svelte-12akn4f"&&(Q.textContent=ie),W=o(a),se=u(a,"UL",{"data-svelte-h":!0}),j(se)!=="svelte-ebxrpt"&&(se.innerHTML=ve),Z=o(a),I=u(a,"A",{id:!0}),Ie(I).forEach(t),S=o(a),g(pe.$$.fragment,a),E=o(a),g(X.$$.fragment,a),N=o(a),le=u(a,"P",{"data-svelte-h":!0}),j(le)!=="svelte-1tgiufu"&&(le.textContent=ue),q=o(a),ye=u(a,"P",{"data-svelte-h":!0}),j(ye)!=="svelte-1vrtere"&&(ye.innerHTML=P),ge=o(a),g(F.$$.fragment,a),R=o(a),me=u(a,"P",{"data-svelte-h":!0}),j(me)!=="svelte-apcbq6"&&(me.innerHTML=K),de=o(a),g(V.$$.fragment,a),D=o(a),ce=u(a,"P",{"data-svelte-h":!0}),j(ce)!=="svelte-1wp411h"&&(ce.textContent=O),he=o(a),g(x.$$.fragment,a),ee=o(a),ne=u(a,"A",{id:!0}),Ie(ne).forEach(t),_=o(a),g(Je.$$.fragment,a),Me=o(a),v=u(a,"P",{"data-svelte-h":!0}),j(v)!=="svelte-4poo92"&&(v.textContent=Ue),z=o(a),g(A.$$.fragment,a),te=o(a),B=u(a,"A",{id:!0}),Ie(B).forEach(t),je=o(a),g(C.$$.fragment,a),ae=o(a),g(U.$$.fragment,a),$e=o(a),we=u(a,"A",{id:!0}),Ie(we).forEach(t),Y=o(a),g(H.$$.fragment,a),l=o(a),c=u(a,"P",{"data-svelte-h":!0}),j(c)!=="svelte-1keox1g"&&(c.textContent=be),Te=o(a),n=u(a,"UL",{"data-svelte-h":!0}),j(n)!=="svelte-4e1ihs"&&(n.innerHTML=h),Re=o(a),g(_e.$$.fragment,a),Ce=o(a),We=u(a,"P",{}),Ie(We).forEach(t),this.h()},h(){Ee(i,"name","hf:doc:metadata"),Ee(i,"content",Tt),Ee(I,"id","data-processing"),Ee(ne,"id","trainer"),Ee(B,"id","pytorch_native"),Ee(we,"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,re,m),M(L,a,m),s(a,oe,m),s(a,Q,m),s(a,W,m),s(a,se,m),s(a,Z,m),s(a,I,m),s(a,S,m),M(pe,a,m),s(a,E,m),M(X,a,m),s(a,N,m),s(a,le,m),s(a,q,m),s(a,ye,m),s(a,ge,m),M(F,a,m),s(a,R,m),s(a,me,m),s(a,de,m),M(V,a,m),s(a,D,m),s(a,ce,m),s(a,he,m),M(x,a,m),s(a,ee,m),s(a,ne,m),s(a,_,m),M(Je,a,m),s(a,Me,m),s(a,v,m),s(a,z,m),M(A,a,m),s(a,te,m),s(a,B,m),s(a,je,m),M(C,a,m),s(a,ae,m),M(U,a,m),s(a,$e,m),s(a,we,m),s(a,Y,m),M(H,a,m),s(a,l,m),s(a,c,m),s(a,Te,m),s(a,n,m),s(a,Re,m),M(_e,a,m),s(a,Ce,m),s(a,We,m),Ze=!0},p(a,[m]){const Ne={};m&2&&(Ne.$$scope={dirty:m,ctx:a}),A.$set(Ne);const Xe={};m&2&&(Xe.$$scope={dirty:m,ctx:a}),U.$set(Xe)},i(a){Ze||(w(k.$$.fragment,a),w(L.$$.fragment,a),w(pe.$$.fragment,a),w(X.$$.fragment,a),w(F.$$.fragment,a),w(V.$$.fragment,a),w(x.$$.fragment,a),w(Je.$$.fragment,a),w(A.$$.fragment,a),w(C.$$.fragment,a),w(U.$$.fragment,a),w(H.$$.fragment,a),w(_e.$$.fragment,a),Ze=!0)},o(a){$(k.$$.fragment,a),$(L.$$.fragment,a),$(pe.$$.fragment,a),$(X.$$.fragment,a),$(F.$$.fragment,a),$(V.$$.fragment,a),$(x.$$.fragment,a),$(Je.$$.fragment,a),$(A.$$.fragment,a),$(C.$$.fragment,a),$(U.$$.fragment,a),$(H.$$.fragment,a),$(_e.$$.fragment,a),Ze=!1},d(a){a&&(t(T),t(p),t(J),t(re),t(oe),t(Q),t(W),t(se),t(Z),t(I),t(S),t(E),t(N),t(le),t(q),t(ye),t(ge),t(R),t(me),t(de),t(D),t(ce),t(he),t(ee),t(ne),t(_),t(Me),t(v),t(z),t(te),t(B),t(je),t(ae),t($e),t(we),t(Y),t(l),t(c),t(Te),t(n),t(Re),t(Ce),t(We)),t(i),b(k,a),b(L,a),b(pe,a),b(X,a),b(F,a),b(V,a),b(x,a),b(Je,a),b(A,a),b(C,a),b(U,a),b(H,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};
