def weight_diff(self, x_tensor, model, clss, clss_num): set_random_seed() def predict_fun(x): rst = model.forward(x) return rst weight_diff, samples_tensor = self.explainer.explain_instance( x_tensor, predict_fun, clss, labels=list(range(clss_num))) return weight_diff, self.dist, samples_tensor
def weight_diff(self, x_tensor, model, clss, clss_num): set_random_seed() dist = 1 w_diff_est_tensor = None samples_tensor = None counter = 0 error = None while dist > self.threshold: counter += 1 try: samples_tensor = unif_sample(x_tensor, self.sample_num, dist) w_diff_est_tensor = reverse(x_tensor, model, samples_tensor, clss) break except Exception as e: error = e dist /= 2.0 logger.debug("Counter: {}".format(counter)) if w_diff_est_tensor is None: logger.exception(error) raise Exception( "Due to the limitations of python's computing error, we cannot estimate the gradient of x given the distance threshold." ) return w_diff_est_tensor, dist, samples_tensor
import torch import matplotlib matplotlib.use('agg') from matplotlib.backends.backend_pdf import PdfPages from openapi.exp.abs_exp import AbsExp from openapi.utils import set_random_seed, PathManager, Plot from openapi.log import getLogger from openapi import config ###################################################################### # LMT related module must be imported to make the pickle load successfully from openapi.mdl.lmt import LinearModelTree, Node, LocalLasso ###################################################################### set_random_seed() logger = getLogger(__name__) class Exp2(AbsExp): def __init__(self, model_name, dataset, expln_name, datasize): AbsExp.__init__(self, model_name, dataset, "2", expln_name, datasize) @staticmethod def plot(model_name, dataset, datasize): exp = "2" path_manager = PathManager(model_name, dataset) dist = defaultdict(list) for expl_name in AbsExp.get_method_names(): result_file = path_manager.result_json_path(