深度学习表示学习:特征学习与迁移学习 1. 技术分析 1.1 表示学习概述 表示学习是自动学习数据特征的过程:
表示学习层次 原始数据 → 低级特征 → 中级特征 → 高级特征 → 任务预测 关键: 层次特征提取 端到端学习 迁移能力1.2 表示学习方法 方法 特点 监督程度 适用场景 自监督学习 无需标注 无监督 预训练 对比学习 对比正负样本 自监督 视觉 度量学习 学习距离度量 弱监督 检索 迁移学习 利用预训练模型 半监督 小数据
1.3 迁移学习策略 迁移学习策略 特征提取: 使用预训练模型特征 微调: 调整部分参数 领域适应: 适应新领域 增量学习: 逐步学习新任务2. 核心功能实现 2.1 自监督学习 import numpy as np class ContrastiveLearning: def __init__(self, encoder, temperature=0.5): self.encoder = encoder self.temperature = temperature def augment(self, x): augmented = x + np.random.normal(0, 0.1, x.shape) return augmented def compute_loss(self, x): x1 = self.augment(x) x2 = self.augment(x) z1 = self.encoder(x1) z2 = self.encoder(x2) z = np.concatenate([z1, z2], axis=0) sim = np.dot(z, z.T) / self.temperature sim = np.exp(sim - np.max(sim, axis=1, keepdims=True)) mask = np.eye(len(z)) mask = np.logical_not(mask) pos_mask = np.zeros((len(z), len(z))) for i in range(len(z) // 2): pos_mask[i, i + len(z) // 2] = 1 pos_mask[i + len(z) // 2, i] = 1 pos_sim = np.sum(sim * pos_mask, axis=1) neg_sim = np.sum(sim * mask, axis=1) loss = -np.log(pos_sim / neg_sim) return np.mean(loss) class SimCLR: def __init__(self, encoder, projection_dim=128): self.encoder = encoder self.projection_head = np.random.randn(encoder.output_dim, projection_dim) def forward(self, x): features = self.encoder(x) projections = features @ self.projection_head return projections def train(self, data, epochs=100): for _ in range(epochs): loss = self.compute_contrastive_loss(data) self._update_parameters(loss) class MoCo: def __init__(self, encoder, queue_size=65536): self.encoder_q = encoder self.encoder_k = encoder.copy() self.queue = np.random.randn(queue_size, encoder.output_dim) self.queue_ptr = 0 def enqueue(self, keys): batch_size = keys.shape[0] ptr = self.queue_ptr self.queue[ptr:ptr+batch_size] = keys self.queue_ptr = (ptr + batch_size) % self.queue.shape[0]2.2 迁移学习 class TransferLearning: def __init__(self, pretrained_model): self.pretrained_model = pretrained_model def freeze_layers(self, num_layers_to_freeze): for i, layer in enumerate(self.pretrained_model.layers[:-num_layers_to_freeze]): for param in layer.parameters(): param.requires_grad = False def unfreeze_all(self): for param in self.pretrained_model.parameters(): param.requires_grad = True def replace_head(self, num_classes): in_features = self.pretrained_model.classifier.in_features self.pretrained_model.classifier = np.random.randn(in_features, num_classes) class FineTuning: def __init__(self, model, lr=1e-5): self.model = model self.lr = lr def train(self, train_data, val_data, epochs=10): for epoch in range(epochs): for batch in train_data: loss = self._compute_loss(batch) self._update_params(loss) def _compute_loss(self, batch): X, y = batch predictions = self.model(X) return np.mean((predictions - y) ** 2) class DomainAdaptation: def __init__(self, source_model): self.source_model = source_model def align_domains(self, source_data, target_data): source_features = self.source_model.extract_features(source_data) target_features = self.source_model.extract_features(target_data) self._domain_alignment_loss(source_features, target_features)2.3 度量学习 class MetricLearning: def __init__(self, embedding_dim=128): self.embedding_dim = embedding_dim self.projection = np.random.randn(embedding_dim, embedding_dim) def triplet_loss(self, anchor, positive, negative, margin=1.0): anchor_emb = anchor @ self.projection positive_emb = positive @ self.projection negative_emb = negative @ self.projection pos_dist = np.sum((anchor_emb - positive_emb) ** 2, axis=1) neg_dist = np.sum((anchor_emb - negative_emb) ** 2, axis=1) loss = np.maximum(0, pos_dist - neg_dist + margin) return np.mean(loss) class SiameseNetwork: def __init__(self, encoder): self.encoder = encoder def forward(self, x1, x2): emb1 = self.encoder(x1) emb2 = self.encoder(x2) distance = np.sum((emb1 - emb2) ** 2, axis=1) return distance def contrastive_loss(self, x1, x2, y, margin=1.0): distance = self.forward(x1, x2) loss = y * distance + (1 - y) * np.maximum(0, margin - distance) return np.mean(loss)3. 性能对比 3.1 自监督方法对比 方法 预训练效果 微调效果 计算成本 SimCLR 高 高 中 MoCo 高 很高 高 Barlow Twins 中 中 低
3.2 迁移学习策略对比 策略 数据效率 计算成本 适用场景 特征提取 高 低 小数据集 全微调 中 高 大数据集 分层微调 高 中 中等数据集
3.3 度量学习效果 方法 检索准确率 训练难度 适用场景 Triplet Loss 高 高 人脸识别 Siamese 中 低 相似度匹配 ArcFace 很高 中 大规模检索
4. 最佳实践 4.1 表示学习策略选择 def choose_representation_learning(task_type, data_size): if data_size < 1000: return 'transfer_learning' elif task_type == 'computer_vision': return 'contrastive_learning' else: return 'self_supervised' class RepresentationLearningSelector: @staticmethod def select(config): strategies = { 'contrastive': ContrastiveLearning, 'moco': MoCo, 'transfer': TransferLearning, 'metric': MetricLearning } return strategies[config['strategy']](**config.get('params', {}))4.2 迁移学习流程 class TransferLearningWorkflow: def __init__(self): pass def run(self, pretrained_model, target_data, config): print("1. 加载预训练模型...") model = self._load_pretrained(pretrained_model) print("2. 冻结底层网络...") self._freeze_layers(model, config.get('freeze_layers', 5)) print("3. 替换分类头...") self._replace_head(model, config['num_classes']) print("4. 微调...") self._fine_tune(model, target_data) return model5. 总结 表示学习是深度学习的核心:
自监督学习 :无需标注学习特征对比学习 :通过对比学习有用特征迁移学习 :利用预训练模型度量学习 :学习距离度量对比数据如下:
MoCo在视觉任务上表现最好 迁移学习在小数据集上最有效 ArcFace是人脸识别的最佳选择 推荐先尝试迁移学习,再考虑自监督