import{S as Kr,i as Wr,s as Rr,e as n,k as d,w as E,t as a,M as Xr,c as p,d as s,m as _,a as i,x as q,h as r,b as g,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 Jt}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 Vt}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,pe,Q,D,ae,K,W,H,Y,I,h,y,J,ee,ge,X,te,qe,oe,we,$e,ke,Be,ze,G,Me,ie,Pe,Le,fe,me,ue,se,le,ce,ve,ye,Te,je,ne,k,L,be,Fe,xe,V,Se,Ee,he,Ce,de,Ae,Oe;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"),pe=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"),ge=a(", "),X=n("a"),te=a("M2M100"),qe=a(", "),oe=n("a"),we=a("Marian"),$e=a(", "),ke=n("a"),Be=a("mBART"),ze=a(", "),G=n("a"),Me=a("MT5"),ie=a(", "),Pe=n("a"),Le=a("MVP"),fe=a(", "),me=n("a"),ue=a("NLLB"),se=a(", "),le=n("a"),ce=a("Pegasus"),ve=a(", "),ye=n("a"),Te=a("PEGASUS-X"),je=a(", "),ne=n("a"),k=a("PLBart"),L=a(", "),be=n("a"),Fe=a("ProphetNet"),xe=a(", "),V=n("a"),Se=a("SwitchTransformers"),Ee=a(", "),he=n("a"),Ce=a("T5"),de=a(", "),Ae=n("a"),Oe=a("XLM-ProphetNet"),this.h()},l(re){l=r(re,`The task illustrated in this tutorial is supported by the following model architectures:

`),c=p(re,"P",{});var w=i(c);o=p(w,"A",{href:!0});var _e=i(o);$=r(_e,"BART"),_e.forEach(s),b=r(w,", "),j=p(w,"A",{href:!0});var Ne=i(j);z=r(Ne,"BigBird-Pegasus"),Ne.forEach(s),B=r(w,", "),v=p(w,"A",{href:!0});var He=i(v);M=r(He,"Blenderbot"),He.forEach(s),F=r(w,", "),U=p(w,"A",{href:!0});var Ue=i(U);N=r(Ue,"BlenderbotSmall"),Ue.forEach(s),O=r(w,", "),C=p(w,"A",{href:!0});var Re=i(C);pe=r(Re,"Encoder decoder"),Re.forEach(s),Q=r(w,", "),D=p(w,"A",{href:!0});var f=i(D);ae=r(f,"FairSeq Machine-Translation"),f.forEach(s),K=r(w,", "),W=p(w,"A",{href:!0});var P=i(W);H=r(P,"GPTSAN-japanese"),P.forEach(s),Y=r(w,", "),I=p(w,"A",{href:!0});var Xe=i(I);h=r(Xe,"LED"),Xe.forEach(s),y=r(w,", "),J=p(w,"A",{href:!0});var Ye=i(J);ee=r(Ye,"LongT5"),Ye.forEach(s),ge=r(w,", "),X=p(w,"A",{href:!0});var at=i(X);te=r(at,"M2M100"),at.forEach(s),qe=r(w,", "),oe=p(w,"A",{href:!0});var Ie=i(oe);we=r(Ie,"Marian"),Ie.forEach(s),$e=r(w,", "),ke=p(w,"A",{href:!0});var De=i(ke);Be=r(De,"mBART"),De.forEach(s),ze=r(w,", "),G=p(w,"A",{href:!0});var et=i(G);Me=r(et,"MT5"),et.forEach(s),ie=r(w,", "),Pe=p(w,"A",{href:!0});var Ge=i(Pe);Le=r(Ge,"MVP"),Ge.forEach(s),fe=r(w,", "),me=p(w,"A",{href:!0});var tt=i(me);ue=r(tt,"NLLB"),tt.forEach(s),se=r(w,", "),le=p(w,"A",{href:!0});var st=i(le);ce=r(st,"Pegasus"),st.forEach(s),ve=r(w,", "),ye=p(w,"A",{href:!0});var Ke=i(ye);Te=r(Ke,"PEGASUS-X"),Ke.forEach(s),je=r(w,", "),ne=p(w,"A",{href:!0});var rt=i(ne);k=r(rt,"PLBart"),rt.forEach(s),L=r(w,", "),be=p(w,"A",{href:!0});var We=i(be);Fe=r(We,"ProphetNet"),We.forEach(s),xe=r(w,", "),V=p(w,"A",{href:!0});var Ze=i(V);Se=r(Ze,"SwitchTransformers"),Ze.forEach(s),Ee=r(w,", "),he=p(w,"A",{href:!0});var Je=i(he);Ce=r(Je,"T5"),Je.forEach(s),de=r(w,", "),Ae=p(w,"A",{href:!0});var it=i(Ae);Oe=r(it,"XLM-ProphetNet"),it.forEach(s),w.forEach(s),this.h()},h(){g(o,"href","../model_doc/bart"),g(j,"href","../model_doc/bigbird_pegasus"),g(v,"href","../model_doc/blenderbot"),g(U,"href","../model_doc/blenderbot-small"),g(C,"href","../model_doc/encoder-decoder"),g(D,"href","../model_doc/fsmt"),g(W,"href","../model_doc/gptsan-japanese"),g(I,"href","../model_doc/led"),g(J,"href","../model_doc/longt5"),g(X,"href","../model_doc/m2m_100"),g(oe,"href","../model_doc/marian"),g(ke,"href","../model_doc/mbart"),g(G,"href","../model_doc/mt5"),g(Pe,"href","../model_doc/mvp"),g(me,"href","../model_doc/nllb"),g(le,"href","../model_doc/pegasus"),g(ye,"href","../model_doc/pegasus_x"),g(ne,"href","../model_doc/plbart"),g(be,"href","../model_doc/prophetnet"),g(V,"href","../model_doc/switch_transformers"),g(he,"href","../model_doc/t5"),g(Ae,"href","../model_doc/xlm-prophetnet")},m(re,w){m(re,l,w),m(re,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,pe),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,ge),t(c,X),t(X,te),t(c,qe),t(c,oe),t(oe,we),t(c,$e),t(c,ke),t(ke,Be),t(c,ze),t(c,G),t(G,Me),t(c,ie),t(c,Pe),t(Pe,Le),t(c,fe),t(c,me),t(me,ue),t(c,se),t(c,le),t(le,ce),t(c,ve),t(c,ye),t(ye,Te),t(c,je),t(c,ne),t(ne,k),t(c,L),t(c,be),t(be,Fe),t(c,xe),t(c,V),t(V,Se),t(c,Ee),t(c,he),t(he,Ce),t(c,de),t(c,Ae),t(Ae,Oe)},d(re){re&&s(l),re&&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 Vt({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 Vt({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(){g(o,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Trainer"),g(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,pe,Q,D,ae,K,W,H,Y,I,h,y,J,ee,ge,X,te,qe,oe,we,$e,ke,Be,ze,G,Me,ie,Pe,Le,fe,me,ue,se,le,ce,ve,ye,Te,je,ne;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)`}}),me=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()`}}),je=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=d(),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=d(),E(v.$$.fragment),M=d(),F=n("p"),U=a("At this point, only three steps remain:"),N=d(),O=n("ol"),C=n("li"),pe=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."),ge=d(),X=n("li"),te=a("Pass the training arguments to "),qe=n("a"),oe=a("Seq2SeqTrainer"),we=a(" along with the model, dataset, tokenizer, data collator, and "),$e=n("code"),ke=a("compute_metrics"),Be=a(" function."),ze=d(),G=n("li"),Me=a("Call "),ie=n("a"),Pe=a("train()"),Le=a(" to finetune your model."),fe=d(),E(me.$$.fragment),ue=d(),se=n("p"),le=a("Once training is completed, share your model to the Hub with the "),ce=n("a"),ve=a("push_to_hub()"),ye=a(" method so everyone can use your model:"),Te=d(),E(je.$$.fragment),this.h()},l(k){q(l.$$.fragment,k),c=_(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 be=i(b);j=r(be,"AutoModelForSeq2SeqLM"),be.forEach(s),z=r(L,":"),L.forEach(s),B=_(k),q(v.$$.fragment,k),M=_(k),F=p(k,"P",{});var Fe=i(F);U=r(Fe,"At this point, only three steps remain:"),Fe.forEach(s),N=_(k),O=p(k,"OL",{});var xe=i(O);C=p(xe,"LI",{});var V=i(C);pe=r(V,"Define your training hyperparameters in "),Q=p(V,"A",{href:!0});var Se=i(Q);D=r(Se,"Seq2SeqTrainingArguments"),Se.forEach(s),ae=r(V,". The only required parameter is "),K=p(V,"CODE",{});var Ee=i(K);W=r(Ee,"output_dir"),Ee.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 Ce=i(y);J=r(Ce,"Trainer"),Ce.forEach(s),ee=r(V," will evaluate the SacreBLEU metric and save the training checkpoint."),V.forEach(s),ge=_(xe),X=p(xe,"LI",{});var de=i(X);te=r(de,"Pass the training arguments to "),qe=p(de,"A",{href:!0});var Ae=i(qe);oe=r(Ae,"Seq2SeqTrainer"),Ae.forEach(s),we=r(de," along with the model, dataset, tokenizer, data collator, and "),$e=p(de,"CODE",{});var Oe=i($e);ke=r(Oe,"compute_metrics"),Oe.forEach(s),Be=r(de," function."),de.forEach(s),ze=_(xe),G=p(xe,"LI",{});var re=i(G);Me=r(re,"Call "),ie=p(re,"A",{href:!0});var w=i(ie);Pe=r(w,"train()"),w.forEach(s),Le=r(re," to finetune your model."),re.forEach(s),xe.forEach(s),fe=_(k),q(me.$$.fragment,k),ue=_(k),se=p(k,"P",{});var _e=i(se);le=r(_e,"Once training is completed, share your model to the Hub with the "),ce=p(_e,"A",{href:!0});var Ne=i(ce);ve=r(Ne,"push_to_hub()"),Ne.forEach(s),ye=r(_e," method so everyone can use your model:"),_e.forEach(s),Te=_(k),q(je.$$.fragment,k),this.h()},h(){g(b,"href","/docs/transformers/v4.27.1/en/model_doc/auto#transformers.AutoModelForSeq2SeqLM"),g(Q,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Seq2SeqTrainingArguments"),g(y,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Trainer"),g(qe,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Seq2SeqTrainer"),g(ie,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Trainer.train"),g(ce,"href","/docs/transformers/v4.27.1/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,pe),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,ge),t(O,X),t(X,te),t(X,qe),t(qe,oe),t(X,we),t(X,$e),t($e,ke),t(X,Be),t(O,ze),t(O,G),t(G,Me),t(G,ie),t(ie,Pe),t(G,Le),m(k,fe,L),T(me,k,L),m(k,ue,L),m(k,se,L),t(se,le),t(se,ce),t(ce,ve),t(se,ye),m(k,Te,L),T(je,k,L),ne=!0},p(k,L){const be={};L&2&&(be.$$scope={dirty:L,ctx:k}),l.$set(be)},i(k){ne||(x(l.$$.fragment,k),x(v.$$.fragment,k),x(me.$$.fragment,k),x(je.$$.fragment,k),ne=!0)},o(k){S(l.$$.fragment,k),S(v.$$.fragment,k),S(me.$$.fragment,k),S(je.$$.fragment,k),ne=!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(fe),A(me,k),k&&s(ue),k&&s(se),k&&s(Te),A(je,k)}}}function oo(R){let l,c;return l=new Vt({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(){g(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,pe,Q,D,ae,K,W,H,Y,I,h,y,J,ee,ge,X,te,qe,oe,we,$e,ke,Be,ze,G,Me,ie,Pe,Le,fe,me,ue,se,le,ce,ve,ye,Te,je,ne,k,L,be,Fe,xe,V,Se,Ee,he,Ce,de,Ae,Oe,re,w,_e,Ne,He,Ue,Re;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)`}}),le=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>)`}}),Se=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]'}}),_e=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),$=d(),b=n("p"),j=a("Then you can load T5 with "),z=n("a"),B=a("TFAutoModelForSeq2SeqLM"),v=a(":"),M=d(),E(F.$$.fragment),U=d(),N=n("p"),O=a("Convert your datasets to the "),C=n("code"),pe=a("tf.data.Dataset"),Q=a(" format with "),D=n("a"),ae=a("prepare_tf_dataset()"),K=a(":"),W=d(),E(H.$$.fragment),Y=d(),I=n("p"),h=a("Configure the model for training with "),y=n("a"),J=n("code"),ee=a("compile"),ge=a(":"),X=d(),E(te.$$.fragment),qe=d(),oe=n("p"),we=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 "),$e=n("a"),ke=a("Keras callbacks"),Be=a("."),ze=d(),G=n("p"),Me=a("Pass your "),ie=n("code"),Pe=a("compute_metrics"),Le=a(" function to "),fe=n("a"),me=a("KerasMetricCallback"),ue=a(":"),se=d(),E(le.$$.fragment),ce=d(),ve=n("p"),ye=a("Specify where to push your model and tokenizer in the "),Te=n("a"),je=a("PushToHubCallback"),ne=a(":"),k=d(),E(L.$$.fragment),be=d(),Fe=n("p"),xe=a("Then bundle your callbacks together:"),V=d(),E(Se.$$.fragment),Ee=d(),he=n("p"),Ce=a("Finally, you\u2019re ready to start training your model! Call "),de=n("a"),Ae=n("code"),Oe=a("fit"),re=a(" with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:"),w=d(),E(_e.$$.fragment),Ne=d(),He=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),$=_(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 Xe=i(z);B=r(Xe,"TFAutoModelForSeq2SeqLM"),Xe.forEach(s),v=r(P,":"),P.forEach(s),M=_(f),q(F.$$.fragment,f),U=_(f),N=p(f,"P",{});var Ye=i(N);O=r(Ye,"Convert your datasets to the "),C=p(Ye,"CODE",{});var at=i(C);pe=r(at,"tf.data.Dataset"),at.forEach(s),Q=r(Ye," format with "),D=p(Ye,"A",{href:!0});var Ie=i(D);ae=r(Ie,"prepare_tf_dataset()"),Ie.forEach(s),K=r(Ye,":"),Ye.forEach(s),W=_(f),q(H.$$.fragment,f),Y=_(f),I=p(f,"P",{});var De=i(I);h=r(De,"Configure the model for training with "),y=p(De,"A",{href:!0,rel:!0});var et=i(y);J=p(et,"CODE",{});var Ge=i(J);ee=r(Ge,"compile"),Ge.forEach(s),et.forEach(s),ge=r(De,":"),De.forEach(s),X=_(f),q(te.$$.fragment,f),qe=_(f),oe=p(f,"P",{});var tt=i(oe);we=r(tt,"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 "),$e=p(tt,"A",{href:!0});var st=i($e);ke=r(st,"Keras callbacks"),st.forEach(s),Be=r(tt,"."),tt.forEach(s),ze=_(f),G=p(f,"P",{});var Ke=i(G);Me=r(Ke,"Pass your "),ie=p(Ke,"CODE",{});var rt=i(ie);Pe=r(rt,"compute_metrics"),rt.forEach(s),Le=r(Ke," function to "),fe=p(Ke,"A",{href:!0});var We=i(fe);me=r(We,"KerasMetricCallback"),We.forEach(s),ue=r(Ke,":"),Ke.forEach(s),se=_(f),q(le.$$.fragment,f),ce=_(f),ve=p(f,"P",{});var Ze=i(ve);ye=r(Ze,"Specify where to push your model and tokenizer in the "),Te=p(Ze,"A",{href:!0});var Je=i(Te);je=r(Je,"PushToHubCallback"),Je.forEach(s),ne=r(Ze,":"),Ze.forEach(s),k=_(f),q(L.$$.fragment,f),be=_(f),Fe=p(f,"P",{});var it=i(Fe);xe=r(it,"Then bundle your callbacks together:"),it.forEach(s),V=_(f),q(Se.$$.fragment,f),Ee=_(f),he=p(f,"P",{});var ft=i(he);Ce=r(ft,"Finally, you\u2019re ready to start training your model! Call "),de=p(ft,"A",{href:!0,rel:!0});var ot=i(de);Ae=p(ot,"CODE",{});var Tt=i(Ae);Oe=r(Tt,"fit"),Tt.forEach(s),ot.forEach(s),re=r(ft," with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:"),ft.forEach(s),w=_(f),q(_e.$$.fragment,f),Ne=_(f),He=p(f,"P",{});var mt=i(He);Ue=r(mt,"Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),mt.forEach(s),this.h()},h(){g(z,"href","/docs/transformers/v4.27.1/en/model_doc/auto#transformers.TFAutoModelForSeq2SeqLM"),g(D,"href","/docs/transformers/v4.27.1/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset"),g(y,"href","https://keras.io/api/models/model_training_apis/#compile-method"),g(y,"rel","nofollow"),g($e,"href","./main_classes/keras_callbacks"),g(fe,"href","/docs/transformers/v4.27.1/en/main_classes/keras_callbacks#transformers.KerasMetricCallback"),g(Te,"href","/docs/transformers/v4.27.1/en/main_classes/keras_callbacks#transformers.PushToHubCallback"),g(de,"href","https://keras.io/api/models/model_training_apis/#fit-method"),g(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,pe),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,ge),m(f,X,P),T(te,f,P),m(f,qe,P),m(f,oe,P),t(oe,we),t(oe,$e),t($e,ke),t(oe,Be),m(f,ze,P),m(f,G,P),t(G,Me),t(G,ie),t(ie,Pe),t(G,Le),t(G,fe),t(fe,me),t(G,ue),m(f,se,P),T(le,f,P),m(f,ce,P),m(f,ve,P),t(ve,ye),t(ve,Te),t(Te,je),t(ve,ne),m(f,k,P),T(L,f,P),m(f,be,P),m(f,Fe,P),t(Fe,xe),m(f,V,P),T(Se,f,P),m(f,Ee,P),m(f,he,P),t(he,Ce),t(he,de),t(de,Ae),t(Ae,Oe),t(he,re),m(f,w,P),T(_e,f,P),m(f,Ne,P),m(f,He,P),t(He,Ue),Re=!0},p(f,P){const Xe={};P&2&&(Xe.$$scope={dirty:P,ctx:f}),l.$set(Xe)},i(f){Re||(x(l.$$.fragment,f),x(o.$$.fragment,f),x(F.$$.fragment,f),x(H.$$.fragment,f),x(te.$$.fragment,f),x(le.$$.fragment,f),x(L.$$.fragment,f),x(Se.$$.fragment,f),x(_e.$$.fragment,f),Re=!0)},o(f){S(l.$$.fragment,f),S(o.$$.fragment,f),S(F.$$.fragment,f),S(H.$$.fragment,f),S(te.$$.fragment,f),S(le.$$.fragment,f),S(L.$$.fragment,f),S(Se.$$.fragment,f),S(_e.$$.fragment,f),Re=!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(qe),f&&s(oe),f&&s(ze),f&&s(G),f&&s(se),A(le,f),f&&s(ce),f&&s(ve),f&&s(k),A(L,f),f&&s(be),f&&s(Fe),f&&s(V),A(Se,f),f&&s(Ee),f&&s(he),f&&s(w),A(_e,f),f&&s(Ne),f&&s(He)}}}function po(R){let l,c;return l=new Vt({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(){g(o,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb"),g(o,"rel","nofollow"),g(j,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb"),g(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,pe,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=d(),E(z.$$.fragment),B=d(),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"),pe=a(" API."),Q=d(),E(D.$$.fragment),ae=d(),K=n("p"),W=a("Decode the generated token ids back into text:"),H=d(),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=_(h),q(z.$$.fragment,h),B=_(h),v=p(h,"P",{});var ee=i(v);M=r(ee,"Use the "),F=p(ee,"A",{href:!0});var ge=i(F);U=r(ge,"generate()"),ge.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),pe=r(ee," API."),ee.forEach(s),Q=_(h),q(D.$$.fragment,h),ae=_(h),K=p(h,"P",{});var te=i(K);W=r(te,"Decode the generated token ids back into text:"),te.forEach(s),H=_(h),q(Y.$$.fragment,h),this.h()},h(){g(F,"href","/docs/transformers/v4.27.1/en/main_classes/text_generation#transformers.GenerationMixin.generate"),g(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,pe),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 Vt({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,pe,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=d(),E(z.$$.fragment),B=d(),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"),pe=a(" API."),Q=d(),E(D.$$.fragment),ae=d(),K=n("p"),W=a("Decode the generated token ids back into text:"),H=d(),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=_(h),q(z.$$.fragment,h),B=_(h),v=p(h,"P",{});var ee=i(v);M=r(ee,"Use the "),F=p(ee,"A",{href:!0});var ge=i(F);U=r(ge,"generate()"),ge.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),pe=r(ee," API."),ee.forEach(s),Q=_(h),q(D.$$.fragment,h),ae=_(h),K=p(h,"P",{});var te=i(K);W=r(te,"Decode the generated token ids back into text:"),te.forEach(s),H=_(h),q(Y.$$.fragment,h),this.h()},h(){g(F,"href","/docs/transformers/v4.27.1/en/main_classes/text_generation#transformers.TFGenerationMixin.generate"),g(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,pe),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 Vt({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,pe,Q,D,ae,K,W,H,Y,I,h,y,J,ee,ge,X,te,qe,oe,we,$e,ke,Be,ze,G,Me,ie,Pe,Le,fe,me,ue,se,le,ce,ve,ye,Te,je,ne,k,L,be,Fe,xe,V,Se,Ee,he,Ce,de,Ae,Oe,re,w,_e,Ne,He,Ue,Re,f,P,Xe,Ye,at,Ie,De,et,Ge,tt,st,Ke,rt,We,Ze,Je,it,ft,ot,Tt,mt,aa,ks,lt,Qt,ra,oa,es,la,na,xt,pa,ts,ia,fa,bs,St,ws,Ve,ma,ss,ca,ua,as,ha,da,rs,_a,ga,vs,At,ys,nt,$a,Ut,ka,ba,os,wa,va,js,dt,Es,ct,_t,ls,Pt,ya,ns,ja,qs,Qe,Ea,zt,qa,Ta,Ft,xa,Sa,Mt,Aa,Pa,Ts,Lt,xs,gt,za,Ct,Fa,Ma,Ss,Dt,As,$t,La,ps,Ca,Da,Ps,ut,kt,is,Bt,Ba,fs,Oa,zs,bt,Fs,wt,Ms,ht,vt,ms,Ot,Ia,cs,Na,Ls,Yt,Ha,Cs,Gt,Ua,Ds,It,Bs,pt,Ya,Kt,Ga,Ka,us,Wa,Ra,Os,Nt,Is,yt,Xa,hs,Za,Ja,Ns,jt,Hs;return j=new Jt({}),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"}}),we=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"}}),fe=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()`}}),ce=new Jt({}),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>)`}}),re=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 Jt({}),We=new Gr({props:{id:"XAR8jnZZuUs"}}),ot=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`}}),At=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>)'}}),dt=new Qa({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[so],pytorch:[eo]},$$scope:{ctx:R}}}),Pt=new Jt({}),Lt=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>)`}}),Dt=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`}}),Bt=new Jt({}),bt=new Qa({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[po],pytorch:[oo]},$$scope:{ctx:R}}}),wt=new ta({props:{$$slots:{default:[io]},$$scope:{ctx:R}}}),Ot=new Jt({}),It=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>'}}),Nt=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>}]`}}),jt=new Qa({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[uo],pytorch:[mo]},$$scope:{ctx:R}}}),{c(){l=n("meta"),c=d(),o=n("h1"),$=n("a"),b=n("span"),E(j.$$.fragment),z=d(),B=n("span"),v=a("Translation"),M=d(),E(F.$$.fragment),U=d(),E(N.$$.fragment),O=d(),C=n("p"),pe=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(),D=n("p"),ae=a("This guide will show you how to:"),K=d(),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"),ge=a(" dataset to translate English text to French."),X=d(),te=n("li"),qe=a("Use your finetuned model for inference."),oe=d(),E(we.$$.fragment),$e=d(),ke=n("p"),Be=a("Before you begin, make sure you have all the necessary libraries installed:"),ze=d(),E(G.$$.fragment),Me=d(),ie=n("p"),Pe=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:"),Le=d(),E(fe.$$.fragment),me=d(),ue=n("h2"),se=n("a"),le=n("span"),E(ce.$$.fragment),ve=d(),ye=n("span"),Te=a("Load OPUS Books dataset"),je=d(),ne=n("p"),k=a("Start by loading the English-French subset of the "),L=n("a"),be=a("OPUS Books"),Fe=a(" dataset from the \u{1F917} Datasets library:"),xe=d(),E(V.$$.fragment),Se=d(),Ee=n("p"),he=a("Split the dataset into a train and test set with the "),Ce=n("code"),de=a("train_test_split"),Ae=a(" method:"),Oe=d(),E(re.$$.fragment),w=d(),_e=n("p"),Ne=a("Then take a look at an example:"),He=d(),E(Ue.$$.fragment),Re=d(),f=n("p"),P=n("code"),Xe=a("translation"),Ye=a(": an English and French translation of the text."),at=d(),Ie=n("h2"),De=n("a"),et=n("span"),E(Ge.$$.fragment),tt=d(),st=n("span"),Ke=a("Preprocess"),rt=d(),E(We.$$.fragment),Ze=d(),Je=n("p"),it=a("The next step is to load a T5 tokenizer to process the English-French language pairs:"),ft=d(),E(ot.$$.fragment),Tt=d(),mt=n("p"),aa=a("The preprocessing function you want to create needs to:"),ks=d(),lt=n("ol"),Qt=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=d(),es=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=d(),xt=n("li"),pa=a("Truncate sequences to be no longer than the maximum length set by the "),ts=n("code"),ia=a("max_length"),fa=a(" parameter."),bs=d(),E(St.$$.fragment),ws=d(),Ve=n("p"),ma=a("To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),ss=n("code"),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=d(),E(At.$$.fragment),ys=d(),nt=n("p"),$a=a("Now create a batch of examples using "),Ut=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=d(),E(dt.$$.fragment),Es=d(),ct=n("h2"),_t=n("a"),ls=n("span"),E(Pt.$$.fragment),ya=d(),ns=n("span"),ja=a("Evaluate"),qs=d(),Qe=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} "),zt=n("a"),qa=a("Evaluate"),Ta=a(" library. For this task, load the "),Ft=n("a"),xa=a("SacreBLEU"),Sa=a(" metric (see the \u{1F917} Evaluate "),Mt=n("a"),Aa=a("quick tour"),Pa=a(" to learn more about how to load and compute a metric):"),Ts=d(),E(Lt.$$.fragment),xs=d(),gt=n("p"),za=a("Then create a function that passes your predictions and labels to "),Ct=n("a"),Fa=a("compute"),Ma=a(" to calculate the SacreBLEU score:"),Ss=d(),E(Dt.$$.fragment),As=d(),$t=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=d(),ut=n("h2"),kt=n("a"),is=n("span"),E(Bt.$$.fragment),Ba=d(),fs=n("span"),Oa=a("Train"),zs=d(),E(bt.$$.fragment),Fs=d(),E(wt.$$.fragment),Ms=d(),ht=n("h2"),vt=n("a"),ms=n("span"),E(Ot.$$.fragment),Ia=d(),cs=n("span"),Na=a("Inference"),Ls=d(),Yt=n("p"),Ha=a("Great, now that you\u2019ve finetuned a model, you can use it for inference!"),Cs=d(),Gt=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=d(),E(It.$$.fragment),Bs=d(),pt=n("p"),Ya=a("The simplest way to try out your finetuned model for inference is to use it in a "),Kt=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=d(),E(Nt.$$.fragment),Is=d(),yt=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=d(),E(jt.$$.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=_(e),o=p(e,"H1",{class:!0});var Ht=i(o);$=p(Ht,"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=_(Ht),B=p(Ht,"SPAN",{});var gs=i(B);v=r(gs,"Translation"),gs.forEach(s),Ht.forEach(s),M=_(e),q(F.$$.fragment,e),U=_(e),q(N.$$.fragment,e),O=_(e),C=p(e,"P",{});var $s=i(C);pe=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=_(e),D=p(e,"P",{});var er=i(D);ae=r(er,"This guide will show you how to:"),er.forEach(s),K=_(e),W=p(e,"OL",{});var Us=i(W);H=p(Us,"LI",{});var Wt=i(H);Y=r(Wt,"Finetune "),I=p(Wt,"A",{href:!0,rel:!0});var tr=i(I);h=r(tr,"T5"),tr.forEach(s),y=r(Wt," on the English-French subset of the "),J=p(Wt,"A",{href:!0,rel:!0});var sr=i(J);ee=r(sr,"OPUS Books"),sr.forEach(s),ge=r(Wt," dataset to translate English text to French."),Wt.forEach(s),X=_(Us),te=p(Us,"LI",{});var ar=i(te);qe=r(ar,"Use your finetuned model for inference."),ar.forEach(s),Us.forEach(s),oe=_(e),q(we.$$.fragment,e),$e=_(e),ke=p(e,"P",{});var rr=i(ke);Be=r(rr,"Before you begin, make sure you have all the necessary libraries installed:"),rr.forEach(s),ze=_(e),q(G.$$.fragment,e),Me=_(e),ie=p(e,"P",{});var or=i(ie);Pe=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),Le=_(e),q(fe.$$.fragment,e),me=_(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);le=p(lr,"SPAN",{});var nr=i(le);q(ce.$$.fragment,nr),nr.forEach(s),lr.forEach(s),ve=_(Ys),ye=p(Ys,"SPAN",{});var pr=i(ye);Te=r(pr,"Load OPUS Books dataset"),pr.forEach(s),Ys.forEach(s),je=_(e),ne=p(e,"P",{});var Gs=i(ne);k=r(Gs,"Start by loading the English-French subset of the "),L=p(Gs,"A",{href:!0,rel:!0});var ir=i(L);be=r(ir,"OPUS Books"),ir.forEach(s),Fe=r(Gs," dataset from the \u{1F917} Datasets library:"),Gs.forEach(s),xe=_(e),q(V.$$.fragment,e),Se=_(e),Ee=p(e,"P",{});var Ks=i(Ee);he=r(Ks,"Split the dataset into a train and test set with the "),Ce=p(Ks,"CODE",{});var fr=i(Ce);de=r(fr,"train_test_split"),fr.forEach(s),Ae=r(Ks," method:"),Ks.forEach(s),Oe=_(e),q(re.$$.fragment,e),w=_(e),_e=p(e,"P",{});var mr=i(_e);Ne=r(mr,"Then take a look at an example:"),mr.forEach(s),He=_(e),q(Ue.$$.fragment,e),Re=_(e),f=p(e,"P",{});var Va=i(f);P=p(Va,"CODE",{});var cr=i(P);Xe=r(cr,"translation"),cr.forEach(s),Ye=r(Va,": an English and French translation of the text."),Va.forEach(s),at=_(e),Ie=p(e,"H2",{class:!0});var Ws=i(Ie);De=p(Ws,"A",{id:!0,class:!0,href:!0});var ur=i(De);et=p(ur,"SPAN",{});var hr=i(et);q(Ge.$$.fragment,hr),hr.forEach(s),ur.forEach(s),tt=_(Ws),st=p(Ws,"SPAN",{});var dr=i(st);Ke=r(dr,"Preprocess"),dr.forEach(s),Ws.forEach(s),rt=_(e),q(We.$$.fragment,e),Ze=_(e),Je=p(e,"P",{});var _r=i(Je);it=r(_r,"The next step is to load a T5 tokenizer to process the English-French language pairs:"),_r.forEach(s),ft=_(e),q(ot.$$.fragment,e),Tt=_(e),mt=p(e,"P",{});var gr=i(mt);aa=r(gr,"The preprocessing function you want to create needs to:"),gr.forEach(s),ks=_(e),lt=p(e,"OL",{});var Rt=i(lt);Qt=p(Rt,"LI",{});var $r=i(Qt);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=_(Rt),es=p(Rt,"LI",{});var kr=i(es);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=_(Rt),xt=p(Rt,"LI",{});var Rs=i(xt);pa=r(Rs,"Truncate sequences to be no longer than the maximum length set by the "),ts=p(Rs,"CODE",{});var br=i(ts);ia=r(br,"max_length"),br.forEach(s),fa=r(Rs," parameter."),Rs.forEach(s),Rt.forEach(s),bs=_(e),q(St.$$.fragment,e),ws=_(e),Ve=p(e,"P",{});var Et=i(Ve);ma=r(Et,"To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),ss=p(Et,"CODE",{});var wr=i(ss);ca=r(wr,"map"),wr.forEach(s),ua=r(Et," method. You can speed up the "),as=p(Et,"CODE",{});var vr=i(as);ha=r(vr,"map"),vr.forEach(s),da=r(Et," function by setting "),rs=p(Et,"CODE",{});var yr=i(rs);_a=r(yr,"batched=True"),yr.forEach(s),ga=r(Et," to process multiple elements of the dataset at once:"),Et.forEach(s),vs=_(e),q(At.$$.fragment,e),ys=_(e),nt=p(e,"P",{});var Xt=i(nt);$a=r(Xt,"Now create a batch of examples using "),Ut=p(Xt,"A",{href:!0});var jr=i(Ut);ka=r(jr,"DataCollatorForSeq2Seq"),jr.forEach(s),ba=r(Xt,". It\u2019s more efficient to "),os=p(Xt,"EM",{});var Er=i(os);wa=r(Er,"dynamically pad"),Er.forEach(s),va=r(Xt," the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length."),Xt.forEach(s),js=_(e),q(dt.$$.fragment,e),Es=_(e),ct=p(e,"H2",{class:!0});var Xs=i(ct);_t=p(Xs,"A",{id:!0,class:!0,href:!0});var qr=i(_t);ls=p(qr,"SPAN",{});var Tr=i(ls);q(Pt.$$.fragment,Tr),Tr.forEach(s),qr.forEach(s),ya=_(Xs),ns=p(Xs,"SPAN",{});var xr=i(ns);ja=r(xr,"Evaluate"),xr.forEach(s),Xs.forEach(s),qs=_(e),Qe=p(e,"P",{});var qt=i(Qe);Ea=r(qt,"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=p(qt,"A",{href:!0,rel:!0});var Sr=i(zt);qa=r(Sr,"Evaluate"),Sr.forEach(s),Ta=r(qt," library. For this task, load the "),Ft=p(qt,"A",{href:!0,rel:!0});var Ar=i(Ft);xa=r(Ar,"SacreBLEU"),Ar.forEach(s),Sa=r(qt," metric (see the \u{1F917} Evaluate "),Mt=p(qt,"A",{href:!0,rel:!0});var Pr=i(Mt);Aa=r(Pr,"quick tour"),Pr.forEach(s),Pa=r(qt," to learn more about how to load and compute a metric):"),qt.forEach(s),Ts=_(e),q(Lt.$$.fragment,e),xs=_(e),gt=p(e,"P",{});var Zs=i(gt);za=r(Zs,"Then create a function that passes your predictions and labels to "),Ct=p(Zs,"A",{href:!0,rel:!0});var zr=i(Ct);Fa=r(zr,"compute"),zr.forEach(s),Ma=r(Zs," to calculate the SacreBLEU score:"),Zs.forEach(s),Ss=_(e),q(Dt.$$.fragment,e),As=_(e),$t=p(e,"P",{});var Js=i($t);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=_(e),ut=p(e,"H2",{class:!0});var Vs=i(ut);kt=p(Vs,"A",{id:!0,class:!0,href:!0});var Mr=i(kt);is=p(Mr,"SPAN",{});var Lr=i(is);q(Bt.$$.fragment,Lr),Lr.forEach(s),Mr.forEach(s),Ba=_(Vs),fs=p(Vs,"SPAN",{});var Cr=i(fs);Oa=r(Cr,"Train"),Cr.forEach(s),Vs.forEach(s),zs=_(e),q(bt.$$.fragment,e),Fs=_(e),q(wt.$$.fragment,e),Ms=_(e),ht=p(e,"H2",{class:!0});var Qs=i(ht);vt=p(Qs,"A",{id:!0,class:!0,href:!0});var Dr=i(vt);ms=p(Dr,"SPAN",{});var Br=i(ms);q(Ot.$$.fragment,Br),Br.forEach(s),Dr.forEach(s),Ia=_(Qs),cs=p(Qs,"SPAN",{});var Or=i(cs);Na=r(Or,"Inference"),Or.forEach(s),Qs.forEach(s),Ls=_(e),Yt=p(e,"P",{});var Ir=i(Yt);Ha=r(Ir,"Great, now that you\u2019ve finetuned a model, you can use it for inference!"),Ir.forEach(s),Cs=_(e),Gt=p(e,"P",{});var Nr=i(Gt);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=_(e),q(It.$$.fragment,e),Bs=_(e),pt=p(e,"P",{});var Zt=i(pt);Ya=r(Zt,"The simplest way to try out your finetuned model for inference is to use it in a "),Kt=p(Zt,"A",{href:!0});var Hr=i(Kt);Ga=r(Hr,"pipeline()"),Hr.forEach(s),Ka=r(Zt,". Instantiate a "),us=p(Zt,"CODE",{});var Ur=i(us);Wa=r(Ur,"pipeline"),Ur.forEach(s),Ra=r(Zt," for translation with your model, and pass your text to it:"),Zt.forEach(s),Os=_(e),q(Nt.$$.fragment,e),Is=_(e),yt=p(e,"P",{});var ea=i(yt);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=_(e),q(jt.$$.fragment,e),this.h()},h(){g(l,"name","hf:doc:metadata"),g(l,"content",JSON.stringify(_o)),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"),g(o,"class","relative group"),g(I,"href","https://huggingface.co/t5-small"),g(I,"rel","nofollow"),g(J,"href","https://huggingface.co/datasets/opus_books"),g(J,"rel","nofollow"),g(se,"id","load-opus-books-dataset"),g(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"),g(se,"href","#load-opus-books-dataset"),g(ue,"class","relative group"),g(L,"href","https://huggingface.co/datasets/opus_books"),g(L,"rel","nofollow"),g(De,"id","preprocess"),g(De,"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(De,"href","#preprocess"),g(Ie,"class","relative group"),g(Ut,"href","/docs/transformers/v4.27.1/en/main_classes/data_collator#transformers.DataCollatorForSeq2Seq"),g(_t,"id","evaluate"),g(_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"),g(_t,"href","#evaluate"),g(ct,"class","relative group"),g(zt,"href","https://huggingface.co/docs/evaluate/index"),g(zt,"rel","nofollow"),g(Ft,"href","https://huggingface.co/spaces/evaluate-metric/sacrebleu"),g(Ft,"rel","nofollow"),g(Mt,"href","https://huggingface.co/docs/evaluate/a_quick_tour"),g(Mt,"rel","nofollow"),g(Ct,"href","https://huggingface.co/docs/evaluate/v0.4.0/en/package_reference/main_classes#evaluate.EvaluationModule.compute"),g(Ct,"rel","nofollow"),g(kt,"id","train"),g(kt,"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(kt,"href","#train"),g(ut,"class","relative group"),g(vt,"id","inference"),g(vt,"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(vt,"href","#inference"),g(ht,"class","relative group"),g(Kt,"href","/docs/transformers/v4.27.1/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,pe),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,ge),t(W,X),t(W,te),t(te,qe),m(e,oe,u),T(we,e,u),m(e,$e,u),m(e,ke,u),t(ke,Be),m(e,ze,u),T(G,e,u),m(e,Me,u),m(e,ie,u),t(ie,Pe),m(e,Le,u),T(fe,e,u),m(e,me,u),m(e,ue,u),t(ue,se),t(se,le),T(ce,le,null),t(ue,ve),t(ue,ye),t(ye,Te),m(e,je,u),m(e,ne,u),t(ne,k),t(ne,L),t(L,be),t(ne,Fe),m(e,xe,u),T(V,e,u),m(e,Se,u),m(e,Ee,u),t(Ee,he),t(Ee,Ce),t(Ce,de),t(Ee,Ae),m(e,Oe,u),T(re,e,u),m(e,w,u),m(e,_e,u),t(_e,Ne),m(e,He,u),T(Ue,e,u),m(e,Re,u),m(e,f,u),t(f,P),t(P,Xe),t(f,Ye),m(e,at,u),m(e,Ie,u),t(Ie,De),t(De,et),T(Ge,et,null),t(Ie,tt),t(Ie,st),t(st,Ke),m(e,rt,u),T(We,e,u),m(e,Ze,u),m(e,Je,u),t(Je,it),m(e,ft,u),T(ot,e,u),m(e,Tt,u),m(e,mt,u),t(mt,aa),m(e,ks,u),m(e,lt,u),t(lt,Qt),t(Qt,ra),t(lt,oa),t(lt,es),t(es,la),t(lt,na),t(lt,xt),t(xt,pa),t(xt,ts),t(ts,ia),t(xt,fa),m(e,bs,u),T(St,e,u),m(e,ws,u),m(e,Ve,u),t(Ve,ma),t(Ve,ss),t(ss,ca),t(Ve,ua),t(Ve,as),t(as,ha),t(Ve,da),t(Ve,rs),t(rs,_a),t(Ve,ga),m(e,vs,u),T(At,e,u),m(e,ys,u),m(e,nt,u),t(nt,$a),t(nt,Ut),t(Ut,ka),t(nt,ba),t(nt,os),t(os,wa),t(nt,va),m(e,js,u),T(dt,e,u),m(e,Es,u),m(e,ct,u),t(ct,_t),t(_t,ls),T(Pt,ls,null),t(ct,ya),t(ct,ns),t(ns,ja),m(e,qs,u),m(e,Qe,u),t(Qe,Ea),t(Qe,zt),t(zt,qa),t(Qe,Ta),t(Qe,Ft),t(Ft,xa),t(Qe,Sa),t(Qe,Mt),t(Mt,Aa),t(Qe,Pa),m(e,Ts,u),T(Lt,e,u),m(e,xs,u),m(e,gt,u),t(gt,za),t(gt,Ct),t(Ct,Fa),t(gt,Ma),m(e,Ss,u),T(Dt,e,u),m(e,As,u),m(e,$t,u),t($t,La),t($t,ps),t(ps,Ca),t($t,Da),m(e,Ps,u),m(e,ut,u),t(ut,kt),t(kt,is),T(Bt,is,null),t(ut,Ba),t(ut,fs),t(fs,Oa),m(e,zs,u),T(bt,e,u),m(e,Fs,u),T(wt,e,u),m(e,Ms,u),m(e,ht,u),t(ht,vt),t(vt,ms),T(Ot,ms,null),t(ht,Ia),t(ht,cs),t(cs,Na),m(e,Ls,u),m(e,Yt,u),t(Yt,Ha),m(e,Cs,u),m(e,Gt,u),t(Gt,Ua),m(e,Ds,u),T(It,e,u),m(e,Bs,u),m(e,pt,u),t(pt,Ya),t(pt,Kt),t(Kt,Ga),t(pt,Ka),t(pt,us),t(us,Wa),t(pt,Ra),m(e,Os,u),T(Nt,e,u),m(e,Is,u),m(e,yt,u),t(yt,Xa),t(yt,hs),t(hs,Za),t(yt,Ja),m(e,Ns,u),T(jt,e,u),Hs=!0},p(e,[u]){const Ht={};u&2&&(Ht.$$scope={dirty:u,ctx:e}),we.$set(Ht);const ds={};u&2&&(ds.$$scope={dirty:u,ctx:e}),dt.$set(ds);const _s={};u&2&&(_s.$$scope={dirty:u,ctx:e}),bt.$set(_s);const gs={};u&2&&(gs.$$scope={dirty:u,ctx:e}),wt.$set(gs);const $s={};u&2&&($s.$$scope={dirty:u,ctx:e}),jt.$set($s)},i(e){Hs||(x(j.$$.fragment,e),x(F.$$.fragment,e),x(N.$$.fragment,e),x(we.$$.fragment,e),x(G.$$.fragment,e),x(fe.$$.fragment,e),x(ce.$$.fragment,e),x(V.$$.fragment,e),x(re.$$.fragment,e),x(Ue.$$.fragment,e),x(Ge.$$.fragment,e),x(We.$$.fragment,e),x(ot.$$.fragment,e),x(St.$$.fragment,e),x(At.$$.fragment,e),x(dt.$$.fragment,e),x(Pt.$$.fragment,e),x(Lt.$$.fragment,e),x(Dt.$$.fragment,e),x(Bt.$$.fragment,e),x(bt.$$.fragment,e),x(wt.$$.fragment,e),x(Ot.$$.fragment,e),x(It.$$.fragment,e),x(Nt.$$.fragment,e),x(jt.$$.fragment,e),Hs=!0)},o(e){S(j.$$.fragment,e),S(F.$$.fragment,e),S(N.$$.fragment,e),S(we.$$.fragment,e),S(G.$$.fragment,e),S(fe.$$.fragment,e),S(ce.$$.fragment,e),S(V.$$.fragment,e),S(re.$$.fragment,e),S(Ue.$$.fragment,e),S(Ge.$$.fragment,e),S(We.$$.fragment,e),S(ot.$$.fragment,e),S(St.$$.fragment,e),S(At.$$.fragment,e),S(dt.$$.fragment,e),S(Pt.$$.fragment,e),S(Lt.$$.fragment,e),S(Dt.$$.fragment,e),S(Bt.$$.fragment,e),S(bt.$$.fragment,e),S(wt.$$.fragment,e),S(Ot.$$.fragment,e),S(It.$$.fragment,e),S(Nt.$$.fragment,e),S(jt.$$.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(oe),A(we,e),e&&s($e),e&&s(ke),e&&s(ze),A(G,e),e&&s(Me),e&&s(ie),e&&s(Le),A(fe,e),e&&s(me),e&&s(ue),A(ce),e&&s(je),e&&s(ne),e&&s(xe),A(V,e),e&&s(Se),e&&s(Ee),e&&s(Oe),A(re,e),e&&s(w),e&&s(_e),e&&s(He),A(Ue,e),e&&s(Re),e&&s(f),e&&s(at),e&&s(Ie),A(Ge),e&&s(rt),A(We,e),e&&s(Ze),e&&s(Je),e&&s(ft),A(ot,e),e&&s(Tt),e&&s(mt),e&&s(ks),e&&s(lt),e&&s(bs),A(St,e),e&&s(ws),e&&s(Ve),e&&s(vs),A(At,e),e&&s(ys),e&&s(nt),e&&s(js),A(dt,e),e&&s(Es),e&&s(ct),A(Pt),e&&s(qs),e&&s(Qe),e&&s(Ts),A(Lt,e),e&&s(xs),e&&s(gt),e&&s(Ss),A(Dt,e),e&&s(As),e&&s($t),e&&s(Ps),e&&s(ut),A(Bt),e&&s(zs),A(bt,e),e&&s(Fs),A(wt,e),e&&s(Ms),e&&s(ht),A(Ot),e&&s(Ls),e&&s(Yt),e&&s(Cs),e&&s(Gt),e&&s(Ds),A(It,e),e&&s(Bs),e&&s(pt),e&&s(Os),A(Nt,e),e&&s(Is),e&&s(yt),e&&s(Ns),A(jt,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};
