Buckets:

hf-doc-build/doc-dev / transformers /pr_42227 /en /trainer_callbacks.html
HuggingFaceDocBuilder's picture
download
raw
30.8 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Callbacks&quot;,&quot;local&quot;:&quot;callbacks&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Creating a callback&quot;,&quot;local&quot;:&quot;creating-a-callback&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Built-in callbacks&quot;,&quot;local&quot;:&quot;built-in-callbacks&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DefaultFlowCallback&quot;,&quot;local&quot;:&quot;defaultflowcallback&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;ProgressCallback and PrinterCallback&quot;,&quot;local&quot;:&quot;progresscallback-and-printercallback&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;EarlyStoppingCallback&quot;,&quot;local&quot;:&quot;earlystoppingcallback&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/transformers/pr_42227/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/entry/start.a067d14a.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/scheduler.31fdf58d.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/singletons.303ebed4.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/index.252883d5.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/paths.afba2d50.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/entry/app.95d76f53.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/preload-helper.21822fa8.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/index.2f76fdf0.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/nodes/0.cb4700b8.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/nodes/671.9cb11e45.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/CopyLLMTxtMenu.bb5a0eb6.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.d0fb4fa3.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/IconCopy.ac192424.js">
<link rel="modulepreload" href="/docs/transformers/pr_42227/en/_app/immutable/chunks/CodeBlock.ab12f8e1.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Callbacks&quot;,&quot;local&quot;:&quot;callbacks&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Creating a callback&quot;,&quot;local&quot;:&quot;creating-a-callback&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Built-in callbacks&quot;,&quot;local&quot;:&quot;built-in-callbacks&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DefaultFlowCallback&quot;,&quot;local&quot;:&quot;defaultflowcallback&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;ProgressCallback and PrinterCallback&quot;,&quot;local&quot;:&quot;progresscallback-and-printercallback&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;EarlyStoppingCallback&quot;,&quot;local&quot;:&quot;earlystoppingcallback&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="callbacks" 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" href="#callbacks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Callbacks</span></h1> <p data-svelte-h="svelte-av500d"><a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.TrainerCallback">TrainerCallback</a> hooks into specific training events (epoch start, evaluation, training end) to modify training state or control flow. Use callbacks to log metrics to experiment trackers like Trackio, customize when saving and evaluation happen, or add other custom behavior. Stack multiple callbacks to combine features.</p> <p data-svelte-h="svelte-ccm8n2">Callbacks can’t modify the training loop itself, like the forward pass. To change what <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.Trainer">Trainer</a> computes, <a href="./trainer_customize">subclass</a> its methods instead.</p> <p data-svelte-h="svelte-jn8wad">The diagram below shows every event a callback can hook into.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->on<span class="hljs-emphasis">_train_</span>begin
└─ for each epoch:
<span class="hljs-code"> on_epoch_begin
└─ for each step:
on_step_begin
├─ for each gradient accumulation substep:
│ on_substep_end
on_pre_optimizer_step ← after gradient clipping, before optimizer.step()
on_optimizer_step ← after optimizer.step()
on_step_end
└─ (conditionally):
on_log
on_evaluate
on_save
on_epoch_end
on_train_end
on_predict / on_prediction_step
on_push_begin</span><!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="creating-a-callback" 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" href="#creating-a-callback"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating a callback</span></h2> <p data-svelte-h="svelte-p2d85a">Subclass <a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.TrainerCallback">TrainerCallback</a> and override one or more event methods from the diagram above. The example below demonstrates three hooks:</p> <ul data-svelte-h="svelte-1hejja7"><li><code>on_epoch_begin</code> prints the current epoch from <a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.TrainerState">TrainerState</a> (a live value) and the learning rate from <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> (a static value).</li> <li><code>on_step_end</code> reads the current learning rate from the lr_scheduler (passed via <code>**kwargs</code>) and sets <code>should_evaluate</code> on <a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.TrainerControl">TrainerControl</a> (see <a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.TrainerControl">TrainerControl</a> for a complete list of control objects) to trigger an evaluation when it drops below the threshold.</li> <li><code>on_train_end</code> prints the best metric and the step where it occurred.</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainerCallback
<span class="hljs-keyword">class</span> <span class="hljs-title class_">EpochLoggerCallback</span>(<span class="hljs-title class_ inherited__">TrainerCallback</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, lr_eval_threshold=<span class="hljs-number">1e-5</span></span>):
self.lr_eval_threshold = lr_eval_threshold
<span class="hljs-keyword">def</span> <span class="hljs-title function_">on_epoch_begin</span>(<span class="hljs-params">self, args, state, control, **kwargs</span>):
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Starting epoch <span class="hljs-subst">{<span class="hljs-built_in">int</span>(state.epoch) + <span class="hljs-number">1</span>}</span>/<span class="hljs-subst">{state.num_train_epochs}</span> &quot;</span>
<span class="hljs-string">f&quot;(lr=<span class="hljs-subst">{args.learning_rate}</span>)&quot;</span>)
<span class="hljs-keyword">def</span> <span class="hljs-title function_">on_step_end</span>(<span class="hljs-params">self, args, state, control, **kwargs</span>):
lr_scheduler = kwargs.get(<span class="hljs-string">&quot;lr_scheduler&quot;</span>)
<span class="hljs-keyword">if</span> lr_scheduler <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
current_lr = lr_scheduler.get_last_lr()[<span class="hljs-number">0</span>]
<span class="hljs-keyword">if</span> current_lr &lt; self.lr_eval_threshold:
control.should_evaluate = <span class="hljs-literal">True</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">on_train_end</span>(<span class="hljs-params">self, args, state, control, **kwargs</span>):
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Training complete! Best metric: <span class="hljs-subst">{state.best_metric}</span> at step <span class="hljs-subst">{state.best_global_step}</span>&quot;</span>)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1domxfe">Register the callback with <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.Trainer">Trainer</a> in the <code>callbacks</code> argument. You can also pass multiple callbacks as a list.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->trainer = Trainer(
callbacks=[EpochLoggerCallback(lr_eval_threshold=<span class="hljs-number">1e-5</span>)],
...,
)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="built-in-callbacks" 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" href="#built-in-callbacks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Built-in callbacks</span></h2> <p data-svelte-h="svelte-yv4jvj">Transformers includes several built-in callbacks that are active by default. Additional <a href="./main_classes/callback#available-callbacks">integrated callbacks</a> log to platforms like <a href="https://huggingface.co/docs/trackio/en/index" rel="nofollow">Trackio</a>.</p> <h3 class="relative group"><a id="defaultflowcallback" 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" href="#defaultflowcallback"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>DefaultFlowCallback</span></h3> <p data-svelte-h="svelte-1f0f0m4"><a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.DefaultFlowCallback">DefaultFlowCallback</a> manages the default logging, evaluation, and checkpoint schedule based on the <code>logging_strategy</code>, <code>eval_strategy</code>, and <code>save_strategy</code> values in <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a>. At the right step or epoch, it sets the corresponding <code>control</code> flags (<code>should_log</code>, <code>should_evaluate</code>, <code>should_save</code>). It also sets <code>should_training_stop</code> when <code>global_step</code> reaches <code>max_steps</code>.</p> <p data-svelte-h="svelte-d3v6sp">Overriding this callback is the main way to customize <em>when</em> logging, evaluation, or saving happens.</p> <h3 class="relative group"><a id="progresscallback-and-printercallback" 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" href="#progresscallback-and-printercallback"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>ProgressCallback and PrinterCallback</span></h3> <p data-svelte-h="svelte-1lwubk3"><a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.Trainer">Trainer</a> automatically picks between these two callbacks based on the <a href="https://huggingface.co/docs/transformers/en/main_classes/trainer#transformers.TrainingArguments.disable_tqdm" rel="nofollow">disable_tqdm</a> field in <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a>.</p> <ul data-svelte-h="svelte-1tw3l9c"><li><a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.ProgressCallback">ProgressCallback</a> is used by default. It displays a tqdm progress bar during training and a separate bar during evaluation or prediction, and prints the latest metrics on each <code>on_log</code> event. During distributed training, it only runs on the main process to avoid duplicate output.</li> <li><a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.PrinterCallback">PrinterCallback</a> is used when <code>disable_tqdm=True</code>. It prints the log dictionary to stdout on every <code>on_log</code> event with no progress bar.</li></ul> <p data-svelte-h="svelte-111xafv">You can also swap them manually with <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.Trainer.remove_callback">remove_callback()</a> and <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.Trainer.add_callback">add_callback()</a>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PrinterCallback
trainer = Trainer(...)
trainer.remove_callback(ProgressCallback)
trainer.add_callback(PrinterCallback)<!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="earlystoppingcallback" 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" href="#earlystoppingcallback"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>EarlyStoppingCallback</span></h3> <p data-svelte-h="svelte-8mbl7p"><a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.EarlyStoppingCallback">EarlyStoppingCallback</a> stops training when an evaluation metric stops improving. After each evaluation, it checks whether the metric improved by more than <code>early_stopping_threshold</code>. If the metric hasn’t improved for <code>early_stopping_patience</code> consecutive evaluations, training stops.</p> <p data-svelte-h="svelte-n01ia2"><a href="/docs/transformers/pr_42227/en/main_classes/callback#transformers.EarlyStoppingCallback">EarlyStoppingCallback</a> requires two <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a>:</p> <ul data-svelte-h="svelte-1qrskde"><li><code>metric_for_best_model</code>, the evaluation metric to monitor</li> <li><code>eval_strategy</code>, whether to evaluate on <code>&quot;steps&quot;</code> or <code>&quot;epoch&quot;</code></li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> EarlyStoppingCallback
trainer = Trainer(
...,
callbacks=[EarlyStoppingCallback(early_stopping_patience=<span class="hljs-number">3</span>, early_stopping_threshold=<span class="hljs-number">0.001</span>)],
)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="next-steps" 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" href="#next-steps"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Next steps</span></h2> <ul data-svelte-h="svelte-19sqi36"><li>See all available <a href="./main_classes/callback#available-callbacks">integrated callbacks</a> for logging to experiment trackers.</li> <li>The <a href="./trainer_customize">Subclassing Trainer methods</a> guide covers overriding <a href="/docs/transformers/pr_42227/en/main_classes/trainer#transformers.Trainer">Trainer</a> methods when you need to change what the training loop computes.</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/trainer_callbacks.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_13nnzth = {
assets: "/docs/transformers/pr_42227/en",
base: "/docs/transformers/pr_42227/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/transformers/pr_42227/en/_app/immutable/entry/start.a067d14a.js"),
import("/docs/transformers/pr_42227/en/_app/immutable/entry/app.95d76f53.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 671],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
30.8 kB
·
Xet hash:
b7d4e860a34e3d961b9caac28f0bea9550664dde9a98d33db09a6da90156e70c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.