跳轉到

3.7 Autoencoder Anomaly Detection

範例程式:Open In Colab

異常偵測常見於設備監控、交易風險、資安事件、品質檢測與感測器資料。這類任務最大的困難是:異常樣本通常很少,而且不一定有完整標籤。Autoencoder 提供了一個實用 baseline:只學正常資料長什麼樣子,再用重建誤差判斷新資料是否異常。

1. Autoencoder 的直覺

Autoencoder 包含 encoder 和 decoder:

input → encoder → latent representation → decoder → reconstructed input

模型的訓練目標不是預測 label,而是重建原始輸入。如果模型只看過正常資料,它會比較擅長重建正常樣本;遇到異常樣本時,重建誤差通常會變大。

2. 建立正常與異常資料

Notebook 使用 make_blobs() 產生正常資料,再用均勻分布建立異常資料。訓練時只使用正常資料:

x_train_normal = x_train[y_train == 0]

這是 autoencoder anomaly detection 的核心假設。

3. 建立 Autoencoder

Encoder 將 10 維輸入壓縮到較小的 latent representation,Decoder 再把 latent representation 重建回原始維度。模型訓練時,輸入和目標是一樣的:

autoencoder.fit(x_train_normal, x_train_normal)

4. Reconstruction Error

訓練完成後,計算每筆資料的重建誤差:

recon = autoencoder.predict(x_test)
error = np.mean(np.square(x_test - recon), axis=1)

分數越高,代表越不像模型學到的正常模式。

5. Threshold

Notebook 使用正常資料重建誤差的第 95 百分位當 threshold:

threshold = np.percentile(train_error, 95)

正式專案中,threshold 要依照誤報成本與漏報成本調整。

6. 如何套用自己的資料?

準備正常資料,整理數值特徵並標準化,只用正常資料訓練 autoencoder,再用少量已知異常資料輔助選 threshold。若不同設備或不同產線的正常分布差很多,建議分設備建立模型。

7. 小結

Autoencoder anomaly detection 適合正常資料多、異常標籤少的情境。它的核心不是分類,而是學會重建正常資料,再用 reconstruction error 建立異常分數。