嵌入原則:數據特征如何 融入 模型的 損失地形
第一節:嵌入原則的基本概念與公式解釋
機器學習中的嵌入原則,就像 “雕刻師” 將 “石塊的紋理” 逐漸融入到 “雕塑的造型” 中。數據特征不再是獨立的輸入,而是被模型 “吸收” 和 “內化”,最終體現在模型的 “損失地形” 上。
核心內容
【嵌入原則的核心思想是,數據特征不是 “外掛” 在模型上的,而是模型 “自身結構” 的一部分。模型通過學習,將數據特征 “編碼” 成 “低維向量”,這些向量 “鑲嵌” 在模型的參數空間中,共同塑造了模型的 “損失地形”。這個 “損失地形” 的 “坡度” 和 “谷底”,直接決定了模型的 “學習方向” 和 “最終性能”。】
嵌入函數的基本公式
嵌入過程可以用一個嵌入函數 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?)=We?x。如果詞向量 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?=We?vawesome? 能夠更好地幫助模型進行情感分類。
第二節:損失景觀與特征融入
損失函數與損失景觀
損失函數 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(θ)=N1?i=1∑N?L(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∑C?yc?log(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?)=lij?d(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=1N?L(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=1C?yc?log(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).
關鍵詞:
#嵌入原則
#數據特征
#損失景觀
#嵌入函數
#損失函數
#梯度下降
#特征融入
#嵌入可視化
#損失景觀可視化
#表示學習