import{S as Yr,i as Gr,s as Kr,e as l,k as _,w as E,t as a,M as Wr,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 Rr,L as ta}from"../../chunks/vendor-hf-doc-builder.js";import{T as ea}from"../../chunks/Tip-hf-doc-builder.js";import{Y as Nr}from"../../chunks/Youtube-hf-doc-builder.js";import{I as Vt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as W}from"../../chunks/CodeBlock-hf-doc-builder.js";import{F as Ja,M as Qt}from"../../chunks/Markdown-hf-doc-builder.js";function Xr(G){let n,c,o,$,b,j,z,M,w,L,F,I,H,K,Z,te,Q,D,R,B,ee,U,N,C,u,y,se,J,re,oe,ae,_e,V,qe,ge,ve,ye,Te,Y,Pe,fe,he,je,pe,me,xe,ie,ce,k,O,ne,X,Be,$e,Fe,Se,ue,ke,Me,le,Ae,be,de,We;return{c(){n=a(`The task illustrated in this tutorial is supported by the following model architectures:

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

`),c=p(we,"P",{});var v=i(c);o=p(v,"A",{href:!0});var Re=i(o);$=r(Re,"BART"),Re.forEach(s),b=r(v,", "),j=p(v,"A",{href:!0});var Ue=i(j);z=r(Ue,"BigBird-Pegasus"),Ue.forEach(s),M=r(v,", "),w=p(v,"A",{href:!0});var Xe=i(w);L=r(Xe,"Blenderbot"),Xe.forEach(s),F=r(v,", "),I=p(v,"A",{href:!0});var Le=i(I);H=r(Le,"BlenderbotSmall"),Le.forEach(s),K=r(v,", "),Z=p(v,"A",{href:!0});var Ce=i(Z);te=r(Ce,"Encoder decoder"),Ce.forEach(s),Q=r(v,", "),D=p(v,"A",{href:!0});var Oe=i(D);R=r(Oe,"FairSeq Machine-Translation"),Oe.forEach(s),B=r(v,", "),ee=p(v,"A",{href:!0});var Ie=i(ee);U=r(Ie,"LED"),Ie.forEach(s),N=r(v,", "),C=p(v,"A",{href:!0});var Ne=i(C);u=r(Ne,"LongT5"),Ne.forEach(s),y=r(v,", "),se=p(v,"A",{href:!0});var f=i(se);J=r(f,"M2M100"),f.forEach(s),re=r(v,", "),oe=p(v,"A",{href:!0});var P=i(oe);ae=r(P,"Marian"),P.forEach(s),_e=r(v,", "),V=p(v,"A",{href:!0});var Ye=i(V);qe=r(Ye,"mBART"),Ye.forEach(s),ge=r(v,", "),ve=p(v,"A",{href:!0});var Ee=i(ve);ye=r(Ee,"MT5"),Ee.forEach(s),Te=r(v,", "),Y=p(v,"A",{href:!0});var He=i(Y);Pe=r(He,"MVP"),He.forEach(s),fe=r(v,", "),he=p(v,"A",{href:!0});var Ze=i(he);je=r(Ze,"NLLB"),Ze.forEach(s),pe=r(v,", "),me=p(v,"A",{href:!0});var De=i(me);xe=r(De,"Pegasus"),De.forEach(s),ie=r(v,", "),ce=p(v,"A",{href:!0});var ot=i(ce);k=r(ot,"PEGASUS-X"),ot.forEach(s),O=r(v,", "),ne=p(v,"A",{href:!0});var Je=i(ne);X=r(Je,"PLBart"),Je.forEach(s),Be=r(v,", "),$e=p(v,"A",{href:!0});var Ve=i($e);Fe=r(Ve,"ProphetNet"),Ve.forEach(s),Se=r(v,", "),ue=p(v,"A",{href:!0});var Qe=i(ue);ke=r(Qe,"SwitchTransformers"),Qe.forEach(s),Me=r(v,", "),le=p(v,"A",{href:!0});var ze=i(le);Ae=r(ze,"T5"),ze.forEach(s),be=r(v,", "),de=p(v,"A",{href:!0});var et=i(de);We=r(et,"XLM-ProphetNet"),et.forEach(s),v.forEach(s),this.h()},h(){d(o,"href","../model_doc/bart"),d(j,"href","../model_doc/bigbird_pegasus"),d(w,"href","../model_doc/blenderbot"),d(I,"href","../model_doc/blenderbot-small"),d(Z,"href","../model_doc/encoder-decoder"),d(D,"href","../model_doc/fsmt"),d(ee,"href","../model_doc/led"),d(C,"href","../model_doc/longt5"),d(se,"href","../model_doc/m2m_100"),d(oe,"href","../model_doc/marian"),d(V,"href","../model_doc/mbart"),d(ve,"href","../model_doc/mt5"),d(Y,"href","../model_doc/mvp"),d(he,"href","../model_doc/nllb"),d(me,"href","../model_doc/pegasus"),d(ce,"href","../model_doc/pegasus_x"),d(ne,"href","../model_doc/plbart"),d($e,"href","../model_doc/prophetnet"),d(ue,"href","../model_doc/switch_transformers"),d(le,"href","../model_doc/t5"),d(de,"href","../model_doc/xlm-prophetnet")},m(we,v){m(we,n,v),m(we,c,v),t(c,o),t(o,$),t(c,b),t(c,j),t(j,z),t(c,M),t(c,w),t(w,L),t(c,F),t(c,I),t(I,H),t(c,K),t(c,Z),t(Z,te),t(c,Q),t(c,D),t(D,R),t(c,B),t(c,ee),t(ee,U),t(c,N),t(c,C),t(C,u),t(c,y),t(c,se),t(se,J),t(c,re),t(c,oe),t(oe,ae),t(c,_e),t(c,V),t(V,qe),t(c,ge),t(c,ve),t(ve,ye),t(c,Te),t(c,Y),t(Y,Pe),t(c,fe),t(c,he),t(he,je),t(c,pe),t(c,me),t(me,xe),t(c,ie),t(c,ce),t(ce,k),t(c,O),t(c,ne),t(ne,X),t(c,Be),t(c,$e),t($e,Fe),t(c,Se),t(c,ue),t(ue,ke),t(c,Me),t(c,le),t(le,Ae),t(c,be),t(c,de),t(de,We)},d(we){we&&s(n),we&&s(c)}}}function Zr(G){let n,c;return n=new W({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(n.$$.fragment)},l(o){q(n.$$.fragment,o)},m(o,$){T(n,o,$),c=!0},p:ta,i(o){c||(x(n.$$.fragment,o),c=!0)},o(o){S(n.$$.fragment,o),c=!1},d(o){A(n,o)}}}function Jr(G){let n,c;return n=new Qt({props:{$$slots:{default:[Zr]},$$scope:{ctx:G}}}),{c(){E(n.$$.fragment)},l(o){q(n.$$.fragment,o)},m(o,$){T(n,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),n.$set(b)},i(o){c||(x(n.$$.fragment,o),c=!0)},o(o){S(n.$$.fragment,o),c=!1},d(o){A(n,o)}}}function Vr(G){let n,c;return n=new W({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(n.$$.fragment)},l(o){q(n.$$.fragment,o)},m(o,$){T(n,o,$),c=!0},p:ta,i(o){c||(x(n.$$.fragment,o),c=!0)},o(o){S(n.$$.fragment,o),c=!1},d(o){A(n,o)}}}function Qr(G){let n,c;return n=new Qt({props:{$$slots:{default:[Vr]},$$scope:{ctx:G}}}),{c(){E(n.$$.fragment)},l(o){q(n.$$.fragment,o)},m(o,$){T(n,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),n.$set(b)},i(o){c||(x(n.$$.fragment,o),c=!0)},o(o){S(n.$$.fragment,o),c=!1},d(o){A(n,o)}}}function eo(G){let n,c,o,$,b,j,z,M;return{c(){n=l("p"),c=a("If you aren\u2019t familiar with finetuning a model with the "),o=l("a"),$=a("Trainer"),b=a(", take a look at the basic tutorial "),j=l("a"),z=a("here"),M=a("!"),this.h()},l(w){n=p(w,"P",{});var L=i(n);c=r(L,"If you aren\u2019t familiar with finetuning a model with the "),o=p(L,"A",{href:!0});var F=i(o);$=r(F,"Trainer"),F.forEach(s),b=r(L,", take a look at the basic tutorial "),j=p(L,"A",{href:!0});var I=i(j);z=r(I,"here"),I.forEach(s),M=r(L,"!"),L.forEach(s),this.h()},h(){d(o,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer"),d(j,"href","../training#train-with-pytorch-trainer")},m(w,L){m(w,n,L),t(n,c),t(n,o),t(o,$),t(n,b),t(n,j),t(j,z),t(n,M)},d(w){w&&s(n)}}}function to(G){let n,c,o,$,b,j,z,M,w,L,F,I,H,K,Z,te,Q,D,R,B,ee,U,N,C,u,y,se,J,re,oe,ae,_e,V,qe,ge,ve,ye,Te,Y,Pe,fe,he,je,pe,me,xe,ie,ce;return n=new ea({props:{$$slots:{default:[eo]},$$scope:{ctx:G}}}),o=new W({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)`}}),Y=new W({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()`}}),ie=new W({props:{code:"trainer.push_to_hub()",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.push_to_hub()'}}),{c(){E(n.$$.fragment),c=a(`
You're ready to start training your model now! Load T5 with [AutoModelForSeq2SeqLM](/docs/transformers/main/en/model_doc/auto#transformers.AutoModelForSeq2SeqLM):

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

	`),q(o.$$.fragment,k),$=g(k),b=p(k,"P",{});var O=i(b);j=r(O,"At this point, only three steps remain:"),O.forEach(s),z=g(k),M=p(k,"OL",{});var ne=i(M);w=p(ne,"LI",{});var X=i(w);L=r(X,"Define your training hyperparameters in "),F=p(X,"A",{href:!0});var Be=i(F);I=r(Be,"Seq2SeqTrainingArguments"),Be.forEach(s),H=r(X,". The only required parameter is "),K=p(X,"CODE",{});var $e=i(K);Z=r($e,"output_dir"),$e.forEach(s),te=r(X," which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),Q=p(X,"CODE",{});var Fe=i(Q);D=r(Fe,"push_to_hub=True"),Fe.forEach(s),R=r(X," (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the "),B=p(X,"A",{href:!0});var Se=i(B);ee=r(Se,"Trainer"),Se.forEach(s),U=r(X," will evaluate the SacreBLEU metric and save the training checkpoint."),X.forEach(s),N=g(ne),C=p(ne,"LI",{});var ue=i(C);u=r(ue,"Pass the training arguments to "),y=p(ue,"A",{href:!0});var ke=i(y);se=r(ke,"Seq2SeqTrainer"),ke.forEach(s),J=r(ue," along with the model, dataset, tokenizer, data collator, and "),re=p(ue,"CODE",{});var Me=i(re);oe=r(Me,"compute_metrics"),Me.forEach(s),ae=r(ue," function."),ue.forEach(s),_e=g(ne),V=p(ne,"LI",{});var le=i(V);qe=r(le,"Call "),ge=p(le,"A",{href:!0});var Ae=i(ge);ve=r(Ae,"train()"),Ae.forEach(s),ye=r(le," to finetune your model."),le.forEach(s),ne.forEach(s),Te=g(k),q(Y.$$.fragment,k),Pe=g(k),fe=p(k,"P",{});var be=i(fe);he=r(be,"Once training is completed, share your model to the Hub with the "),je=p(be,"A",{href:!0});var de=i(je);pe=r(de,"push_to_hub()"),de.forEach(s),me=r(be," method so everyone can use your model:"),be.forEach(s),xe=g(k),q(ie.$$.fragment,k),this.h()},h(){d(F,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Seq2SeqTrainingArguments"),d(B,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer"),d(y,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Seq2SeqTrainer"),d(ge,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer.train"),d(je,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer.push_to_hub")},m(k,O){T(n,k,O),m(k,c,O),T(o,k,O),m(k,$,O),m(k,b,O),t(b,j),m(k,z,O),m(k,M,O),t(M,w),t(w,L),t(w,F),t(F,I),t(w,H),t(w,K),t(K,Z),t(w,te),t(w,Q),t(Q,D),t(w,R),t(w,B),t(B,ee),t(w,U),t(M,N),t(M,C),t(C,u),t(C,y),t(y,se),t(C,J),t(C,re),t(re,oe),t(C,ae),t(M,_e),t(M,V),t(V,qe),t(V,ge),t(ge,ve),t(V,ye),m(k,Te,O),T(Y,k,O),m(k,Pe,O),m(k,fe,O),t(fe,he),t(fe,je),t(je,pe),t(fe,me),m(k,xe,O),T(ie,k,O),ce=!0},p(k,O){const ne={};O&2&&(ne.$$scope={dirty:O,ctx:k}),n.$set(ne)},i(k){ce||(x(n.$$.fragment,k),x(o.$$.fragment,k),x(Y.$$.fragment,k),x(ie.$$.fragment,k),ce=!0)},o(k){S(n.$$.fragment,k),S(o.$$.fragment,k),S(Y.$$.fragment,k),S(ie.$$.fragment,k),ce=!1},d(k){A(n,k),k&&s(c),A(o,k),k&&s($),k&&s(b),k&&s(z),k&&s(M),k&&s(Te),A(Y,k),k&&s(Pe),k&&s(fe),k&&s(xe),A(ie,k)}}}function so(G){let n,c;return n=new Qt({props:{$$slots:{default:[to]},$$scope:{ctx:G}}}),{c(){E(n.$$.fragment)},l(o){q(n.$$.fragment,o)},m(o,$){T(n,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),n.$set(b)},i(o){c||(x(n.$$.fragment,o),c=!0)},o(o){S(n.$$.fragment,o),c=!1},d(o){A(n,o)}}}function ao(G){let n,c,o,$,b;return{c(){n=l("p"),c=a("If you aren\u2019t familiar with finetuning a model with Keras, take a look at the basic tutorial "),o=l("a"),$=a("here"),b=a("!"),this.h()},l(j){n=p(j,"P",{});var z=i(n);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 M=i(o);$=r(M,"here"),M.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,n,z),t(n,c),t(n,o),t(o,$),t(n,b)},d(j){j&&s(n)}}}function ro(G){let n,c,o,$,b,j,z,M,w,L,F,I,H,K,Z,te,Q,D,R,B,ee,U,N,C,u,y,se,J,re,oe,ae,_e,V,qe,ge,ve,ye,Te,Y,Pe,fe,he,je,pe,me,xe,ie,ce,k,O,ne,X,Be,$e,Fe,Se,ue,ke,Me,le,Ae,be,de,We,we,v,Re,Ue,Xe,Le,Ce,Oe,Ie,Ne;return n=new ea({props:{$$slots:{default:[ao]},$$scope:{ctx:G}}}),o=new W({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 W({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)`}}),U=new W({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>)`}}),ae=new W({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)`}}),ce=new W({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)`}}),Se=new W({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 W({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]'}}),Le=new W({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(n.$$.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=l("p"),j=a("Then you can load T5 with "),z=l("a"),M=a("TFAutoModelForSeq2SeqLM"),w=a(":"),L=_(),E(F.$$.fragment),I=_(),H=l("p"),K=a("Convert your datasets to the "),Z=l("code"),te=a("tf.data.Dataset"),Q=a(" format with "),D=l("a"),R=a("prepare_tf_dataset()"),B=a(":"),ee=_(),E(U.$$.fragment),N=_(),C=l("p"),u=a("Configure the model for training with "),y=l("a"),se=l("code"),J=a("compile"),re=a(":"),oe=_(),E(ae.$$.fragment),_e=_(),V=l("p"),qe=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=l("a"),ve=a("Keras callbacks"),ye=a("."),Te=_(),Y=l("p"),Pe=a("Pass your "),fe=l("code"),he=a("compute_metrics"),je=a(" function to "),pe=l("a"),me=a("KerasMetricCallback"),xe=a(":"),ie=_(),E(ce.$$.fragment),k=_(),O=l("p"),ne=a("Specify where to push your model and tokenizer in the "),X=l("a"),Be=a("PushToHubCallback"),$e=a(":"),Fe=_(),E(Se.$$.fragment),ue=_(),ke=l("p"),Me=a("Then bundle your callbacks together:"),le=_(),E(Ae.$$.fragment),be=_(),de=l("p"),We=a("Finally, you\u2019re ready to start training your model! Call "),we=l("a"),v=l("code"),Re=a("fit"),Ue=a(" with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:"),Xe=_(),E(Le.$$.fragment),Ce=_(),Oe=l("p"),Ie=a("Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),this.h()},l(f){q(n.$$.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 Ye=i(z);M=r(Ye,"TFAutoModelForSeq2SeqLM"),Ye.forEach(s),w=r(P,":"),P.forEach(s),L=g(f),q(F.$$.fragment,f),I=g(f),H=p(f,"P",{});var Ee=i(H);K=r(Ee,"Convert your datasets to the "),Z=p(Ee,"CODE",{});var He=i(Z);te=r(He,"tf.data.Dataset"),He.forEach(s),Q=r(Ee," format with "),D=p(Ee,"A",{href:!0});var Ze=i(D);R=r(Ze,"prepare_tf_dataset()"),Ze.forEach(s),B=r(Ee,":"),Ee.forEach(s),ee=g(f),q(U.$$.fragment,f),N=g(f),C=p(f,"P",{});var De=i(C);u=r(De,"Configure the model for training with "),y=p(De,"A",{href:!0,rel:!0});var ot=i(y);se=p(ot,"CODE",{});var Je=i(se);J=r(Je,"compile"),Je.forEach(s),ot.forEach(s),re=r(De,":"),De.forEach(s),oe=g(f),q(ae.$$.fragment,f),_e=g(f),V=p(f,"P",{});var Ve=i(V);qe=r(Ve,"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(Ve,"A",{href:!0});var Qe=i(ge);ve=r(Qe,"Keras callbacks"),Qe.forEach(s),ye=r(Ve,"."),Ve.forEach(s),Te=g(f),Y=p(f,"P",{});var ze=i(Y);Pe=r(ze,"Pass your "),fe=p(ze,"CODE",{});var et=i(fe);he=r(et,"compute_metrics"),et.forEach(s),je=r(ze," function to "),pe=p(ze,"A",{href:!0});var nt=i(pe);me=r(nt,"KerasMetricCallback"),nt.forEach(s),xe=r(ze,":"),ze.forEach(s),ie=g(f),q(ce.$$.fragment,f),k=g(f),O=p(f,"P",{});var ct=i(O);ne=r(ct,"Specify where to push your model and tokenizer in the "),X=p(ct,"A",{href:!0});var Et=i(X);Be=r(Et,"PushToHubCallback"),Et.forEach(s),$e=r(ct,":"),ct.forEach(s),Fe=g(f),q(Se.$$.fragment,f),ue=g(f),ke=p(f,"P",{});var tt=i(ke);Me=r(tt,"Then bundle your callbacks together:"),tt.forEach(s),le=g(f),q(Ae.$$.fragment,f),be=g(f),de=p(f,"P",{});var lt=i(de);We=r(lt,"Finally, you\u2019re ready to start training your model! Call "),we=p(lt,"A",{href:!0,rel:!0});var pt=i(we);v=p(pt,"CODE",{});var Nt=i(v);Re=r(Nt,"fit"),Nt.forEach(s),pt.forEach(s),Ue=r(lt," with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:"),lt.forEach(s),Xe=g(f),q(Le.$$.fragment,f),Ce=g(f),Oe=p(f,"P",{});var qt=i(Oe);Ie=r(qt,"Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),qt.forEach(s),this.h()},h(){d(z,"href","/docs/transformers/main/en/model_doc/auto#transformers.TFAutoModelForSeq2SeqLM"),d(D,"href","/docs/transformers/main/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(pe,"href","/docs/transformers/main/en/main_classes/keras_callbacks#transformers.KerasMetricCallback"),d(X,"href","/docs/transformers/main/en/main_classes/keras_callbacks#transformers.PushToHubCallback"),d(we,"href","https://keras.io/api/models/model_training_apis/#fit-method"),d(we,"rel","nofollow")},m(f,P){T(n,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,M),t(b,w),m(f,L,P),T(F,f,P),m(f,I,P),m(f,H,P),t(H,K),t(H,Z),t(Z,te),t(H,Q),t(H,D),t(D,R),t(H,B),m(f,ee,P),T(U,f,P),m(f,N,P),m(f,C,P),t(C,u),t(C,y),t(y,se),t(se,J),t(C,re),m(f,oe,P),T(ae,f,P),m(f,_e,P),m(f,V,P),t(V,qe),t(V,ge),t(ge,ve),t(V,ye),m(f,Te,P),m(f,Y,P),t(Y,Pe),t(Y,fe),t(fe,he),t(Y,je),t(Y,pe),t(pe,me),t(Y,xe),m(f,ie,P),T(ce,f,P),m(f,k,P),m(f,O,P),t(O,ne),t(O,X),t(X,Be),t(O,$e),m(f,Fe,P),T(Se,f,P),m(f,ue,P),m(f,ke,P),t(ke,Me),m(f,le,P),T(Ae,f,P),m(f,be,P),m(f,de,P),t(de,We),t(de,we),t(we,v),t(v,Re),t(de,Ue),m(f,Xe,P),T(Le,f,P),m(f,Ce,P),m(f,Oe,P),t(Oe,Ie),Ne=!0},p(f,P){const Ye={};P&2&&(Ye.$$scope={dirty:P,ctx:f}),n.$set(Ye)},i(f){Ne||(x(n.$$.fragment,f),x(o.$$.fragment,f),x(F.$$.fragment,f),x(U.$$.fragment,f),x(ae.$$.fragment,f),x(ce.$$.fragment,f),x(Se.$$.fragment,f),x(Ae.$$.fragment,f),x(Le.$$.fragment,f),Ne=!0)},o(f){S(n.$$.fragment,f),S(o.$$.fragment,f),S(F.$$.fragment,f),S(U.$$.fragment,f),S(ae.$$.fragment,f),S(ce.$$.fragment,f),S(Se.$$.fragment,f),S(Ae.$$.fragment,f),S(Le.$$.fragment,f),Ne=!1},d(f){A(n,f),f&&s(c),A(o,f),f&&s($),f&&s(b),f&&s(L),A(F,f),f&&s(I),f&&s(H),f&&s(ee),A(U,f),f&&s(N),f&&s(C),f&&s(oe),A(ae,f),f&&s(_e),f&&s(V),f&&s(Te),f&&s(Y),f&&s(ie),A(ce,f),f&&s(k),f&&s(O),f&&s(Fe),A(Se,f),f&&s(ue),f&&s(ke),f&&s(le),A(Ae,f),f&&s(be),f&&s(de),f&&s(Xe),A(Le,f),f&&s(Ce),f&&s(Oe)}}}function oo(G){let n,c;return n=new Qt({props:{$$slots:{default:[ro]},$$scope:{ctx:G}}}),{c(){E(n.$$.fragment)},l(o){q(n.$$.fragment,o)},m(o,$){T(n,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),n.$set(b)},i(o){c||(x(n.$$.fragment,o),c=!0)},o(o){S(n.$$.fragment,o),c=!1},d(o){A(n,o)}}}function no(G){let n,c,o,$,b,j,z,M;return{c(){n=l("p"),c=a(`For a more in-depth example of how to finetune a model for translation, take a look at the corresponding
`),o=l("a"),$=a("PyTorch notebook"),b=a(`
or `),j=l("a"),z=a("TensorFlow notebook"),M=a("."),this.h()},l(w){n=p(w,"P",{});var L=i(n);c=r(L,`For a more in-depth example of how to finetune a model for translation, take a look at the corresponding
`),o=p(L,"A",{href:!0,rel:!0});var F=i(o);$=r(F,"PyTorch notebook"),F.forEach(s),b=r(L,`
or `),j=p(L,"A",{href:!0,rel:!0});var I=i(j);z=r(I,"TensorFlow notebook"),I.forEach(s),M=r(L,"."),L.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(w,L){m(w,n,L),t(n,c),t(n,o),t(o,$),t(n,b),t(n,j),t(j,z),t(n,M)},d(w){w&&s(n)}}}function lo(G){let n,c,o,$,b,j,z,M,w,L,F,I,H,K,Z,te,Q,D,R,B,ee,U,N,C;return z=new W({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 W({props:{code:`from transformers import AutoModelForSeq2SeqLM

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

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSeq2SeqLM.from_pretrained(<span class="hljs-string">&quot;my_awesome_opus_books_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(inputs, max_new_tokens=<span class="hljs-number">40</span>, do_sample=<span class="hljs-literal">True</span>, top_k=<span class="hljs-number">30</span>, top_p=<span class="hljs-number">0.95</span>)`}}),N=new W({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(){n=l("p"),c=a("Tokenize the text and return the "),o=l("code"),$=a("input_ids"),b=a(" as PyTorch tensors:"),j=_(),E(z.$$.fragment),M=_(),w=l("p"),L=a("Use the "),F=l("a"),I=a("generate()"),H=a(" method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),K=l("a"),Z=a("Text Generation"),te=a(" API."),Q=_(),E(D.$$.fragment),R=_(),B=l("p"),ee=a("Decode the generated token ids back into text:"),U=_(),E(N.$$.fragment),this.h()},l(u){n=p(u,"P",{});var y=i(n);c=r(y,"Tokenize the text and return the "),o=p(y,"CODE",{});var se=i(o);$=r(se,"input_ids"),se.forEach(s),b=r(y," as PyTorch tensors:"),y.forEach(s),j=g(u),q(z.$$.fragment,u),M=g(u),w=p(u,"P",{});var J=i(w);L=r(J,"Use the "),F=p(J,"A",{href:!0});var re=i(F);I=r(re,"generate()"),re.forEach(s),H=r(J," method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the "),K=p(J,"A",{href:!0});var oe=i(K);Z=r(oe,"Text Generation"),oe.forEach(s),te=r(J," API."),J.forEach(s),Q=g(u),q(D.$$.fragment,u),R=g(u),B=p(u,"P",{});var ae=i(B);ee=r(ae,"Decode the generated token ids back into text:"),ae.forEach(s),U=g(u),q(N.$$.fragment,u),this.h()},h(){d(F,"href","/docs/transformers/main/en/main_classes/text_generation#transformers.GenerationMixin.generate"),d(K,"href","./main_classes/text_generation")},m(u,y){m(u,n,y),t(n,c),t(n,o),t(o,$),t(n,b),m(u,j,y),T(z,u,y),m(u,M,y),m(u,w,y),t(w,L),t(w,F),t(F,I),t(w,H),t(w,K),t(K,Z),t(w,te),m(u,Q,y),T(D,u,y),m(u,R,y),m(u,B,y),t(B,ee),m(u,U,y),T(N,u,y),C=!0},p:ta,i(u){C||(x(z.$$.fragment,u),x(D.$$.fragment,u),x(N.$$.fragment,u),C=!0)},o(u){S(z.$$.fragment,u),S(D.$$.fragment,u),S(N.$$.fragment,u),C=!1},d(u){u&&s(n),u&&s(j),A(z,u),u&&s(M),u&&s(w),u&&s(Q),A(D,u),u&&s(R),u&&s(B),u&&s(U),A(N,u)}}}function po(G){let n,c;return n=new Qt({props:{$$slots:{default:[lo]},$$scope:{ctx:G}}}),{c(){E(n.$$.fragment)},l(o){q(n.$$.fragment,o)},m(o,$){T(n,o,$),c=!0},p(o,$){const b={};$&2&&(b.$$scope={dirty:$,ctx:o}),n.$set(b)},i(o){c||(x(n.$$.fragment,o),c=!0)},o(o){S(n.$$.fragment,o),c=!1},d(o){A(n,o)}}}function io(G){let n,c,o,$,b,j,z,M,w,L,F,I,H,K,Z,te,Q,D,R,B,ee,U,N,C;return z=new W({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 W({props:{code:`from transformers import TFAutoModelForSeq2SeqLM

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

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