可解釋性指標

Python 的 Explainable AI

Fouad Trad

Machine Learning Engineer

一致性

  • 評估模型以不同子集訓練時,解釋的穩定性。
  • 一致性低 → 解釋不穩健

顯示資料集被分成兩個子集:subset 1 與 subset 2 的圖片。

Python 的 Explainable AI

一致性

  • 評估模型以不同子集訓練時,解釋的穩定性。
  • 一致性低 → 解釋不穩健

模型在各個子集上分別進行訓練。

Python 的 Explainable AI

一致性

  • 評估模型以不同子集訓練時,解釋的穩定性。
  • 一致性低 → 解釋不穩健

每次在子集上訓練模型後,推得特徵重要度,並計算其餘弦相似度。

Python 的 Explainable AI

用餘弦相似度量一致性

 

 

 

顯示一致性的關鍵數值與意義:1 代表高度一致的解釋。

Python 的 Explainable AI

用餘弦相似度量一致性

 

 

 

顯示一致性的關鍵數值與意義:1 代表高度一致,0 代表沒有一致性。

Python 的 Explainable AI

用餘弦相似度量一致性

 

 

 

顯示一致性的關鍵數值與意義:1 代表高度一致,0 代表沒有一致性,-1 代表解釋相反。

Python 的 Explainable AI

入學資料集

GRE Score TOEFL Score University Rating SOP LOR CGPA Chance of Admit
337 118 4 4.5 4.5 9.65 0.92
324 107 4 4 4.5 8.87 0.76
316 104 3 3 3.5 8 0.72
322 110 3 3.5 2.5 8.67 0.8
314 103 2 2 3 8.21 0.45

 

  • X1y1:資料集的第一部分
  • X2y2:資料集的第二部分
  • model1model2:隨機森林迴歸器
Python 的 Explainable AI

計算一致性

from sklearn.metrics.pairwise import cosine_similarity

explainer1 = shap.TreeExplainer(model1) explainer2 = shap.TreeExplainer(model2)
shap_values1 = explainer1.shap_values(X1) shap_values2 = explainer2.shap_values(X2)
feature_importance1 = np.mean(np.abs(shap_values1), axis=0) feature_importance2 = np.mean(np.abs(shap_values2), axis=0)
consistency = cosine_similarity([feature_importance1], [feature_importance2]) print("Consistency between SHAP values:", consistency)
Consistency between SHAP values: [[0.99706516]]
Python 的 Explainable AI

忠實度(Faithfulness)

  • 評估重要特徵是否影響模型預測。
  • 忠實度低 → 會誤導你對模型推理的信任
  • 對敏感領域很實用

顯示模型對輸入樣本產生原始預測的圖片。

Python 的 Explainable AI

忠實度(Faithfulness)

  • 評估重要特徵是否影響模型預測。
  • 忠實度低 → 會誤導你對模型推理的信任。
  • 對敏感領域很實用。

使用 SHAP 或 LIME 在局部解釋此原始預測。

Python 的 Explainable AI

忠實度(Faithfulness)

  • 評估重要特徵是否影響模型預測。
  • 忠實度低 → 會誤導你對模型推理的信任。
  • 對敏感領域很實用。

將修改後的樣本輸入模型,產生新的預測。

Python 的 Explainable AI

忠實度(Faithfulness)

  • 評估重要特徵是否影響模型預測。
  • 忠實度低 → 會誤導你對模型推理的信任。
  • 對敏感領域很實用。

顯示忠實度公式:新預測與原始預測的差值取絕對值。

Python 的 Explainable AI

計算忠實度

X_instance = X_test.iloc[[0]]

original_prediction = model.predict_proba(X_instance)[0, 1] print(f"Original prediction: {original_prediction}")
Original prediction: 0.43

顯示對所選樣本的 LIME 特徵重要度解釋的圖片。

Python 的 Explainable AI

計算忠實度

X_instance['GRE Score'] = 310  


new_prediction = model.predict_proba(X_instance)[0, 1] print(f"Prediction after perturbing {important_feature}: {new_prediction}")
faithfulness_score = np.abs(original_prediction - new_prediction) print(f"Local Faithfulness Score: {faithfulness_score}")
Prediction after perturbing GRE Score: 0.77

Local Faithfulness Score: 0.34
Python 的 Explainable AI

一起來練習吧!

Python 的 Explainable AI

Preparing Video For Download...