import{S as Yr,i as Kr,s as Gr,e as i,k as h,w as q,t as o,M as Wr,c as f,d as t,m as d,a as m,x as E,h as n,b as $,G as s,g as p,y as T,q as x,o as S,B as A,v as Zr,L as ta}from"../../chunks/vendor-hf-doc-builder.js";import{T as ea}from"../../chunks/Tip-hf-doc-builder.js";import{Y as Nr}from"../../chunks/Youtube-hf-doc-builder.js";import{I as Qt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as H}from"../../chunks/CodeBlock-hf-doc-builder.js";import{F as Xa,M as Vt}from"../../chunks/Markdown-hf-doc-builder.js";function Jr(O){let r,_,a,g,b;return{c(){r=i("p"),_=o("See the translation "),a=i("a"),g=o("task page"),b=o(" for more information about its associated models, datasets, and metrics."),this.h()},l(y){r=f(y,"P",{});var v=m(r);_=n(v,"See the translation "),a=f(v,"A",{href:!0,rel:!0});var F=m(a);g=n(F,"task page"),F.forEach(t),b=n(v," for more information about its associated models, datasets, and metrics."),v.forEach(t),this.h()},h(){$(a,"href","https://huggingface.co/tasks/translation"),$(a,"rel","nofollow")},m(y,v){p(y,r,v),s(r,_),s(r,a),s(a,g),s(r,b)},d(y){y&&t(r)}}}function Rr(O){let r,_;return r=new H({props:{code:`from transformers import DataCollatorForSeq2Seq

data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForSeq2Seq

<span class="hljs-meta">&gt;&gt;&gt; </span>data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model)`}}),{c(){q(r.$$.fragment)},l(a){E(r.$$.fragment,a)},m(a,g){T(r,a,g),_=!0},p:ta,i(a){_||(x(r.$$.fragment,a),_=!0)},o(a){S(r.$$.fragment,a),_=!1},d(a){A(r,a)}}}function Xr(O){let r,_;return r=new Vt({props:{$$slots:{default:[Rr]},$$scope:{ctx:O}}}),{c(){q(r.$$.fragment)},l(a){E(r.$$.fragment,a)},m(a,g){T(r,a,g),_=!0},p(a,g){const b={};g&2&&(b.$$scope={dirty:g,ctx:a}),r.$set(b)},i(a){_||(x(r.$$.fragment,a),_=!0)},o(a){S(r.$$.fragment,a),_=!1},d(a){A(r,a)}}}function Qr(O){let r,_;return r=new H({props:{code:`from transformers import DataCollatorForSeq2Seq

data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model, return_tensors="tf")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForSeq2Seq

<span class="hljs-meta">&gt;&gt;&gt; </span>data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`}}),{c(){q(r.$$.fragment)},l(a){E(r.$$.fragment,a)},m(a,g){T(r,a,g),_=!0},p:ta,i(a){_||(x(r.$$.fragment,a),_=!0)},o(a){S(r.$$.fragment,a),_=!1},d(a){A(r,a)}}}function Vr(O){let r,_;return r=new Vt({props:{$$slots:{default:[Qr]},$$scope:{ctx:O}}}),{c(){q(r.$$.fragment)},l(a){E(r.$$.fragment,a)},m(a,g){T(r,a,g),_=!0},p(a,g){const b={};g&2&&(b.$$scope={dirty:g,ctx:a}),r.$set(b)},i(a){_||(x(r.$$.fragment,a),_=!0)},o(a){S(r.$$.fragment,a),_=!1},d(a){A(r,a)}}}function eo(O){let r,_,a,g,b,y,v,F;return{c(){r=i("p"),_=o("If you aren\u2019t familiar with finetuning a model with the "),a=i("a"),g=o("Trainer"),b=o(", take a look at the basic tutorial "),y=i("a"),v=o("here"),F=o("!"),this.h()},l(w){r=f(w,"P",{});var M=m(r);_=n(M,"If you aren\u2019t familiar with finetuning a model with the "),a=f(M,"A",{href:!0});var P=m(a);g=n(P,"Trainer"),P.forEach(t),b=n(M,", take a look at the basic tutorial "),y=f(M,"A",{href:!0});var Y=m(y);v=n(Y,"here"),Y.forEach(t),F=n(M,"!"),M.forEach(t),this.h()},h(){$(a,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer"),$(y,"href","../training#train-with-pytorch-trainer")},m(w,M){p(w,r,M),s(r,_),s(r,a),s(a,g),s(r,b),s(r,y),s(y,v),s(r,F)},d(w){w&&t(r)}}}function to(O){let r,_,a,g,b,y,v,F,w,M,P,Y,U,K,X,Q,R,I,G,L,se,B,N,C,c,j,oe,Z,ee,le,V,fe,te,ve,me,Ae,$e,ye,J,ze,ne,ke,be,ae,he,je,re,de;return r=new ea({props:{$$slots:{default:[eo]},$$scope:{ctx:O}}}),a=new H({props:{code:`from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer

model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSeq2SeqLM.from_pretrained(<span class="hljs-string">&quot;t5-small&quot;</span>)`}}),J=new H({props:{code:`training_args = Seq2SeqTrainingArguments(
    output_dir="my_awesome_opus_books_model",
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    weight_decay=0.01,
    save_total_limit=3,
    num_train_epochs=2,
    predict_with_generate=True,
    fp16=True,
    push_to_hub=True,
)

trainer = Seq2SeqTrainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_books["train"],
    eval_dataset=tokenized_books["test"],
    tokenizer=tokenizer,
    data_collator=data_collator,
    compute_metrics=compute_metrics,
)

trainer.train()`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>training_args = Seq2SeqTrainingArguments(
<span class="hljs-meta">... </span>    output_dir=<span class="hljs-string">&quot;my_awesome_opus_books_model&quot;</span>,
<span class="hljs-meta">... </span>    evaluation_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
<span class="hljs-meta">... </span>    learning_rate=<span class="hljs-number">2e-5</span>,
<span class="hljs-meta">... </span>    per_device_train_batch_size=<span class="hljs-number">16</span>,
<span class="hljs-meta">... </span>    per_device_eval_batch_size=<span class="hljs-number">16</span>,
<span class="hljs-meta">... </span>    weight_decay=<span class="hljs-number">0.01</span>,
<span class="hljs-meta">... </span>    save_total_limit=<span class="hljs-number">3</span>,
<span class="hljs-meta">... </span>    num_train_epochs=<span class="hljs-number">2</span>,
<span class="hljs-meta">... </span>    predict_with_generate=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>    fp16=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>    push_to_hub=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>trainer = Seq2SeqTrainer(
<span class="hljs-meta">... </span>    model=model,
<span class="hljs-meta">... </span>    args=training_args,
<span class="hljs-meta">... </span>    train_dataset=tokenized_books[<span class="hljs-string">&quot;train&quot;</span>],
<span class="hljs-meta">... </span>    eval_dataset=tokenized_books[<span class="hljs-string">&quot;test&quot;</span>],
<span class="hljs-meta">... </span>    tokenizer=tokenizer,
<span class="hljs-meta">... </span>    data_collator=data_collator,
<span class="hljs-meta">... </span>    compute_metrics=compute_metrics,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()`}}),re=new H({props:{code:"trainer.push_to_hub()",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.push_to_hub()'}}),{c(){q(r.$$.fragment),_=o(`
You're ready to start training your model now! Load T5 with [AutoModelForSeq2SeqLM](/docs/transformers/v4.25.1/en/model_doc/auto#transformers.AutoModelForSeq2SeqLM):

	`),q(a.$$.fragment),g=h(),b=i("p"),y=o("At this point, only three steps remain:"),v=h(),F=i("ol"),w=i("li"),M=o("Define your training hyperparameters in "),P=i("a"),Y=o("Seq2SeqTrainingArguments"),U=o(". The only required parameter is "),K=i("code"),X=o("output_dir"),Q=o(" which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),R=i("code"),I=o("push_to_hub=True"),G=o(" (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the "),L=i("a"),se=o("Trainer"),B=o(" will evaluate the SacreBLEU metric and save the training checkpoint."),N=h(),C=i("li"),c=o("Pass the training arguments to "),j=i("a"),oe=o("Seq2SeqTrainer"),Z=o(" along with the model, dataset, tokenizer, data collator, and "),ee=i("code"),le=o("compute_metrics"),V=o(" function."),fe=h(),te=i("li"),ve=o("Call "),me=i("a"),Ae=o("train()"),$e=o(" to finetune your model."),ye=h(),q(J.$$.fragment),ze=h(),ne=i("p"),ke=o("Once training is completed, share your model to the Hub with the "),be=i("a"),ae=o("push_to_hub()"),he=o(" method so everyone can use your model:"),je=h(),q(re.$$.fragment),this.h()},l(k){E(r.$$.fragment,k),_=n(k,`
You're ready to start training your model now! Load T5 with [AutoModelForSeq2SeqLM](/docs/transformers/v4.25.1/en/model_doc/auto#transformers.AutoModelForSeq2SeqLM):

	`),E(a.$$.fragment,k),g=d(k),b=f(k,"P",{});var D=m(b);y=n(D,"At this point, only three steps remain:"),D.forEach(t),v=d(k),F=f(k,"OL",{});var pe=m(F);w=f(pe,"LI",{});var W=m(w);M=n(W,"Define your training hyperparameters in "),P=f(W,"A",{href:!0});var Ne=m(P);Y=n(Ne,"Seq2SeqTrainingArguments"),Ne.forEach(t),U=n(W,". The only required parameter is "),K=f(W,"CODE",{});var qe=m(K);X=n(qe,"output_dir"),qe.forEach(t),Q=n(W," which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),R=f(W,"CODE",{});var Oe=m(R);I=n(Oe,"push_to_hub=True"),Oe.forEach(t),G=n(W," (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the "),L=f(W,"A",{href:!0});var Ee=m(L);se=n(Ee,"Trainer"),Ee.forEach(t),B=n(W," will evaluate the SacreBLEU metric and save the training checkpoint."),W.forEach(t),N=d(pe),C=f(pe,"LI",{});var _e=m(C);c=n(_e,"Pass the training arguments to "),j=f(_e,"A",{href:!0});var ue=m(j);oe=n(ue,"Seq2SeqTrainer"),ue.forEach(t),Z=n(_e," along with the model, dataset, tokenizer, data collator, and "),ee=f(_e,"CODE",{});var Ie=m(ee);le=n(Ie,"compute_metrics"),Ie.forEach(t),V=n(_e," function."),_e.forEach(t),fe=d(pe),te=f(pe,"LI",{});var ie=m(te);ve=n(ie,"Call "),me=f(ie,"A",{href:!0});var Te=m(me);Ae=n(Te,"train()"),Te.forEach(t),$e=n(ie," to finetune your model."),ie.forEach(t),pe.forEach(t),ye=d(k),E(J.$$.fragment,k),ze=d(k),ne=f(k,"P",{});var ce=m(ne);ke=n(ce,"Once training is completed, share your model to the Hub with the "),be=f(ce,"A",{href:!0});var we=m(be);ae=n(we,"push_to_hub()"),we.forEach(t),he=n(ce," method so everyone can use your model:"),ce.forEach(t),je=d(k),E(re.$$.fragment,k),this.h()},h(){$(P,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Seq2SeqTrainingArguments"),$(L,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer"),$(j,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Seq2SeqTrainer"),$(me,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer.train"),$(be,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer.push_to_hub")},m(k,D){T(r,k,D),p(k,_,D),T(a,k,D),p(k,g,D),p(k,b,D),s(b,y),p(k,v,D),p(k,F,D),s(F,w),s(w,M),s(w,P),s(P,Y),s(w,U),s(w,K),s(K,X),s(w,Q),s(w,R),s(R,I),s(w,G),s(w,L),s(L,se),s(w,B),s(F,N),s(F,C),s(C,c),s(C,j),s(j,oe),s(C,Z),s(C,ee),s(ee,le),s(C,V),s(F,fe),s(F,te),s(te,ve),s(te,me),s(me,Ae),s(te,$e),p(k,ye,D),T(J,k,D),p(k,ze,D),p(k,ne,D),s(ne,ke),s(ne,be),s(be,ae),s(ne,he),p(k,je,D),T(re,k,D),de=!0},p(k,D){const pe={};D&2&&(pe.$$scope={dirty:D,ctx:k}),r.$set(pe)},i(k){de||(x(r.$$.fragment,k),x(a.$$.fragment,k),x(J.$$.fragment,k),x(re.$$.fragment,k),de=!0)},o(k){S(r.$$.fragment,k),S(a.$$.fragment,k),S(J.$$.fragment,k),S(re.$$.fragment,k),de=!1},d(k){A(r,k),k&&t(_),A(a,k),k&&t(g),k&&t(b),k&&t(v),k&&t(F),k&&t(ye),A(J,k),k&&t(ze),k&&t(ne),k&&t(je),A(re,k)}}}function so(O){let r,_;return r=new Vt({props:{$$slots:{default:[to]},$$scope:{ctx:O}}}),{c(){q(r.$$.fragment)},l(a){E(r.$$.fragment,a)},m(a,g){T(r,a,g),_=!0},p(a,g){const b={};g&2&&(b.$$scope={dirty:g,ctx:a}),r.$set(b)},i(a){_||(x(r.$$.fragment,a),_=!0)},o(a){S(r.$$.fragment,a),_=!1},d(a){A(r,a)}}}function ao(O){let r,_,a,g,b;return{c(){r=i("p"),_=o("If you aren\u2019t familiar with finetuning a model with Keras, take a look at the basic tutorial "),a=i("a"),g=o("here"),b=o("!"),this.h()},l(y){r=f(y,"P",{});var v=m(r);_=n(v,"If you aren\u2019t familiar with finetuning a model with Keras, take a look at the basic tutorial "),a=f(v,"A",{href:!0});var F=m(a);g=n(F,"here"),F.forEach(t),b=n(v,"!"),v.forEach(t),this.h()},h(){$(a,"href","../training#train-a-tensorflow-model-with-keras")},m(y,v){p(y,r,v),s(r,_),s(r,a),s(a,g),s(r,b)},d(y){y&&t(r)}}}function ro(O){let r,_,a,g,b,y,v,F,w,M,P,Y,U,K,X,Q,R,I,G,L,se,B,N,C,c,j,oe,Z,ee,le,V,fe,te,ve,me,Ae,$e,ye,J,ze,ne,ke,be,ae,he,je,re,de,k,D,pe,W,Ne,qe,Oe,Ee,_e,ue,Ie,ie,Te,ce,we,wt,Fe,Se,at,Ye,rt,Pe,Me,He,Ue,Ke;return r=new ea({props:{$$slots:{default:[ao]},$$scope:{ctx:O}}}),a=new H({props:{code:`from transformers import AdamWeightDecay

optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamWeightDecay

<span class="hljs-meta">&gt;&gt;&gt; </span>optimizer = AdamWeightDecay(learning_rate=<span class="hljs-number">2e-5</span>, weight_decay_rate=<span class="hljs-number">0.01</span>)`}}),P=new H({props:{code:`from transformers import TFAutoModelForSeq2SeqLM

model = TFAutoModelForSeq2SeqLM.from_pretrained("t5-small")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSeq2SeqLM

<span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForSeq2SeqLM.from_pretrained(<span class="hljs-string">&quot;t5-small&quot;</span>)`}}),B=new H({props:{code:`tf_train_set = model.prepare_tf_dataset(
    tokenized_books["train"],
    shuffle=True,
    batch_size=16,
    collate_fn=data_collator,
)

tf_test_set = model.prepare_tf_dataset(
    tokenized_books["test"],
    shuffle=False,
    batch_size=16,
    collate_fn=data_collator,
)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>tf_train_set = model.prepare_tf_dataset(
<span class="hljs-meta">... </span>    tokenized_books[<span class="hljs-string">&quot;train&quot;</span>],
<span class="hljs-meta">... </span>    shuffle=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>    batch_size=<span class="hljs-number">16</span>,
<span class="hljs-meta">... </span>    collate_fn=data_collator,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>tf_test_set = model.prepare_tf_dataset(
<span class="hljs-meta">... </span>    tokenized_books[<span class="hljs-string">&quot;test&quot;</span>],
<span class="hljs-meta">... </span>    shuffle=<span class="hljs-literal">False</span>,
<span class="hljs-meta">... </span>    batch_size=<span class="hljs-number">16</span>,
<span class="hljs-meta">... </span>    collate_fn=data_collator,
<span class="hljs-meta">... </span>)`}}),V=new H({props:{code:`import tensorflow as tf

model.compile(optimizer=optimizer)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf

<span class="hljs-meta">&gt;&gt;&gt; </span>model.<span class="hljs-built_in">compile</span>(optimizer=optimizer)`}}),de=new H({props:{code:`from transformers.keras_callbacks import KerasMetricCallback

metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers.keras_callbacks <span class="hljs-keyword">import</span> KerasMetricCallback

<span class="hljs-meta">&gt;&gt;&gt; </span>metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set)`}}),Ee=new H({props:{code:`from transformers.keras_callbacks import PushToHubCallback

push_to_hub_callback = PushToHubCallback(
    output_dir="my_awesome_opus_books_model",
    tokenizer=tokenizer,
)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers.keras_callbacks <span class="hljs-keyword">import</span> PushToHubCallback

<span class="hljs-meta">&gt;&gt;&gt; </span>push_to_hub_callback = PushToHubCallback(
<span class="hljs-meta">... </span>    output_dir=<span class="hljs-string">&quot;my_awesome_opus_books_model&quot;</span>,
<span class="hljs-meta">... </span>    tokenizer=tokenizer,
<span class="hljs-meta">... </span>)`}}),Te=new H({props:{code:"callbacks = [metric_callback, push_to_hub_callback]",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>callbacks = [metric_callback, push_to_hub_callback]'}}),Pe=new H({props:{code:"model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=callbacks)",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=<span class="hljs-number">3</span>, callbacks=callbacks)'}}),{c(){q(r.$$.fragment),_=o(`
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:

	`),q(a.$$.fragment),g=h(),b=i("p"),y=o("Then you can load T5 with "),v=i("a"),F=o("TFAutoModelForSeq2SeqLM"),w=o(":"),M=h(),q(P.$$.fragment),Y=h(),U=i("p"),K=o("Convert your datasets to the "),X=i("code"),Q=o("tf.data.Dataset"),R=o(" format with "),I=i("a"),G=o("prepare_tf_dataset()"),L=o(":"),se=h(),q(B.$$.fragment),N=h(),C=i("p"),c=o("Configure the model for training with "),j=i("a"),oe=i("code"),Z=o("compile"),ee=o(":"),le=h(),q(V.$$.fragment),fe=h(),te=i("p"),ve=o("The last two things to setup before you start training is to compute the SacreBLEU metric from the predictions, and provide a way to push your model to the Hub. Both are done by using "),me=i("a"),Ae=o("Keras callbacks"),$e=o("."),ye=h(),J=i("p"),ze=o("Pass your "),ne=i("code"),ke=o("compute_metrics"),be=o(" function to "),ae=i("a"),he=o("KerasMetricCallback"),je=o(":"),re=h(),q(de.$$.fragment),k=h(),D=i("p"),pe=o("Specify where to push your model and tokenizer in the "),W=i("a"),Ne=o("PushToHubCallback"),qe=o(":"),Oe=h(),q(Ee.$$.fragment),_e=h(),ue=i("p"),Ie=o("Then bundle your callbacks together:"),ie=h(),q(Te.$$.fragment),ce=h(),we=i("p"),wt=o("Finally, you\u2019re ready to start training your model! Call "),Fe=i("a"),Se=i("code"),at=o("fit"),Ye=o(" with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:"),rt=h(),q(Pe.$$.fragment),Me=h(),He=i("p"),Ue=o("Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),this.h()},l(l){E(r.$$.fragment,l),_=n(l,`
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:

	`),E(a.$$.fragment,l),g=d(l),b=f(l,"P",{});var z=m(b);y=n(z,"Then you can load T5 with "),v=f(z,"A",{href:!0});var Ge=m(v);F=n(Ge,"TFAutoModelForSeq2SeqLM"),Ge.forEach(t),w=n(z,":"),z.forEach(t),M=d(l),E(P.$$.fragment,l),Y=d(l),U=f(l,"P",{});var ge=m(U);K=n(ge,"Convert your datasets to the "),X=f(ge,"CODE",{});var Be=m(X);Q=n(Be,"tf.data.Dataset"),Be.forEach(t),R=n(ge," format with "),I=f(ge,"A",{href:!0});var ot=m(I);G=n(ot,"prepare_tf_dataset()"),ot.forEach(t),L=n(ge,":"),ge.forEach(t),se=d(l),E(B.$$.fragment,l),N=d(l),C=f(l,"P",{});var Le=m(C);c=n(Le,"Configure the model for training with "),j=f(Le,"A",{href:!0,rel:!0});var Bt=m(j);oe=f(Bt,"CODE",{});var nt=m(oe);Z=n(nt,"compile"),nt.forEach(t),Bt.forEach(t),ee=n(Le,":"),Le.forEach(t),le=d(l),E(V.$$.fragment,l),fe=d(l),te=f(l,"P",{});var lt=m(te);ve=n(lt,"The last two things to setup before you start training is to compute the SacreBLEU metric from the predictions, and provide a way to push your model to the Hub. Both are done by using "),me=f(lt,"A",{href:!0});var vt=m(me);Ae=n(vt,"Keras callbacks"),vt.forEach(t),$e=n(lt,"."),lt.forEach(t),ye=d(l),J=f(l,"P",{});var xe=m(J);ze=n(xe,"Pass your "),ne=f(xe,"CODE",{});var yt=m(ne);ke=n(yt,"compute_metrics"),yt.forEach(t),be=n(xe," function to "),ae=f(xe,"A",{href:!0});var Xe=m(ae);he=n(Xe,"KerasMetricCallback"),Xe.forEach(t),je=n(xe,":"),xe.forEach(t),re=d(l),E(de.$$.fragment,l),k=d(l),D=f(l,"P",{});var pt=m(D);pe=n(pt,"Specify where to push your model and tokenizer in the "),W=f(pt,"A",{href:!0});var jt=m(W);Ne=n(jt,"PushToHubCallback"),jt.forEach(t),qe=n(pt,":"),pt.forEach(t),Oe=d(l),E(Ee.$$.fragment,l),_e=d(l),ue=f(l,"P",{});var We=m(ue);Ie=n(We,"Then bundle your callbacks together:"),We.forEach(t),ie=d(l),E(Te.$$.fragment,l),ce=d(l),we=f(l,"P",{});var Qe=m(we);wt=n(Qe,"Finally, you\u2019re ready to start training your model! Call "),Fe=f(Qe,"A",{href:!0,rel:!0});var Ve=m(Fe);Se=f(Ve,"CODE",{});var Nt=m(Se);at=n(Nt,"fit"),Nt.forEach(t),Ve.forEach(t),Ye=n(Qe," with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:"),Qe.forEach(t),rt=d(l),E(Pe.$$.fragment,l),Me=d(l),He=f(l,"P",{});var qt=m(He);Ue=n(qt,"Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),qt.forEach(t),this.h()},h(){$(v,"href","/docs/transformers/v4.25.1/en/model_doc/auto#transformers.TFAutoModelForSeq2SeqLM"),$(I,"href","/docs/transformers/v4.25.1/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset"),$(j,"href","https://keras.io/api/models/model_training_apis/#compile-method"),$(j,"rel","nofollow"),$(me,"href","./main_classes/keras_callbacks"),$(ae,"href","/docs/transformers/v4.25.1/en/main_classes/keras_callbacks#transformers.KerasMetricCallback"),$(W,"href","/docs/transformers/v4.25.1/en/main_classes/keras_callbacks#transformers.PushToHubCallback"),$(Fe,"href","https://keras.io/api/models/model_training_apis/#fit-method"),$(Fe,"rel","nofollow")},m(l,z){T(r,l,z),p(l,_,z),T(a,l,z),p(l,g,z),p(l,b,z),s(b,y),s(b,v),s(v,F),s(b,w),p(l,M,z),T(P,l,z),p(l,Y,z),p(l,U,z),s(U,K),s(U,X),s(X,Q),s(U,R),s(U,I),s(I,G),s(U,L),p(l,se,z),T(B,l,z),p(l,N,z),p(l,C,z),s(C,c),s(C,j),s(j,oe),s(oe,Z),s(C,ee),p(l,le,z),T(V,l,z),p(l,fe,z),p(l,te,z),s(te,ve),s(te,me),s(me,Ae),s(te,$e),p(l,ye,z),p(l,J,z),s(J,ze),s(J,ne),s(ne,ke),s(J,be),s(J,ae),s(ae,he),s(J,je),p(l,re,z),T(de,l,z),p(l,k,z),p(l,D,z),s(D,pe),s(D,W),s(W,Ne),s(D,qe),p(l,Oe,z),T(Ee,l,z),p(l,_e,z),p(l,ue,z),s(ue,Ie),p(l,ie,z),T(Te,l,z),p(l,ce,z),p(l,we,z),s(we,wt),s(we,Fe),s(Fe,Se),s(Se,at),s(we,Ye),p(l,rt,z),T(Pe,l,z),p(l,Me,z),p(l,He,z),s(He,Ue),Ke=!0},p(l,z){const Ge={};z&2&&(Ge.$$scope={dirty:z,ctx:l}),r.$set(Ge)},i(l){Ke||(x(r.$$.fragment,l),x(a.$$.fragment,l),x(P.$$.fragment,l),x(B.$$.fragment,l),x(V.$$.fragment,l),x(de.$$.fragment,l),x(Ee.$$.fragment,l),x(Te.$$.fragment,l),x(Pe.$$.fragment,l),Ke=!0)},o(l){S(r.$$.fragment,l),S(a.$$.fragment,l),S(P.$$.fragment,l),S(B.$$.fragment,l),S(V.$$.fragment,l),S(de.$$.fragment,l),S(Ee.$$.fragment,l),S(Te.$$.fragment,l),S(Pe.$$.fragment,l),Ke=!1},d(l){A(r,l),l&&t(_),A(a,l),l&&t(g),l&&t(b),l&&t(M),A(P,l),l&&t(Y),l&&t(U),l&&t(se),A(B,l),l&&t(N),l&&t(C),l&&t(le),A(V,l),l&&t(fe),l&&t(te),l&&t(ye),l&&t(J),l&&t(re),A(de,l),l&&t(k),l&&t(D),l&&t(Oe),A(Ee,l),l&&t(_e),l&&t(ue),l&&t(ie),A(Te,l),l&&t(ce),l&&t(we),l&&t(rt),A(Pe,l),l&&t(Me),l&&t(He)}}}function oo(O){let r,_;return r=new Vt({props:{$$slots:{default:[ro]},$$scope:{ctx:O}}}),{c(){q(r.$$.fragment)},l(a){E(r.$$.fragment,a)},m(a,g){T(r,a,g),_=!0},p(a,g){const b={};g&2&&(b.$$scope={dirty:g,ctx:a}),r.$set(b)},i(a){_||(x(r.$$.fragment,a),_=!0)},o(a){S(r.$$.fragment,a),_=!1},d(a){A(r,a)}}}function no(O){let r,_,a,g,b,y,v,F;return{c(){r=i("p"),_=o(`For a more in-depth example of how to finetune a model for translation, take a look at the corresponding
`),a=i("a"),g=o("PyTorch notebook"),b=o(`
or `),y=i("a"),v=o("TensorFlow notebook"),F=o("."),this.h()},l(w){r=f(w,"P",{});var M=m(r);_=n(M,`For a more in-depth example of how to finetune a model for translation, take a look at the corresponding
`),a=f(M,"A",{href:!0,rel:!0});var P=m(a);g=n(P,"PyTorch notebook"),P.forEach(t),b=n(M,`
or `),y=f(M,"A",{href:!0,rel:!0});var Y=m(y);v=n(Y,"TensorFlow notebook"),Y.forEach(t),F=n(M,"."),M.forEach(t),this.h()},h(){$(a,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb"),$(a,"rel","nofollow"),$(y,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb"),$(y,"rel","nofollow")},m(w,M){p(w,r,M),s(r,_),s(r,a),s(a,g),s(r,b),s(r,y),s(y,v),s(r,F)},d(w){w&&t(r)}}}function lo(O){let r,_,a,g,b,y,v,F,w,M,P,Y,U,K,X,Q,R,I,G,L,se,B,N,C;return v=new H({props:{code:`from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("my_awesome_opus_books_model")
inputs = tokenizer(text, return_tensors="pt").input_ids`,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;my_awesome_opus_books_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(text, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>).input_ids`}}),I=new H({props:{code:`from transformers import AutoModelForSeq2SeqLM

model = AutoModelForSeq2SeqLM.from_pretrained("my_awesome_opus_books_model")
outputs = model.generate(inputs, max_new_tokens=40, do_sample=True, top_k=30, top_p=0.95)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSeq2SeqLM

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSeq2SeqLM.from_pretrained(<span class="hljs-string">&quot;my_awesome_opus_books_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(inputs, max_new_tokens=<span class="hljs-number">40</span>, do_sample=<span class="hljs-literal">True</span>, top_k=<span class="hljs-number">30</span>, top_p=<span class="hljs-number">0.95</span>)`}}),N=new H({props:{code:"tokenizer.decode(outputs[0], skip_special_tokens=True)",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.decode(outputs[<span class="hljs-number">0</span>], skip_special_tokens=<span class="hljs-literal">True</span>)
<span class="hljs-string">&#x27;Les lign\xE9es partagent des ressources avec des bact\xE9ries enfixant l&#x27;</span>azote.<span class="hljs-string">&#x27;</span>`}}),{c(){r=i("p"),_=o("Tokenize the text and return the "),a=i("code"),g=o("input_ids"),b=o(" as PyTorch tensors:"),y=h(),q(v.$$.fragment),F=h(),w=i("p"),M=o("Use the "),P=i("a"),Y=o("generate()"),U=o(" method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),K=i("a"),X=o("Text Generation"),Q=o(" API."),R=h(),q(I.$$.fragment),G=h(),L=i("p"),se=o("Decode the generated token ids back into text:"),B=h(),q(N.$$.fragment),this.h()},l(c){r=f(c,"P",{});var j=m(r);_=n(j,"Tokenize the text and return the "),a=f(j,"CODE",{});var oe=m(a);g=n(oe,"input_ids"),oe.forEach(t),b=n(j," as PyTorch tensors:"),j.forEach(t),y=d(c),E(v.$$.fragment,c),F=d(c),w=f(c,"P",{});var Z=m(w);M=n(Z,"Use the "),P=f(Z,"A",{href:!0});var ee=m(P);Y=n(ee,"generate()"),ee.forEach(t),U=n(Z," method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),K=f(Z,"A",{href:!0});var le=m(K);X=n(le,"Text Generation"),le.forEach(t),Q=n(Z," API."),Z.forEach(t),R=d(c),E(I.$$.fragment,c),G=d(c),L=f(c,"P",{});var V=m(L);se=n(V,"Decode the generated token ids back into text:"),V.forEach(t),B=d(c),E(N.$$.fragment,c),this.h()},h(){$(P,"href","/docs/transformers/v4.25.1/en/main_classes/text_generation#transformers.GenerationMixin.generate"),$(K,"href","./main_classes/text_generation")},m(c,j){p(c,r,j),s(r,_),s(r,a),s(a,g),s(r,b),p(c,y,j),T(v,c,j),p(c,F,j),p(c,w,j),s(w,M),s(w,P),s(P,Y),s(w,U),s(w,K),s(K,X),s(w,Q),p(c,R,j),T(I,c,j),p(c,G,j),p(c,L,j),s(L,se),p(c,B,j),T(N,c,j),C=!0},p:ta,i(c){C||(x(v.$$.fragment,c),x(I.$$.fragment,c),x(N.$$.fragment,c),C=!0)},o(c){S(v.$$.fragment,c),S(I.$$.fragment,c),S(N.$$.fragment,c),C=!1},d(c){c&&t(r),c&&t(y),A(v,c),c&&t(F),c&&t(w),c&&t(R),A(I,c),c&&t(G),c&&t(L),c&&t(B),A(N,c)}}}function po(O){let r,_;return r=new Vt({props:{$$slots:{default:[lo]},$$scope:{ctx:O}}}),{c(){q(r.$$.fragment)},l(a){E(r.$$.fragment,a)},m(a,g){T(r,a,g),_=!0},p(a,g){const b={};g&2&&(b.$$scope={dirty:g,ctx:a}),r.$set(b)},i(a){_||(x(r.$$.fragment,a),_=!0)},o(a){S(r.$$.fragment,a),_=!1},d(a){A(r,a)}}}function io(O){let r,_,a,g,b,y,v,F,w,M,P,Y,U,K,X,Q,R,I,G,L,se,B,N,C;return v=new H({props:{code:`from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("my_awesome_opus_books_model")
inputs = tokenizer(text, return_tensors="tf").input_ids`,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;my_awesome_opus_books_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(text, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>).input_ids`}}),I=new H({props:{code:`from transformers import TFAutoModelForSeq2SeqLM

model = TFAutoModelForSeq2SeqLM.from_pretrained("my_awesome_opus_books_model")
outputs = model.generate(inputs, max_new_tokens=40, do_sample=True, top_k=30, top_p=0.95)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSeq2SeqLM

<span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForSeq2SeqLM.from_pretrained(<span class="hljs-string">&quot;my_awesome_opus_books_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(inputs, max_new_tokens=<span class="hljs-number">40</span>, do_sample=<span class="hljs-literal">True</span>, top_k=<span class="hljs-number">30</span>, top_p=<span class="hljs-number">0.95</span>)`}}),N=new H({props:{code:"tokenizer.decode(outputs[0], skip_special_tokens=True)",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.decode(outputs[<span class="hljs-number">0</span>], skip_special_tokens=<span class="hljs-literal">True</span>)
<span class="hljs-string">&#x27;Les lugumes partagent les ressources avec des bact\xE9ries fixatrices d&#x27;</span>azote.<span class="hljs-string">&#x27;</span>`}}),{c(){r=i("p"),_=o("Tokenize the text and return the "),a=i("code"),g=o("input_ids"),b=o(" as TensorFlow tensors:"),y=h(),q(v.$$.fragment),F=h(),w=i("p"),M=o("Use the "),P=i("a"),Y=o("generate()"),U=o(" method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),K=i("a"),X=o("Text Generation"),Q=o(" API."),R=h(),q(I.$$.fragment),G=h(),L=i("p"),se=o("Decode the generated token ids back into text:"),B=h(),q(N.$$.fragment),this.h()},l(c){r=f(c,"P",{});var j=m(r);_=n(j,"Tokenize the text and return the "),a=f(j,"CODE",{});var oe=m(a);g=n(oe,"input_ids"),oe.forEach(t),b=n(j," as TensorFlow tensors:"),j.forEach(t),y=d(c),E(v.$$.fragment,c),F=d(c),w=f(c,"P",{});var Z=m(w);M=n(Z,"Use the "),P=f(Z,"A",{href:!0});var ee=m(P);Y=n(ee,"generate()"),ee.forEach(t),U=n(Z," method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),K=f(Z,"A",{href:!0});var le=m(K);X=n(le,"Text Generation"),le.forEach(t),Q=n(Z," API."),Z.forEach(t),R=d(c),E(I.$$.fragment,c),G=d(c),L=f(c,"P",{});var V=m(L);se=n(V,"Decode the generated token ids back into text:"),V.forEach(t),B=d(c),E(N.$$.fragment,c),this.h()},h(){$(P,"href","/docs/transformers/v4.25.1/en/main_classes/text_generation#transformers.TFGenerationMixin.generate"),$(K,"href","./main_classes/text_generation")},m(c,j){p(c,r,j),s(r,_),s(r,a),s(a,g),s(r,b),p(c,y,j),T(v,c,j),p(c,F,j),p(c,w,j),s(w,M),s(w,P),s(P,Y),s(w,U),s(w,K),s(K,X),s(w,Q),p(c,R,j),T(I,c,j),p(c,G,j),p(c,L,j),s(L,se),p(c,B,j),T(N,c,j),C=!0},p:ta,i(c){C||(x(v.$$.fragment,c),x(I.$$.fragment,c),x(N.$$.fragment,c),C=!0)},o(c){S(v.$$.fragment,c),S(I.$$.fragment,c),S(N.$$.fragment,c),C=!1},d(c){c&&t(r),c&&t(y),A(v,c),c&&t(F),c&&t(w),c&&t(R),A(I,c),c&&t(G),c&&t(L),c&&t(B),A(N,c)}}}function fo(O){let r,_;return r=new Vt({props:{$$slots:{default:[io]},$$scope:{ctx:O}}}),{c(){q(r.$$.fragment)},l(a){E(r.$$.fragment,a)},m(a,g){T(r,a,g),_=!0},p(a,g){const b={};g&2&&(b.$$scope={dirty:g,ctx:a}),r.$set(b)},i(a){_||(x(r.$$.fragment,a),_=!0)},o(a){S(r.$$.fragment,a),_=!1},d(a){A(r,a)}}}function mo(O){let r,_,a,g,b,y,v,F,w,M,P,Y,U,K,X,Q,R,I,G,L,se,B,N,C,c,j,oe,Z,ee,le,V,fe,te,ve,me,Ae,$e,ye,J,ze,ne,ke,be,ae,he,je,re,de,k,D,pe,W,Ne,qe,Oe,Ee,_e,ue,Ie,ie,Te,ce,we,wt,Fe,Se,at,Ye,rt,Pe,Me,He,Ue,Ke,l,z,Ge,ge,Be,ot,Le,Bt,nt,lt,vt,xe,yt,Xe,pt,jt,We,Qe,Ve,Nt,qt,Ze,es,sa,aa,ts,ra,oa,Et,na,ss,la,pa,ks,Tt,bs,Ce,ia,xt,fa,ma,as,ua,ca,rs,ha,da,ws,St,vs,Je,_a,Yt,ga,$a,os,ka,ba,ys,it,js,et,ft,ns,At,wa,ls,va,qs,De,ya,zt,ja,qa,Ft,Ea,Ta,Pt,xa,Sa,Es,Mt,Ts,mt,Aa,Lt,za,Fa,xs,Ct,Ss,ut,Pa,ps,Ma,La,As,tt,ct,is,Dt,Ca,fs,Da,zs,ht,Fs,dt,Ps,st,_t,ms,Ot,Oa,us,Ia,Ms,Kt,Ha,Ls,Gt,Ua,Cs,It,Ds,Re,Ba,Wt,Na,Ya,cs,Ka,Ga,Os,Ht,Is,gt,Wa,hs,Za,Ja,Hs,$t,Us;return y=new Qt({}),P=new Nr({props:{id:"1JvfrvZgi6c"}}),fe=new ea({props:{$$slots:{default:[Jr]},$$scope:{ctx:O}}}),$e=new H({props:{code:"pip install transformers datasets evaluate",highlighted:"pip install transformers datasets evaluate"}}),ke=new H({props:{code:`from huggingface_hub import notebook_login

notebook_login()`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

<span class="hljs-meta">&gt;&gt;&gt; </span>notebook_login()`}}),re=new Qt({}),ue=new H({props:{code:`from datasets import load_dataset

books = load_dataset("opus_books", "en-fr")`,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>books = load_dataset(<span class="hljs-string">&quot;opus_books&quot;</span>, <span class="hljs-string">&quot;en-fr&quot;</span>)`}}),Se=new H({props:{code:'books = books["train"].train_test_split(test_size=0.2)',highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>books = books[<span class="hljs-string">&quot;train&quot;</span>].train_test_split(test_size=<span class="hljs-number">0.2</span>)'}}),Me=new H({props:{code:'books["train"][0]',highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>books[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>]
{<span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-string">&#x27;90560&#x27;</span>,
 <span class="hljs-string">&#x27;translation&#x27;</span>: {<span class="hljs-string">&#x27;en&#x27;</span>: <span class="hljs-string">&#x27;But this lofty plateau measured only a few fathoms, and soon we reentered Our Element.&#x27;</span>,
  <span class="hljs-string">&#x27;fr&#x27;</span>: <span class="hljs-string">&#x27;Mais ce plateau \xE9lev\xE9 ne mesurait que quelques toises, et bient\xF4t nous f\xFBmes rentr\xE9s dans notre \xE9l\xE9ment.&#x27;</span>}}`}}),Le=new Qt({}),xe=new Nr({props:{id:"XAR8jnZZuUs"}}),We=new H({props:{code:`from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("t5-small")`,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;t5-small&quot;</span>)`}}),Tt=new H({props:{code:`source_lang = "en"
target_lang = "fr"
prefix = "translate English to French: "


def preprocess_function(examples):
    inputs = [prefix + example[source_lang] for example in examples["translation"]]
    targets = [example[target_lang] for example in examples["translation"]]
    model_inputs = tokenizer(inputs, text_target=targets, max_length=128, truncation=True)
    return model_inputs`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>source_lang = <span class="hljs-string">&quot;en&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>target_lang = <span class="hljs-string">&quot;fr&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>prefix = <span class="hljs-string">&quot;translate English to French: &quot;</span>


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>):
<span class="hljs-meta">... </span>    inputs = [prefix + example[source_lang] <span class="hljs-keyword">for</span> example <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;translation&quot;</span>]]
<span class="hljs-meta">... </span>    targets = [example[target_lang] <span class="hljs-keyword">for</span> example <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;translation&quot;</span>]]
<span class="hljs-meta">... </span>    model_inputs = tokenizer(inputs, text_target=targets, max_length=<span class="hljs-number">128</span>, truncation=<span class="hljs-literal">True</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> model_inputs`}}),St=new H({props:{code:"tokenized_books = books.map(preprocess_function, batched=True)",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_books = books.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>)'}}),it=new Xa({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Vr],pytorch:[Xr]},$$scope:{ctx:O}}}),At=new Qt({}),Mt=new H({props:{code:`import evaluate

sacrebleu = evaluate.load("sacrebleu")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> evaluate

<span class="hljs-meta">&gt;&gt;&gt; </span>sacrebleu = evaluate.load(<span class="hljs-string">&quot;sacrebleu&quot;</span>)`}}),Ct=new H({props:{code:`import numpy as np


def postprocess_text(preds, labels):
    preds = [pred.strip() for pred in preds]
    labels = [[label.strip()] for label in labels]

    return preds, labels


def compute_metrics(eval_preds):
    preds, labels = eval_preds
    if isinstance(preds, tuple):
        preds = preds[0]
    decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)

    labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)

    decoded_preds, decoded_labels = postprocess_text(decoded_preds, decoded_labels)

    result = metric.compute(predictions=decoded_preds, references=decoded_labels)
    result = {"bleu": result["score"]}

    prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds]
    result["gen_len"] = np.mean(prediction_lens)
    result = {k: round(v, 4) for k, v in result.items()}
    return result`,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">def</span> <span class="hljs-title function_">postprocess_text</span>(<span class="hljs-params">preds, labels</span>):
<span class="hljs-meta">... </span>    preds = [pred.strip() <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> preds]
<span class="hljs-meta">... </span>    labels = [[label.strip()] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> labels]

<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> preds, labels


<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_preds</span>):
<span class="hljs-meta">... </span>    preds, labels = eval_preds
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> <span class="hljs-built_in">isinstance</span>(preds, <span class="hljs-built_in">tuple</span>):
<span class="hljs-meta">... </span>        preds = preds[<span class="hljs-number">0</span>]
<span class="hljs-meta">... </span>    decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=<span class="hljs-literal">True</span>)

<span class="hljs-meta">... </span>    labels = np.where(labels != -<span class="hljs-number">100</span>, labels, tokenizer.pad_token_id)
<span class="hljs-meta">... </span>    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>)

<span class="hljs-meta">... </span>    decoded_preds, decoded_labels = postprocess_text(decoded_preds, decoded_labels)

<span class="hljs-meta">... </span>    result = metric.compute(predictions=decoded_preds, references=decoded_labels)
<span class="hljs-meta">... </span>    result = {<span class="hljs-string">&quot;bleu&quot;</span>: result[<span class="hljs-string">&quot;score&quot;</span>]}

<span class="hljs-meta">... </span>    prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> preds]
<span class="hljs-meta">... </span>    result[<span class="hljs-string">&quot;gen_len&quot;</span>] = np.mean(prediction_lens)
<span class="hljs-meta">... </span>    result = {k: <span class="hljs-built_in">round</span>(v, <span class="hljs-number">4</span>) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> result.items()}
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> result`}}),Dt=new Qt({}),ht=new Xa({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[oo],pytorch:[so]},$$scope:{ctx:O}}}),dt=new ea({props:{$$slots:{default:[no]},$$scope:{ctx:O}}}),Ot=new Qt({}),It=new H({props:{code:'text = "translate English to French: Legumes share resources with nitrogen-fixing bacteria."',highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>text = <span class="hljs-string">&quot;translate English to French: Legumes share resources with nitrogen-fixing bacteria.&quot;</span>'}}),Ht=new H({props:{code:`from transformers import pipeline

translator = pipeline("translation", model="my_awesome_opus_books_model")
translator(text)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

<span class="hljs-meta">&gt;&gt;&gt; </span>translator = pipeline(<span class="hljs-string">&quot;translation&quot;</span>, model=<span class="hljs-string">&quot;my_awesome_opus_books_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>translator(text)
[{<span class="hljs-string">&#x27;translation_text&#x27;</span>: <span class="hljs-string">&#x27;Legumes partagent des ressources avec des bact\xE9ries azotantes.&#x27;</span>}]`}}),$t=new Xa({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[fo],pytorch:[po]},$$scope:{ctx:O}}}),{c(){r=i("meta"),_=h(),a=i("h1"),g=i("a"),b=i("span"),q(y.$$.fragment),v=h(),F=i("span"),w=o("Translation"),M=h(),q(P.$$.fragment),Y=h(),U=i("p"),K=o("Translation converts a sequence of text from one language to another. It is one of several tasks you can formulate as a sequence-to-sequence problem, a powerful framework for returning some output from an input, like translation or summarization. Translation systems are commonly used for translation between different language texts, but it can also be used for speech or some combination in between like text-to-speech or speech-to-text."),X=h(),Q=i("p"),R=o("This guide will show you how to:"),I=h(),G=i("ol"),L=i("li"),se=o("Finetune "),B=i("a"),N=o("T5"),C=o(" on the English-French subset of the "),c=i("a"),j=o("OPUS Books"),oe=o(" dataset to translate English text to French."),Z=h(),ee=i("li"),le=o("Use your finetuned model for inference."),V=h(),q(fe.$$.fragment),te=h(),ve=i("p"),me=o("Before you begin, make sure you have all the necessary libraries installed:"),Ae=h(),q($e.$$.fragment),ye=h(),J=i("p"),ze=o("We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login:"),ne=h(),q(ke.$$.fragment),be=h(),ae=i("h2"),he=i("a"),je=i("span"),q(re.$$.fragment),de=h(),k=i("span"),D=o("Load OPUS Books dataset"),pe=h(),W=i("p"),Ne=o("Start by loading the English-French subset of the "),qe=i("a"),Oe=o("OPUS Books"),Ee=o(" dataset from the \u{1F917} Datasets library:"),_e=h(),q(ue.$$.fragment),Ie=h(),ie=i("p"),Te=o("Split the dataset into a train and test set with the "),ce=i("a"),we=o("train_test_split"),wt=o(" method:"),Fe=h(),q(Se.$$.fragment),at=h(),Ye=i("p"),rt=o("Then take a look at an example:"),Pe=h(),q(Me.$$.fragment),He=h(),Ue=i("p"),Ke=i("code"),l=o("translation"),z=o(": an English and French translation of the text."),Ge=h(),ge=i("h2"),Be=i("a"),ot=i("span"),q(Le.$$.fragment),Bt=h(),nt=i("span"),lt=o("Preprocess"),vt=h(),q(xe.$$.fragment),yt=h(),Xe=i("p"),pt=o("The next step is to load a T5 tokenizer to process the English-French language pairs:"),jt=h(),q(We.$$.fragment),Qe=h(),Ve=i("p"),Nt=o("The preprocessing function you want to create needs to:"),qt=h(),Ze=i("ol"),es=i("li"),sa=o("Prefix the input with a prompt so T5 knows this is a translation task. Some models capable of multiple NLP tasks require prompting for specific tasks."),aa=h(),ts=i("li"),ra=o("Tokenize the input (English) and target (French) separately because you can\u2019t tokenize French text with a tokenizer pretrained on an English vocabulary."),oa=h(),Et=i("li"),na=o("Truncate sequences to be no longer than the maximum length set by the "),ss=i("code"),la=o("max_length"),pa=o(" parameter."),ks=h(),q(Tt.$$.fragment),bs=h(),Ce=i("p"),ia=o("To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),xt=i("a"),fa=o("map"),ma=o(" method. You can speed up the "),as=i("code"),ua=o("map"),ca=o(" function by setting "),rs=i("code"),ha=o("batched=True"),da=o(" to process multiple elements of the dataset at once:"),ws=h(),q(St.$$.fragment),vs=h(),Je=i("p"),_a=o("Now create a batch of examples using "),Yt=i("a"),ga=o("DataCollatorForSeq2Seq"),$a=o(". It\u2019s more efficient to "),os=i("em"),ka=o("dynamically pad"),ba=o(" the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximium length."),ys=h(),q(it.$$.fragment),js=h(),et=i("h2"),ft=i("a"),ns=i("span"),q(At.$$.fragment),wa=h(),ls=i("span"),va=o("Evaluate"),qs=h(),De=i("p"),ya=o("Including a metric during training is often helpful for evaluating your model\u2019s performance. You can quickly load a evaluation method with the \u{1F917} "),zt=i("a"),ja=o("Evaluate"),qa=o(" library. For this task, load the "),Ft=i("a"),Ea=o("SacreBLEU"),Ta=o(" metric (see the \u{1F917} Evaluate "),Pt=i("a"),xa=o("quick tour"),Sa=o(" to learn more about how to load and compute a metric):"),Es=h(),q(Mt.$$.fragment),Ts=h(),mt=i("p"),Aa=o("Then create a function that passes your predictions and labels to "),Lt=i("a"),za=o("compute"),Fa=o(" to calculate the SacreBLEU score:"),xs=h(),q(Ct.$$.fragment),Ss=h(),ut=i("p"),Pa=o("Your "),ps=i("code"),Ma=o("compute_metrics"),La=o(" function is ready to go now, and you\u2019ll return to it when you setup your training."),As=h(),tt=i("h2"),ct=i("a"),is=i("span"),q(Dt.$$.fragment),Ca=h(),fs=i("span"),Da=o("Train"),zs=h(),q(ht.$$.fragment),Fs=h(),q(dt.$$.fragment),Ps=h(),st=i("h2"),_t=i("a"),ms=i("span"),q(Ot.$$.fragment),Oa=h(),us=i("span"),Ia=o("Inference"),Ms=h(),Kt=i("p"),Ha=o("Great, now that you\u2019ve finetuned a model, you can use it for inference!"),Ls=h(),Gt=i("p"),Ua=o("Come up with some text you\u2019d like to translate to another language. For T5, you need to prefix your input depending on the task you\u2019re working on. For translation from English to French, you should prefix your input as shown below:"),Cs=h(),q(It.$$.fragment),Ds=h(),Re=i("p"),Ba=o("The simplest way to try out your finetuned model for inference is to use it in a "),Wt=i("a"),Na=o("pipeline()"),Ya=o(". Instantiate a "),cs=i("code"),Ka=o("pipeline"),Ga=o(" for translation with your model, and pass your text to it:"),Os=h(),q(Ht.$$.fragment),Is=h(),gt=i("p"),Wa=o("You can also manually replicate the results of the "),hs=i("code"),Za=o("pipeline"),Ja=o(" if you\u2019d like:"),Hs=h(),q($t.$$.fragment),this.h()},l(e){const u=Wr('[data-svelte="svelte-1phssyn"]',document.head);r=f(u,"META",{name:!0,content:!0}),u.forEach(t),_=d(e),a=f(e,"H1",{class:!0});var Ut=m(a);g=f(Ut,"A",{id:!0,class:!0,href:!0});var ds=m(g);b=f(ds,"SPAN",{});var _s=m(b);E(y.$$.fragment,_s),_s.forEach(t),ds.forEach(t),v=d(Ut),F=f(Ut,"SPAN",{});var gs=m(F);w=n(gs,"Translation"),gs.forEach(t),Ut.forEach(t),M=d(e),E(P.$$.fragment,e),Y=d(e),U=f(e,"P",{});var $s=m(U);K=n($s,"Translation converts a sequence of text from one language to another. It is one of several tasks you can formulate as a sequence-to-sequence problem, a powerful framework for returning some output from an input, like translation or summarization. Translation systems are commonly used for translation between different language texts, but it can also be used for speech or some combination in between like text-to-speech or speech-to-text."),$s.forEach(t),X=d(e),Q=f(e,"P",{});var Qa=m(Q);R=n(Qa,"This guide will show you how to:"),Qa.forEach(t),I=d(e),G=f(e,"OL",{});var Bs=m(G);L=f(Bs,"LI",{});var Zt=m(L);se=n(Zt,"Finetune "),B=f(Zt,"A",{href:!0,rel:!0});var Va=m(B);N=n(Va,"T5"),Va.forEach(t),C=n(Zt," on the English-French subset of the "),c=f(Zt,"A",{href:!0,rel:!0});var er=m(c);j=n(er,"OPUS Books"),er.forEach(t),oe=n(Zt," dataset to translate English text to French."),Zt.forEach(t),Z=d(Bs),ee=f(Bs,"LI",{});var tr=m(ee);le=n(tr,"Use your finetuned model for inference."),tr.forEach(t),Bs.forEach(t),V=d(e),E(fe.$$.fragment,e),te=d(e),ve=f(e,"P",{});var sr=m(ve);me=n(sr,"Before you begin, make sure you have all the necessary libraries installed:"),sr.forEach(t),Ae=d(e),E($e.$$.fragment,e),ye=d(e),J=f(e,"P",{});var ar=m(J);ze=n(ar,"We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login:"),ar.forEach(t),ne=d(e),E(ke.$$.fragment,e),be=d(e),ae=f(e,"H2",{class:!0});var Ns=m(ae);he=f(Ns,"A",{id:!0,class:!0,href:!0});var rr=m(he);je=f(rr,"SPAN",{});var or=m(je);E(re.$$.fragment,or),or.forEach(t),rr.forEach(t),de=d(Ns),k=f(Ns,"SPAN",{});var nr=m(k);D=n(nr,"Load OPUS Books dataset"),nr.forEach(t),Ns.forEach(t),pe=d(e),W=f(e,"P",{});var Ys=m(W);Ne=n(Ys,"Start by loading the English-French subset of the "),qe=f(Ys,"A",{href:!0,rel:!0});var lr=m(qe);Oe=n(lr,"OPUS Books"),lr.forEach(t),Ee=n(Ys," dataset from the \u{1F917} Datasets library:"),Ys.forEach(t),_e=d(e),E(ue.$$.fragment,e),Ie=d(e),ie=f(e,"P",{});var Ks=m(ie);Te=n(Ks,"Split the dataset into a train and test set with the "),ce=f(Ks,"A",{href:!0,rel:!0});var pr=m(ce);we=n(pr,"train_test_split"),pr.forEach(t),wt=n(Ks," method:"),Ks.forEach(t),Fe=d(e),E(Se.$$.fragment,e),at=d(e),Ye=f(e,"P",{});var ir=m(Ye);rt=n(ir,"Then take a look at an example:"),ir.forEach(t),Pe=d(e),E(Me.$$.fragment,e),He=d(e),Ue=f(e,"P",{});var Ra=m(Ue);Ke=f(Ra,"CODE",{});var fr=m(Ke);l=n(fr,"translation"),fr.forEach(t),z=n(Ra,": an English and French translation of the text."),Ra.forEach(t),Ge=d(e),ge=f(e,"H2",{class:!0});var Gs=m(ge);Be=f(Gs,"A",{id:!0,class:!0,href:!0});var mr=m(Be);ot=f(mr,"SPAN",{});var ur=m(ot);E(Le.$$.fragment,ur),ur.forEach(t),mr.forEach(t),Bt=d(Gs),nt=f(Gs,"SPAN",{});var cr=m(nt);lt=n(cr,"Preprocess"),cr.forEach(t),Gs.forEach(t),vt=d(e),E(xe.$$.fragment,e),yt=d(e),Xe=f(e,"P",{});var hr=m(Xe);pt=n(hr,"The next step is to load a T5 tokenizer to process the English-French language pairs:"),hr.forEach(t),jt=d(e),E(We.$$.fragment,e),Qe=d(e),Ve=f(e,"P",{});var dr=m(Ve);Nt=n(dr,"The preprocessing function you want to create needs to:"),dr.forEach(t),qt=d(e),Ze=f(e,"OL",{});var Jt=m(Ze);es=f(Jt,"LI",{});var _r=m(es);sa=n(_r,"Prefix the input with a prompt so T5 knows this is a translation task. Some models capable of multiple NLP tasks require prompting for specific tasks."),_r.forEach(t),aa=d(Jt),ts=f(Jt,"LI",{});var gr=m(ts);ra=n(gr,"Tokenize the input (English) and target (French) separately because you can\u2019t tokenize French text with a tokenizer pretrained on an English vocabulary."),gr.forEach(t),oa=d(Jt),Et=f(Jt,"LI",{});var Ws=m(Et);na=n(Ws,"Truncate sequences to be no longer than the maximum length set by the "),ss=f(Ws,"CODE",{});var $r=m(ss);la=n($r,"max_length"),$r.forEach(t),pa=n(Ws," parameter."),Ws.forEach(t),Jt.forEach(t),ks=d(e),E(Tt.$$.fragment,e),bs=d(e),Ce=f(e,"P",{});var kt=m(Ce);ia=n(kt,"To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),xt=f(kt,"A",{href:!0,rel:!0});var kr=m(xt);fa=n(kr,"map"),kr.forEach(t),ma=n(kt," method. You can speed up the "),as=f(kt,"CODE",{});var br=m(as);ua=n(br,"map"),br.forEach(t),ca=n(kt," function by setting "),rs=f(kt,"CODE",{});var wr=m(rs);ha=n(wr,"batched=True"),wr.forEach(t),da=n(kt," to process multiple elements of the dataset at once:"),kt.forEach(t),ws=d(e),E(St.$$.fragment,e),vs=d(e),Je=f(e,"P",{});var Rt=m(Je);_a=n(Rt,"Now create a batch of examples using "),Yt=f(Rt,"A",{href:!0});var vr=m(Yt);ga=n(vr,"DataCollatorForSeq2Seq"),vr.forEach(t),$a=n(Rt,". It\u2019s more efficient to "),os=f(Rt,"EM",{});var yr=m(os);ka=n(yr,"dynamically pad"),yr.forEach(t),ba=n(Rt," the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximium length."),Rt.forEach(t),ys=d(e),E(it.$$.fragment,e),js=d(e),et=f(e,"H2",{class:!0});var Zs=m(et);ft=f(Zs,"A",{id:!0,class:!0,href:!0});var jr=m(ft);ns=f(jr,"SPAN",{});var qr=m(ns);E(At.$$.fragment,qr),qr.forEach(t),jr.forEach(t),wa=d(Zs),ls=f(Zs,"SPAN",{});var Er=m(ls);va=n(Er,"Evaluate"),Er.forEach(t),Zs.forEach(t),qs=d(e),De=f(e,"P",{});var bt=m(De);ya=n(bt,"Including a metric during training is often helpful for evaluating your model\u2019s performance. You can quickly load a evaluation method with the \u{1F917} "),zt=f(bt,"A",{href:!0,rel:!0});var Tr=m(zt);ja=n(Tr,"Evaluate"),Tr.forEach(t),qa=n(bt," library. For this task, load the "),Ft=f(bt,"A",{href:!0,rel:!0});var xr=m(Ft);Ea=n(xr,"SacreBLEU"),xr.forEach(t),Ta=n(bt," metric (see the \u{1F917} Evaluate "),Pt=f(bt,"A",{href:!0,rel:!0});var Sr=m(Pt);xa=n(Sr,"quick tour"),Sr.forEach(t),Sa=n(bt," to learn more about how to load and compute a metric):"),bt.forEach(t),Es=d(e),E(Mt.$$.fragment,e),Ts=d(e),mt=f(e,"P",{});var Js=m(mt);Aa=n(Js,"Then create a function that passes your predictions and labels to "),Lt=f(Js,"A",{href:!0,rel:!0});var Ar=m(Lt);za=n(Ar,"compute"),Ar.forEach(t),Fa=n(Js," to calculate the SacreBLEU score:"),Js.forEach(t),xs=d(e),E(Ct.$$.fragment,e),Ss=d(e),ut=f(e,"P",{});var Rs=m(ut);Pa=n(Rs,"Your "),ps=f(Rs,"CODE",{});var zr=m(ps);Ma=n(zr,"compute_metrics"),zr.forEach(t),La=n(Rs," function is ready to go now, and you\u2019ll return to it when you setup your training."),Rs.forEach(t),As=d(e),tt=f(e,"H2",{class:!0});var Xs=m(tt);ct=f(Xs,"A",{id:!0,class:!0,href:!0});var Fr=m(ct);is=f(Fr,"SPAN",{});var Pr=m(is);E(Dt.$$.fragment,Pr),Pr.forEach(t),Fr.forEach(t),Ca=d(Xs),fs=f(Xs,"SPAN",{});var Mr=m(fs);Da=n(Mr,"Train"),Mr.forEach(t),Xs.forEach(t),zs=d(e),E(ht.$$.fragment,e),Fs=d(e),E(dt.$$.fragment,e),Ps=d(e),st=f(e,"H2",{class:!0});var Qs=m(st);_t=f(Qs,"A",{id:!0,class:!0,href:!0});var Lr=m(_t);ms=f(Lr,"SPAN",{});var Cr=m(ms);E(Ot.$$.fragment,Cr),Cr.forEach(t),Lr.forEach(t),Oa=d(Qs),us=f(Qs,"SPAN",{});var Dr=m(us);Ia=n(Dr,"Inference"),Dr.forEach(t),Qs.forEach(t),Ms=d(e),Kt=f(e,"P",{});var Or=m(Kt);Ha=n(Or,"Great, now that you\u2019ve finetuned a model, you can use it for inference!"),Or.forEach(t),Ls=d(e),Gt=f(e,"P",{});var Ir=m(Gt);Ua=n(Ir,"Come up with some text you\u2019d like to translate to another language. For T5, you need to prefix your input depending on the task you\u2019re working on. For translation from English to French, you should prefix your input as shown below:"),Ir.forEach(t),Cs=d(e),E(It.$$.fragment,e),Ds=d(e),Re=f(e,"P",{});var Xt=m(Re);Ba=n(Xt,"The simplest way to try out your finetuned model for inference is to use it in a "),Wt=f(Xt,"A",{href:!0});var Hr=m(Wt);Na=n(Hr,"pipeline()"),Hr.forEach(t),Ya=n(Xt,". Instantiate a "),cs=f(Xt,"CODE",{});var Ur=m(cs);Ka=n(Ur,"pipeline"),Ur.forEach(t),Ga=n(Xt," for translation with your model, and pass your text to it:"),Xt.forEach(t),Os=d(e),E(Ht.$$.fragment,e),Is=d(e),gt=f(e,"P",{});var Vs=m(gt);Wa=n(Vs,"You can also manually replicate the results of the "),hs=f(Vs,"CODE",{});var Br=m(hs);Za=n(Br,"pipeline"),Br.forEach(t),Ja=n(Vs," if you\u2019d like:"),Vs.forEach(t),Hs=d(e),E($t.$$.fragment,e),this.h()},h(){$(r,"name","hf:doc:metadata"),$(r,"content",JSON.stringify(uo)),$(g,"id","translation"),$(g,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),$(g,"href","#translation"),$(a,"class","relative group"),$(B,"href","https://huggingface.co/t5-small"),$(B,"rel","nofollow"),$(c,"href","https://huggingface.co/datasets/opus_books"),$(c,"rel","nofollow"),$(he,"id","load-opus-books-dataset"),$(he,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),$(he,"href","#load-opus-books-dataset"),$(ae,"class","relative group"),$(qe,"href","https://huggingface.co/datasets/opus_books"),$(qe,"rel","nofollow"),$(ce,"href","https://huggingface.co/docs/datasets/v2.7.1/en/package_reference/main_classes#datasets.Dataset.train_test_split"),$(ce,"rel","nofollow"),$(Be,"id","preprocess"),$(Be,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),$(Be,"href","#preprocess"),$(ge,"class","relative group"),$(xt,"href","https://huggingface.co/docs/datasets/v2.7.1/en/package_reference/main_classes#datasets.Dataset.map"),$(xt,"rel","nofollow"),$(Yt,"href","/docs/transformers/v4.25.1/en/main_classes/data_collator#transformers.DataCollatorForSeq2Seq"),$(ft,"id","evaluate"),$(ft,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),$(ft,"href","#evaluate"),$(et,"class","relative group"),$(zt,"href","https://huggingface.co/docs/evaluate/index"),$(zt,"rel","nofollow"),$(Ft,"href","https://huggingface.co/spaces/evaluate-metric/sacrebleu"),$(Ft,"rel","nofollow"),$(Pt,"href","https://huggingface.co/docs/evaluate/a_quick_tour"),$(Pt,"rel","nofollow"),$(Lt,"href","https://huggingface.co/docs/evaluate/v0.3.0/en/package_reference/main_classes#evaluate.EvaluationModule.compute"),$(Lt,"rel","nofollow"),$(ct,"id","train"),$(ct,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),$(ct,"href","#train"),$(tt,"class","relative group"),$(_t,"id","inference"),$(_t,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),$(_t,"href","#inference"),$(st,"class","relative group"),$(Wt,"href","/docs/transformers/v4.25.1/en/main_classes/pipelines#transformers.pipeline")},m(e,u){s(document.head,r),p(e,_,u),p(e,a,u),s(a,g),s(g,b),T(y,b,null),s(a,v),s(a,F),s(F,w),p(e,M,u),T(P,e,u),p(e,Y,u),p(e,U,u),s(U,K),p(e,X,u),p(e,Q,u),s(Q,R),p(e,I,u),p(e,G,u),s(G,L),s(L,se),s(L,B),s(B,N),s(L,C),s(L,c),s(c,j),s(L,oe),s(G,Z),s(G,ee),s(ee,le),p(e,V,u),T(fe,e,u),p(e,te,u),p(e,ve,u),s(ve,me),p(e,Ae,u),T($e,e,u),p(e,ye,u),p(e,J,u),s(J,ze),p(e,ne,u),T(ke,e,u),p(e,be,u),p(e,ae,u),s(ae,he),s(he,je),T(re,je,null),s(ae,de),s(ae,k),s(k,D),p(e,pe,u),p(e,W,u),s(W,Ne),s(W,qe),s(qe,Oe),s(W,Ee),p(e,_e,u),T(ue,e,u),p(e,Ie,u),p(e,ie,u),s(ie,Te),s(ie,ce),s(ce,we),s(ie,wt),p(e,Fe,u),T(Se,e,u),p(e,at,u),p(e,Ye,u),s(Ye,rt),p(e,Pe,u),T(Me,e,u),p(e,He,u),p(e,Ue,u),s(Ue,Ke),s(Ke,l),s(Ue,z),p(e,Ge,u),p(e,ge,u),s(ge,Be),s(Be,ot),T(Le,ot,null),s(ge,Bt),s(ge,nt),s(nt,lt),p(e,vt,u),T(xe,e,u),p(e,yt,u),p(e,Xe,u),s(Xe,pt),p(e,jt,u),T(We,e,u),p(e,Qe,u),p(e,Ve,u),s(Ve,Nt),p(e,qt,u),p(e,Ze,u),s(Ze,es),s(es,sa),s(Ze,aa),s(Ze,ts),s(ts,ra),s(Ze,oa),s(Ze,Et),s(Et,na),s(Et,ss),s(ss,la),s(Et,pa),p(e,ks,u),T(Tt,e,u),p(e,bs,u),p(e,Ce,u),s(Ce,ia),s(Ce,xt),s(xt,fa),s(Ce,ma),s(Ce,as),s(as,ua),s(Ce,ca),s(Ce,rs),s(rs,ha),s(Ce,da),p(e,ws,u),T(St,e,u),p(e,vs,u),p(e,Je,u),s(Je,_a),s(Je,Yt),s(Yt,ga),s(Je,$a),s(Je,os),s(os,ka),s(Je,ba),p(e,ys,u),T(it,e,u),p(e,js,u),p(e,et,u),s(et,ft),s(ft,ns),T(At,ns,null),s(et,wa),s(et,ls),s(ls,va),p(e,qs,u),p(e,De,u),s(De,ya),s(De,zt),s(zt,ja),s(De,qa),s(De,Ft),s(Ft,Ea),s(De,Ta),s(De,Pt),s(Pt,xa),s(De,Sa),p(e,Es,u),T(Mt,e,u),p(e,Ts,u),p(e,mt,u),s(mt,Aa),s(mt,Lt),s(Lt,za),s(mt,Fa),p(e,xs,u),T(Ct,e,u),p(e,Ss,u),p(e,ut,u),s(ut,Pa),s(ut,ps),s(ps,Ma),s(ut,La),p(e,As,u),p(e,tt,u),s(tt,ct),s(ct,is),T(Dt,is,null),s(tt,Ca),s(tt,fs),s(fs,Da),p(e,zs,u),T(ht,e,u),p(e,Fs,u),T(dt,e,u),p(e,Ps,u),p(e,st,u),s(st,_t),s(_t,ms),T(Ot,ms,null),s(st,Oa),s(st,us),s(us,Ia),p(e,Ms,u),p(e,Kt,u),s(Kt,Ha),p(e,Ls,u),p(e,Gt,u),s(Gt,Ua),p(e,Cs,u),T(It,e,u),p(e,Ds,u),p(e,Re,u),s(Re,Ba),s(Re,Wt),s(Wt,Na),s(Re,Ya),s(Re,cs),s(cs,Ka),s(Re,Ga),p(e,Os,u),T(Ht,e,u),p(e,Is,u),p(e,gt,u),s(gt,Wa),s(gt,hs),s(hs,Za),s(gt,Ja),p(e,Hs,u),T($t,e,u),Us=!0},p(e,[u]){const Ut={};u&2&&(Ut.$$scope={dirty:u,ctx:e}),fe.$set(Ut);const ds={};u&2&&(ds.$$scope={dirty:u,ctx:e}),it.$set(ds);const _s={};u&2&&(_s.$$scope={dirty:u,ctx:e}),ht.$set(_s);const gs={};u&2&&(gs.$$scope={dirty:u,ctx:e}),dt.$set(gs);const $s={};u&2&&($s.$$scope={dirty:u,ctx:e}),$t.$set($s)},i(e){Us||(x(y.$$.fragment,e),x(P.$$.fragment,e),x(fe.$$.fragment,e),x($e.$$.fragment,e),x(ke.$$.fragment,e),x(re.$$.fragment,e),x(ue.$$.fragment,e),x(Se.$$.fragment,e),x(Me.$$.fragment,e),x(Le.$$.fragment,e),x(xe.$$.fragment,e),x(We.$$.fragment,e),x(Tt.$$.fragment,e),x(St.$$.fragment,e),x(it.$$.fragment,e),x(At.$$.fragment,e),x(Mt.$$.fragment,e),x(Ct.$$.fragment,e),x(Dt.$$.fragment,e),x(ht.$$.fragment,e),x(dt.$$.fragment,e),x(Ot.$$.fragment,e),x(It.$$.fragment,e),x(Ht.$$.fragment,e),x($t.$$.fragment,e),Us=!0)},o(e){S(y.$$.fragment,e),S(P.$$.fragment,e),S(fe.$$.fragment,e),S($e.$$.fragment,e),S(ke.$$.fragment,e),S(re.$$.fragment,e),S(ue.$$.fragment,e),S(Se.$$.fragment,e),S(Me.$$.fragment,e),S(Le.$$.fragment,e),S(xe.$$.fragment,e),S(We.$$.fragment,e),S(Tt.$$.fragment,e),S(St.$$.fragment,e),S(it.$$.fragment,e),S(At.$$.fragment,e),S(Mt.$$.fragment,e),S(Ct.$$.fragment,e),S(Dt.$$.fragment,e),S(ht.$$.fragment,e),S(dt.$$.fragment,e),S(Ot.$$.fragment,e),S(It.$$.fragment,e),S(Ht.$$.fragment,e),S($t.$$.fragment,e),Us=!1},d(e){t(r),e&&t(_),e&&t(a),A(y),e&&t(M),A(P,e),e&&t(Y),e&&t(U),e&&t(X),e&&t(Q),e&&t(I),e&&t(G),e&&t(V),A(fe,e),e&&t(te),e&&t(ve),e&&t(Ae),A($e,e),e&&t(ye),e&&t(J),e&&t(ne),A(ke,e),e&&t(be),e&&t(ae),A(re),e&&t(pe),e&&t(W),e&&t(_e),A(ue,e),e&&t(Ie),e&&t(ie),e&&t(Fe),A(Se,e),e&&t(at),e&&t(Ye),e&&t(Pe),A(Me,e),e&&t(He),e&&t(Ue),e&&t(Ge),e&&t(ge),A(Le),e&&t(vt),A(xe,e),e&&t(yt),e&&t(Xe),e&&t(jt),A(We,e),e&&t(Qe),e&&t(Ve),e&&t(qt),e&&t(Ze),e&&t(ks),A(Tt,e),e&&t(bs),e&&t(Ce),e&&t(ws),A(St,e),e&&t(vs),e&&t(Je),e&&t(ys),A(it,e),e&&t(js),e&&t(et),A(At),e&&t(qs),e&&t(De),e&&t(Es),A(Mt,e),e&&t(Ts),e&&t(mt),e&&t(xs),A(Ct,e),e&&t(Ss),e&&t(ut),e&&t(As),e&&t(tt),A(Dt),e&&t(zs),A(ht,e),e&&t(Fs),A(dt,e),e&&t(Ps),e&&t(st),A(Ot),e&&t(Ms),e&&t(Kt),e&&t(Ls),e&&t(Gt),e&&t(Cs),A(It,e),e&&t(Ds),e&&t(Re),e&&t(Os),A(Ht,e),e&&t(Is),e&&t(gt),e&&t(Hs),A($t,e)}}}const uo={local:"translation",sections:[{local:"load-opus-books-dataset",title:"Load OPUS Books dataset"},{local:"preprocess",title:"Preprocess"},{local:"evaluate",title:"Evaluate"},{local:"train",title:"Train"},{local:"inference",title:"Inference"}],title:"Translation"};function co(O){return Zr(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class wo extends Yr{constructor(r){super();Kr(this,r,co,mo,Gr,{})}}export{wo as default,uo as metadata};
