import{S as Xo,i as Zo,s as er,e as i,k as c,w as y,t as n,M as tr,c as p,d as t,m as d,a as u,x as v,h as o,b,G as s,g as f,y as x,q,o as E,B as T,v as sr,L as ca}from"../../chunks/vendor-hf-doc-builder.js";import{T as ha}from"../../chunks/Tip-hf-doc-builder.js";import{Y as Ko}from"../../chunks/Youtube-hf-doc-builder.js";import{I as Ut}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as B}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as ar}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";import{F as Yn,M as Yt}from"../../chunks/Markdown-hf-doc-builder.js";function nr(S){let r,g,a,_,w;return{c(){r=i("p"),g=n("See the question answering "),a=i("a"),_=n("task page"),w=n(" for more information about other forms of question answering and their associated models, datasets, and metrics."),this.h()},l(j){r=p(j,"P",{});var z=u(r);g=o(z,"See the question answering "),a=p(z,"A",{href:!0,rel:!0});var D=u(a);_=o(D,"task page"),D.forEach(t),w=o(z," for more information about other forms of question answering and their associated models, datasets, and metrics."),z.forEach(t),this.h()},h(){b(a,"href","https://huggingface.co/tasks/question-answering"),b(a,"rel","nofollow")},m(j,z){f(j,r,z),s(r,g),s(r,a),s(a,_),s(r,w)},d(j){j&&t(r)}}}function or(S){let r,g;return r=new B({props:{code:`from transformers import DefaultDataCollator

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

<span class="hljs-meta">&gt;&gt;&gt; </span>data_collator = DefaultDataCollator()`}}),{c(){y(r.$$.fragment)},l(a){v(r.$$.fragment,a)},m(a,_){x(r,a,_),g=!0},p:ca,i(a){g||(q(r.$$.fragment,a),g=!0)},o(a){E(r.$$.fragment,a),g=!1},d(a){T(r,a)}}}function rr(S){let r,g;return r=new Yt({props:{$$slots:{default:[or]},$$scope:{ctx:S}}}),{c(){y(r.$$.fragment)},l(a){v(r.$$.fragment,a)},m(a,_){x(r,a,_),g=!0},p(a,_){const w={};_&2&&(w.$$scope={dirty:_,ctx:a}),r.$set(w)},i(a){g||(q(r.$$.fragment,a),g=!0)},o(a){E(r.$$.fragment,a),g=!1},d(a){T(r,a)}}}function lr(S){let r,g;return r=new B({props:{code:`from transformers import DefaultDataCollator

data_collator = DefaultDataCollator(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> DefaultDataCollator

<span class="hljs-meta">&gt;&gt;&gt; </span>data_collator = DefaultDataCollator(return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`}}),{c(){y(r.$$.fragment)},l(a){v(r.$$.fragment,a)},m(a,_){x(r,a,_),g=!0},p:ca,i(a){g||(q(r.$$.fragment,a),g=!0)},o(a){E(r.$$.fragment,a),g=!1},d(a){T(r,a)}}}function ir(S){let r,g;return r=new Yt({props:{$$slots:{default:[lr]},$$scope:{ctx:S}}}),{c(){y(r.$$.fragment)},l(a){v(r.$$.fragment,a)},m(a,_){x(r,a,_),g=!0},p(a,_){const w={};_&2&&(w.$$scope={dirty:_,ctx:a}),r.$set(w)},i(a){g||(q(r.$$.fragment,a),g=!0)},o(a){E(r.$$.fragment,a),g=!1},d(a){T(r,a)}}}function pr(S){let r,g,a,_,w,j,z,D;return{c(){r=i("p"),g=n("If you aren\u2019t familiar with finetuning a model with the "),a=i("a"),_=n("Trainer"),w=n(", take a look at the basic tutorial "),j=i("a"),z=n("here"),D=n("!"),this.h()},l(P){r=p(P,"P",{});var C=u(r);g=o(C,"If you aren\u2019t familiar with finetuning a model with the "),a=p(C,"A",{href:!0});var F=u(a);_=o(F,"Trainer"),F.forEach(t),w=o(C,", take a look at the basic tutorial "),j=p(C,"A",{href:!0});var M=u(j);z=o(M,"here"),M.forEach(t),D=o(C,"!"),C.forEach(t),this.h()},h(){b(a,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer"),b(j,"href","../training#train-with-pytorch-trainer")},m(P,C){f(P,r,C),s(r,g),s(r,a),s(a,_),s(r,w),s(r,j),s(j,z),s(r,D)},d(P){P&&t(r)}}}function ur(S){let r,g,a,_,w,j,z,D,P,C,F,M,Q,N,J,Z,I,H,G,se,R,O,Y,l,k,W,K,X,U,be,ae,ge,ee,le,ne,ve,ue,fe,pe,me,V,$e;return r=new ha({props:{$$slots:{default:[pr]},$$scope:{ctx:S}}}),a=new B({props:{code:`from transformers import AutoModelForQuestionAnswering, TrainingArguments, Trainer

model = AutoModelForQuestionAnswering.from_pretrained("distilbert-base-uncased")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForQuestionAnswering, TrainingArguments, Trainer

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForQuestionAnswering.from_pretrained(<span class="hljs-string">&quot;distilbert-base-uncased&quot;</span>)`}}),ee=new B({props:{code:`training_args = TrainingArguments(
    output_dir="my_awesome_qa_model",
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    num_train_epochs=3,
    weight_decay=0.01,
    push_to_hub=True,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_squad["train"],
    eval_dataset=tokenized_squad["test"],
    tokenizer=tokenizer,
    data_collator=data_collator,
)

trainer.train()`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments(
<span class="hljs-meta">... </span>    output_dir=<span class="hljs-string">&quot;my_awesome_qa_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>    num_train_epochs=<span class="hljs-number">3</span>,
<span class="hljs-meta">... </span>    weight_decay=<span class="hljs-number">0.01</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 = Trainer(
<span class="hljs-meta">... </span>    model=model,
<span class="hljs-meta">... </span>    args=training_args,
<span class="hljs-meta">... </span>    train_dataset=tokenized_squad[<span class="hljs-string">&quot;train&quot;</span>],
<span class="hljs-meta">... </span>    eval_dataset=tokenized_squad[<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>)

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

	`),y(a.$$.fragment),_=c(),w=i("p"),j=n("At this point, only three steps remain:"),z=c(),D=i("ol"),P=i("li"),C=n("Define your training hyperparameters in "),F=i("a"),M=n("TrainingArguments"),Q=n(". The only required parameter is "),N=i("code"),J=n("output_dir"),Z=n(" which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),I=i("code"),H=n("push_to_hub=True"),G=n(" (you need to be signed in to Hugging Face to upload your model)."),se=c(),R=i("li"),O=n("Pass the training arguments to "),Y=i("a"),l=n("Trainer"),k=n(" along with the model, dataset, tokenizer, and data collator."),W=c(),K=i("li"),X=n("Call "),U=i("a"),be=n("train()"),ae=n(" to finetune your model."),ge=c(),y(ee.$$.fragment),le=c(),ne=i("p"),ve=n("Once training is completed, share your model to the Hub with the "),ue=i("a"),fe=n("push_to_hub()"),pe=n(" method so everyone can use your model:"),me=c(),y(V.$$.fragment),this.h()},l($){v(r.$$.fragment,$),g=o($,`
You're ready to start training your model now! Load DistilBERT with [AutoModelForQuestionAnswering](/docs/transformers/v4.25.1/en/model_doc/auto#transformers.AutoModelForQuestionAnswering):

	`),v(a.$$.fragment,$),_=d($),w=p($,"P",{});var L=u(w);j=o(L,"At this point, only three steps remain:"),L.forEach(t),z=d($),D=p($,"OL",{});var oe=u(D);P=p(oe,"LI",{});var te=u(P);C=o(te,"Define your training hyperparameters in "),F=p(te,"A",{href:!0});var je=u(F);M=o(je,"TrainingArguments"),je.forEach(t),Q=o(te,". The only required parameter is "),N=p(te,"CODE",{});var re=u(N);J=o(re,"output_dir"),re.forEach(t),Z=o(te," which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),I=p(te,"CODE",{});var xe=u(I);H=o(xe,"push_to_hub=True"),xe.forEach(t),G=o(te," (you need to be signed in to Hugging Face to upload your model)."),te.forEach(t),se=d(oe),R=p(oe,"LI",{});var ie=u(R);O=o(ie,"Pass the training arguments to "),Y=p(ie,"A",{href:!0});var he=u(Y);l=o(he,"Trainer"),he.forEach(t),k=o(ie," along with the model, dataset, tokenizer, and data collator."),ie.forEach(t),W=d(oe),K=p(oe,"LI",{});var ce=u(K);X=o(ce,"Call "),U=p(ce,"A",{href:!0});var m=u(U);be=o(m,"train()"),m.forEach(t),ae=o(ce," to finetune your model."),ce.forEach(t),oe.forEach(t),ge=d($),v(ee.$$.fragment,$),le=d($),ne=p($,"P",{});var A=u(ne);ve=o(A,"Once training is completed, share your model to the Hub with the "),ue=p(A,"A",{href:!0});var de=u(ue);fe=o(de,"push_to_hub()"),de.forEach(t),pe=o(A," method so everyone can use your model:"),A.forEach(t),me=d($),v(V.$$.fragment,$),this.h()},h(){b(F,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.TrainingArguments"),b(Y,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer"),b(U,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer.train"),b(ue,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer.push_to_hub")},m($,L){x(r,$,L),f($,g,L),x(a,$,L),f($,_,L),f($,w,L),s(w,j),f($,z,L),f($,D,L),s(D,P),s(P,C),s(P,F),s(F,M),s(P,Q),s(P,N),s(N,J),s(P,Z),s(P,I),s(I,H),s(P,G),s(D,se),s(D,R),s(R,O),s(R,Y),s(Y,l),s(R,k),s(D,W),s(D,K),s(K,X),s(K,U),s(U,be),s(K,ae),f($,ge,L),x(ee,$,L),f($,le,L),f($,ne,L),s(ne,ve),s(ne,ue),s(ue,fe),s(ne,pe),f($,me,L),x(V,$,L),$e=!0},p($,L){const oe={};L&2&&(oe.$$scope={dirty:L,ctx:$}),r.$set(oe)},i($){$e||(q(r.$$.fragment,$),q(a.$$.fragment,$),q(ee.$$.fragment,$),q(V.$$.fragment,$),$e=!0)},o($){E(r.$$.fragment,$),E(a.$$.fragment,$),E(ee.$$.fragment,$),E(V.$$.fragment,$),$e=!1},d($){T(r,$),$&&t(g),T(a,$),$&&t(_),$&&t(w),$&&t(z),$&&t(D),$&&t(ge),T(ee,$),$&&t(le),$&&t(ne),$&&t(me),T(V,$)}}}function fr(S){let r,g;return r=new Yt({props:{$$slots:{default:[ur]},$$scope:{ctx:S}}}),{c(){y(r.$$.fragment)},l(a){v(r.$$.fragment,a)},m(a,_){x(r,a,_),g=!0},p(a,_){const w={};_&2&&(w.$$scope={dirty:_,ctx:a}),r.$set(w)},i(a){g||(q(r.$$.fragment,a),g=!0)},o(a){E(r.$$.fragment,a),g=!1},d(a){T(r,a)}}}function mr(S){let r,g,a,_,w;return{c(){r=i("p"),g=n("If you aren\u2019t familiar with finetuning a model with Keras, take a look at the basic tutorial "),a=i("a"),_=n("here"),w=n("!"),this.h()},l(j){r=p(j,"P",{});var z=u(r);g=o(z,"If you aren\u2019t familiar with finetuning a model with Keras, take a look at the basic tutorial "),a=p(z,"A",{href:!0});var D=u(a);_=o(D,"here"),D.forEach(t),w=o(z,"!"),z.forEach(t),this.h()},h(){b(a,"href","../training#train-a-tensorflow-model-with-keras")},m(j,z){f(j,r,z),s(r,g),s(r,a),s(a,_),s(r,w)},d(j){j&&t(r)}}}function hr(S){let r,g,a,_,w,j,z,D,P,C,F,M,Q,N,J,Z,I,H,G,se,R,O,Y,l,k,W,K,X,U,be,ae,ge,ee,le,ne,ve,ue,fe,pe,me,V,$e,$,L,oe,te,je,re,xe,ie,he,ce;return r=new ha({props:{$$slots:{default:[mr]},$$scope:{ctx:S}}}),a=new B({props:{code:`from transformers import create_optimizer

batch_size = 16
num_epochs = 2
total_train_steps = (len(tokenized_squad["train"]) // batch_size) * num_epochs
optimizer, schedule = create_optimizer(
    init_lr=2e-5,
    num_warmup_steps=0,
    num_train_steps=total_train_steps,
)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> create_optimizer

<span class="hljs-meta">&gt;&gt;&gt; </span>batch_size = <span class="hljs-number">16</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>num_epochs = <span class="hljs-number">2</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>total_train_steps = (<span class="hljs-built_in">len</span>(tokenized_squad[<span class="hljs-string">&quot;train&quot;</span>]) // batch_size) * num_epochs
<span class="hljs-meta">&gt;&gt;&gt; </span>optimizer, schedule = create_optimizer(
<span class="hljs-meta">... </span>    init_lr=<span class="hljs-number">2e-5</span>,
<span class="hljs-meta">... </span>    num_warmup_steps=<span class="hljs-number">0</span>,
<span class="hljs-meta">... </span>    num_train_steps=total_train_steps,
<span class="hljs-meta">... </span>)`}}),F=new B({props:{code:`from transformers import TFAutoModelForQuestionAnswering

model = TFAutoModelForQuestionAnswering("distilbert-base-uncased")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForQuestionAnswering

<span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForQuestionAnswering(<span class="hljs-string">&quot;distilbert-base-uncased&quot;</span>)`}}),O=new B({props:{code:`tf_train_set = model.prepare_tf_dataset(
    tokenized_squad["train"],
    shuffle=True,
    batch_size=16,
    collate_fn=data_collator,
)

tf_validation_set = model.prepare_tf_dataset(
    tokenized_squad["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_squad[<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_validation_set = model.prepare_tf_dataset(
<span class="hljs-meta">... </span>    tokenized_squad[<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 B({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)`}}),pe=new B({props:{code:`from transformers.keras_callbacks import PushToHubCallback

callback = PushToHubCallback(
    output_dir="my_awesome_qa_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>callback = PushToHubCallback(
<span class="hljs-meta">... </span>    output_dir=<span class="hljs-string">&quot;my_awesome_qa_model&quot;</span>,
<span class="hljs-meta">... </span>    tokenizer=tokenizer,
<span class="hljs-meta">... </span>)`}}),re=new B({props:{code:"model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=[callback])",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=<span class="hljs-number">3</span>, callbacks=[callback])'}}),{c(){y(r.$$.fragment),g=n(`
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:

	`),y(a.$$.fragment),_=c(),w=i("p"),j=n("Then you can load DistilBERT with "),z=i("a"),D=n("TFAutoModelForQuestionAnswering"),P=n(":"),C=c(),y(F.$$.fragment),M=c(),Q=i("p"),N=n("Convert your datasets to the "),J=i("code"),Z=n("tf.data.Dataset"),I=n(" format with "),H=i("a"),G=n("prepare_tf_dataset()"),se=n(":"),R=c(),y(O.$$.fragment),Y=c(),l=i("p"),k=n("Configure the model for training with "),W=i("a"),K=i("code"),X=n("compile"),U=n(":"),be=c(),y(ae.$$.fragment),ge=c(),ee=i("p"),le=n("The last thing to setup before you start training is to provide a way to push your model to the Hub. This can be done by specifying where to push your model and tokenizer in the "),ne=i("a"),ve=n("PushToHubCallback"),ue=n(":"),fe=c(),y(pe.$$.fragment),me=c(),V=i("p"),$e=n("Finally, you\u2019re ready to start training your model! Call "),$=i("a"),L=i("code"),oe=n("fit"),te=n(" with your training and validation datasets, the number of epochs, and your callback to finetune the model:"),je=c(),y(re.$$.fragment),xe=c(),ie=i("p"),he=n("Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),this.h()},l(m){v(r.$$.fragment,m),g=o(m,`
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:

	`),v(a.$$.fragment,m),_=d(m),w=p(m,"P",{});var A=u(w);j=o(A,"Then you can load DistilBERT with "),z=p(A,"A",{href:!0});var de=u(z);D=o(de,"TFAutoModelForQuestionAnswering"),de.forEach(t),P=o(A,":"),A.forEach(t),C=d(m),v(F.$$.fragment,m),M=d(m),Q=p(m,"P",{});var _e=u(Q);N=o(_e,"Convert your datasets to the "),J=p(_e,"CODE",{});var yt=u(J);Z=o(yt,"tf.data.Dataset"),yt.forEach(t),I=o(_e," format with "),H=p(_e,"A",{href:!0});var He=u(H);G=o(He,"prepare_tf_dataset()"),He.forEach(t),se=o(_e,":"),_e.forEach(t),R=d(m),v(O.$$.fragment,m),Y=d(m),l=p(m,"P",{});var Le=u(l);k=o(Le,"Configure the model for training with "),W=p(Le,"A",{href:!0,rel:!0});var at=u(W);K=p(at,"CODE",{});var Fe=u(K);X=o(Fe,"compile"),Fe.forEach(t),at.forEach(t),U=o(Le,":"),Le.forEach(t),be=d(m),v(ae.$$.fragment,m),ge=d(m),ee=p(m,"P",{});var Be=u(ee);le=o(Be,"The last thing to setup before you start training is to provide a way to push your model to the Hub. This can be done by specifying where to push your model and tokenizer in the "),ne=p(Be,"A",{href:!0});var nt=u(ne);ve=o(nt,"PushToHubCallback"),nt.forEach(t),ue=o(Be,":"),Be.forEach(t),fe=d(m),v(pe.$$.fragment,m),me=d(m),V=p(m,"P",{});var ke=u(V);$e=o(ke,"Finally, you\u2019re ready to start training your model! Call "),$=p(ke,"A",{href:!0,rel:!0});var ot=u($);L=p(ot,"CODE",{});var we=u(L);oe=o(we,"fit"),we.forEach(t),ot.forEach(t),te=o(ke," with your training and validation datasets, the number of epochs, and your callback to finetune the model:"),ke.forEach(t),je=d(m),v(re.$$.fragment,m),xe=d(m),ie=p(m,"P",{});var vt=u(ie);he=o(vt,"Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!"),vt.forEach(t),this.h()},h(){b(z,"href","/docs/transformers/v4.25.1/en/model_doc/auto#transformers.TFAutoModelForQuestionAnswering"),b(H,"href","/docs/transformers/v4.25.1/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset"),b(W,"href","https://keras.io/api/models/model_training_apis/#compile-method"),b(W,"rel","nofollow"),b(ne,"href","/docs/transformers/v4.25.1/en/main_classes/keras_callbacks#transformers.PushToHubCallback"),b($,"href","https://keras.io/api/models/model_training_apis/#fit-method"),b($,"rel","nofollow")},m(m,A){x(r,m,A),f(m,g,A),x(a,m,A),f(m,_,A),f(m,w,A),s(w,j),s(w,z),s(z,D),s(w,P),f(m,C,A),x(F,m,A),f(m,M,A),f(m,Q,A),s(Q,N),s(Q,J),s(J,Z),s(Q,I),s(Q,H),s(H,G),s(Q,se),f(m,R,A),x(O,m,A),f(m,Y,A),f(m,l,A),s(l,k),s(l,W),s(W,K),s(K,X),s(l,U),f(m,be,A),x(ae,m,A),f(m,ge,A),f(m,ee,A),s(ee,le),s(ee,ne),s(ne,ve),s(ee,ue),f(m,fe,A),x(pe,m,A),f(m,me,A),f(m,V,A),s(V,$e),s(V,$),s($,L),s(L,oe),s(V,te),f(m,je,A),x(re,m,A),f(m,xe,A),f(m,ie,A),s(ie,he),ce=!0},p(m,A){const de={};A&2&&(de.$$scope={dirty:A,ctx:m}),r.$set(de)},i(m){ce||(q(r.$$.fragment,m),q(a.$$.fragment,m),q(F.$$.fragment,m),q(O.$$.fragment,m),q(ae.$$.fragment,m),q(pe.$$.fragment,m),q(re.$$.fragment,m),ce=!0)},o(m){E(r.$$.fragment,m),E(a.$$.fragment,m),E(F.$$.fragment,m),E(O.$$.fragment,m),E(ae.$$.fragment,m),E(pe.$$.fragment,m),E(re.$$.fragment,m),ce=!1},d(m){T(r,m),m&&t(g),T(a,m),m&&t(_),m&&t(w),m&&t(C),T(F,m),m&&t(M),m&&t(Q),m&&t(R),T(O,m),m&&t(Y),m&&t(l),m&&t(be),T(ae,m),m&&t(ge),m&&t(ee),m&&t(fe),T(pe,m),m&&t(me),m&&t(V),m&&t(je),T(re,m),m&&t(xe),m&&t(ie)}}}function cr(S){let r,g;return r=new Yt({props:{$$slots:{default:[hr]},$$scope:{ctx:S}}}),{c(){y(r.$$.fragment)},l(a){v(r.$$.fragment,a)},m(a,_){x(r,a,_),g=!0},p(a,_){const w={};_&2&&(w.$$scope={dirty:_,ctx:a}),r.$set(w)},i(a){g||(q(r.$$.fragment,a),g=!0)},o(a){E(r.$$.fragment,a),g=!1},d(a){T(r,a)}}}function dr(S){let r,g,a,_,w,j,z,D;return{c(){r=i("p"),g=n(`For a more in-depth example of how to finetune a model for question answering, take a look at the corresponding
`),a=i("a"),_=n("PyTorch notebook"),w=n(`
or `),j=i("a"),z=n("TensorFlow notebook"),D=n("."),this.h()},l(P){r=p(P,"P",{});var C=u(r);g=o(C,`For a more in-depth example of how to finetune a model for question answering, take a look at the corresponding
`),a=p(C,"A",{href:!0,rel:!0});var F=u(a);_=o(F,"PyTorch notebook"),F.forEach(t),w=o(C,`
or `),j=p(C,"A",{href:!0,rel:!0});var M=u(j);z=o(M,"TensorFlow notebook"),M.forEach(t),D=o(C,"."),C.forEach(t),this.h()},h(){b(a,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb"),b(a,"rel","nofollow"),b(j,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb"),b(j,"rel","nofollow")},m(P,C){f(P,r,C),s(r,g),s(r,a),s(a,_),s(r,w),s(r,j),s(j,z),s(r,D)},d(P){P&&t(r)}}}function _r(S){let r,g,a,_,w,j,z,D,P,C,F,M,Q,N,J,Z,I,H,G,se,R,O,Y;return _=new B({props:{code:`from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model")
inputs = tokenizer(question, context, return_tensors="pt")`,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_qa_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(question, context, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)`}}),M=new B({props:{code:`from transformers import AutoModelForQuestionAnswering

model = AutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model")
with torch.no_grad():
    outputs = model(**inputs)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForQuestionAnswering

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForQuestionAnswering.from_pretrained(<span class="hljs-string">&quot;my_awesome_qa_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">with</span> torch.no_grad():
<span class="hljs-meta">... </span>    outputs = model(**inputs)`}}),I=new B({props:{code:`answer_start_index = outputs.start_logits.argmax()
answer_end_index = outputs.end_logits.argmax()`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>answer_start_index = outputs.start_logits.argmax()
<span class="hljs-meta">&gt;&gt;&gt; </span>answer_end_index = outputs.end_logits.argmax()`}}),O=new B({props:{code:`predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
tokenizer.decode(predict_answer_tokens)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>predict_answer_tokens = inputs.input_ids[<span class="hljs-number">0</span>, answer_start_index : answer_end_index + <span class="hljs-number">1</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.decode(predict_answer_tokens)
<span class="hljs-string">&#x27;176 billion parameters and can generate text in 46 languages natural languages and 13&#x27;</span>`}}),{c(){r=i("p"),g=n("Tokenize the text and return PyTorch tensors:"),a=c(),y(_.$$.fragment),w=c(),j=i("p"),z=n("Pass your inputs to the model and return the "),D=i("code"),P=n("logits"),C=n(":"),F=c(),y(M.$$.fragment),Q=c(),N=i("p"),J=n("Get the highest probability from the model output for the start and end positions:"),Z=c(),y(I.$$.fragment),H=c(),G=i("p"),se=n("Decode the predicted tokens to get the answer:"),R=c(),y(O.$$.fragment)},l(l){r=p(l,"P",{});var k=u(r);g=o(k,"Tokenize the text and return PyTorch tensors:"),k.forEach(t),a=d(l),v(_.$$.fragment,l),w=d(l),j=p(l,"P",{});var W=u(j);z=o(W,"Pass your inputs to the model and return the "),D=p(W,"CODE",{});var K=u(D);P=o(K,"logits"),K.forEach(t),C=o(W,":"),W.forEach(t),F=d(l),v(M.$$.fragment,l),Q=d(l),N=p(l,"P",{});var X=u(N);J=o(X,"Get the highest probability from the model output for the start and end positions:"),X.forEach(t),Z=d(l),v(I.$$.fragment,l),H=d(l),G=p(l,"P",{});var U=u(G);se=o(U,"Decode the predicted tokens to get the answer:"),U.forEach(t),R=d(l),v(O.$$.fragment,l)},m(l,k){f(l,r,k),s(r,g),f(l,a,k),x(_,l,k),f(l,w,k),f(l,j,k),s(j,z),s(j,D),s(D,P),s(j,C),f(l,F,k),x(M,l,k),f(l,Q,k),f(l,N,k),s(N,J),f(l,Z,k),x(I,l,k),f(l,H,k),f(l,G,k),s(G,se),f(l,R,k),x(O,l,k),Y=!0},p:ca,i(l){Y||(q(_.$$.fragment,l),q(M.$$.fragment,l),q(I.$$.fragment,l),q(O.$$.fragment,l),Y=!0)},o(l){E(_.$$.fragment,l),E(M.$$.fragment,l),E(I.$$.fragment,l),E(O.$$.fragment,l),Y=!1},d(l){l&&t(r),l&&t(a),T(_,l),l&&t(w),l&&t(j),l&&t(F),T(M,l),l&&t(Q),l&&t(N),l&&t(Z),T(I,l),l&&t(H),l&&t(G),l&&t(R),T(O,l)}}}function gr(S){let r,g;return r=new Yt({props:{$$slots:{default:[_r]},$$scope:{ctx:S}}}),{c(){y(r.$$.fragment)},l(a){v(r.$$.fragment,a)},m(a,_){x(r,a,_),g=!0},p(a,_){const w={};_&2&&(w.$$scope={dirty:_,ctx:a}),r.$set(w)},i(a){g||(q(r.$$.fragment,a),g=!0)},o(a){E(r.$$.fragment,a),g=!1},d(a){T(r,a)}}}function $r(S){let r,g,a,_,w,j,z,D,P,C,F,M,Q,N,J,Z,I,H,G,se,R,O,Y;return _=new B({props:{code:`from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model")
inputs = tokenizer(question, text, 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> AutoTokenizer

<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;my_awesome_qa_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(question, text, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`}}),M=new B({props:{code:`from transformers import TFAutoModelForQuestionAnswering

model = TFAutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model")
outputs = model(**inputs)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForQuestionAnswering

<span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForQuestionAnswering.from_pretrained(<span class="hljs-string">&quot;my_awesome_qa_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model(**inputs)`}}),I=new B({props:{code:`answer_start_index = int(tf.math.argmax(outputs.start_logits, axis=-1)[0])
answer_end_index = int(tf.math.argmax(outputs.end_logits, axis=-1)[0])`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>answer_start_index = <span class="hljs-built_in">int</span>(tf.math.argmax(outputs.start_logits, axis=-<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>])
<span class="hljs-meta">&gt;&gt;&gt; </span>answer_end_index = <span class="hljs-built_in">int</span>(tf.math.argmax(outputs.end_logits, axis=-<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>])`}}),O=new B({props:{code:`predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
tokenizer.decode(predict_answer_tokens)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>predict_answer_tokens = inputs.input_ids[<span class="hljs-number">0</span>, answer_start_index : answer_end_index + <span class="hljs-number">1</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.decode(predict_answer_tokens)
<span class="hljs-string">&#x27;176 billion parameters and can generate text in 46 languages natural languages and 13&#x27;</span>`}}),{c(){r=i("p"),g=n("Tokenize the text and return TensorFlow tensors:"),a=c(),y(_.$$.fragment),w=c(),j=i("p"),z=n("Pass your inputs to the model and return the "),D=i("code"),P=n("logits"),C=n(":"),F=c(),y(M.$$.fragment),Q=c(),N=i("p"),J=n("Get the highest probability from the model output for the start and end positions:"),Z=c(),y(I.$$.fragment),H=c(),G=i("p"),se=n("Decode the predicted tokens to get the answer:"),R=c(),y(O.$$.fragment)},l(l){r=p(l,"P",{});var k=u(r);g=o(k,"Tokenize the text and return TensorFlow tensors:"),k.forEach(t),a=d(l),v(_.$$.fragment,l),w=d(l),j=p(l,"P",{});var W=u(j);z=o(W,"Pass your inputs to the model and return the "),D=p(W,"CODE",{});var K=u(D);P=o(K,"logits"),K.forEach(t),C=o(W,":"),W.forEach(t),F=d(l),v(M.$$.fragment,l),Q=d(l),N=p(l,"P",{});var X=u(N);J=o(X,"Get the highest probability from the model output for the start and end positions:"),X.forEach(t),Z=d(l),v(I.$$.fragment,l),H=d(l),G=p(l,"P",{});var U=u(G);se=o(U,"Decode the predicted tokens to get the answer:"),U.forEach(t),R=d(l),v(O.$$.fragment,l)},m(l,k){f(l,r,k),s(r,g),f(l,a,k),x(_,l,k),f(l,w,k),f(l,j,k),s(j,z),s(j,D),s(D,P),s(j,C),f(l,F,k),x(M,l,k),f(l,Q,k),f(l,N,k),s(N,J),f(l,Z,k),x(I,l,k),f(l,H,k),f(l,G,k),s(G,se),f(l,R,k),x(O,l,k),Y=!0},p:ca,i(l){Y||(q(_.$$.fragment,l),q(M.$$.fragment,l),q(I.$$.fragment,l),q(O.$$.fragment,l),Y=!0)},o(l){E(_.$$.fragment,l),E(M.$$.fragment,l),E(I.$$.fragment,l),E(O.$$.fragment,l),Y=!1},d(l){l&&t(r),l&&t(a),T(_,l),l&&t(w),l&&t(j),l&&t(F),T(M,l),l&&t(Q),l&&t(N),l&&t(Z),T(I,l),l&&t(H),l&&t(G),l&&t(R),T(O,l)}}}function wr(S){let r,g;return r=new Yt({props:{$$slots:{default:[$r]},$$scope:{ctx:S}}}),{c(){y(r.$$.fragment)},l(a){v(r.$$.fragment,a)},m(a,_){x(r,a,_),g=!0},p(a,_){const w={};_&2&&(w.$$scope={dirty:_,ctx:a}),r.$set(w)},i(a){g||(q(r.$$.fragment,a),g=!0)},o(a){E(r.$$.fragment,a),g=!1},d(a){T(r,a)}}}function br(S){let r,g,a,_,w,j,z,D,P,C,F,M,Q,N,J,Z,I,H,G,se,R,O,Y,l,k,W,K,X,U,be,ae,ge,ee,le,ne,ve,ue,fe,pe,me,V,$e,$,L,oe,te,je,re,xe,ie,he,ce,m,A,de,_e,yt,He,Le,at,Fe,Be,nt,ke,ot,we,vt,Wt,da,_a,rt,ga,$a,xs,lt,qs,xt,wa,Es,it,Ts,qt,ba,As,Te,Et,Vt,ja,ka,ya,Tt,Jt,va,xa,qa,At,Kt,Ea,Ta,Ds,Me,Ne,Xt,pt,Aa,Zt,Da,zs,ut,Ps,Ae,za,es,Pa,Ca,ts,Fa,Ma,Cs,ft,Fs,Dt,Oa,Ms,De,qe,Qa,ss,Sa,Ia,as,Ha,La,ns,Ba,Na,Ga,Oe,Ra,os,Ua,Ya,rs,Wa,Va,Ja,Ee,Ka,mt,Xa,Za,ls,en,tn,is,sn,an,Os,ze,nn,ps,on,rn,us,ln,pn,Qs,ht,Ss,ye,un,ct,fn,mn,fs,hn,cn,ms,dn,_n,Is,dt,Hs,Pe,gn,zt,$n,wn,Pt,bn,jn,Ls,Ge,Bs,Qe,Re,hs,_t,kn,cs,yn,Ns,Ue,Gs,Ye,Rs,Se,We,ds,gt,vn,_s,xn,Us,Ve,qn,Ct,En,Tn,Ys,Je,An,$t,Dn,zn,Ws,Ie,Ke,gs,wt,Pn,$s,Cn,Vs,Ft,Fn,Js,Mt,Mn,Ks,bt,Xs,Ce,On,Ot,Qn,Sn,ws,In,Hn,Zs,jt,ea,Xe,Ln,bs,Bn,Nn,ta,Ze,sa;return j=new Ut({}),F=new ar({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/question_answering.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/question_answering.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/question_answering.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/question_answering.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/question_answering.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/question_answering.ipynb"}]}}),Q=new Ko({props:{id:"ajPx5LwJD-I"}}),V=new ha({props:{$$slots:{default:[nr]},$$scope:{ctx:S}}}),te=new B({props:{code:"pip install transformers datasets evaluate",highlighted:"pip install transformers datasets evaluate"}}),he=new B({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()`}}),_e=new Ut({}),ke=new B({props:{code:`from datasets import load_dataset

squad = load_dataset("squad", split="train[:5000]")`,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>squad = load_dataset(<span class="hljs-string">&quot;squad&quot;</span>, split=<span class="hljs-string">&quot;train[:5000]&quot;</span>)`}}),lt=new B({props:{code:"squad = squad.train_test_split(test_size=0.2)",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>squad = squad.train_test_split(test_size=<span class="hljs-number">0.2</span>)'}}),it=new B({props:{code:'squad["train"][0]',highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>squad[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>]
{<span class="hljs-string">&#x27;answers&#x27;</span>: {<span class="hljs-string">&#x27;answer_start&#x27;</span>: [<span class="hljs-number">515</span>], <span class="hljs-string">&#x27;text&#x27;</span>: [<span class="hljs-string">&#x27;Saint Bernadette Soubirous&#x27;</span>]},
 <span class="hljs-string">&#x27;context&#x27;</span>: <span class="hljs-string">&#x27;Architecturally, the school has a Catholic character. Atop the Main Building\\&#x27;s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend &quot;Venite Ad Me Omnes&quot;. Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.&#x27;</span>,
 <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-string">&#x27;5733be284776f41900661182&#x27;</span>,
 <span class="hljs-string">&#x27;question&#x27;</span>: <span class="hljs-string">&#x27;To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?&#x27;</span>,
 <span class="hljs-string">&#x27;title&#x27;</span>: <span class="hljs-string">&#x27;University_of_Notre_Dame&#x27;</span>
}`}}),pt=new Ut({}),ut=new Ko({props:{id:"qgaM0weJHpA"}}),ft=new B({props:{code:`from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")`,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;distilbert-base-uncased&quot;</span>)`}}),ht=new B({props:{code:`def preprocess_function(examples):
    questions = [q.strip() for q in examples["question"]]
    inputs = tokenizer(
        questions,
        examples["context"],
        max_length=384,
        truncation="only_second",
        return_offsets_mapping=True,
        padding="max_length",
    )

    offset_mapping = inputs.pop("offset_mapping")
    answers = examples["answers"]
    start_positions = []
    end_positions = []

    for i, offset in enumerate(offset_mapping):
        answer = answers[i]
        start_char = answer["answer_start"][0]
        end_char = answer["answer_start"][0] + len(answer["text"][0])
        sequence_ids = inputs.sequence_ids(i)

        # Find the start and end of the context
        idx = 0
        while sequence_ids[idx] != 1:
            idx += 1
        context_start = idx
        while sequence_ids[idx] == 1:
            idx += 1
        context_end = idx - 1

        # If the answer is not fully inside the context, label it (0, 0)
        if offset[context_start][0] > end_char or offset[context_end][1] < start_char:
            start_positions.append(0)
            end_positions.append(0)
        else:
            # Otherwise it's the start and end token positions
            idx = context_start
            while idx <= context_end and offset[idx][0] <= start_char:
                idx += 1
            start_positions.append(idx - 1)

            idx = context_end
            while idx >= context_start and offset[idx][1] >= end_char:
                idx -= 1
            end_positions.append(idx + 1)

    inputs["start_positions"] = start_positions
    inputs["end_positions"] = end_positions
    return inputs`,highlighted:`<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>    questions = [q.strip() <span class="hljs-keyword">for</span> q <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;question&quot;</span>]]
<span class="hljs-meta">... </span>    inputs = tokenizer(
<span class="hljs-meta">... </span>        questions,
<span class="hljs-meta">... </span>        examples[<span class="hljs-string">&quot;context&quot;</span>],
<span class="hljs-meta">... </span>        max_length=<span class="hljs-number">384</span>,
<span class="hljs-meta">... </span>        truncation=<span class="hljs-string">&quot;only_second&quot;</span>,
<span class="hljs-meta">... </span>        return_offsets_mapping=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>        padding=<span class="hljs-string">&quot;max_length&quot;</span>,
<span class="hljs-meta">... </span>    )

<span class="hljs-meta">... </span>    offset_mapping = inputs.pop(<span class="hljs-string">&quot;offset_mapping&quot;</span>)
<span class="hljs-meta">... </span>    answers = examples[<span class="hljs-string">&quot;answers&quot;</span>]
<span class="hljs-meta">... </span>    start_positions = []
<span class="hljs-meta">... </span>    end_positions = []

<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> i, offset <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(offset_mapping):
<span class="hljs-meta">... </span>        answer = answers[i]
<span class="hljs-meta">... </span>        start_char = answer[<span class="hljs-string">&quot;answer_start&quot;</span>][<span class="hljs-number">0</span>]
<span class="hljs-meta">... </span>        end_char = answer[<span class="hljs-string">&quot;answer_start&quot;</span>][<span class="hljs-number">0</span>] + <span class="hljs-built_in">len</span>(answer[<span class="hljs-string">&quot;text&quot;</span>][<span class="hljs-number">0</span>])
<span class="hljs-meta">... </span>        sequence_ids = inputs.sequence_ids(i)

<span class="hljs-meta">... </span>        <span class="hljs-comment"># Find the start and end of the context</span>
<span class="hljs-meta">... </span>        idx = <span class="hljs-number">0</span>
<span class="hljs-meta">... </span>        <span class="hljs-keyword">while</span> sequence_ids[idx] != <span class="hljs-number">1</span>:
<span class="hljs-meta">... </span>            idx += <span class="hljs-number">1</span>
<span class="hljs-meta">... </span>        context_start = idx
<span class="hljs-meta">... </span>        <span class="hljs-keyword">while</span> sequence_ids[idx] == <span class="hljs-number">1</span>:
<span class="hljs-meta">... </span>            idx += <span class="hljs-number">1</span>
<span class="hljs-meta">... </span>        context_end = idx - <span class="hljs-number">1</span>

<span class="hljs-meta">... </span>        <span class="hljs-comment"># If the answer is not fully inside the context, label it (0, 0)</span>
<span class="hljs-meta">... </span>        <span class="hljs-keyword">if</span> offset[context_start][<span class="hljs-number">0</span>] &gt; end_char <span class="hljs-keyword">or</span> offset[context_end][<span class="hljs-number">1</span>] &lt; start_char:
<span class="hljs-meta">... </span>            start_positions.append(<span class="hljs-number">0</span>)
<span class="hljs-meta">... </span>            end_positions.append(<span class="hljs-number">0</span>)
<span class="hljs-meta">... </span>        <span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>            <span class="hljs-comment"># Otherwise it&#x27;s the start and end token positions</span>
<span class="hljs-meta">... </span>            idx = context_start
<span class="hljs-meta">... </span>            <span class="hljs-keyword">while</span> idx &lt;= context_end <span class="hljs-keyword">and</span> offset[idx][<span class="hljs-number">0</span>] &lt;= start_char:
<span class="hljs-meta">... </span>                idx += <span class="hljs-number">1</span>
<span class="hljs-meta">... </span>            start_positions.append(idx - <span class="hljs-number">1</span>)

<span class="hljs-meta">... </span>            idx = context_end
<span class="hljs-meta">... </span>            <span class="hljs-keyword">while</span> idx &gt;= context_start <span class="hljs-keyword">and</span> offset[idx][<span class="hljs-number">1</span>] &gt;= end_char:
<span class="hljs-meta">... </span>                idx -= <span class="hljs-number">1</span>
<span class="hljs-meta">... </span>            end_positions.append(idx + <span class="hljs-number">1</span>)

<span class="hljs-meta">... </span>    inputs[<span class="hljs-string">&quot;start_positions&quot;</span>] = start_positions
<span class="hljs-meta">... </span>    inputs[<span class="hljs-string">&quot;end_positions&quot;</span>] = end_positions
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> inputs`}}),dt=new B({props:{code:'tokenized_squad = squad.map(preprocess_function, batched=True, remove_columns=squad["train"].column_names)',highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_squad = squad.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>, remove_columns=squad[<span class="hljs-string">&quot;train&quot;</span>].column_names)'}}),Ge=new Yn({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[ir],pytorch:[rr]},$$scope:{ctx:S}}}),_t=new Ut({}),Ue=new Yn({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[cr],pytorch:[fr]},$$scope:{ctx:S}}}),Ye=new ha({props:{$$slots:{default:[dr]},$$scope:{ctx:S}}}),gt=new Ut({}),wt=new Ut({}),bt=new B({props:{code:`question = "How many programming languages does BLOOM support?"
context = "BLOOM has 176 billion parameters and can generate text in 46 languages natural languages and 13 programming languages."`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>question = <span class="hljs-string">&quot;How many programming languages does BLOOM support?&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>context = <span class="hljs-string">&quot;BLOOM has 176 billion parameters and can generate text in 46 languages natural languages and 13 programming languages.&quot;</span>`}}),jt=new B({props:{code:`from transformers import pipeline

question_answerer = pipeline("question-answering", model="my_awesome_qa_model")
question_answerer(question=question, context=context)`,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>question_answerer = pipeline(<span class="hljs-string">&quot;question-answering&quot;</span>, model=<span class="hljs-string">&quot;my_awesome_qa_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>question_answerer(question=question, context=context)
{<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.2058267742395401</span>,
 <span class="hljs-string">&#x27;start&#x27;</span>: <span class="hljs-number">10</span>,
 <span class="hljs-string">&#x27;end&#x27;</span>: <span class="hljs-number">95</span>,
 <span class="hljs-string">&#x27;answer&#x27;</span>: <span class="hljs-string">&#x27;176 billion parameters and can generate text in 46 languages natural languages and 13&#x27;</span>}`}}),Ze=new Yn({props:{pytorch:!0,tensorflow:!0,jax:!1,$$slots:{tensorflow:[wr],pytorch:[gr]},$$scope:{ctx:S}}}),{c(){r=i("meta"),g=c(),a=i("h1"),_=i("a"),w=i("span"),y(j.$$.fragment),z=c(),D=i("span"),P=n("Question answering"),C=c(),y(F.$$.fragment),M=c(),y(Q.$$.fragment),N=c(),J=i("p"),Z=n("Question answering tasks return an answer given a question. If you\u2019ve ever asked a virtual assistant like Alexa, Siri or Google what the weather is, then you\u2019ve used a question answering model before. There are two common types of question answering tasks:"),I=c(),H=i("ul"),G=i("li"),se=n("Extractive: extract the answer from the given context."),R=c(),O=i("li"),Y=n("Abstractive: generate an answer from the context that correctly answers the question."),l=c(),k=i("p"),W=n("This guide will show you how to:"),K=c(),X=i("ol"),U=i("li"),be=n("Finetune "),ae=i("a"),ge=n("DistilBERT"),ee=n(" on the "),le=i("a"),ne=n("SQuAD"),ve=n(" dataset for extractive question answering."),ue=c(),fe=i("li"),pe=n("Use your finetuned model for inference."),me=c(),y(V.$$.fragment),$e=c(),$=i("p"),L=n("Before you begin, make sure you have all the necessary libraries installed:"),oe=c(),y(te.$$.fragment),je=c(),re=i("p"),xe=n("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:"),ie=c(),y(he.$$.fragment),ce=c(),m=i("h2"),A=i("a"),de=i("span"),y(_e.$$.fragment),yt=c(),He=i("span"),Le=n("Load SQuAD dataset"),at=c(),Fe=i("p"),Be=n("Start by loading a smaller subset of the SQuAD dataset from the \u{1F917} Datasets library. This\u2019ll give you a chance to experiment and make sure everythings works before spending more time training on the full dataset."),nt=c(),y(ke.$$.fragment),ot=c(),we=i("p"),vt=n("Split the dataset\u2019s "),Wt=i("code"),da=n("train"),_a=n(" split into a train and test set with the "),rt=i("a"),ga=n("train_test_split"),$a=n(" method:"),xs=c(),y(lt.$$.fragment),qs=c(),xt=i("p"),wa=n("Then take a look at an example:"),Es=c(),y(it.$$.fragment),Ts=c(),qt=i("p"),ba=n("There are several important fields here:"),As=c(),Te=i("ul"),Et=i("li"),Vt=i("code"),ja=n("answers"),ka=n(": the starting location of the answer token and the answer text."),ya=c(),Tt=i("li"),Jt=i("code"),va=n("context"),xa=n(": background information from which the model needs to extract the answer."),qa=c(),At=i("li"),Kt=i("code"),Ea=n("question"),Ta=n(": the question a model should answer."),Ds=c(),Me=i("h2"),Ne=i("a"),Xt=i("span"),y(pt.$$.fragment),Aa=c(),Zt=i("span"),Da=n("Preprocess"),zs=c(),y(ut.$$.fragment),Ps=c(),Ae=i("p"),za=n("The next step is to load a DistilBERT tokenizer to process the "),es=i("code"),Pa=n("question"),Ca=n(" and "),ts=i("code"),Fa=n("context"),Ma=n(" fields:"),Cs=c(),y(ft.$$.fragment),Fs=c(),Dt=i("p"),Oa=n("There are a few preprocessing steps particular to question answering tasks you should be aware of:"),Ms=c(),De=i("ol"),qe=i("li"),Qa=n("Some examples in a dataset may have a very long "),ss=i("code"),Sa=n("context"),Ia=n(" that exceeds the maximum input length of the model. To deal with longer sequences, truncate only the "),as=i("code"),Ha=n("context"),La=n(" by setting "),ns=i("code"),Ba=n('truncation="only_second"'),Na=n("."),Ga=c(),Oe=i("li"),Ra=n("Next, map the start and end positions of the answer to the original "),os=i("code"),Ua=n("context"),Ya=n(` by setting
`),rs=i("code"),Wa=n("return_offset_mapping=True"),Va=n("."),Ja=c(),Ee=i("li"),Ka=n("With the mapping in hand, now you can find the start and end tokens of the answer. Use the "),mt=i("a"),Xa=n("sequence_ids"),Za=n(` method to
find which part of the offset corresponds to the `),ls=i("code"),en=n("question"),tn=n(" and which corresponds to the "),is=i("code"),sn=n("context"),an=n("."),Os=c(),ze=i("p"),nn=n("Here is how you can create a function to truncate and map the start and end tokens of the "),ps=i("code"),on=n("answer"),rn=n(" to the "),us=i("code"),ln=n("context"),pn=n(":"),Qs=c(),y(ht.$$.fragment),Ss=c(),ye=i("p"),un=n("To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),ct=i("a"),fn=n("map"),mn=n(" function. You can speed up the "),fs=i("code"),hn=n("map"),cn=n(" function by setting "),ms=i("code"),dn=n("batched=True"),_n=n(" to process multiple elements of the dataset at once. Remove any columns you don\u2019t need:"),Is=c(),y(dt.$$.fragment),Hs=c(),Pe=i("p"),gn=n("Now create a batch of examples using "),zt=i("a"),$n=n("DefaultDataCollator"),wn=n(". Unlike other data collators in \u{1F917} Transformers, the "),Pt=i("a"),bn=n("DefaultDataCollator"),jn=n(" does not apply any additional preprocessing such as padding."),Ls=c(),y(Ge.$$.fragment),Bs=c(),Qe=i("h2"),Re=i("a"),hs=i("span"),y(_t.$$.fragment),kn=c(),cs=i("span"),yn=n("Train"),Ns=c(),y(Ue.$$.fragment),Gs=c(),y(Ye.$$.fragment),Rs=c(),Se=i("h2"),We=i("a"),ds=i("span"),y(gt.$$.fragment),vn=c(),_s=i("span"),xn=n("Evaluate"),Us=c(),Ve=i("p"),qn=n("Evaluation for question answering requires a significant amount of postprocessing. To avoid taking up too much of your time, this guide skips the evaluation step. The "),Ct=i("a"),En=n("Trainer"),Tn=n(" still calculates the evaluation loss during training so you\u2019re not completely in the dark about your model\u2019s performance."),Ys=c(),Je=i("p"),An=n("If have more time and you\u2019re interested in how to evaluate your model for question answering, take a look at the "),$t=i("a"),Dn=n("Question answering"),zn=n(" chapter from the \u{1F917} Hugging Face Course!"),Ws=c(),Ie=i("h2"),Ke=i("a"),gs=i("span"),y(wt.$$.fragment),Pn=c(),$s=i("span"),Cn=n("Inference"),Vs=c(),Ft=i("p"),Fn=n("Great, now that you\u2019ve finetuned a model, you can use it for inference!"),Js=c(),Mt=i("p"),Mn=n("Come up with a question and some context you\u2019d like the model to predict:"),Ks=c(),y(bt.$$.fragment),Xs=c(),Ce=i("p"),On=n("The simplest way to try out your finetuned model for inference is to use it in a "),Ot=i("a"),Qn=n("pipeline()"),Sn=n(". Instantiate a "),ws=i("code"),In=n("pipeline"),Hn=n(" for question answering with your model, and pass your text to it:"),Zs=c(),y(jt.$$.fragment),ea=c(),Xe=i("p"),Ln=n("You can also manually replicate the results of the "),bs=i("code"),Bn=n("pipeline"),Nn=n(" if you\u2019d like:"),ta=c(),y(Ze.$$.fragment),this.h()},l(e){const h=tr('[data-svelte="svelte-1phssyn"]',document.head);r=p(h,"META",{name:!0,content:!0}),h.forEach(t),g=d(e),a=p(e,"H1",{class:!0});var kt=u(a);_=p(kt,"A",{id:!0,class:!0,href:!0});var js=u(_);w=p(js,"SPAN",{});var ks=u(w);v(j.$$.fragment,ks),ks.forEach(t),js.forEach(t),z=d(kt),D=p(kt,"SPAN",{});var ys=u(D);P=o(ys,"Question answering"),ys.forEach(t),kt.forEach(t),C=d(e),v(F.$$.fragment,e),M=d(e),v(Q.$$.fragment,e),N=d(e),J=p(e,"P",{});var vs=u(J);Z=o(vs,"Question answering tasks return an answer given a question. If you\u2019ve ever asked a virtual assistant like Alexa, Siri or Google what the weather is, then you\u2019ve used a question answering model before. There are two common types of question answering tasks:"),vs.forEach(t),I=d(e),H=p(e,"UL",{});var aa=u(H);G=p(aa,"LI",{});var Wn=u(G);se=o(Wn,"Extractive: extract the answer from the given context."),Wn.forEach(t),R=d(aa),O=p(aa,"LI",{});var Vn=u(O);Y=o(Vn,"Abstractive: generate an answer from the context that correctly answers the question."),Vn.forEach(t),aa.forEach(t),l=d(e),k=p(e,"P",{});var Jn=u(k);W=o(Jn,"This guide will show you how to:"),Jn.forEach(t),K=d(e),X=p(e,"OL",{});var na=u(X);U=p(na,"LI",{});var Qt=u(U);be=o(Qt,"Finetune "),ae=p(Qt,"A",{href:!0,rel:!0});var Kn=u(ae);ge=o(Kn,"DistilBERT"),Kn.forEach(t),ee=o(Qt," on the "),le=p(Qt,"A",{href:!0,rel:!0});var Xn=u(le);ne=o(Xn,"SQuAD"),Xn.forEach(t),ve=o(Qt," dataset for extractive question answering."),Qt.forEach(t),ue=d(na),fe=p(na,"LI",{});var Zn=u(fe);pe=o(Zn,"Use your finetuned model for inference."),Zn.forEach(t),na.forEach(t),me=d(e),v(V.$$.fragment,e),$e=d(e),$=p(e,"P",{});var eo=u($);L=o(eo,"Before you begin, make sure you have all the necessary libraries installed:"),eo.forEach(t),oe=d(e),v(te.$$.fragment,e),je=d(e),re=p(e,"P",{});var to=u(re);xe=o(to,"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:"),to.forEach(t),ie=d(e),v(he.$$.fragment,e),ce=d(e),m=p(e,"H2",{class:!0});var oa=u(m);A=p(oa,"A",{id:!0,class:!0,href:!0});var so=u(A);de=p(so,"SPAN",{});var ao=u(de);v(_e.$$.fragment,ao),ao.forEach(t),so.forEach(t),yt=d(oa),He=p(oa,"SPAN",{});var no=u(He);Le=o(no,"Load SQuAD dataset"),no.forEach(t),oa.forEach(t),at=d(e),Fe=p(e,"P",{});var oo=u(Fe);Be=o(oo,"Start by loading a smaller subset of the SQuAD dataset from the \u{1F917} Datasets library. This\u2019ll give you a chance to experiment and make sure everythings works before spending more time training on the full dataset."),oo.forEach(t),nt=d(e),v(ke.$$.fragment,e),ot=d(e),we=p(e,"P",{});var St=u(we);vt=o(St,"Split the dataset\u2019s "),Wt=p(St,"CODE",{});var ro=u(Wt);da=o(ro,"train"),ro.forEach(t),_a=o(St," split into a train and test set with the "),rt=p(St,"A",{href:!0,rel:!0});var lo=u(rt);ga=o(lo,"train_test_split"),lo.forEach(t),$a=o(St," method:"),St.forEach(t),xs=d(e),v(lt.$$.fragment,e),qs=d(e),xt=p(e,"P",{});var io=u(xt);wa=o(io,"Then take a look at an example:"),io.forEach(t),Es=d(e),v(it.$$.fragment,e),Ts=d(e),qt=p(e,"P",{});var po=u(qt);ba=o(po,"There are several important fields here:"),po.forEach(t),As=d(e),Te=p(e,"UL",{});var It=u(Te);Et=p(It,"LI",{});var Gn=u(Et);Vt=p(Gn,"CODE",{});var uo=u(Vt);ja=o(uo,"answers"),uo.forEach(t),ka=o(Gn,": the starting location of the answer token and the answer text."),Gn.forEach(t),ya=d(It),Tt=p(It,"LI",{});var Rn=u(Tt);Jt=p(Rn,"CODE",{});var fo=u(Jt);va=o(fo,"context"),fo.forEach(t),xa=o(Rn,": background information from which the model needs to extract the answer."),Rn.forEach(t),qa=d(It),At=p(It,"LI",{});var Un=u(At);Kt=p(Un,"CODE",{});var mo=u(Kt);Ea=o(mo,"question"),mo.forEach(t),Ta=o(Un,": the question a model should answer."),Un.forEach(t),It.forEach(t),Ds=d(e),Me=p(e,"H2",{class:!0});var ra=u(Me);Ne=p(ra,"A",{id:!0,class:!0,href:!0});var ho=u(Ne);Xt=p(ho,"SPAN",{});var co=u(Xt);v(pt.$$.fragment,co),co.forEach(t),ho.forEach(t),Aa=d(ra),Zt=p(ra,"SPAN",{});var _o=u(Zt);Da=o(_o,"Preprocess"),_o.forEach(t),ra.forEach(t),zs=d(e),v(ut.$$.fragment,e),Ps=d(e),Ae=p(e,"P",{});var Ht=u(Ae);za=o(Ht,"The next step is to load a DistilBERT tokenizer to process the "),es=p(Ht,"CODE",{});var go=u(es);Pa=o(go,"question"),go.forEach(t),Ca=o(Ht," and "),ts=p(Ht,"CODE",{});var $o=u(ts);Fa=o($o,"context"),$o.forEach(t),Ma=o(Ht," fields:"),Ht.forEach(t),Cs=d(e),v(ft.$$.fragment,e),Fs=d(e),Dt=p(e,"P",{});var wo=u(Dt);Oa=o(wo,"There are a few preprocessing steps particular to question answering tasks you should be aware of:"),wo.forEach(t),Ms=d(e),De=p(e,"OL",{});var Lt=u(De);qe=p(Lt,"LI",{});var et=u(qe);Qa=o(et,"Some examples in a dataset may have a very long "),ss=p(et,"CODE",{});var bo=u(ss);Sa=o(bo,"context"),bo.forEach(t),Ia=o(et," that exceeds the maximum input length of the model. To deal with longer sequences, truncate only the "),as=p(et,"CODE",{});var jo=u(as);Ha=o(jo,"context"),jo.forEach(t),La=o(et," by setting "),ns=p(et,"CODE",{});var ko=u(ns);Ba=o(ko,'truncation="only_second"'),ko.forEach(t),Na=o(et,"."),et.forEach(t),Ga=d(Lt),Oe=p(Lt,"LI",{});var Bt=u(Oe);Ra=o(Bt,"Next, map the start and end positions of the answer to the original "),os=p(Bt,"CODE",{});var yo=u(os);Ua=o(yo,"context"),yo.forEach(t),Ya=o(Bt,` by setting
`),rs=p(Bt,"CODE",{});var vo=u(rs);Wa=o(vo,"return_offset_mapping=True"),vo.forEach(t),Va=o(Bt,"."),Bt.forEach(t),Ja=d(Lt),Ee=p(Lt,"LI",{});var tt=u(Ee);Ka=o(tt,"With the mapping in hand, now you can find the start and end tokens of the answer. Use the "),mt=p(tt,"A",{href:!0,rel:!0});var xo=u(mt);Xa=o(xo,"sequence_ids"),xo.forEach(t),Za=o(tt,` method to
find which part of the offset corresponds to the `),ls=p(tt,"CODE",{});var qo=u(ls);en=o(qo,"question"),qo.forEach(t),tn=o(tt," and which corresponds to the "),is=p(tt,"CODE",{});var Eo=u(is);sn=o(Eo,"context"),Eo.forEach(t),an=o(tt,"."),tt.forEach(t),Lt.forEach(t),Os=d(e),ze=p(e,"P",{});var Nt=u(ze);nn=o(Nt,"Here is how you can create a function to truncate and map the start and end tokens of the "),ps=p(Nt,"CODE",{});var To=u(ps);on=o(To,"answer"),To.forEach(t),rn=o(Nt," to the "),us=p(Nt,"CODE",{});var Ao=u(us);ln=o(Ao,"context"),Ao.forEach(t),pn=o(Nt,":"),Nt.forEach(t),Qs=d(e),v(ht.$$.fragment,e),Ss=d(e),ye=p(e,"P",{});var st=u(ye);un=o(st,"To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),ct=p(st,"A",{href:!0,rel:!0});var Do=u(ct);fn=o(Do,"map"),Do.forEach(t),mn=o(st," function. You can speed up the "),fs=p(st,"CODE",{});var zo=u(fs);hn=o(zo,"map"),zo.forEach(t),cn=o(st," function by setting "),ms=p(st,"CODE",{});var Po=u(ms);dn=o(Po,"batched=True"),Po.forEach(t),_n=o(st," to process multiple elements of the dataset at once. Remove any columns you don\u2019t need:"),st.forEach(t),Is=d(e),v(dt.$$.fragment,e),Hs=d(e),Pe=p(e,"P",{});var Gt=u(Pe);gn=o(Gt,"Now create a batch of examples using "),zt=p(Gt,"A",{href:!0});var Co=u(zt);$n=o(Co,"DefaultDataCollator"),Co.forEach(t),wn=o(Gt,". Unlike other data collators in \u{1F917} Transformers, the "),Pt=p(Gt,"A",{href:!0});var Fo=u(Pt);bn=o(Fo,"DefaultDataCollator"),Fo.forEach(t),jn=o(Gt," does not apply any additional preprocessing such as padding."),Gt.forEach(t),Ls=d(e),v(Ge.$$.fragment,e),Bs=d(e),Qe=p(e,"H2",{class:!0});var la=u(Qe);Re=p(la,"A",{id:!0,class:!0,href:!0});var Mo=u(Re);hs=p(Mo,"SPAN",{});var Oo=u(hs);v(_t.$$.fragment,Oo),Oo.forEach(t),Mo.forEach(t),kn=d(la),cs=p(la,"SPAN",{});var Qo=u(cs);yn=o(Qo,"Train"),Qo.forEach(t),la.forEach(t),Ns=d(e),v(Ue.$$.fragment,e),Gs=d(e),v(Ye.$$.fragment,e),Rs=d(e),Se=p(e,"H2",{class:!0});var ia=u(Se);We=p(ia,"A",{id:!0,class:!0,href:!0});var So=u(We);ds=p(So,"SPAN",{});var Io=u(ds);v(gt.$$.fragment,Io),Io.forEach(t),So.forEach(t),vn=d(ia),_s=p(ia,"SPAN",{});var Ho=u(_s);xn=o(Ho,"Evaluate"),Ho.forEach(t),ia.forEach(t),Us=d(e),Ve=p(e,"P",{});var pa=u(Ve);qn=o(pa,"Evaluation for question answering requires a significant amount of postprocessing. To avoid taking up too much of your time, this guide skips the evaluation step. The "),Ct=p(pa,"A",{href:!0});var Lo=u(Ct);En=o(Lo,"Trainer"),Lo.forEach(t),Tn=o(pa," still calculates the evaluation loss during training so you\u2019re not completely in the dark about your model\u2019s performance."),pa.forEach(t),Ys=d(e),Je=p(e,"P",{});var ua=u(Je);An=o(ua,"If have more time and you\u2019re interested in how to evaluate your model for question answering, take a look at the "),$t=p(ua,"A",{href:!0,rel:!0});var Bo=u($t);Dn=o(Bo,"Question answering"),Bo.forEach(t),zn=o(ua," chapter from the \u{1F917} Hugging Face Course!"),ua.forEach(t),Ws=d(e),Ie=p(e,"H2",{class:!0});var fa=u(Ie);Ke=p(fa,"A",{id:!0,class:!0,href:!0});var No=u(Ke);gs=p(No,"SPAN",{});var Go=u(gs);v(wt.$$.fragment,Go),Go.forEach(t),No.forEach(t),Pn=d(fa),$s=p(fa,"SPAN",{});var Ro=u($s);Cn=o(Ro,"Inference"),Ro.forEach(t),fa.forEach(t),Vs=d(e),Ft=p(e,"P",{});var Uo=u(Ft);Fn=o(Uo,"Great, now that you\u2019ve finetuned a model, you can use it for inference!"),Uo.forEach(t),Js=d(e),Mt=p(e,"P",{});var Yo=u(Mt);Mn=o(Yo,"Come up with a question and some context you\u2019d like the model to predict:"),Yo.forEach(t),Ks=d(e),v(bt.$$.fragment,e),Xs=d(e),Ce=p(e,"P",{});var Rt=u(Ce);On=o(Rt,"The simplest way to try out your finetuned model for inference is to use it in a "),Ot=p(Rt,"A",{href:!0});var Wo=u(Ot);Qn=o(Wo,"pipeline()"),Wo.forEach(t),Sn=o(Rt,". Instantiate a "),ws=p(Rt,"CODE",{});var Vo=u(ws);In=o(Vo,"pipeline"),Vo.forEach(t),Hn=o(Rt," for question answering with your model, and pass your text to it:"),Rt.forEach(t),Zs=d(e),v(jt.$$.fragment,e),ea=d(e),Xe=p(e,"P",{});var ma=u(Xe);Ln=o(ma,"You can also manually replicate the results of the "),bs=p(ma,"CODE",{});var Jo=u(bs);Bn=o(Jo,"pipeline"),Jo.forEach(t),Nn=o(ma," if you\u2019d like:"),ma.forEach(t),ta=d(e),v(Ze.$$.fragment,e),this.h()},h(){b(r,"name","hf:doc:metadata"),b(r,"content",JSON.stringify(jr)),b(_,"id","question-answering"),b(_,"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"),b(_,"href","#question-answering"),b(a,"class","relative group"),b(ae,"href","https://huggingface.co/distilbert-base-uncased"),b(ae,"rel","nofollow"),b(le,"href","https://huggingface.co/datasets/squad"),b(le,"rel","nofollow"),b(A,"id","load-squad-dataset"),b(A,"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"),b(A,"href","#load-squad-dataset"),b(m,"class","relative group"),b(rt,"href","https://huggingface.co/docs/datasets/v2.7.1/en/package_reference/main_classes#datasets.Dataset.train_test_split"),b(rt,"rel","nofollow"),b(Ne,"id","preprocess"),b(Ne,"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"),b(Ne,"href","#preprocess"),b(Me,"class","relative group"),b(mt,"href","https://huggingface.co/docs/tokenizers/v0.13.2/en/api/encoding#tokenizers.Encoding.sequence_ids"),b(mt,"rel","nofollow"),b(ct,"href","https://huggingface.co/docs/datasets/v2.7.1/en/package_reference/main_classes#datasets.Dataset.map"),b(ct,"rel","nofollow"),b(zt,"href","/docs/transformers/v4.25.1/en/main_classes/data_collator#transformers.DefaultDataCollator"),b(Pt,"href","/docs/transformers/v4.25.1/en/main_classes/data_collator#transformers.DefaultDataCollator"),b(Re,"id","train"),b(Re,"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"),b(Re,"href","#train"),b(Qe,"class","relative group"),b(We,"id","evaluate"),b(We,"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"),b(We,"href","#evaluate"),b(Se,"class","relative group"),b(Ct,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer"),b($t,"href","https://huggingface.co/course/chapter7/7?fw=pt#postprocessing"),b($t,"rel","nofollow"),b(Ke,"id","inference"),b(Ke,"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"),b(Ke,"href","#inference"),b(Ie,"class","relative group"),b(Ot,"href","/docs/transformers/v4.25.1/en/main_classes/pipelines#transformers.pipeline")},m(e,h){s(document.head,r),f(e,g,h),f(e,a,h),s(a,_),s(_,w),x(j,w,null),s(a,z),s(a,D),s(D,P),f(e,C,h),x(F,e,h),f(e,M,h),x(Q,e,h),f(e,N,h),f(e,J,h),s(J,Z),f(e,I,h),f(e,H,h),s(H,G),s(G,se),s(H,R),s(H,O),s(O,Y),f(e,l,h),f(e,k,h),s(k,W),f(e,K,h),f(e,X,h),s(X,U),s(U,be),s(U,ae),s(ae,ge),s(U,ee),s(U,le),s(le,ne),s(U,ve),s(X,ue),s(X,fe),s(fe,pe),f(e,me,h),x(V,e,h),f(e,$e,h),f(e,$,h),s($,L),f(e,oe,h),x(te,e,h),f(e,je,h),f(e,re,h),s(re,xe),f(e,ie,h),x(he,e,h),f(e,ce,h),f(e,m,h),s(m,A),s(A,de),x(_e,de,null),s(m,yt),s(m,He),s(He,Le),f(e,at,h),f(e,Fe,h),s(Fe,Be),f(e,nt,h),x(ke,e,h),f(e,ot,h),f(e,we,h),s(we,vt),s(we,Wt),s(Wt,da),s(we,_a),s(we,rt),s(rt,ga),s(we,$a),f(e,xs,h),x(lt,e,h),f(e,qs,h),f(e,xt,h),s(xt,wa),f(e,Es,h),x(it,e,h),f(e,Ts,h),f(e,qt,h),s(qt,ba),f(e,As,h),f(e,Te,h),s(Te,Et),s(Et,Vt),s(Vt,ja),s(Et,ka),s(Te,ya),s(Te,Tt),s(Tt,Jt),s(Jt,va),s(Tt,xa),s(Te,qa),s(Te,At),s(At,Kt),s(Kt,Ea),s(At,Ta),f(e,Ds,h),f(e,Me,h),s(Me,Ne),s(Ne,Xt),x(pt,Xt,null),s(Me,Aa),s(Me,Zt),s(Zt,Da),f(e,zs,h),x(ut,e,h),f(e,Ps,h),f(e,Ae,h),s(Ae,za),s(Ae,es),s(es,Pa),s(Ae,Ca),s(Ae,ts),s(ts,Fa),s(Ae,Ma),f(e,Cs,h),x(ft,e,h),f(e,Fs,h),f(e,Dt,h),s(Dt,Oa),f(e,Ms,h),f(e,De,h),s(De,qe),s(qe,Qa),s(qe,ss),s(ss,Sa),s(qe,Ia),s(qe,as),s(as,Ha),s(qe,La),s(qe,ns),s(ns,Ba),s(qe,Na),s(De,Ga),s(De,Oe),s(Oe,Ra),s(Oe,os),s(os,Ua),s(Oe,Ya),s(Oe,rs),s(rs,Wa),s(Oe,Va),s(De,Ja),s(De,Ee),s(Ee,Ka),s(Ee,mt),s(mt,Xa),s(Ee,Za),s(Ee,ls),s(ls,en),s(Ee,tn),s(Ee,is),s(is,sn),s(Ee,an),f(e,Os,h),f(e,ze,h),s(ze,nn),s(ze,ps),s(ps,on),s(ze,rn),s(ze,us),s(us,ln),s(ze,pn),f(e,Qs,h),x(ht,e,h),f(e,Ss,h),f(e,ye,h),s(ye,un),s(ye,ct),s(ct,fn),s(ye,mn),s(ye,fs),s(fs,hn),s(ye,cn),s(ye,ms),s(ms,dn),s(ye,_n),f(e,Is,h),x(dt,e,h),f(e,Hs,h),f(e,Pe,h),s(Pe,gn),s(Pe,zt),s(zt,$n),s(Pe,wn),s(Pe,Pt),s(Pt,bn),s(Pe,jn),f(e,Ls,h),x(Ge,e,h),f(e,Bs,h),f(e,Qe,h),s(Qe,Re),s(Re,hs),x(_t,hs,null),s(Qe,kn),s(Qe,cs),s(cs,yn),f(e,Ns,h),x(Ue,e,h),f(e,Gs,h),x(Ye,e,h),f(e,Rs,h),f(e,Se,h),s(Se,We),s(We,ds),x(gt,ds,null),s(Se,vn),s(Se,_s),s(_s,xn),f(e,Us,h),f(e,Ve,h),s(Ve,qn),s(Ve,Ct),s(Ct,En),s(Ve,Tn),f(e,Ys,h),f(e,Je,h),s(Je,An),s(Je,$t),s($t,Dn),s(Je,zn),f(e,Ws,h),f(e,Ie,h),s(Ie,Ke),s(Ke,gs),x(wt,gs,null),s(Ie,Pn),s(Ie,$s),s($s,Cn),f(e,Vs,h),f(e,Ft,h),s(Ft,Fn),f(e,Js,h),f(e,Mt,h),s(Mt,Mn),f(e,Ks,h),x(bt,e,h),f(e,Xs,h),f(e,Ce,h),s(Ce,On),s(Ce,Ot),s(Ot,Qn),s(Ce,Sn),s(Ce,ws),s(ws,In),s(Ce,Hn),f(e,Zs,h),x(jt,e,h),f(e,ea,h),f(e,Xe,h),s(Xe,Ln),s(Xe,bs),s(bs,Bn),s(Xe,Nn),f(e,ta,h),x(Ze,e,h),sa=!0},p(e,[h]){const kt={};h&2&&(kt.$$scope={dirty:h,ctx:e}),V.$set(kt);const js={};h&2&&(js.$$scope={dirty:h,ctx:e}),Ge.$set(js);const ks={};h&2&&(ks.$$scope={dirty:h,ctx:e}),Ue.$set(ks);const ys={};h&2&&(ys.$$scope={dirty:h,ctx:e}),Ye.$set(ys);const vs={};h&2&&(vs.$$scope={dirty:h,ctx:e}),Ze.$set(vs)},i(e){sa||(q(j.$$.fragment,e),q(F.$$.fragment,e),q(Q.$$.fragment,e),q(V.$$.fragment,e),q(te.$$.fragment,e),q(he.$$.fragment,e),q(_e.$$.fragment,e),q(ke.$$.fragment,e),q(lt.$$.fragment,e),q(it.$$.fragment,e),q(pt.$$.fragment,e),q(ut.$$.fragment,e),q(ft.$$.fragment,e),q(ht.$$.fragment,e),q(dt.$$.fragment,e),q(Ge.$$.fragment,e),q(_t.$$.fragment,e),q(Ue.$$.fragment,e),q(Ye.$$.fragment,e),q(gt.$$.fragment,e),q(wt.$$.fragment,e),q(bt.$$.fragment,e),q(jt.$$.fragment,e),q(Ze.$$.fragment,e),sa=!0)},o(e){E(j.$$.fragment,e),E(F.$$.fragment,e),E(Q.$$.fragment,e),E(V.$$.fragment,e),E(te.$$.fragment,e),E(he.$$.fragment,e),E(_e.$$.fragment,e),E(ke.$$.fragment,e),E(lt.$$.fragment,e),E(it.$$.fragment,e),E(pt.$$.fragment,e),E(ut.$$.fragment,e),E(ft.$$.fragment,e),E(ht.$$.fragment,e),E(dt.$$.fragment,e),E(Ge.$$.fragment,e),E(_t.$$.fragment,e),E(Ue.$$.fragment,e),E(Ye.$$.fragment,e),E(gt.$$.fragment,e),E(wt.$$.fragment,e),E(bt.$$.fragment,e),E(jt.$$.fragment,e),E(Ze.$$.fragment,e),sa=!1},d(e){t(r),e&&t(g),e&&t(a),T(j),e&&t(C),T(F,e),e&&t(M),T(Q,e),e&&t(N),e&&t(J),e&&t(I),e&&t(H),e&&t(l),e&&t(k),e&&t(K),e&&t(X),e&&t(me),T(V,e),e&&t($e),e&&t($),e&&t(oe),T(te,e),e&&t(je),e&&t(re),e&&t(ie),T(he,e),e&&t(ce),e&&t(m),T(_e),e&&t(at),e&&t(Fe),e&&t(nt),T(ke,e),e&&t(ot),e&&t(we),e&&t(xs),T(lt,e),e&&t(qs),e&&t(xt),e&&t(Es),T(it,e),e&&t(Ts),e&&t(qt),e&&t(As),e&&t(Te),e&&t(Ds),e&&t(Me),T(pt),e&&t(zs),T(ut,e),e&&t(Ps),e&&t(Ae),e&&t(Cs),T(ft,e),e&&t(Fs),e&&t(Dt),e&&t(Ms),e&&t(De),e&&t(Os),e&&t(ze),e&&t(Qs),T(ht,e),e&&t(Ss),e&&t(ye),e&&t(Is),T(dt,e),e&&t(Hs),e&&t(Pe),e&&t(Ls),T(Ge,e),e&&t(Bs),e&&t(Qe),T(_t),e&&t(Ns),T(Ue,e),e&&t(Gs),T(Ye,e),e&&t(Rs),e&&t(Se),T(gt),e&&t(Us),e&&t(Ve),e&&t(Ys),e&&t(Je),e&&t(Ws),e&&t(Ie),T(wt),e&&t(Vs),e&&t(Ft),e&&t(Js),e&&t(Mt),e&&t(Ks),T(bt,e),e&&t(Xs),e&&t(Ce),e&&t(Zs),T(jt,e),e&&t(ea),e&&t(Xe),e&&t(ta),T(Ze,e)}}}const jr={local:"question-answering",sections:[{local:"load-squad-dataset",title:"Load SQuAD dataset"},{local:"preprocess",title:"Preprocess"},{local:"train",title:"Train"},{local:"evaluate",title:"Evaluate"},{local:"inference",title:"Inference"}],title:"Question answering"};function kr(S){return sr(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Dr extends Xo{constructor(r){super();Zo(this,r,kr,br,er,{})}}export{Dr as default,jr as metadata};
