在本地保存固定模型

发布于 2025-01-25 21:25:41 字数 718 浏览 5 评论 0 原文

我试图了解如何在本地保存微调的模型,而不是将其推向集线器。

我已经完成了一些教程,在微调的最后一步,模型正在运行 Trainer.train()。然后,指令通常是: trainer.push_to_hub

但是如果我不想推到轮毂怎么办?我想在本地保存模型,然后可以将其从自己的计算机加载到将来的任务中,以便我可以进行推断而无需重新调整。

我该怎么做?

例如:最初从拥抱面上加载模型:

model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=5)
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=small_train_dataset,
    eval_dataset=small_eval_dataset,
    compute_metrics=compute_metrics,
)

trainer.train()

以某种方式保存本地训练有素的新型号,以便下次我可以通过

model = 'some local directory where model and configs (?) got saved'

I'm trying to understand how to save a fine-tuned model locally, instead of pushing it to the hub.

I've done some tutorials and at the last step of fine-tuning a model is running trainer.train() . And then the instruction is usually: trainer.push_to_hub

But what if I don't want to push to the hub? I want to save the model locally, and then later be able to load it from my own computer into future task so I can do inference without re-tuning.

How can I do that?

eg: Initially load a model from hugging face:

model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=5)
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=small_train_dataset,
    eval_dataset=small_eval_dataset,
    compute_metrics=compute_metrics,
)

trainer.train()

Somehow save the new trained model locally, so that next time I can pass

model = 'some local directory where model and configs (?) got saved'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2025-02-01 21:25:42

您可以使用

trainer.save_model("path/to/model")

或者,或者,或者,方法

model.save_pretrained("path/to/model")

然后,在重新加载模型时,将保存的路径指定为:

AutoModelForSequenceClassification.from_pretrained("path/to/model")

You can use the save_model method:

trainer.save_model("path/to/model")

Or alternatively, the save_pretrained method:

model.save_pretrained("path/to/model")

Then, when reloading your model, specify the path you saved to:

AutoModelForSequenceClassification.from_pretrained("path/to/model")
紫轩蝶泪 2025-02-01 21:25:42

您可以使用

trainer.model._save_pretrained("file_path/model_name")

You can do it using

trainer.model._save_pretrained("file_path/model_name")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文