0Pricing
Deep Learning Academy · 课时

微调 Hugging Face 模型

让预训练 Transformer 适应您的数据

微调 Hugging Face 模型 是 CoddyKit 上的免费 Deep Learning Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Deep Learning Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Deep Learning Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

A Hub of Pretrained Models

Hugging Face hosts thousands of ready-to-use transformers for text, vision, and audio. You can fine-tune one on your own data in minutes. 🤗

Install Transformers

The transformers library gives you models, tokenizers, and training tools in one package.

pip install transformers datasets

Load a Tokenizer

Text must become numbers first. The tokenizer that ships with each model knows exactly how to split and encode your text.

from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained('bert-base-uncased')

Load a Model With a New Head

Pick a class for your task. AutoModelForSequenceClassification loads the backbone and attaches a fresh classifier head for your labels.

from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)

Tokenize Your Dataset

Run every example through the tokenizer with padding and truncation so all inputs share the same length.

def encode(b):
    return tok(b['text'], truncation=True, padding='max_length')

TrainingArguments

TrainingArguments bundles your hyperparameters: learning rate, batch size, epochs, and where to save checkpoints.

from transformers import TrainingArguments
args = TrainingArguments(output_dir='out', learning_rate=2e-5, num_train_epochs=3)

Keep That Rate Low

Notice the rate is just 2e-5. As with any fine-tune, a small learning rate protects the pretrained weights from being wrecked.

The Trainer

The Trainer class wraps the whole loop: forward pass, loss, backward, step, and evaluation. You skip writing it by hand.

from transformers import Trainer
trainer = Trainer(model=model, args=args, train_dataset=train_ds)

Train in One Call

Once everything is wired up, kick off fine-tuning with a single train call and watch the loss fall.

trainer.train()

Evaluate and Predict

After training, call evaluate for metrics on held-out data, then use predict to score brand-new examples.

trainer.evaluate()

Save and Share

Persist your tuned model with save_pretrained, then reload it anywhere or push it to the Hub for others to use.

model.save_pretrained('my-classifier')

Quick Check

Which Hugging Face class runs the full training loop for you so you don't write it by hand?

Recap

You tokenized data, loaded a model with a fresh head, set a low rate in TrainingArguments, and fine-tuned it with the Trainer. 🎯

常见问题解答

「微调 Hugging Face 模型」课时是免费的吗?

是的 — 「微调 Hugging Face 模型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Deep Learning Academy 课程的其余内容,请升级到 CoddyKit PRO。 Deep Learning Academy 课程共包含 4 节课。

「微调 Hugging Face 模型」这节课中我会学到什么?

让预训练 Transformer 适应您的数据 你通过在浏览器中直接运行的动手代码来练习 Deep Learning Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Deep Learning Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Deep Learning Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「微调 Hugging Face 模型」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Deep Learning Academy 课中编写并运行代码吗?

能。每节 Deep Learning Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 冻结骨干网络,训练任务头
  2. 使用更低的学习率微调
  3. 分层差异化学习率
  4. 微调 Hugging Face 模型
← 返回 Deep Learning Academy