huggingface实战
Hugging Face 实战通常指在实际项目中应用 Hugging Face 提供的强大自然语言处理工具,如 Transformers 库。这个库包含了预训练的语言模型(如 BERT、GPT-3 等),以及用于训练、微调和部署模型的各种组件。通过 Hugging Face,开发者可以进行以下操作:
文本分类:利用预训练模型进行情感分析、新闻分类等任务,只需加载模型,调整前向传播路径,并对输入数据进行适当的编码。
序列标注:如命名实体识别,可以使用像 BERT-NER 的模型,对文本中的特定实体进行标注。
问答系统:例如基于检索式或生成式的 QA 模型,像 DPR 和 FiD,可以构建聊天机器人或信息提取系统。
文本生成:使用 GPT-Neo 或者 GPT-3 进行文本续写、故事创作、代码自动生成等。
翻译:利用 MarianMT 系列模型进行跨语言交流。
对话系统:通过 ChatBot 构建能理解和回应用户查询的人工智能助手。
要开始 Hugging Face 实战,你需要安装 transformers 库,熟悉 PyTorch 或 TensorFlow API,然后根据具体需求选择合适的模型和配置。如果你需要进一步了解,可以从官方文档、教程和示例代码入手。
huggingface transformers实战
Hugging Face Transformers 是一个基于 PyTorch 和 TensorFlow 的自然语言处理(NLP)库,它提供了用于训练、微调和使用最先进的预训练模型的工具和接口。以下是使用 Hugging Face Transformers 进行实战的一些示例。
- 文本分类
文本分类是将文本分为不同的类别或标签的任务。在这个示例中,我们将使用 Hugging Face Transformers 中的 DistilBERT 模型来训练一个情感分析分类器,以将电影评论分为正面或负面。
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
import torch
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased')
# 训练数据
train_texts = ["I really liked this movie", "The plot was boring and predictable"]
train_labels = [1, 0]
# 将文本编码为输入张量
train_encodings = tokenizer(train_texts, truncation=True, padding=True)
# 将标签编码为张量
train_labels = torch.tensor(train_labels)
# 训练模型
model.train()
optimizer = torch.optim.AdamW(model.parameters(), lr=5e-5)
for epoch in range(3):
optimizer.zero_grad()
outputs = model(**train_encodings, labels=train_labels)
loss = outputs.loss
loss.backward()
optimizer.step()
# 预测新的评论
texts = ["This is a great movie", "I hated this movie"]
encodings = tokenizer(texts, truncation=True, padding=True)
model.eval()
with torch.no_grad():
outputs = model(**encodings)
predictions = torch.argmax(outputs.logits, dim=1)
print(predictions)
- 问答系统
问答系统是回答用户提出的问题的模型。在这个示例中,我们将使用 Hugging Face Transformers 中的 DistilBERT 模型和 SQuAD 数据集来训练一个简单的问答系统。
from transformers import DistilBertTokenizer, DistilBertForQuestionAnswering
import torch
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertForQuestionAnswering.from_pretrained('distilbert-base-uncased')
# 加载 SQuAD 数据集
from transformers import squad_convert_examples_to_features, SquadExample, SquadFeatures, squad_processors
processor = squad_processors['squad']
examples = processor.get_train_examples('data')
features = squad_convert_examples_to_features(examples=examples, tokenizer=tokenizer, max_seq_length=384, doc_stride=128, max_query_length=64, is_training=True)
# 训练模型
model.train()
optimizer = torch.optim.AdamW(model.parameters(), lr=5e-5)
for epoch in range(3):
for feature in features:
optimizer.zero_grad()
outputs = model(input_ids=torch.tensor([feature.input_ids]), attention_mask=torch.tensor([feature.attention_mask]), start_positions=torch.tensor([feature.start_position]), end_positions=torch.tensor([feature.end_position]))
loss = outputs.loss
loss.backward()
optimizer.step()
# 预测新的问题
text = "What is the capital of France?"
question = "What country's capital is Paris?"
inputs = tokenizer.encode_plus(question, text, add_special_tokens=True, return_tensors='pt')
model.eval()
with torch.no_grad():
start_scores, end_scores = model(**inputs)
start_index = torch.argmax(start_scores)
end_index = torch.argmax(end_scores)
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs['input_ids'][0][start_index:end_index+1]))
print(answer)
- 文本生成
文本生成是使用预训练模型生成自然语言文本的任务。在这个示例中,我们将使用 Hugging Face Transformers 中的 GPT-2 模型生成一些小说的开头。
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import torch
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')
# 生成新的文本
seed_text = "In a hole in the ground there lived a hobbit."
encoded = tokenizer.encode(seed_text, return_tensors='pt')
model.eval()
with torch.no_grad():
output = model.generate(encoded, max_length=100, do_sample=True)
generated = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated)
这些示例只是 Hugging Face Transformers 库的一部分功能。您可以通过访问 Hugging Face Transformers 官方文档来了解更多信息。
huggingface transformer实战
Hugging Face Transformers 实战教程示例项目
加载预训练模型并执行文本分类任务
为了展示如何利用Hugging Face Transformers库进行实际操作,下面提供了一个简单的Python脚本实例,该实例展示了如何加载预训练的语言模型,并将其应用于二元情感分析任务。
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
# 加载预训练的情感分析pipeline
classifier = pipeline('sentiment-analysis')
# 测试输入语句列表
test_sentences = ["I love programming!", "This movie was terrible."]
# 对测试句子进行预测
results = classifier(test_sentences)
for result in results:
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
这段代码首先导入必要的模块,接着创建一个用于情感分析的pipeline
对象。之后定义了一些待测字符串组成的列表作为输入数据源。最后遍历输出结果,打印每条记录对应的标签及其置信度得分[^3]。
使用自定义数据集微调BERT模型
对于更复杂的场景,则可能涉及到基于自有标注的数据来优化现有模型的表现。这里给出一段更为完整的流程:
- 准备好CSV文件格式的数据集;
- 定义PyTorch Dataset类读取上述数据;
- 构建DataLoader迭代器供后续训练过程调用;
- 初始化指定架构(如BERT)的基础模型以及相应的分词器;
- 设定超参数配置项;
- 启动fine-tuning阶段直至收敛;
- 验证最终效果并通过保存最佳权重完成部署前准备工作。
具体实现细节如下所示:
import torch
from datasets import load_dataset
from transformers import BertForSequenceClassification, Trainer, TrainingArguments, BertTokenizerFast
# 加载本地csv文件形式的小规模样例数据集
dataset = load_dataset('csv', data_files={'train': 'path/to/train.csv', 'validation': 'path/to/val.csv'})
tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
def preprocess_function(examples):
return tokenizer(examples['text'], truncation=True, padding='max_length')
encoded_datasets = dataset.map(preprocess_function, batched=True)
columns_to_return = ['input_ids', 'attention_mask', 'labels']
encoded_datasets.set_format(type='torch', columns=columns_to_return)
training_args = TrainingArguments(
output_dir='./results',
evaluation_strategy="epoch",
learning_rate=2e-5,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
num_train_epochs=3,
weight_decay=0.01,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=encoded_datasets['train'],
eval_dataset=encoded_datasets['validation']
)
trainer.train()
此段程序片段主要针对已有结构化的表格型数据实施了初步清理工作;随后借助于Transformers内置工具完成了tokenization环节;紧接着设置了若干关键性的hyperparameters选项;再者便是正式开启了finetune周期直到达到预期目标为止[^1]。
相关推荐













