NLP 知识速查表
本文档提供自然语言处理常用技术、API 和概念的快速参考。
常用库安装
# 基础库
pip install numpy pandas
# NLP 核心库
pip install nltk spacy jieba
# 词向量
pip install gensim
# 深度学习
pip install torch tensorflow
# Hugging Face
pip install transformers datasets tokenizers
# 评估工具
pip install seqeval sklearn-crfsuite
# 中文处理
pip install opencc # 繁简转换
文本预处理
分词
# NLTK 英文分词
from nltk.tokenize import word_tokenize
tokens = word_tokenize("Hello, world!")
# spaCy 分词
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Hello, world!")
tokens = [token.text for token in doc]
# jieba 中文分词
import jieba
seg_list = jieba.cut("自然语言处理", cut_all=False)
停用词
# NLTK 英文停用词
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
# 中文停用词(自定义)
stop_words = {'的', '了', '在', '是', '我', '有', '和', '就'}
文本清洗
import re
# 去除 HTML 标签
clean_text = re.sub(r'<[^>]+>', '', html_text)
# 去除特殊字符
clean_text = re.sub(r'[^\u4e00-\u9fa5a-zA-Z0-9\s]', '', text)
# 合并多余空格
clean_text = re.sub(r'\s+', ' ', text)
词向量
Word2Vec
from gensim.models import Word2Vec
# 训练
model = Word2Vec(sentences, vector_size=100, window=5, min_count=1, sg=1)
# 获取词向量
vector = model.wv['词语']
# 相似度
similarity = model.wv.similarity('词1', '词2')
# 最相似词
similar_words = model.wv.most_similar('词语', topn=10)
FastText
from gensim.models import FastText
model = FastText(sentences, vector_size=100, window=5, min_n=3, max_n=6)
# 可处理未登录词
vector = model.wv['未登录词']
Hugging Face Transformers
Pipeline 快速使用
from transformers import pipeline
# 情感分析
classifier = pipeline("sentiment-analysis")
result = classifier("I love this!")
# 命名实体识别
ner = pipeline("ner", aggregation_strategy="simple")
result = ner("Apple is based in Cupertino.")
# 文本生成
generator = pipeline("text-generation", model="gpt2")
result = generator("Once upon a time")
# 问答
qa = pipeline("question-answering")
result = qa(question="What is NLP?", context="NLP is...")
# 填空
fill = pipeline("fill-mask", model="bert-base-chinese")
result = fill("自然语言处理是[MASK]智能的分支。")
加载模型
from transformers import AutoModel, AutoTokenizer
model_name = "bert-base-chinese"
# 加载分词器和模型
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
# 分词
inputs = tokenizer("自然语言处理", return_tensors="pt")
# 获取输出
outputs = model(**inputs)
文本分类
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained("bert-base-chinese", num_labels=2)
# 预测
inputs = tokenizer("文本内容", return_tensors="pt")
outputs = model(**inputs)
predicted_class = outputs.logits.argmax().item()
文本生成
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("gpt2")
# 生成
inputs = tokenizer("Prompt text", return_tensors="pt")
outputs = model.generate(**inputs, max_length=100, temperature=0.7)
generated_text = tokenizer.decode(outputs[0])
序列标注
BIO 标签
| 标签 | 含义 |
|---|---|
| B-X | 实体 X 开始 |
| I-X | 实体 X 内部 |
| O | 非实体 |
spaCy NER
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Apple CEO Tim Cook announced new products.")
for ent in doc.ents:
print(ent.text, ent.label_)
评估指标
from seqeval.metrics import precision_score, recall_score, f1_score
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
文本分类
传统方法
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
pipeline = Pipeline([
('tfidf', TfidfVectorizer()),
('clf', LogisticRegression())
])
pipeline.fit(texts, labels)
predictions = pipeline.predict(new_texts)
深度学习评估
from sklearn.metrics import classification_report
print(classification_report(y_true, y_pred, target_names=class_names))
语言模型
困惑度
import math
def perplexity(loss, n):
return math.exp(loss / n)
文本生成参数
| 参数 | 说明 | 推荐值 |
|---|---|---|
| temperature | 控制随机性 | 0.7-1.0 |
| top_k | 保留前 k 个候选 | 50 |
| top_p | 核采样阈值 | 0.9-0.95 |
| max_length | 最大生成长度 | 根据任务 |
| do_sample | 是否采样 | True |
常见中文模型
| 模型 | 用途 | Hugging Face ID |
|---|---|---|
| BERT 中文 | 通用理解 | bert-base-chinese |
| RoBERTa 中文 | 通用理解 | hfl/chinese-roberta-wwm-ext |
| GPT2 中文 | 文本生成 | uer/gpt2-chinese-cluecorpussmall |
| BART 中文 | 序列到序列 | fnlp/bart-base-chinese |
| T5 中文 | 序列到序列 | Langboat/Mengzi-T5-base |
常见任务模型选择
| 任务 | 推荐模型架构 | 示例 |
|---|---|---|
| 文本分类 | BERT、RoBERTa | BERT + 分类头 |
| 序列标注 | BERT + CRF | BERT-CRF |
| 文本生成 | GPT、BART | GPT-2 |
| 机器翻译 | T5、mBART | T5 |
| 问答 | BERT | BERT for QA |
| 摘要生成 | BART、T5 | BART |
训练参数推荐
学习率
| 模型类型 | 推荐学习率 |
|---|---|
| BERT 微调 | 2e-5 ~ 5e-5 |
| GPT 微调 | 1e-5 ~ 5e-5 |
| 从头训练 | 1e-4 ~ 1e-3 |
批次大小
| 场景 | 推荐批次 |
|---|---|
| 微调 | 16-32 |
| 预训练 | 256-512 |
| 大模型 | 1-4(配合梯度累积) |
常见问题解决
CUDA 内存不足
# 减小批次大小
batch_size = 4
# 使用梯度累积
gradient_accumulation_steps = 4
# 使用混合精度
from torch.cuda.amp import autocast
处理长文本
# 截断
inputs = tokenizer(text, max_length=512, truncation=True)
# 滑动窗口
inputs = tokenizer(text, max_length=512, stride=128, return_overflowing_tokens=True)
未登录词处理
# 使用 FastText
from gensim.models import FastText
model = FastText(sentences)
vector = model.wv['未登录词']
# 使用子词分词
tokenizer = AutoTokenizer.from_pretrained("bert-base-chinese")
性能优化
模型量化
from transformers import BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16
)
LoRA 微调
from peft import LoraConfig, get_peft_model
config = LoraConfig(r=8, lora_alpha=32, target_modules=["query", "value"])
model = get_peft_model(model, config)
常用评估指标
| 任务 | 指标 |
|---|---|
| 分类 | Accuracy、F1、Precision、Recall |
| 序列标注 | Entity-level F1 |
| 生成 | BLEU、ROUGE |
| 语言模型 | Perplexity |
| 问答 | Exact Match、F1 |
资源链接
- Hugging Face 文档: https://huggingface.co/docs
- spaCy 文档: https://spacy.io/api/doc
- NLTK 文档: https://www.nltk.org/
- jieba 文档: https://github.com/fxsjy/jieba
- Gensim 文档: https://radimrehurek.com/gensim/