huggingface / huggingface/setfit

Model checkpoints saved during the training are unusable

Open
#526 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
2.8k
Forks
267
Avg merge
36m
Merged PRs (30d)
5

Description

Step 1: Train a model:

```
model_name = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
model = SetFitModel.from_pretrained(
model_name,
multi_target_strategy="multi-output",
use_differentiable_head=True,
head_params={"out_features": len(id2label)},
)

args = TrainingArguments(
output_dir=MODEL_DIR,
batch_size=32,
num_epochs=20,
evaluation_strategy='epoch',
save_strategy='epoch',
save_total_limit=4,
sampling_strategy='unique',
)
args.eval_strategy = args.evaluation_strategy

trainer = Trainer(
model=model,
args=args,
train_dataset=dataset["train"],
eval_dataset=dataset["validation"],
metric=batch_multi_label_metric,
)
trainer.train()
```

Step 2: Save the model explicitly. The examples in docs always do it, but there's no clear communication that this is **absolutely necessary** and, in fact, **the only way** to use the model later:
```
model.save_pretrained(MODEL_DIR / "explicit_save")
```

Step 3: Try to load from the latest checkpoint
```
checkpoint_model = SetFitModel.from_pretrained(
MODEL_DIR / "step_26560",
)
```
Without any warning, this model will not perform well, because the classifier (head) weights have not been loaded or even saved in the first place. If we compare this model's head with the one we saved explicitly, the difference is obvious:
```
explicit_model = SetFitModel.from_pretrained(
MODEL_DIR / "explicit_save",
)

checkpoint_head_weights = next(checkpoint_model.model_head.named_parameters())[1]
explicit_head_weights = next(explicit_model.model_head.named_parameters())[1]

fig1 = px.line(checkpoint_head_weights.detach().numpy().ravel())
fig2 = px.line(explicit_head_weights.detach().numpy().ravel())
```
![newplot](https://github.com/huggingface/setfit/assets/75306162/500e2161-c5c7-4277-a50b-b3339296854a)
![newplot (1)](https://github.com/huggingface/setfit/assets/75306162/0b3d12ca-4c02-4104-a3b6-dfe0e88944e8)

So if I didn't mess something up, my proposal would be to ether make this behavior clear to the user, or better to fix it so that the checkpoints would be usable.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.