嵌入原则:数据特征如何 融入 模型的 损失地形
第一节:嵌入原则的基本概念与公式解释
机器学习中的嵌入原则,就像 “雕刻师” 将 “石块的纹理” 逐渐融入到 “雕塑的造型” 中。数据特征不再是独立的输入,而是被模型 “吸收” 和 “内化”,最终体现在模型的 “损失地形” 上。
核心内容
【嵌入原则的核心思想是,数据特征不是 “外挂” 在模型上的,而是模型 “自身结构” 的一部分。模型通过学习,将数据特征 “编码” 成 “低维向量”,这些向量 “镶嵌” 在模型的参数空间中,共同塑造了模型的 “损失地形”。这个 “损失地形” 的 “坡度” 和 “谷底”,直接决定了模型的 “学习方向” 和 “最终性能”。】
嵌入函数的基本公式
嵌入过程可以用一个嵌入函数 E E E 来表示,它将原始数据特征 x x x 映射到一个低维的嵌入向量 e e e。
e = E ( x ; W e ) e = E(x; W_e) e=E(x;We)
变量解释:
- e e e:嵌入向量,低维空间中数据特征的表示。
- E E E:嵌入函数,通常是一个神经网络层(如线性层、全连接层)。
- x x x:原始数据特征,模型的输入。
- W e W_e We:嵌入函数的参数,例如嵌入层的权重矩阵。
具体实例与推演【【通俗讲解,打比方来讲解!】】
以文本情感分类为例,理解嵌入原则的应用。
-
步骤:
- 原始文本输入:例如句子 “这部电影真棒!”。
- 特征提取:将文本转换为词向量,例如使用 Word2Vec 或 GloVe 预训练的词向量。假设 “真棒” 这个词的词向量为 v awesome v_\text{awesome} vawesome。
- 嵌入层:模型包含一个嵌入层 E E E,将词向量 v awesome v_\text{awesome} vawesome 作为输入,通过学习得到一个新的嵌入向量 e awesome = E ( v awesome ; W e ) e_\text{awesome} = E(v_\text{awesome}; W_e) eawesome=E(vawesome;We)。
- 损失函数:情感分类任务的损失函数(如交叉熵损失)会根据模型的预测情感和真实情感计算损失值。
- 梯度下降:梯度下降算法会根据损失值,调整模型参数(包括嵌入层参数 W e W_e We),使得模型能够更好地将 “真棒” 这类词语的嵌入向量与 “积极情感” 关联起来。
-
应用公式:
假设嵌入函数 E E E 是一个简单的线性变换: E ( x ; W e ) = W e x E(x; W_e) = W_e x E(x;We)=Wex。如果词向量 v awesome = [ 0.2 , 0.5 , − 0.1 ] v_\text{awesome} = [0.2, 0.5, -0.1] vawesome=[0.2,0.5,−0.1],嵌入矩阵 W e W_e We 在训练过程中不断更新,使得 e awesome = W e v awesome e_\text{awesome} = W_e v_\text{awesome} eawesome=Wevawesome 能够更好地帮助模型进行情感分类。
第二节:损失景观与特征融入
损失函数与损失景观
损失函数 L ( y ^ , y ) L(\hat{y}, y) L(y^,y) 度量了模型预测 y ^ \hat{y} y^ 与真实标签 y y y 之间的差异。损失景观可以理解为模型参数空间上的一个 “地形图”,高度表示损失值,“山峰” 代表损失值高,“山谷” 代表损失值低。
L ( θ ) = 1 N ∑ i = 1 N L ( f ( x i ; θ ) , y i ) \mathcal{L}(\theta) = \frac{1}{N} \sum_{i=1}^{N} L(f(x_i; \theta), y_i) L(θ)=N1i=1∑NL(f(xi;θ),yi)
变量解释:
- L ( θ ) \mathcal{L}(\theta) L(θ):损失景观函数,表示模型参数 θ \theta θ 对应的平均损失值。
- θ \theta θ:模型参数集合,包括嵌入层参数 W e W_e We 和其他模型层的参数。
- N N N:训练样本数量。
- L L L:单个样本的损失函数。
- f ( x i ; θ ) f(x_i; \theta) f(xi;θ):模型函数,输入 x i x_i xi,参数为 θ \theta θ,输出预测值。
- y i y_i yi:第 i i i 个样本的真实标签。
特征融入损失景观的过程
嵌入原则的核心在于,数据特征通过嵌入函数 E E E 参与到损失函数的计算中,并最终 “塑造” 了损失景观。
- 特征影响预测:嵌入向量 e = E ( x ; W e ) e = E(x; W_e) e=E(x;We) 作为模型的输入,直接影响模型的预测结果 y ^ = f ′ ( e ; θ ′ ) \hat{y} = f'(e; \theta') y^=f′(e;θ′),其中 f ′ f' f′ 是模型主体部分, θ ′ \theta' θ′ 是模型主体部分的参数。
- 预测影响损失:预测结果 y ^ \hat{y} y^ 与真实标签 y y y 共同决定了损失值 L ( y ^ , y ) L(\hat{y}, y) L(y^,y)。
- 损失驱动学习:梯度下降算法根据损失值 L ( θ ) \mathcal{L}(\theta) L(θ) 的梯度,更新模型参数 θ = [ W e , θ ′ ] \theta = [W_e, \theta'] θ=[We,θ′],包括嵌入层参数 W e W_e We。
- 特征融入景观:随着训练的进行,嵌入层参数 W e W_e We 不断调整,使得嵌入向量 e e e 能够更好地反映数据特征 x x x,从而 “优化” 损失景观,使其 “山谷” 更深更广,“山峰” 更矮更平缓。
第三节:公式探索与推演运算
损失函数的选择与影响
不同的损失函数会塑造不同的损失景观,从而影响特征融入的方式和模型的学习效果。常见的损失函数包括:
-
均方误差损失 (MSE):常用于回归任务。
L MSE ( y ^ , y ) = 1 2 ( y ^ − y ) 2 L_\text{MSE}(\hat{y}, y) = \frac{1}{2} (\hat{y} - y)^2 LMSE(y^,y)=21(y^−y)2
变量解释:
- L MSE L_\text{MSE} LMSE:均方误差损失值。
- y ^ \hat{y} y^:模型预测值。
- y y y:真实标签值。
-
交叉熵损失 (Cross-Entropy):常用于分类任务。
L CE ( y ^ , y ) = − ∑ c = 1 C y c log ( y ^ c ) L_\text{CE}(\hat{y}, y) = - \sum_{c=1}^{C} y_c \log(\hat{y}_c) LCE(y^,y)=−c=1∑Cyclog(y^c)
变量解释:
- L CE L_\text{CE} LCE:交叉熵损失值。
- C C C:类别数量。
- y c y_c yc:真实标签的 one-hot 编码,类别 c c c 为 1,其余为 0。
- y ^ c \hat{y}_c y^c:模型预测的样本属于类别 c c c 的概率。
-
对比损失 (Contrastive Loss):常用于学习相似性度量和嵌入表示。
L Contrastive ( e i , e j , l i j ) = l i j d ( e i , e j ) 2 + ( 1 − l i j ) max ( 0 , m − d ( e i , e j ) ) 2 L_\text{Contrastive}(e_i, e_j, l_{ij}) = l_{ij} d(e_i, e_j)^2 + (1 - l_{ij}) \max(0, m - d(e_i, e_j))^2 LContrastive(ei,ej,lij)=lijd(ei,ej)2+(1−lij)max(0,m−d(ei,ej))2
变量解释:
- L Contrastive L_\text{Contrastive} LContrastive:对比损失值。
- e i , e j e_i, e_j ei,ej:样本 i i i 和 j j j 的嵌入向量。
- l i j l_{ij} lij:标签,若样本 i i i 和 j j j 相似则为 1,不相似则为 0。
- d ( e i , e j ) d(e_i, e_j) d(ei,ej):嵌入向量 e i e_i ei 和 e j e_j ej 之间的距离度量(如欧氏距离)。
- m m m:边界值 (margin),用于控制不相似样本之间的最小距离。
梯度下降与损失景观优化
梯度下降算法是优化损失景观的关键。其迭代更新公式为:
θ t + 1 = θ t − η ∇ L ( θ t ) \theta_{t+1} = \theta_t - \eta \nabla \mathcal{L}(\theta_t) θt+1=θt−η∇L(θt)
变量解释:
- θ t + 1 \theta_{t+1} θt+1:第 t + 1 t+1 t+1 次迭代的模型参数。
- θ t \theta_t θt:第 t t t 次迭代的模型参数。
- η \eta η:学习率,控制参数更新的步长。
- ∇ L ( θ t ) \nabla \mathcal{L}(\theta_t) ∇L(θt):损失景观函数在 θ t \theta_t θt 处的梯度,指示损失值下降最快的方向。
梯度下降算法就像 “登山者” 在 “损失地形” 上寻找 “最低点”。通过不断迭代,模型参数 θ \theta θ 沿着梯度方向移动,最终到达损失景观的 “谷底”,此时模型达到最优状态。
公式推导
对比损失公式的理解:
对比损失公式旨在学习到一种嵌入表示,使得相似的样本在嵌入空间中距离较近,不相似的样本距离较远。
- 相似样本 ( l i j = 1 l_{ij} = 1 lij=1):损失函数变为 L Contrastive = d ( e i , e j ) 2 L_\text{Contrastive} = d(e_i, e_j)^2 LContrastive=d(ei,ej)2,目标是缩小相似样本的嵌入向量距离 d ( e i , e j ) d(e_i, e_j) d(ei,ej)。
- 不相似样本 ( l i j = 0 l_{ij} = 0 lij=0):损失函数变为 L Contrastive = max ( 0 , m − d ( e i , e j ) ) 2 L_\text{Contrastive} = \max(0, m - d(e_i, e_j))^2 LContrastive=max(0,m−d(ei,ej))2,目标是增大不相似样本的嵌入向量距离 d ( e i , e j ) d(e_i, e_j) d(ei,ej),至少要大于边界值 m m m。
通过这种方式,对比损失能够有效地引导模型学习到区分相似性和不相似性的嵌入表示,从而将数据特征融入到损失景观中。
第四节:相似公式比对
公式/概念 | 共同点 | 不同点 |
---|---|---|
e = E ( x ; W e ) e = E(x; W_e) e=E(x;We) (嵌入函数) | 将原始特征映射到低维空间 | 具体实现方式不同,可以是线性层、非线性层等 |
L ( θ ) = 1 N ∑ i = 1 N L ( f ( x i ; θ ) , y i ) \mathcal{L}(\theta) = \frac{1}{N} \sum_{i=1}^{N} L(f(x_i; \theta), y_i) L(θ)=N1∑i=1NL(f(xi;θ),yi) (损失景观) | 度量模型性能,指导模型学习 | 具体损失函数 L L L 不同,适用于不同任务 |
L MSE ( y ^ , y ) = 1 2 ( y ^ − y ) 2 L_\text{MSE}(\hat{y}, y) = \frac{1}{2} (\hat{y} - y)^2 LMSE(y^,y)=21(y^−y)2 (MSE 损失) | 回归任务常用损失函数 | 对预测值和真实值之差的平方敏感 |
L CE ( y ^ , y ) = − ∑ c = 1 C y c log ( y ^ c ) L_\text{CE}(\hat{y}, y) = - \sum_{c=1}^{C} y_c \log(\hat{y}_c) LCE(y^,y)=−∑c=1Cyclog(y^c) (交叉熵损失) | 分类任务常用损失函数 | 度量预测概率分布与真实分布的差异 |
L Contrastive ( e i , e j , l i j ) L_\text{Contrastive}(e_i, e_j, l_{ij}) LContrastive(ei,ej,lij) (对比损失) | 学习相似性度量和嵌入表示 | 针对样本对,鼓励相似样本嵌入靠近,不相似样本嵌入远离 |
第五节:核心代码与可视化
以下 Python 代码演示了如何使用 PyTorch
构建一个简单的模型,包含一个嵌入层,并使用 MNIST 数据集进行训练,可视化嵌入向量的分布,以及损失景观的简化表示。
# This code performs the following functions:
# 1. Defines a simple neural network model with an embedding layer for MNIST digit classification.
# 2. Trains the model on the MNIST dataset.
# 3. Visualizes the embeddings of the MNIST digits in a 2D space using PCA.
# 4. Visualizes the loss landscape (simplified 1D representation) during training.
# 5. Enhances visualizations with seaborn aesthetics and matplotlib annotations.
# 6. Outputs intermediate data and visualizations for analysis and debugging.import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from torch.utils.data import DataLoader# 1. Define the Model with Embedding Layer
class EmbeddingModel(nn.Module):def __init__(self, embedding_dim=2, num_classes=10):super(EmbeddingModel, self).__init__()self.embedding = nn.Embedding(10, embedding_dim) # Embedding layer for digits 0-9 (one-hot encoded implicitly)self.fc = nn.Linear(embedding_dim, num_classes) # Linear layer for classificationdef forward(self, x):embedded = self.embedding(x) # Get embedding for input digit indexoutput = self.fc(embedded) # Classification layerreturn output# 2. Load MNIST Dataset and Data Loader
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) # MNIST normalization
train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)# 3. Initialize Model, Loss Function, and Optimizer
model = EmbeddingModel()
criterion = nn.CrossEntropyLoss() # Cross-entropy loss for classification
optimizer = optim.Adam(model.parameters(), lr=0.01) # Adam optimizer# 4. Training Loop and Loss Tracking
epochs = 10
losses = [] # List to store loss values during trainingfor epoch in range(epochs):running_loss = 0.0for i, data in enumerate(train_loader, 0):inputs, labels = datainputs = labels # Use labels as input indices for embedding layer (simplified example for embedding visualization)optimizer.zero_grad() # Zero gradientsoutputs = model(inputs) # Forward passloss = criterion(outputs, labels) # Calculate lossloss.backward() # Backpropagationoptimizer.step() # Update weightsrunning_loss += loss.item()epoch_loss = running_loss / len(train_loader) # Average loss per epochlosses.append(epoch_loss) # Store epoch lossprint(f'Epoch {epoch+1}, Loss: {epoch_loss:.4f}')print('Finished Training')# 5. Visualize Embeddings using PCA
digit_indices = torch.arange(10) # Indices for digits 0-9
embeddings = model.embedding(digit_indices).detach().numpy() # Get embeddings for digits
pca = PCA(n_components=2) # PCA for 2D visualization
embeddings_pca = pca.fit_transform(embeddings) # Reduce embedding dimensionalityplt.figure(figsize=(8, 6))
sns.scatterplot(x=embeddings_pca[:, 0], y=embeddings_pca[:, 1], hue=np.arange(10), palette=sns.color_palette("tab10", 10), s=100) # Scatter plot of embeddings
plt.title('2D Embedding Visualization of MNIST Digits (PCA)', fontsize=14)
plt.xlabel('PCA Component 1', fontsize=12)
plt.ylabel('PCA Component 2', fontsize=12)
for i in range(10):plt.annotate(str(i), xy=(embeddings_pca[i, 0], embeddings_pca[i, 1]), xytext=(embeddings_pca[i, 0]+0.02, embeddings_pca[i, 1]+0.02), fontsize=10, color='black') # Annotate points with digit labels
plt.grid(True, linestyle='--', alpha=0.6)
plt.legend(title='Digits', loc='upper right')
plt.tight_layout()
plt.show()# 6. Visualize Loss Landscape (Simplified 1D - Loss Curve)
plt.figure(figsize=(8, 5))
plt.plot(range(1, epochs + 1), losses, marker='o', linestyle='-', color='skyblue', linewidth=2) # Line plot of loss curve
plt.title('Loss Landscape (Simplified 1D - Loss Curve)', fontsize=14)
plt.xlabel('Epoch', fontsize=12)
plt.ylabel('Loss', fontsize=12)
plt.grid(True, linestyle=':', alpha=0.7)
plt.annotate(f'Final Loss: {losses[-1]:.4f}', xy=(epochs, losses[-1]), xytext=(epochs-2, losses[-1]+0.1),arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"), fontsize=10, color='darkgreen') # Annotation 1
plt.axhline(y=min(losses), color='red', linestyle='--', linewidth=1, label=f'Minimum Loss: {min(losses):.4f}') # Highlight 1
plt.legend(loc='upper right')
plt.tight_layout()
plt.show()# 7. Output Intermediate Data and Information
print("\n--- Embedding Vectors (Digit 0 to 9) ---")
print(embeddings)
print("\n--- PCA Reduced Embeddings (First 5) ---")
print(embeddings_pca[:5])
print("\n--- Loss Values per Epoch ---")
print(losses)
输出内容 | 描述 |
---|---|
嵌入向量 (数字 0 到 9) | 显示模型学习到的数字 0 到 9 的嵌入向量,展示特征在低维空间的表示。 |
PCA 降维后的嵌入向量 (前 5 个) | 输出使用 PCA 降维到 2D 后的前 5 个嵌入向量,用于可视化展示。 |
每轮训练的损失值 | 显示每轮训练的平均损失值,用于观察损失景观的下降趋势。 |
MNIST 数字 2D 嵌入可视化散点图 | 可视化展示 MNIST 数字的嵌入向量在 2D 空间中的分布,颜色区分不同数字,观察特征聚类情况。 |
损失景观简化 1D 表示折线图 (损失曲线) | 绘制损失曲线,展示训练过程中损失值随 epoch 变化的趋势,简化表示损失景观的下降过程。 |
代码功能实现:
- 构建带嵌入层的模型:定义一个包含嵌入层的简单神经网络模型,用于 MNIST 数字分类。
- MNIST 数据集训练:使用 MNIST 数据集训练模型,学习数字的嵌入表示。
- 嵌入向量可视化:使用 PCA 将高维嵌入向量降维到 2D,并绘制散点图可视化数字的嵌入分布。
- 损失景观简化可视化:绘制损失曲线,展示训练过程中损失值的变化,简化表示损失景观的优化过程。
- 输出中间数据:输出嵌入向量、PCA 降维后的嵌入向量和损失值,方便分析和调试。
第六节:参考信息源
-
深度学习与嵌入表示:
- Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press. (第 5 章:Representation Learning)
- LeCun, Y., Bengio, Y., & Hinton, G. (2015). Deep learning. Nature, 521(7553), 436-444.
-
损失景观与优化:
- Li, H., Xu, Z., Taylor, G., Studer, C., & Goldstein, T. (2018). Visualizing the Loss Landscape of Neural Nets. Advances in Neural Information Processing Systems, 31.
- Choromanska, A., Bachmann, P., Lossilla, D., Cremers, D., & Rackauckas, C. (2015). Open Problem: The Landscape of Deep Learning Networks. ArXiv Preprint ArXiv:1412.8776.
-
嵌入技术应用:
- Mikolov, T., Chen, K., Corrado, G., & Dean, J. (2013). Efficient Estimation of Word Representations in Vector Space. ArXiv Preprint ArXiv:1301.3781. (Word2Vec)
- Pennington, J., Socher, R., & Manning, C. D. (2014). GloVe: Global Vectors for Word Representation. Proceedings of the 2014 Conference on Empirical Methods in Natural Language Processing (EMNLP). (GloVe)
参考文献链接:
- Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.
- LeCun, Y., Bengio, Y., & Hinton, G. (2015). Deep learning. Nature, 521(7553), 436-444.
- Li, H., Xu, Z., Taylor, G., Studer, C., & Goldstein, T. (2018). Visualizing the Loss Landscape of Neural Nets. Advances in Neural Information Processing Systems, 31.
- Choromanska, A., Bachmann, P., Lossilla, D., Cremers, D., & Rackauckas, C. (2015). Open Problem: The Landscape of Deep Learning Networks. ArXiv Preprint ArXiv:1412.8776.
- Mikolov, T., Chen, K., Corrado, G., & Dean, J. (2013). Efficient Estimation of Word Representations in Vector Space. ArXiv Preprint ArXiv:1301.3781.
- Pennington, J., Socher, R., & Manning, C. D. (2014). GloVe: Global Vectors for Word Representation. Proceedings of the 2014 Conference on Empirical Methods in Natural Language Processing (EMNLP).
关键词:
#嵌入原则
#数据特征
#损失景观
#嵌入函数
#损失函数
#梯度下降
#特征融入
#嵌入可视化
#损失景观可视化
#表示学习