Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jpang625 authored Jun 2, 2024
1 parent d4f0c55 commit 44b69a9
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions opensv/data_shapley/solvers/improved_kernel_shap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from typing import Optional, Any

import numpy as np
from tqdm import trange
from scipy.special import comb

from opensv.utils.utils import get_utility

Array = np.ndarray

def improved_kernel_shap(
x_train: Array,
y_train: Array,
x_valid: Optional[Array] = None,
y_valid: Optional[Array] = None,
clf: Optional[Any] = None,
num_perm: int=500,
) -> Array:
N = len(y_train)
val = np.zeros(N)
idxes = list(range(N))
utilities = np.empty(num_perm)
coef = np.empty(num_perm)

coalitons = np.zeros((num_perm,N))
weight_list = np.array([1 / (cl * (N-cl)) for cl in range(1,N)])
coef_list = np.array([(N-1) / (comb(N,cl)* cl * (N-cl)) for cl in range(1,N)])

final_acc = get_utility(x_train, y_train, x_valid, y_valid, clf)
init_acc = get_utility([], [], x_valid, y_valid, clf )

for _ in range(num_perm/2):
np.random.shuffle(idxes)
i = np.random.choice(a=range(1, N), p=np.array(weight_list) / np.sum(weight_list))
x_temp, y_temp = x_train[idxes[:i], :], y_train[idxes[:i]]
utilities[2*_] = get_utility(x_temp, y_temp, x_valid, y_valid, clf)
coef[2*_] = coef_list[i-1]
coalitons[2*_,idxes[:i]] = 1
x_temp, y_temp = x_train[idxes[i:], :], y_train[idxes[i:]]
utilities[2*_+1] = get_utility(x_temp, y_temp, x_valid, y_valid, clf)
coef[2*_+1] = coef_list[i-1]
coalitons[2*_+1,idxes[i:]] = 1

A = np.einsum('ij,ik->jk', coalitons, coalitons) / (num_perm/2)
b = np.sum(coalitons * (utilities - init_acc)[:, np.newaxis], axis=0) / (num_perm/2)

Ainv = np.linalg.inv(A)
val = Ainv @ (b - np.ones(N)*(np.sum(Ainv @ b) - final_acc + init_acc)/np.sum(Ainv))
return val

0 comments on commit 44b69a9

Please sign in to comment.