> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/fr/notions-de-base/finetuning-from-last-checkpoint.md).

# Fine-tuning à partir du dernier point de contrôle

Vous devez modifier le `Trainer` d'abord pour ajouter `save_strategy` et `save_steps`. Ci-dessous, un point de contrôle est enregistré toutes les 50 étapes dans le dossier `outputs`.

```python
trainer = SFTTrainer(
    ....
    args = TrainingArguments(
        ....
        output_dir = "outputs",
        save_strategy = "steps",
        save_steps = 50,
    ),
)
```

Puis, dans le trainer, faites :

```python
trainer_stats = trainer.train(resume_from_checkpoint = True)
```

Ce qui commencera à partir du dernier point de contrôle et poursuivra l'entraînement.

### Intégration Wandb

```
# Installer la bibliothèque
!pip install wandb --upgrade

# Configuration de Wandb
!wandb login <token>

import os

os.environ["WANDB_PROJECT"] = "<name>"
os.environ["WANDB_LOG_MODEL"] = "checkpoint"
```

Puis, dans `TrainingArguments()` définissez

```
report_to = "wandb",
logging_steps = 1, # Modifier si nécessaire
save_steps = 100 # Modifier si nécessaire
run_name = "<name>" # (Facultatif)
```

Pour entraîner le modèle, faites `trainer.train()`; pour reprendre l'entraînement, faites

```
import wandb
run = wandb.init()
artifact = run.use_artifact('<username>/<Wandb-project-name>/<run-id>', type='model')
artifact_dir = artifact.download()
trainer.train(resume_from_checkpoint=artifact_dir)
```

## :question:Comment puis-je faire de l'arrêt anticipé ?

Si vous souhaitez arrêter ou mettre en pause l'exécution du fine-tuning / entraînement parce que la perte de validation ne diminue pas, vous pouvez utiliser l'arrêt anticipé, qui stoppe le processus d'entraînement. Utilisez `EarlyStoppingCallback`.

Comme d'habitude, configurez votre trainer et votre ensemble de données de validation. Ce qui suit est utilisé pour arrêter l'exécution de l'entraînement si le `eval_loss` (la perte de validation) ne diminue pas après environ 3 étapes.

```python
from trl import SFTConfig, SFTTrainer
trainer = SFTTrainer(
    args = SFTConfig(
        fp16_full_eval = True,
        per_device_eval_batch_size = 2,
        eval_accumulation_steps = 4,
        output_dir = "training_checkpoints", # emplacement des points de contrôle enregistrés pour l'arrêt anticipé
        save_strategy = "steps",             # enregistrer le modèle toutes les N étapes
        save_steps = 10,                     # nombre d'étapes avant l'enregistrement du modèle
        save_total_limit = 3,                # ne conserver que 3 points de contrôle enregistrés pour économiser de l'espace disque
        eval_strategy = "steps",             # évaluer toutes les N étapes
        eval_steps = 10,                     # nombre d'étapes avant d'effectuer l'évaluation
        load_best_model_at_end = True,       # DOIT ÊTRE UTILISÉ pour l'arrêt anticipé
        metric_for_best_model = "eval_loss", # métrique sur laquelle nous voulons appliquer l'arrêt anticipé
        greater_is_better = False,           # plus la perte de validation est faible, mieux c'est
    ),
    model = model,
    tokenizer = tokenizer,
    train_dataset = new_dataset["train"],
    eval_dataset = new_dataset["test"],
)
```

Nous ajoutons ensuite le callback, qui peut également être personnalisé :

```python
from transformers import EarlyStoppingCallback
early_stopping_callback = EarlyStoppingCallback(
    early_stopping_patience = 3,     # Combien d'étapes nous attendrons si la perte de validation ne diminue pas
                                     # Par exemple, la perte peut augmenter, puis diminuer après 3 étapes
    early_stopping_threshold = 0.0,  # Peut être défini plus haut - définit de combien la perte doit diminuer jusqu'à
                                     # ce que nous considérions un arrêt anticipé. Par exemple, 0.01 signifie que si la perte était
                                     # 0.02 puis 0.01, nous considérons qu'il faut arrêter l'exécution de manière anticipée.
)
trainer.add_callback(early_stopping_callback)
```

Puis entraînez le modèle comme d'habitude via `trainer.train() .`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://unsloth.ai/docs/fr/notions-de-base/finetuning-from-last-checkpoint.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
