共计 2777 个字符,预计需要花费 7 分钟才能阅读完成。
核心概念定义与关系梳理
-
机器学习(Machine Learning, ML):通过算法使计算机系统从数据中自动学习模式(patterns)并做出决策的学科,分为监督学习(Supervised Learning)、无监督学习(Unsupervised Learning)和强化学习(Reinforcement Learning)三大范式。

-
深度学习(Deep Learning, DL):机器学习的分支,利用多层神经网络(Neural Networks)进行特征自动提取和表示学习(Representation Learning),显著提升在图像、语音、文本等复杂数据的处理能力。
-
神经网络(Neural Network, NN):模仿生物神经元连接方式的计算模型,由输入层(Input Layer)、隐藏层(Hidden Layers)和输出层(Output Layer)构成,基础单元为感知机(Perceptron)。
-
自然语言处理(Natural Language Processing, NLP):人工智能领域的重要方向,专注于计算机对人类语言的理解与生成,其技术演进可分为规则驱动(Rule-based)、统计学习(Statistical Learning)和深度学习三个阶段。
监督学习与无监督学习在 NLP 中的对比
监督学习典型场景
-
文本分类(Text Classification):给定标注好的新闻文本,训练模型预测类别(如政治、体育)。常用交叉熵损失函数(Cross-Entropy Loss)和微调(Fine-tuning)策略。
-
命名实体识别(Named Entity Recognition, NER):序列标注任务,需标注文本中的人名、地点等实体。采用 BiLSTM-CRF 架构时需注意标签一致性约束。
-
机器翻译(Machine Translation):基于平行语料训练 seq2seq 模型,需处理输入输出长度不对齐问题,注意力机制(Attention Mechanism)是关键。
无监督学习典型场景
-
词向量训练(Word Embedding):通过 Skip-gram 或 CBOW 模型学习词的分布式表示(Distributed Representation),词相似度计算是其重要评估指标。
-
主题建模(Topic Modeling):LDA 算法可自动发现文档集合中的潜在主题(Latent Topics),适用于新闻聚类和推荐系统。
-
预训练语言模型 (Pretrained Language Models):BERT 的 Masked Language Modeling(MLM) 任务本质上属于自监督学习(Self-supervised Learning),利用大规模无标注数据捕捉语言通用特征。
BERT 架构详解与实现
模型架构图解
graph TD
A[Input Layer] --> B[Token Embeddings]
A --> C[Segment Embeddings]
A --> D[Position Embeddings]
B --> E[Transformer Encoder]
C --> E
D --> E
E --> F[CLS Token Output]
E --> G[Sequence Output]
关键超参数说明
- hidden_size:768(Base)或 1024(Large),决定模型容量
- num_hidden_layers:Transformer 层数,通常 12 层(Base)或 24 层(Large)
- num_attention_heads:多头注意力机制的头数,需能被 hidden_size 整除
- max_position_embeddings:512,输入序列最大长度限制
PyTorch 实现示例
import torch
import torch.nn as nn
from transformers import BertModel, BertTokenizer
class BertTextClassifier(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.bert = BertModel.from_pretrained('bert-base-uncased')
self.dropout = nn.Dropout(0.1) # 防止过拟合
self.classifier = nn.Linear(768, num_classes) # 分类头
def forward(self, input_ids, attention_mask):
# GPU 加速
input_ids = input_ids.to('cuda')
attention_mask = attention_mask.to('cuda')
# BERT 编码
outputs = self.bert(
input_ids=input_ids,
attention_mask=attention_mask
)
pooled_output = outputs.pooler_output
# 梯度裁剪(防止梯度爆炸)torch.nn.utils.clip_grad_norm_(self.parameters(), 1.0)
# 分类预测
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
return logits
生产环境优化策略
模型量化部署
- 动态量化(Dynamic Quantization):将 FP32 转为 INT8,推理速度提升 2 - 4 倍,需测试精度损失是否在可接受范围(通常 <3%)
- 量化感知训练(QAT):在训练阶段模拟量化过程,相比训练后量化(PTQ)能更好保持精度
OOV 问题解决方案
- 子词切分(Subword Tokenization):采用 WordPiece 或 BPE 算法,将生僻词分解为已知子词
- 字符级嵌入(Character-level Embeddings):补充使用字符 CNN 处理未登录词
- 领域自适应(Domain Adaptation):在目标领域数据上继续预训练
显存优化技巧
- 梯度检查点(Gradient Checkpointing):以时间换空间,减少约 60% 显存占用
- 混合精度训练(Mixed Precision Training):FP16+FP32 组合,需使用 AMP 工具包
- 模型并行(Model Parallelism):将超大模型拆分到多张 GPU
延伸思考
- 预训练模型迁移评估:除准确率外,应关注
- 领域分布偏移 (Domain Shift) 程度
- 少样本学习 (Few-shot Learning) 表现
-
特征可解释性(Interpretability)
-
RNN vs Transformer 对比:
- RNN 优势:序列建模理论完备,超参少
- Transformer 优势:并行计算,长距离依赖捕捉能力强
- 折中方案:Longformer、Reformer 等改进架构

