測試模型

Developing Machine Learning Models for Production

Sinan Ozdemir

Data Scientist, Entrepreneur, and Author

個體 vs 群體的公平性

個體公平

以相似方式對待相似個體。

  • 例如:對經歷相近的人提供相近的工作機會。

群體公平

平等對待不同群體。

  • 例如:在學校錄取模型中,不因種族或族裔而歧視任何人。
Developing Machine Learning Models for Production

留置(Holdout)測試

  • 留置測試以獨立資料集檢查模型可靠性
  • 評估未訓練資料上的表現,與訓練時相似
  • 偵測過度擬合或擬合不足等問題

robots using data

Developing Machine Learning Models for Production

檢查模型漂移

概念漂移(Concept drift)

  • 特徵與回應之間關係的改變
    • 例如:情感分析演算法
      • 詞義可能改變(稱某事「sick」反而表示很好)

預測漂移(Prediction drift)

  • 模型預測分佈的改變
    • 例如:暫時性斷線讓你的聊天機器人中「outage」意圖比例上升
    • 不一定「出錯」,但此漂移可能讓使用者回應時間變慢
Developing Machine Learning Models for Production

檢測模型漂移範例

設定:

# Import necessary libraries and split data
X_train, X_test, y_train, y_test = ...

訓練模型:

# Train a classifier on the training data
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)

# Calculate the accuracy on test data
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")

稍後測試漂移:

X_test_drift = X_test + 1.0  # Simulate data drift
# Calculate the accuracy on drifted data
y_pred_drift = clf.predict(X_test_drift)
accuracy_drift = accuracy_score(
    y_test, y_pred_drift)
print(f"Accuracy with drift: {accuracy_drift}")

# Drift detection threshold based on the accuracy
drift_threshold = accuracy * 0.9

# Check for drop in accuracy on the drifted data
if accuracy_drift < drift_threshold:
    print("Concept drift detected!")
else:
    print("No concept drift detected.")
Developing Machine Learning Models for Production

複雜模型 vs 基準模型的成本

  • 複雜模型成本更高,且可能影響效率
  • 延遲(Latency):處理單一輸入並產生預測所需時間
  • 吞吐量(Throughput):模型在給定時間內可產生的預測數
  • 測試延遲與吞吐量以判斷模型所需的複雜度
  • 目標是在準確度與效率間取得平衡
Developing Machine Learning Models for Production

一起來練習吧!

Developing Machine Learning Models for Production

Preparing Video For Download...