import{s as Ne,o as ze,n as ye}from"../chunks/scheduler.9bc65507.js";import{S as Ve,i as Pe,g as l,s as i,u as k,A as Ue,h as c,f as a,c as d,j as g,v as w,y as N,k as u,o as r,a as p,w as y,d as v,t as $,x as T}from"../chunks/index.78c82d43.js";import{D as He}from"../chunks/Docstring.f3d04bd2.js";import{C as ve}from"../chunks/CodeBlock.48618ec2.js";import{I as ke}from"../chunks/IconCopyLink.bedaa44d.js";import{E as we}from"../chunks/ExampleCodeBlock.872b014d.js";function Be(j){let t,h;return t=new ve({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9tZXRyaWMlMEElMEFyb3VnZV9tZXRyaWMlMjAlM0QlMjBsb2FkX21ldHJpYyglMjJyb3VnZSUyMiklMEElMEElMEFkZWYlMjByb3VnZV9mbihwcmVkaWN0aW9ucyUyQyUyMGxhYmVscyklM0ElMEElMjAlMjAlMjAlMjBkZWNvZGVkX3ByZWRpY3Rpb25zJTIwJTNEJTIwdG9rZW5pemVyLmJhdGNoX2RlY29kZShwcmVkaWN0aW9ucyUyQyUyMHNraXBfc3BlY2lhbF90b2tlbnMlM0RUcnVlKSUwQSUyMCUyMCUyMCUyMGRlY29kZWRfbGFiZWxzJTIwJTNEJTIwdG9rZW5pemVyLmJhdGNoX2RlY29kZShsYWJlbHMlMkMlMjBza2lwX3NwZWNpYWxfdG9rZW5zJTNEVHJ1ZSklMEElMjAlMjAlMjAlMjByZXN1bHQlMjAlM0QlMjByb3VnZV9tZXRyaWMuY29tcHV0ZShwcmVkaWN0aW9ucyUzRGRlY29kZWRfcHJlZGljdGlvbnMlMkMlMjByZWZlcmVuY2VzJTNEZGVjb2RlZF9sYWJlbHMpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTdCa2V5JTNBJTIwdmFsdWUubWlkLmZtZWFzdXJlJTIwKiUyMDEwMCUyMGZvciUyMGtleSUyQyUyMHZhbHVlJTIwaW4lMjByZXN1bHQuaXRlbXMoKSU3RA==",highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_metric

rouge_metric = load_metric(<span class="hljs-string">&quot;rouge&quot;</span>)


<span class="hljs-keyword">def</span> <span class="hljs-title function_">rouge_fn</span>(<span class="hljs-params">predictions, labels</span>):
    decoded_predictions = tokenizer.batch_decode(predictions, skip_special_tokens=<span class="hljs-literal">True</span>)
    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>)
    result = rouge_metric.compute(predictions=decoded_predictions, references=decoded_labels)
    <span class="hljs-keyword">return</span> {key: value.mid.fmeasure * <span class="hljs-number">100</span> <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> result.items()}`}}),{c(){k(t.$$.fragment)},l(o){w(t.$$.fragment,o)},m(o,m){y(t,o,m),h=!0},p:ye,i(o){h||(v(t.$$.fragment,o),h=!0)},o(o){$(t.$$.fragment,o),h=!1},d(o){T(t,o)}}}function Se(j){let t,h="The above function will return a dict containing values which will be logged like any other Keras metric:",o,m,b;return m=new ve({props:{code:"JTdCJ3JvdWdlMSclM0ElMjAzNy40MTk5JTJDJTIwJ3JvdWdlMiclM0ElMjAxMy45NzY4JTJDJTIwJ3JvdWdlTCclM0ElMjAzNC4zNjElMkMlMjAncm91Z2VMc3VtJyUzQSUyMDM1LjA3ODE=",highlighted:'{&#x27;rouge1&#x27;: <span class="hljs-number">37.4199</span>, &#x27;rouge2&#x27;: <span class="hljs-number">13.9768</span>, &#x27;rougeL&#x27;: <span class="hljs-number">34.361</span>, &#x27;rougeLsum&#x27;: <span class="hljs-number">35.0781</span>'}}),{c(){t=l("p"),t.textContent=h,o=i(),k(m.$$.fragment)},l(n){t=c(n,"P",{"data-svelte-h":!0}),N(t)!=="svelte-zfl4ie"&&(t.textContent=h),o=d(n),w(m.$$.fragment,n)},m(n,J){p(n,t,J),p(n,o,J),y(m,n,J),b=!0},p:ye,i(n){b||(v(m.$$.fragment,n),b=!0)},o(n){$(m.$$.fragment,n),b=!1},d(n){n&&(a(t),a(o)),T(m,n)}}}function Ge(j){let t,h;return t=new ve({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycy5rZXJhc19jYWxsYmFja3MlMjBpbXBvcnQlMjBQdXNoVG9IdWJDYWxsYmFjayUwQSUwQXB1c2hfdG9faHViX2NhbGxiYWNrJTIwJTNEJTIwUHVzaFRvSHViQ2FsbGJhY2soJTBBJTIwJTIwJTIwJTIwb3V0cHV0X2RpciUzRCUyMi4lMkZtb2RlbF9zYXZlJTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9rZW5pemVyJTNEdG9rZW5pemVyJTJDJTBBJTIwJTIwJTIwJTIwaHViX21vZGVsX2lkJTNEJTIyZ3B0NS03eGxhcmdlJTIyJTJDJTBBKSUwQSUwQW1vZGVsLmZpdCh0cmFpbl9kYXRhc2V0JTJDJTIwY2FsbGJhY2tzJTNEJTVCcHVzaF90b19odWJfY2FsbGJhY2slNUQp",highlighted:`<span class="hljs-keyword">from</span> transformers.keras_callbacks <span class="hljs-keyword">import</span> PushToHubCallback

push_to_hub_callback = PushToHubCallback(
    output_dir=<span class="hljs-string">&quot;./model_save&quot;</span>,
    tokenizer=tokenizer,
    hub_model_id=<span class="hljs-string">&quot;gpt5-7xlarge&quot;</span>,
)

model.fit(train_dataset, callbacks=[push_to_hub_callback])`}}),{c(){k(t.$$.fragment)},l(o){w(t.$$.fragment,o)},m(o,m){y(t,o,m),h=!0},p:ye,i(o){h||(v(t.$$.fragment,o),h=!0)},o(o){$(t.$$.fragment,o),h=!1},d(o){T(t,o)}}}function Ae(j){let t,h,o,m,b,n,J,z,de,A,$e="Keras callbacks",te,V,Te=`When training a Transformers model with Keras, there are some library-specific callbacks available to automate common
tasks:`,ae,x,I,L,P,pe,X,Me="KerasMetricCallback",oe,f,U,he,F,Je=`Callback to compute metrics at the end of every epoch. Unlike normal Keras metrics, these do not need to be
compilable by TF. It is particularly useful for common NLP metrics like BLEU and ROUGE that require string
operations or generation loops that cannot be compiled. Predictions (or generations) will be computed on the
<code>eval_dataset</code> before being passed to the <code>metric_fn</code> in <code>np.ndarray</code> format. The <code>metric_fn</code> should compute
metrics and return a dict mapping metric names to metric values.`,me,Y,xe=`We provide an example of a suitable metric_fn that computes ROUGE scores for a summarization model below. Note that
this example skips some post-processing for readability and simplicity, and should probably not be used as-is!`,ue,W,fe,Z,se,C,E,D,B,be,K,Ce="PushToHubCallback",ne,_,S,ge,R,je=`Callback that will save and push the model to the Hub regularly. By default, it pushes once per epoch, but this can
be changed with the <code>save_strategy</code> argument. Pushed models can be accessed like any other model on the hub, such
as with the <code>from_pretrained</code> method.`,_e,H,re,Q,le;return z=new ke({}),P=new ke({}),U=new He({props:{name:"class transformers.KerasMetricCallback",anchor:"transformers.KerasMetricCallback",parameters:[{name:"metric_fn",val:": typing.Callable"},{name:"eval_dataset",val:": typing.Union[tensorflow.python.data.ops.dataset_ops.DatasetV2, numpy.ndarray, tensorflow.python.framework.ops.Tensor, tuple, dict]"},{name:"output_cols",val:": typing.Optional[typing.List[str]] = None"},{name:"label_cols",val:": typing.Optional[typing.List[str]] = None"},{name:"batch_size",val:": typing.Optional[int] = None"},{name:"predict_with_generate",val:": bool = False"},{name:"use_xla_generation",val:": bool = False"},{name:"generate_kwargs",val:": typing.Optional[dict] = None"}],parametersDescription:[{anchor:"transformers.KerasMetricCallback.metric_fn",description:`<strong>metric_fn</strong> (<code>Callable</code>) &#x2014;
Metric function provided by the user. It will be called with two arguments - <code>predictions</code> and <code>labels</code>.
These contain the model&#x2019;s outputs and matching labels from the dataset. It should return a dict mapping
metric names to numerical values.`,name:"metric_fn"},{anchor:"transformers.KerasMetricCallback.eval_dataset",description:`<strong>eval_dataset</strong> (<code>tf.data.Dataset</code> or <code>dict</code> or <code>tuple</code> or <code>np.ndarray</code> or <code>tf.Tensor</code>) &#x2014;
Validation data to be used to generate predictions for the <code>metric_fn</code>.`,name:"eval_dataset"},{anchor:"transformers.KerasMetricCallback.output_cols",description:"<strong>output_cols</strong> (`List[str], <em>optional</em>) &#x2014;\nA list of columns to be retained from the model output as the predictions. Defaults to all.",name:"output_cols"},{anchor:"transformers.KerasMetricCallback.label_cols",description:`<strong>label_cols</strong> (&#x2019;<code>List[str]</code>, <em>optional</em>&#x2019;) &#x2014;
A list of columns to be retained from the input dataset as the labels. Will be autodetected if this is not
supplied.`,name:"label_cols"},{anchor:"transformers.KerasMetricCallback.batch_size",description:`<strong>batch_size</strong> (<code>int</code>, <em>optional</em>) &#x2014;
Batch size. Only used when the data is not a pre-batched <code>tf.data.Dataset</code>.`,name:"batch_size"},{anchor:"transformers.KerasMetricCallback.predict_with_generate",description:`<strong>predict_with_generate</strong> (<code>bool</code>, <em>optional</em>, defaults to <code>False</code>) &#x2014;
Whether we should use <code>model.generate()</code> to get outputs for the model.`,name:"predict_with_generate"},{anchor:"transformers.KerasMetricCallback.use_xla_generation",description:`<strong>use_xla_generation</strong> (<code>bool</code>, <em>optional</em>, defaults to <code>False</code>) &#x2014;
If we&#x2019;re generating, whether to compile model generation with XLA. This can massively increase the speed of
generation (up to 100X speedup) but will require a new XLA compilation for each input shape. When using XLA
generation, it&#x2019;s a good idea to pad your inputs to the same size, or to use the <code>pad_to_multiple_of</code>
argument in your <code>tokenizer</code> or <code>DataCollator</code>, which will reduce the number of unique input shapes and
save a lot of compilation time. This option has no effect is <code>predict_with_generate</code> is <code>False</code>.`,name:"use_xla_generation"},{anchor:"transformers.KerasMetricCallback.generate_kwargs",description:`<strong>generate_kwargs</strong> (<code>dict</code>, <em>optional</em>) &#x2014;
Keyword arguments to pass to <code>model.generate()</code> when generating. Has no effect if <code>predict_with_generate</code>
is <code>False</code>.`,name:"generate_kwargs"}],source:"https://github.com/huggingface/transformers/blob/v4.33.3/src/transformers/keras_callbacks.py#L20"}}),W=new we({props:{anchor:"transformers.KerasMetricCallback.example",$$slots:{default:[Be]},$$scope:{ctx:j}}}),Z=new we({props:{anchor:"transformers.KerasMetricCallback.example-2",$$slots:{default:[Se]},$$scope:{ctx:j}}}),B=new ke({}),S=new He({props:{name:"class transformers.PushToHubCallback",anchor:"transformers.PushToHubCallback",parameters:[{name:"output_dir",val:": typing.Union[str, pathlib.Path]"},{name:"save_strategy",val:": typing.Union[str, transformers.trainer_utils.IntervalStrategy] = 'epoch'"},{name:"save_steps",val:": typing.Optional[int] = None"},{name:"tokenizer",val:": typing.Optional[transformers.tokenization_utils_base.PreTrainedTokenizerBase] = None"},{name:"hub_model_id",val:": typing.Optional[str] = None"},{name:"hub_token",val:": typing.Optional[str] = None"},{name:"checkpoint",val:": bool = False"},{name:"**model_card_args",val:""}],parametersDescription:[{anchor:"transformers.PushToHubCallback.output_dir",description:`<strong>output_dir</strong> (<code>str</code>) &#x2014;
The output directory where the model predictions and checkpoints will be written and synced with the
repository on the Hub.`,name:"output_dir"},{anchor:"transformers.PushToHubCallback.save_strategy",description:`<strong>save_strategy</strong> (<code>str</code> or <a href="/docs/transformers/v4.33.3/en/internal/trainer_utils#transformers.IntervalStrategy">IntervalStrategy</a>, <em>optional</em>, defaults to <code>&quot;epoch&quot;</code>) &#x2014;
The checkpoint save strategy to adopt during training. Possible values are:</p>
<ul>
<li><code>&quot;no&quot;</code>: Save is done at the end of training.</li>
<li><code>&quot;epoch&quot;</code>: Save is done at the end of each epoch.</li>
<li><code>&quot;steps&quot;</code>: Save is done every <code>save_steps</code></li>
</ul>`,name:"save_strategy"},{anchor:"transformers.PushToHubCallback.save_steps",description:`<strong>save_steps</strong> (<code>int</code>, <em>optional</em>) &#x2014;
The number of steps between saves when using the &#x201C;steps&#x201D; <code>save_strategy</code>.`,name:"save_steps"},{anchor:"transformers.PushToHubCallback.tokenizer",description:`<strong>tokenizer</strong> (<code>PreTrainedTokenizerBase</code>, <em>optional</em>) &#x2014;
The tokenizer used by the model. If supplied, will be uploaded to the repo alongside the weights.`,name:"tokenizer"},{anchor:"transformers.PushToHubCallback.hub_model_id",description:`<strong>hub_model_id</strong> (<code>str</code>, <em>optional</em>) &#x2014;
The name of the repository to keep in sync with the local <code>output_dir</code>. It can be a simple model ID in
which case the model will be pushed in your namespace. Otherwise it should be the whole repository name,
for instance <code>&quot;user_name/model&quot;</code>, which allows you to push to an organization you are a member of with
<code>&quot;organization_name/model&quot;</code>.</p>
<p>Will default to the name of <code>output_dir</code>.`,name:"hub_model_id"},{anchor:"transformers.PushToHubCallback.hub_token",description:`<strong>hub_token</strong> (<code>str</code>, <em>optional</em>) &#x2014;
The token to use to push the model to the Hub. Will default to the token in the cache folder obtained with
<code>huggingface-cli login</code>.`,name:"hub_token"},{anchor:"transformers.PushToHubCallback.checkpoint",description:`<strong>checkpoint</strong> (<code>bool</code>, <em>optional</em>, defaults to <code>False</code>) &#x2014;
Whether to save full training checkpoints (including epoch and optimizer state) to allow training to be
resumed. Only usable when <code>save_strategy</code> is <code>&quot;epoch&quot;</code>.`,name:"checkpoint"}],source:"https://github.com/huggingface/transformers/blob/v4.33.3/src/transformers/keras_callbacks.py#L268"}}),H=new we({props:{anchor:"transformers.PushToHubCallback.example",$$slots:{default:[Ge]},$$scope:{ctx:j}}}),{c(){t=l("meta"),h=i(),o=l("p"),m=i(),b=l("h1"),n=l("a"),J=l("span"),k(z.$$.fragment),de=i(),A=l("span"),A.textContent=$e,te=i(),V=l("p"),V.textContent=Te,ae=i(),x=l("h2"),I=l("a"),L=l("span"),k(P.$$.fragment),pe=i(),X=l("span"),X.textContent=Me,oe=i(),f=l("div"),k(U.$$.fragment),he=i(),F=l("p"),F.innerHTML=Je,me=i(),Y=l("p"),Y.textContent=xe,ue=i(),k(W.$$.fragment),fe=i(),k(Z.$$.fragment),se=i(),C=l("h2"),E=l("a"),D=l("span"),k(B.$$.fragment),be=i(),K=l("span"),K.textContent=Ce,ne=i(),_=l("div"),k(S.$$.fragment),ge=i(),R=l("p"),R.innerHTML=je,_e=i(),k(H.$$.fragment),re=i(),Q=l("p"),this.h()},l(e){const s=Ue("svelte-1phssyn",document.head);t=c(s,"META",{name:!0,content:!0}),s.forEach(a),h=d(e),o=c(e,"P",{}),g(o).forEach(a),m=d(e),b=c(e,"H1",{class:!0});var G=g(b);n=c(G,"A",{id:!0,class:!0,href:!0});var O=g(n);J=c(O,"SPAN",{});var ee=g(J);w(z.$$.fragment,ee),ee.forEach(a),O.forEach(a),de=d(G),A=c(G,"SPAN",{"data-svelte-h":!0}),N(A)!=="svelte-sos9ph"&&(A.textContent=$e),G.forEach(a),te=d(e),V=c(e,"P",{"data-svelte-h":!0}),N(V)!=="svelte-15h4dbj"&&(V.textContent=Te),ae=d(e),x=c(e,"H2",{class:!0});var ce=g(x);I=c(ce,"A",{id:!0,class:!0,href:!0});var Ie=g(I);L=c(Ie,"SPAN",{});var We=g(L);w(P.$$.fragment,We),We.forEach(a),Ie.forEach(a),pe=d(ce),X=c(ce,"SPAN",{"data-svelte-h":!0}),N(X)!=="svelte-usi0tq"&&(X.textContent=Me),ce.forEach(a),oe=d(e),f=c(e,"DIV",{class:!0});var M=g(f);w(U.$$.fragment,M),he=d(M),F=c(M,"P",{"data-svelte-h":!0}),N(F)!=="svelte-d9jxhh"&&(F.innerHTML=Je),me=d(M),Y=c(M,"P",{"data-svelte-h":!0}),N(Y)!=="svelte-hu0773"&&(Y.textContent=xe),ue=d(M),w(W.$$.fragment,M),fe=d(M),w(Z.$$.fragment,M),M.forEach(a),se=d(e),C=c(e,"H2",{class:!0});var ie=g(C);E=c(ie,"A",{id:!0,class:!0,href:!0});var Ze=g(E);D=c(Ze,"SPAN",{});var Ee=g(D);w(B.$$.fragment,Ee),Ee.forEach(a),Ze.forEach(a),be=d(ie),K=c(ie,"SPAN",{"data-svelte-h":!0}),N(K)!=="svelte-22rlwk"&&(K.textContent=Ce),ie.forEach(a),ne=d(e),_=c(e,"DIV",{class:!0});var q=g(_);w(S.$$.fragment,q),ge=d(q),R=c(q,"P",{"data-svelte-h":!0}),N(R)!=="svelte-1o3veql"&&(R.innerHTML=je),_e=d(q),w(H.$$.fragment,q),q.forEach(a),re=d(e),Q=c(e,"P",{}),g(Q).forEach(a),this.h()},h(){u(t,"name","hf:doc:metadata"),u(t,"content",JSON.stringify(Xe)),u(n,"id","keras-callbacks"),u(n,"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"),u(n,"href","#keras-callbacks"),u(b,"class","relative group"),u(I,"id","transformers.KerasMetricCallback"),u(I,"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"),u(I,"href","#transformers.KerasMetricCallback"),u(x,"class","relative group"),u(f,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),u(E,"id","transformers.PushToHubCallback"),u(E,"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"),u(E,"href","#transformers.PushToHubCallback"),u(C,"class","relative group"),u(_,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8")},m(e,s){r(document.head,t),p(e,h,s),p(e,o,s),p(e,m,s),p(e,b,s),r(b,n),r(n,J),y(z,J,null),r(b,de),r(b,A),p(e,te,s),p(e,V,s),p(e,ae,s),p(e,x,s),r(x,I),r(I,L),y(P,L,null),r(x,pe),r(x,X),p(e,oe,s),p(e,f,s),y(U,f,null),r(f,he),r(f,F),r(f,me),r(f,Y),r(f,ue),y(W,f,null),r(f,fe),y(Z,f,null),p(e,se,s),p(e,C,s),r(C,E),r(E,D),y(B,D,null),r(C,be),r(C,K),p(e,ne,s),p(e,_,s),y(S,_,null),r(_,ge),r(_,R),r(_,_e),y(H,_,null),p(e,re,s),p(e,Q,s),le=!0},p(e,[s]){const G={};s&2&&(G.$$scope={dirty:s,ctx:e}),W.$set(G);const O={};s&2&&(O.$$scope={dirty:s,ctx:e}),Z.$set(O);const ee={};s&2&&(ee.$$scope={dirty:s,ctx:e}),H.$set(ee)},i(e){le||(v(z.$$.fragment,e),v(P.$$.fragment,e),v(U.$$.fragment,e),v(W.$$.fragment,e),v(Z.$$.fragment,e),v(B.$$.fragment,e),v(S.$$.fragment,e),v(H.$$.fragment,e),le=!0)},o(e){$(z.$$.fragment,e),$(P.$$.fragment,e),$(U.$$.fragment,e),$(W.$$.fragment,e),$(Z.$$.fragment,e),$(B.$$.fragment,e),$(S.$$.fragment,e),$(H.$$.fragment,e),le=!1},d(e){e&&(a(h),a(o),a(m),a(b),a(te),a(V),a(ae),a(x),a(oe),a(f),a(se),a(C),a(ne),a(_),a(re),a(Q)),a(t),T(z),T(P),T(U),T(W),T(Z),T(B),T(S),T(H)}}}const Xe={local:"keras-callbacks",sections:[{local:"transformers.KerasMetricCallback",title:"KerasMetricCallback"},{local:"transformers.PushToHubCallback",title:"PushToHubCallback"}],title:"Keras callbacks"};function Fe(j){return ze(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Qe extends Ve{constructor(t){super(),Pe(this,t,Fe,Ae,Ne,{})}}export{Qe as component};
