import{S as Kr,i as Wr,s as Rr,e as n,k as _,w as E,t as a,M as Xr,c as p,d as s,m as g,a as i,x as q,h as r,b as d,G as t,g as m,y as T,q as x,o as S,B as A,v as Zr,L as sa}from"../../chunks/vendor-hf-doc-builder.js";import{T as ta}from"../../chunks/Tip-hf-doc-builder.js";import{Y as Gr}from"../../chunks/Youtube-hf-doc-builder.js";import{I as Vt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as Z}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Jr}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";import{F as Qa,M as Qt}from"../../chunks/Markdown-hf-doc-builder.js";function Vr(R){let l,c,o,$,b,j,z,B,v,M,F,U,N,O,C,ne,Q,D,ae,K,W,H,Y,I,h,y,J,ee,_e,X,te,Te,re,be,ge,$e,Ie,Le,G,De,pe,Fe,Be,ie,fe,ue,se,oe,me,we,ve,xe,ye,le,k,L,ke,Ce,Se,V,Ae,je,he,Me,de,Pe,Ne,Ee,ze,ce;return{c(){l=a(`The task illustrated in this tutorial is supported by the following model architectures:

`),c=n("p"),o=n("a"),$=a("BART"),b=a(", "),j=n("a"),z=a("BigBird-Pegasus"),B=a(", "),v=n("a"),M=a("Blenderbot"),F=a(", "),U=n("a"),N=a("BlenderbotSmall"),O=a(", "),C=n("a"),ne=a("Encoder decoder"),Q=a(", "),D=n("a"),ae=a("FairSeq Machine-Translation"),K=a(", "),W=n("a"),H=a("GPTSAN-japanese"),Y=a(", "),I=n("a"),h=a("LED"),y=a(", "),J=n("a"),ee=a("LongT5"),_e=a(", "),X=n("a"),te=a("M2M100"),Te=a(", "),re=n("a"),be=a("Marian"),ge=a(", "),$e=n("a"),Ie=a("mBART"),Le=a(", "),G=n("a"),De=a("MT5"),pe=a(", "),Fe=n("a"),Be=a("MVP"),ie=a(", "),fe=n("a"),ue=a("NLLB"),se=a(", "),oe=n("a"),me=a("NLLB-MOE"),we=a(", "),ve=n("a"),xe=a("Pegasus"),ye=a(", "),le=n("a"),k=a("PEGASUS-X"),L=a(", "),ke=n("a"),Ce=a("PLBart"),Se=a(", "),V=n("a"),Ae=a("ProphetNet"),je=a(", "),he=n("a"),Me=a("SwitchTransformers"),de=a(", "),Pe=n("a"),Ne=a("T5"),Ee=a(", "),ze=n("a"),ce=a("XLM-ProphetNet"),this.h()},l(qe){l=r(qe,`The task illustrated in this tutorial is supported by the following model architectures:

`),c=p(qe,"P",{});var w=i(c);o=p(w,"A",{href:!0});var Ue=i(o);$=r(Ue,"BART"),Ue.forEach(s),b=r(w,", "),j=p(w,"A",{href:!0});var Xe=i(j);z=r(Xe,"BigBird-Pegasus"),Xe.forEach(s),B=r(w,", "),v=p(w,"A",{href:!0});var f=i(v);M=r(f,"Blenderbot"),f.forEach(s),F=r(w,", "),U=p(w,"A",{href:!0});var P=i(U);N=r(P,"BlenderbotSmall"),P.forEach(s),O=r(w,", "),C=p(w,"A",{href:!0});var Ze=i(C);ne=r(Ze,"Encoder decoder"),Ze.forEach(s),Q=r(w,", "),D=p(w,"A",{href:!0});var Ye=i(D);ae=r(Ye,"FairSeq Machine-Translation"),Ye.forEach(s),K=r(w,", "),W=p(w,"A",{href:!0});var lt=i(W);H=r(lt,"GPTSAN-japanese"),lt.forEach(s),Y=r(w,", "),I=p(w,"A",{href:!0});var He=i(I);h=r(He,"LED"),He.forEach(s),y=r(w,", "),J=p(w,"A",{href:!0});var Oe=i(J);ee=r(Oe,"LongT5"),Oe.forEach(s),_e=r(w,", "),X=p(w,"A",{href:!0});var at=i(X);te=r(at,"M2M100"),at.forEach(s),Te=r(w,", "),re=p(w,"A",{href:!0});var Ge=i(re);be=r(Ge,"Marian"),Ge.forEach(s),ge=r(w,", "),$e=p(w,"A",{href:!0});var rt=i($e);Ie=r(rt,"mBART"),rt.forEach(s),Le=r(w,", "),G=p(w,"A",{href:!0});var ot=i(G);De=r(ot,"MT5"),ot.forEach(s),pe=r(w,", "),Fe=p(w,"A",{href:!0});var Ke=i(Fe);Be=r(Ke,"MVP"),Ke.forEach(s),ie=r(w,", "),fe=p(w,"A",{href:!0});var nt=i(fe);ue=r(nt,"NLLB"),nt.forEach(s),se=r(w,", "),oe=p(w,"A",{href:!0});var We=i(oe);me=r(We,"NLLB-MOE"),We.forEach(s),we=r(w,", "),ve=p(w,"A",{href:!0});var Je=i(ve);xe=r(Je,"Pegasus"),Je.forEach(s),ye=r(w,", "),le=p(w,"A",{href:!0});var Ve=i(le);k=r(Ve,"PEGASUS-X"),Ve.forEach(s),L=r(w,", "),ke=p(w,"A",{href:!0});var ct=i(ke);Ce=r(ct,"PLBart"),ct.forEach(s),Se=r(w,", "),V=p(w,"A",{href:!0});var Qe=i(V);Ae=r(Qe,"ProphetNet"),Qe.forEach(s),je=r(w,", "),he=p(w,"A",{href:!0});var Re=i(he);Me=r(Re,"SwitchTransformers"),Re.forEach(s),de=r(w,", "),Pe=p(w,"A",{href:!0});var pt=i(Pe);Ne=r(pt,"T5"),pt.forEach(s),Ee=r(w,", "),ze=p(w,"A",{href:!0});var et=i(ze);ce=r(et,"XLM-ProphetNet"),et.forEach(s),w.forEach(s),this.h()},h(){d(o,"href","../model_doc/bart"),d(j,"href","../model_doc/bigbird_pegasus"),d(v,"href","../model_doc/blenderbot"),d(U,"href","../model_doc/blenderbot-small"),d(C,"href","../model_doc/encoder-decoder"),d(D,"href","../model_doc/fsmt"),d(W,"href","../model_doc/gptsan-japanese"),d(I,"href","../model_doc/led"),d(J,"href","../model_doc/longt5"),d(X,"href","../model_doc/m2m_100"),d(re,"href","../model_doc/marian"),d($e,"href","../model_doc/mbart"),d(G,"href","../model_doc/mt5"),d(Fe,"href","../model_doc/mvp"),d(fe,"href","../model_doc/nllb"),d(oe,"href","../model_doc/nllb-moe"),d(ve,"href","../model_doc/pegasus"),d(le,"href","../model_doc/pegasus_x"),d(ke,"href","../model_doc/plbart"),d(V,"href","../model_doc/prophetnet"),d(he,"href","../model_doc/switch_transformers"),d(Pe,"href","../model_doc/t5"),d(ze,"href","../model_doc/xlm-prophetnet")},m(qe,w){m(qe,l,w),m(qe,c,w),t(c,o),t(o,$),t(c,b),t(c,j),t(j,z),t(c,B),t(c,v),t(v,M),t(c,F),t(c,U),t(U,N),t(c,O),t(c,C),t(C,ne),t(c,Q),t(c,D),t(D,ae),t(c,K),t(c,W),t(W,H),t(c,Y),t(c,I),t(I,h),t(c,y),t(c,J),t(J,ee),t(c,_e),t(c,X),t(X,te),t(c,Te),t(c,re),t(re,be),t(c,ge),t(c,$e),t($e,Ie),t(c,Le),t(c,G),t(G,De),t(c,pe),t(c,Fe),t(Fe,Be),t(c,ie),t(c,fe),t(fe,ue),t(c,se),t(c,oe),t(oe,me),t(c,we),t(c,ve),t(ve,xe),t(c,ye),t(c,le),t(le,k),t(c,L),t(c,ke),t(ke,Ce),t(c,Se),t(c,V),t(V,Ae),t(c,je),t(c,he),t(he,Me),t(c,de),t(c,Pe),t(Pe,Ne),t(c,Ee),t(c,ze),t(ze,ce)},d(qe){qe&&s(l),qe&&s(c)}}}function Qr(R){let l,c;return l=new Z({props:{code:`from transformers import DataCollatorForSeq2Seq

data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint)`,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=checkpoint)`}}),{c(){E(l.$$.fragment)},l(o){q(l.$$.fragment,o)},m(o,$){T(l,o,$),c=!0},p:sa,i(o){c||(x(l.$$.fragment,o),c=!0)},o(o){S(l.$$.fragment,o),c=!1},d(o){A(l,o)}}}function eo(R){let l,c;return l=new Qt({props:{$$slots:{default:[Qr]},$$scope:{ctx:R}}}),{c(){E(l.$$.fragment)},l(o){q(l.$$.fragment,o)},m(o,$){T(l,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),l.$set(b)},i(o){c||(x(l.$$.fragment,o),c=!0)},o(o){S(l.$$.fragment,o),c=!1},d(o){A(l,o)}}}function to(R){let l,c;return l=new Z({props:{code:`from transformers import DataCollatorForSeq2Seq

data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint, 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=checkpoint, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`}}),{c(){E(l.$$.fragment)},l(o){q(l.$$.fragment,o)},m(o,$){T(l,o,$),c=!0},p:sa,i(o){c||(x(l.$$.fragment,o),c=!0)},o(o){S(l.$$.fragment,o),c=!1},d(o){A(l,o)}}}function so(R){let l,c;return l=new Qt({props:{$$slots:{default:[to]},$$scope:{ctx:R}}}),{c(){E(l.$$.fragment)},l(o){q(l.$$.fragment,o)},m(o,$){T(l,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),l.$set(b)},i(o){c||(x(l.$$.fragment,o),c=!0)},o(o){S(l.$$.fragment,o),c=!1},d(o){A(l,o)}}}function ao(R){let l,c,o,$,b,j,z,B;return{c(){l=n("p"),c=a("If you aren\u2019t familiar with finetuning a model with the "),o=n("a"),$=a("Trainer"),b=a(", take a look at the basic tutorial "),j=n("a"),z=a("here"),B=a("!"),this.h()},l(v){l=p(v,"P",{});var M=i(l);c=r(M,"If you aren\u2019t familiar with finetuning a model with the "),o=p(M,"A",{href:!0});var F=i(o);$=r(F,"Trainer"),F.forEach(s),b=r(M,", take a look at the basic tutorial "),j=p(M,"A",{href:!0});var U=i(j);z=r(U,"here"),U.forEach(s),B=r(M,"!"),M.forEach(s),this.h()},h(){d(o,"href","/docs/transformers/v4.29.0/en/main_classes/trainer#transformers.Trainer"),d(j,"href","../training#train-with-pytorch-trainer")},m(v,M){m(v,l,M),t(l,c),t(l,o),t(o,$),t(l,b),t(l,j),t(j,z),t(l,B)},d(v){v&&s(l)}}}function ro(R){let l,c,o,$,b,j,z,B,v,M,F,U,N,O,C,ne,Q,D,ae,K,W,H,Y,I,h,y,J,ee,_e,X,te,Te,re,be,ge,$e,Ie,Le,G,De,pe,Fe,Be,ie,fe,ue,se,oe,me,we,ve,xe,ye,le;return l=new ta({props:{$$slots:{default:[ao]},$$scope:{ctx:R}}}),v=new Z({props:{code:`from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer

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

model = TFAutoModelForSeq2SeqLM.from_pretrained(checkpoint)`,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(checkpoint)`}}),H=new Z({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>)`}}),te=new Z({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)`}}),oe=new Z({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)`}}),L=new Z({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>)`}}),Ae=new Z({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]'}}),ce=new Z({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(){E(l.$$.fragment),c=a(`
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:

	`),E(o.$$.fragment),$=_(),b=n("p"),j=a("Then you can load T5 with "),z=n("a"),B=a("TFAutoModelForSeq2SeqLM"),v=a(":"),M=_(),E(F.$$.fragment),U=_(),N=n("p"),O=a("Convert your datasets to the "),C=n("code"),ne=a("tf.data.Dataset"),Q=a(" format with "),D=n("a"),ae=a("prepare_tf_dataset()"),K=a(":"),W=_(),E(H.$$.fragment),Y=_(),I=n("p"),h=a("Configure the model for training with "),y=n("a"),J=n("code"),ee=a("compile"),_e=a(":"),X=_(),E(te.$$.fragment),Te=_(),re=n("p"),be=a("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 "),ge=n("a"),$e=a("Keras callbacks"),Ie=a("."),Le=_(),G=n("p"),De=a("Pass your "),pe=n("code"),Fe=a("compute_metrics"),Be=a(" function to "),ie=n("a"),fe=a("KerasMetricCallback"),ue=a(":"),se=_(),E(oe.$$.fragment),me=_(),we=n("p"),ve=a("Specify where to push your model and tokenizer in the "),xe=n("a"),ye=a("PushToHubCallback"),le=a(":"),k=_(),E(L.$$.fragment),ke=_(),Ce=n("p"),Se=a("Then bundle your callbacks together:"),V=_(),E(Ae.$$.fragment),je=_(),he=n("p"),Me=a("Finally, you\u2019re ready to start training your model! Call "),de=n("a"),Pe=n("code"),Ne=a("fit"),Ee=a(" with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:"),ze=_(),E(ce.$$.fragment),qe=_(),w=n("p"),Ue=a("Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),this.h()},l(f){q(l.$$.fragment,f),c=r(f,`
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:

	`),q(o.$$.fragment,f),$=g(f),b=p(f,"P",{});var P=i(b);j=r(P,"Then you can load T5 with "),z=p(P,"A",{href:!0});var Ze=i(z);B=r(Ze,"TFAutoModelForSeq2SeqLM"),Ze.forEach(s),v=r(P,":"),P.forEach(s),M=g(f),q(F.$$.fragment,f),U=g(f),N=p(f,"P",{});var Ye=i(N);O=r(Ye,"Convert your datasets to the "),C=p(Ye,"CODE",{});var lt=i(C);ne=r(lt,"tf.data.Dataset"),lt.forEach(s),Q=r(Ye," format with "),D=p(Ye,"A",{href:!0});var He=i(D);ae=r(He,"prepare_tf_dataset()"),He.forEach(s),K=r(Ye,":"),Ye.forEach(s),W=g(f),q(H.$$.fragment,f),Y=g(f),I=p(f,"P",{});var Oe=i(I);h=r(Oe,"Configure the model for training with "),y=p(Oe,"A",{href:!0,rel:!0});var at=i(y);J=p(at,"CODE",{});var Ge=i(J);ee=r(Ge,"compile"),Ge.forEach(s),at.forEach(s),_e=r(Oe,":"),Oe.forEach(s),X=g(f),q(te.$$.fragment,f),Te=g(f),re=p(f,"P",{});var rt=i(re);be=r(rt,"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 "),ge=p(rt,"A",{href:!0});var ot=i(ge);$e=r(ot,"Keras callbacks"),ot.forEach(s),Ie=r(rt,"."),rt.forEach(s),Le=g(f),G=p(f,"P",{});var Ke=i(G);De=r(Ke,"Pass your "),pe=p(Ke,"CODE",{});var nt=i(pe);Fe=r(nt,"compute_metrics"),nt.forEach(s),Be=r(Ke," function to "),ie=p(Ke,"A",{href:!0});var We=i(ie);fe=r(We,"KerasMetricCallback"),We.forEach(s),ue=r(Ke,":"),Ke.forEach(s),se=g(f),q(oe.$$.fragment,f),me=g(f),we=p(f,"P",{});var Je=i(we);ve=r(Je,"Specify where to push your model and tokenizer in the "),xe=p(Je,"A",{href:!0});var Ve=i(xe);ye=r(Ve,"PushToHubCallback"),Ve.forEach(s),le=r(Je,":"),Je.forEach(s),k=g(f),q(L.$$.fragment,f),ke=g(f),Ce=p(f,"P",{});var ct=i(Ce);Se=r(ct,"Then bundle your callbacks together:"),ct.forEach(s),V=g(f),q(Ae.$$.fragment,f),je=g(f),he=p(f,"P",{});var Qe=i(he);Me=r(Qe,"Finally, you\u2019re ready to start training your model! Call "),de=p(Qe,"A",{href:!0,rel:!0});var Re=i(de);Pe=p(Re,"CODE",{});var pt=i(Pe);Ne=r(pt,"fit"),pt.forEach(s),Re.forEach(s),Ee=r(Qe," with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:"),Qe.forEach(s),ze=g(f),q(ce.$$.fragment,f),qe=g(f),w=p(f,"P",{});var et=i(w);Ue=r(et,"Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),et.forEach(s),this.h()},h(){d(z,"href","/docs/transformers/v4.29.0/en/model_doc/auto#transformers.TFAutoModelForSeq2SeqLM"),d(D,"href","/docs/transformers/v4.29.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset"),d(y,"href","https://keras.io/api/models/model_training_apis/#compile-method"),d(y,"rel","nofollow"),d(ge,"href","../main_classes/keras_callbacks"),d(ie,"href","/docs/transformers/v4.29.0/en/main_classes/keras_callbacks#transformers.KerasMetricCallback"),d(xe,"href","/docs/transformers/v4.29.0/en/main_classes/keras_callbacks#transformers.PushToHubCallback"),d(de,"href","https://keras.io/api/models/model_training_apis/#fit-method"),d(de,"rel","nofollow")},m(f,P){T(l,f,P),m(f,c,P),T(o,f,P),m(f,$,P),m(f,b,P),t(b,j),t(b,z),t(z,B),t(b,v),m(f,M,P),T(F,f,P),m(f,U,P),m(f,N,P),t(N,O),t(N,C),t(C,ne),t(N,Q),t(N,D),t(D,ae),t(N,K),m(f,W,P),T(H,f,P),m(f,Y,P),m(f,I,P),t(I,h),t(I,y),t(y,J),t(J,ee),t(I,_e),m(f,X,P),T(te,f,P),m(f,Te,P),m(f,re,P),t(re,be),t(re,ge),t(ge,$e),t(re,Ie),m(f,Le,P),m(f,G,P),t(G,De),t(G,pe),t(pe,Fe),t(G,Be),t(G,ie),t(ie,fe),t(G,ue),m(f,se,P),T(oe,f,P),m(f,me,P),m(f,we,P),t(we,ve),t(we,xe),t(xe,ye),t(we,le),m(f,k,P),T(L,f,P),m(f,ke,P),m(f,Ce,P),t(Ce,Se),m(f,V,P),T(Ae,f,P),m(f,je,P),m(f,he,P),t(he,Me),t(he,de),t(de,Pe),t(Pe,Ne),t(he,Ee),m(f,ze,P),T(ce,f,P),m(f,qe,P),m(f,w,P),t(w,Ue),Xe=!0},p(f,P){const Ze={};P&2&&(Ze.$$scope={dirty:P,ctx:f}),l.$set(Ze)},i(f){Xe||(x(l.$$.fragment,f),x(o.$$.fragment,f),x(F.$$.fragment,f),x(H.$$.fragment,f),x(te.$$.fragment,f),x(oe.$$.fragment,f),x(L.$$.fragment,f),x(Ae.$$.fragment,f),x(ce.$$.fragment,f),Xe=!0)},o(f){S(l.$$.fragment,f),S(o.$$.fragment,f),S(F.$$.fragment,f),S(H.$$.fragment,f),S(te.$$.fragment,f),S(oe.$$.fragment,f),S(L.$$.fragment,f),S(Ae.$$.fragment,f),S(ce.$$.fragment,f),Xe=!1},d(f){A(l,f),f&&s(c),A(o,f),f&&s($),f&&s(b),f&&s(M),A(F,f),f&&s(U),f&&s(N),f&&s(W),A(H,f),f&&s(Y),f&&s(I),f&&s(X),A(te,f),f&&s(Te),f&&s(re),f&&s(Le),f&&s(G),f&&s(se),A(oe,f),f&&s(me),f&&s(we),f&&s(k),A(L,f),f&&s(ke),f&&s(Ce),f&&s(V),A(Ae,f),f&&s(je),f&&s(he),f&&s(ze),A(ce,f),f&&s(qe),f&&s(w)}}}function po(R){let l,c;return l=new Qt({props:{$$slots:{default:[no]},$$scope:{ctx:R}}}),{c(){E(l.$$.fragment)},l(o){q(l.$$.fragment,o)},m(o,$){T(l,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),l.$set(b)},i(o){c||(x(l.$$.fragment,o),c=!0)},o(o){S(l.$$.fragment,o),c=!1},d(o){A(l,o)}}}function io(R){let l,c,o,$,b,j,z,B;return{c(){l=n("p"),c=a(`For a more in-depth example of how to finetune a model for translation, take a look at the corresponding
`),o=n("a"),$=a("PyTorch notebook"),b=a(`
or `),j=n("a"),z=a("TensorFlow notebook"),B=a("."),this.h()},l(v){l=p(v,"P",{});var M=i(l);c=r(M,`For a more in-depth example of how to finetune a model for translation, take a look at the corresponding
`),o=p(M,"A",{href:!0,rel:!0});var F=i(o);$=r(F,"PyTorch notebook"),F.forEach(s),b=r(M,`
or `),j=p(M,"A",{href:!0,rel:!0});var U=i(j);z=r(U,"TensorFlow notebook"),U.forEach(s),B=r(M,"."),M.forEach(s),this.h()},h(){d(o,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb"),d(o,"rel","nofollow"),d(j,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb"),d(j,"rel","nofollow")},m(v,M){m(v,l,M),t(l,c),t(l,o),t(o,$),t(l,b),t(l,j),t(j,z),t(l,B)},d(v){v&&s(l)}}}function fo(R){let l,c,o,$,b,j,z,B,v,M,F,U,N,O,C,ne,Q,D,ae,K,W,H,Y,I;return z=new Z({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`}}),D=new Z({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>)`}}),Y=new Z({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(){l=n("p"),c=a("Tokenize the text and return the "),o=n("code"),$=a("input_ids"),b=a(" as PyTorch tensors:"),j=_(),E(z.$$.fragment),B=_(),v=n("p"),M=a("Use the "),F=n("a"),U=a("generate()"),N=a(" method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),O=n("a"),C=a("Text Generation"),ne=a(" API."),Q=_(),E(D.$$.fragment),ae=_(),K=n("p"),W=a("Decode the generated token ids back into text:"),H=_(),E(Y.$$.fragment),this.h()},l(h){l=p(h,"P",{});var y=i(l);c=r(y,"Tokenize the text and return the "),o=p(y,"CODE",{});var J=i(o);$=r(J,"input_ids"),J.forEach(s),b=r(y," as PyTorch tensors:"),y.forEach(s),j=g(h),q(z.$$.fragment,h),B=g(h),v=p(h,"P",{});var ee=i(v);M=r(ee,"Use the "),F=p(ee,"A",{href:!0});var _e=i(F);U=r(_e,"generate()"),_e.forEach(s),N=r(ee," method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),O=p(ee,"A",{href:!0});var X=i(O);C=r(X,"Text Generation"),X.forEach(s),ne=r(ee," API."),ee.forEach(s),Q=g(h),q(D.$$.fragment,h),ae=g(h),K=p(h,"P",{});var te=i(K);W=r(te,"Decode the generated token ids back into text:"),te.forEach(s),H=g(h),q(Y.$$.fragment,h),this.h()},h(){d(F,"href","/docs/transformers/v4.29.0/en/main_classes/text_generation#transformers.GenerationMixin.generate"),d(O,"href","../main_classes/text_generation")},m(h,y){m(h,l,y),t(l,c),t(l,o),t(o,$),t(l,b),m(h,j,y),T(z,h,y),m(h,B,y),m(h,v,y),t(v,M),t(v,F),t(F,U),t(v,N),t(v,O),t(O,C),t(v,ne),m(h,Q,y),T(D,h,y),m(h,ae,y),m(h,K,y),t(K,W),m(h,H,y),T(Y,h,y),I=!0},p:sa,i(h){I||(x(z.$$.fragment,h),x(D.$$.fragment,h),x(Y.$$.fragment,h),I=!0)},o(h){S(z.$$.fragment,h),S(D.$$.fragment,h),S(Y.$$.fragment,h),I=!1},d(h){h&&s(l),h&&s(j),A(z,h),h&&s(B),h&&s(v),h&&s(Q),A(D,h),h&&s(ae),h&&s(K),h&&s(H),A(Y,h)}}}function mo(R){let l,c;return l=new Qt({props:{$$slots:{default:[fo]},$$scope:{ctx:R}}}),{c(){E(l.$$.fragment)},l(o){q(l.$$.fragment,o)},m(o,$){T(l,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),l.$set(b)},i(o){c||(x(l.$$.fragment,o),c=!0)},o(o){S(l.$$.fragment,o),c=!1},d(o){A(l,o)}}}function co(R){let l,c,o,$,b,j,z,B,v,M,F,U,N,O,C,ne,Q,D,ae,K,W,H,Y,I;return z=new Z({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`}}),D=new Z({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>)`}}),Y=new Z({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(){l=n("p"),c=a("Tokenize the text and return the "),o=n("code"),$=a("input_ids"),b=a(" as TensorFlow tensors:"),j=_(),E(z.$$.fragment),B=_(),v=n("p"),M=a("Use the "),F=n("a"),U=a("generate()"),N=a(" method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),O=n("a"),C=a("Text Generation"),ne=a(" API."),Q=_(),E(D.$$.fragment),ae=_(),K=n("p"),W=a("Decode the generated token ids back into text:"),H=_(),E(Y.$$.fragment),this.h()},l(h){l=p(h,"P",{});var y=i(l);c=r(y,"Tokenize the text and return the "),o=p(y,"CODE",{});var J=i(o);$=r(J,"input_ids"),J.forEach(s),b=r(y," as TensorFlow tensors:"),y.forEach(s),j=g(h),q(z.$$.fragment,h),B=g(h),v=p(h,"P",{});var ee=i(v);M=r(ee,"Use the "),F=p(ee,"A",{href:!0});var _e=i(F);U=r(_e,"generate()"),_e.forEach(s),N=r(ee," method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),O=p(ee,"A",{href:!0});var X=i(O);C=r(X,"Text Generation"),X.forEach(s),ne=r(ee," API."),ee.forEach(s),Q=g(h),q(D.$$.fragment,h),ae=g(h),K=p(h,"P",{});var te=i(K);W=r(te,"Decode the generated token ids back into text:"),te.forEach(s),H=g(h),q(Y.$$.fragment,h),this.h()},h(){d(F,"href","/docs/transformers/v4.29.0/en/main_classes/text_generation#transformers.TFGenerationMixin.generate"),d(O,"href","../main_classes/text_generation")},m(h,y){m(h,l,y),t(l,c),t(l,o),t(o,$),t(l,b),m(h,j,y),T(z,h,y),m(h,B,y),m(h,v,y),t(v,M),t(v,F),t(F,U),t(v,N),t(v,O),t(O,C),t(v,ne),m(h,Q,y),T(D,h,y),m(h,ae,y),m(h,K,y),t(K,W),m(h,H,y),T(Y,h,y),I=!0},p:sa,i(h){I||(x(z.$$.fragment,h),x(D.$$.fragment,h),x(Y.$$.fragment,h),I=!0)},o(h){S(z.$$.fragment,h),S(D.$$.fragment,h),S(Y.$$.fragment,h),I=!1},d(h){h&&s(l),h&&s(j),A(z,h),h&&s(B),h&&s(v),h&&s(Q),A(D,h),h&&s(ae),h&&s(K),h&&s(H),A(Y,h)}}}function uo(R){let l,c;return l=new Qt({props:{$$slots:{default:[co]},$$scope:{ctx:R}}}),{c(){E(l.$$.fragment)},l(o){q(l.$$.fragment,o)},m(o,$){T(l,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),l.$set(b)},i(o){c||(x(l.$$.fragment,o),c=!0)},o(o){S(l.$$.fragment,o),c=!1},d(o){A(l,o)}}}function ho(R){let l,c,o,$,b,j,z,B,v,M,F,U,N,O,C,ne,Q,D,ae,K,W,H,Y,I,h,y,J,ee,_e,X,te,Te,re,be,ge,$e,Ie,Le,G,De,pe,Fe,Be,ie,fe,ue,se,oe,me,we,ve,xe,ye,le,k,L,ke,Ce,Se,V,Ae,je,he,Me,de,Pe,Ne,Ee,ze,ce,qe,w,Ue,Xe,f,P,Ze,Ye,lt,He,Oe,at,Ge,rt,ot,Ke,nt,We,Je,Ve,ct,Qe,Re,pt,et,aa,ks,it,es,ra,oa,ts,la,na,xt,pa,ss,ia,fa,bs,St,ws,tt,ma,At,ca,ua,as,ha,da,rs,_a,ga,vs,Pt,ys,ft,$a,Yt,ka,ba,os,wa,va,js,_t,Es,ut,gt,ls,zt,ya,ns,ja,qs,st,Ea,Ft,qa,Ta,Mt,xa,Sa,Lt,Aa,Pa,Ts,Ct,xs,$t,za,Dt,Fa,Ma,Ss,Bt,As,kt,La,ps,Ca,Da,Ps,ht,bt,is,Ot,Ba,fs,Oa,zs,wt,Fs,vt,Ms,dt,yt,ms,It,Ia,cs,Na,Ls,Gt,Ha,Cs,Kt,Ua,Ds,Nt,Bs,mt,Ya,Wt,Ga,Ka,us,Wa,Ra,Os,Ht,Is,jt,Xa,hs,Za,Ja,Ns,Et,Hs;return j=new Vt({}),F=new Jr({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/translation.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/translation.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/translation.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/translation.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/translation.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/translation.ipynb"}]}}),N=new Gr({props:{id:"1JvfrvZgi6c"}}),be=new ta({props:{$$slots:{default:[Vr]},$$scope:{ctx:R}}}),G=new Z({props:{code:"pip install transformers datasets evaluate sacrebleu",highlighted:"pip install transformers datasets evaluate sacrebleu"}}),ie=new Z({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()`}}),me=new Vt({}),V=new Z({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>)`}}),Ee=new Z({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>)'}}),Ue=new Z({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>}}`}}),Ge=new Vt({}),We=new Gr({props:{id:"XAR8jnZZuUs"}}),Re=new Z({props:{code:`from transformers import AutoTokenizer

checkpoint = "t5-small"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)`,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>checkpoint = <span class="hljs-string">&quot;t5-small&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint)`}}),St=new Z({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`}}),Pt=new Z({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>)'}}),_t=new Qa({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[so],pytorch:[eo]},$$scope:{ctx:R}}}),zt=new Vt({}),Ct=new Z({props:{code:`import evaluate

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