> 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/zh/ji-chu-zhi-shi/finetuning-from-last-checkpoint.md).

# 从上一个检查点继续微调

检查点功能可让你保存微调进度，以便暂停后继续。

你必须编辑 `训练器` 首先以添加 `save_strategy` 和 `save_steps`。下面将在每 50 步保存一个检查点到文件夹 `outputs`.

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

然后在训练器中这样做：

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

这将从最新的检查点开始并继续训练。

### Wandb 集成

```
# 安装库
!pip install wandb --upgrade

# 设置 Wandb
!wandb login <token>

import os

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

然后在 `TrainingArguments()` 设置

```
report_to = "wandb",
logging_steps = 1, # 如有需要可更改
save_steps = 100 # 如有需要可更改
run_name = "<name>" #（可选）
```

训练模型时，执行 `trainer.train()`；要恢复训练，请执行

```
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:我该如何进行早停？

如果你想在评估损失不再下降时停止或暂停微调 / 训练运行，那么你可以使用早停，它会停止训练过程。使用 `EarlyStoppingCallback`.

像往常一样，设置好你的训练器和评估数据集。下面用于在以下情况下停止训练运行： `eval_loss` （评估损失）在大约 3 步后没有下降。

```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", # 用于早停保存检查点的位置
        save_strategy = "steps",             # 每 N 步保存一次模型
        save_steps = 10,                     # 距离保存模型还需多少步
        save_total_limit = 3,                # 只保留 3 个已保存的检查点以节省磁盘空间
        eval_strategy = "steps",             # 每 N 步评估一次
        eval_steps = 10,                     # 距离进行评估还需多少步
        load_best_model_at_end = True,       # 早停必须使用
        metric_for_best_model = "eval_loss", # 我们希望基于其进行早停的指标
        greater_is_better = False,           # 评估损失越低越好
    ),
    model = model,
    tokenizer = tokenizer,
    train_dataset = new_dataset["train"],
    eval_dataset = new_dataset["test"],
)
```

然后添加回调，它也可以自定义：

```python
from transformers import EarlyStoppingCallback
early_stopping_callback = EarlyStoppingCallback(
    early_stopping_patience = 3,     # 如果评估损失没有下降，我们会等待多少步
                                     # 例如，损失可能会上升，但会在 3 步后下降
    early_stopping_threshold = 0.0,  # 可以设得更高 - 用于设置损失至少下降多少，直到
                                     # 我们认为可以早停。例如 0.01 表示如果损失是
                                     # 0.02 then 0.01，我们认为该运行可以早停。
)
trainer.add_callback(early_stopping_callback)
```

然后像平常一样通过以下方式训练模型： `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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/zh/ji-chu-zhi-shi/finetuning-from-last-checkpoint.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
