Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model checkpoints saved during the training are unusable #526

Open
n-splv opened this issue May 21, 2024 · 0 comments
Open

Model checkpoints saved during the training are unusable #526

n-splv opened this issue May 21, 2024 · 0 comments

Comments

@n-splv
Copy link

n-splv commented May 21, 2024

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
newplot (1)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant