Open pellungrobe opened 10 months ago
Quick fix per questa issue: riscrivere le funzioni fitness_equal e fitness_notequal in neighgen.py come segue:
def fitness_equal(self, x, x1):
feature_similarity_score = 1.0 - cdist(x.reshape(1, -1), x1.reshape(1, -1), metric=self.metric).ravel()[0]
# feature_similarity = feature_similarity_score if feature_similarity_score >= self.eta1 else 0.0
feature_similarity = sigmoid(feature_similarity_score) if feature_similarity_score < 1.0 else 0.0
# feature_similarity = sigmoid(feature_similarity_score)
y = self.bb_predict(x.reshape(1, -1))[0]
y1 = self.bb_predict(x1.reshape(1, -1))[0]
if(y.shape == ()):
y = self.bb_predict(x.reshape(1, -1))
if(y1.shape == ()):
y1 = self.bb_predict(x1.reshape(1, -1))
target_similarity_score = 1.0 - hamming(y, y1)
# target_similarity = target_similarity_score if target_similarity_score >= self.eta2 else 0.0
target_similarity = sigmoid(target_similarity_score)
evaluation = self.alpha1 * feature_similarity + self.alpha2 * target_similarity
return evaluation,
def fitness_notequal(self, x, x1):
feature_similarity_score = 1.0 - cdist(x.reshape(1, -1), x1.reshape(1, -1), metric=self.metric).ravel()[0]
# feature_similarity = feature_similarity_score if feature_similarity_score >= self.eta1 else 0.0
feature_similarity = sigmoid(feature_similarity_score)
y = self.bb_predict(x.reshape(1, -1))[0]
y1 = self.bb_predict(x1.reshape(1, -1))[0]
if(y.shape == ()):
y = self.bb_predict(x.reshape(1, -1))
if(y1.shape == ()):
y1 = self.bb_predict(x1.reshape(1, -1))
target_similarity_score = 1.0 - hamming(y, y1)
# target_similarity = target_similarity_score if target_similarity_score < self.eta2 else 0.0
target_similarity = 1.0 - sigmoid(target_similarity_score)
evaluation = self.alpha1 * feature_similarity + self.alpha2 * target_similarity
return evaluation,
Attenzione: testato con modello RandomForest di sklearn, non ho fatto ulteriori test con altri modelli
Anche clonando manualmente il codice in locale, si ottiene il seguente errore:
explainer = LoreTabularExplainer(bbox) config = {'neigh_type':'rndgen', 'size':1000, 'ocr':0.1, 'ngen':10} explainer.fit(df, target, config) exp = explainer.explain(inst)
ValueError Traceback (most recent call last) in <cell line: 4>()
2 config = {'neigh_type':'rndgen', 'size':1000, 'ocr':0.1, 'ngen':10}
3 explainer.fit(df, target, config)
----> 4 exp = explainer.explain(inst)
5 print(exp)
8 frames /content/XAI-course_2021/Tabular/xailib/explainers/lore_explainer.py in explain(self, x) 165 166 def explain(self, x): --> 167 exp = self.lore_explainer.explain_instance(x, samples=1000, use_weights=True) 168 return LoreTabularExplanation(exp)
/content/XAI-course_2021/Tabular/externals/lore/lore/lorem.py in explain_instance(self, x, samples, use_weights, metric) 68 if self.verbose: 69 print('generating neighborhood - %s' % self.neigh_type) ---> 70 Z = self.neighgen_fn(x, samples) 71 else: 72 Z = samples
/content/XAI-course_2021/Tabular/externals/lore/lore/neighgen.py in generate(self, x, num_samples) 344 345 def generate(self, x, num_samples=1000): --> 346 Zg = GeneticGenerator.generate(self, x, num_samples // 2) 347 Zr = RandomGenerator.generate(self, x, num_samples // 2) 348 Z = np.concatenate((Zg, Zr[1:]), axis=0)
/content/XAI-course_2021/Tabular/externals/lore/lore/neighgen.py in generate(self, x, num_samples) 133 134 toolbox_eq = self.setup_toolbox(x, self.fitness_equal, num_samples_eq) --> 135 population_eq, halloffame_eq, logbook_eq = self.fit(toolbox_eq, num_samples_eq) 136 Z_eq = self.add_halloffame(population_eq, halloffame_eq) 137 # print(logbook_eq)
/content/XAI-course_2021/Tabular/externals/lore/lore/neighgen.py in fit(self, toolbox, population_size) 226 stats.register("max", np.max) 227 --> 228 population, logbook = algorithms.eaSimple(population, toolbox, cxpb=self.cxpb, mutpb=self.mutpb, 229 ngen=self.ngen, stats=stats, halloffame=halloffame, 230 verbose=self.verbose)
/usr/local/lib/python3.10/dist-packages/deap/algorithms.py in eaSimple(population, toolbox, cxpb, mutpb, ngen, stats, halloffame, verbose) 149 invalid_ind = [ind for ind in population if not ind.fitness.valid] 150 fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) --> 151 for ind, fit in zip(invalid_ind, fitnesses): 152 ind.fitness.values = fit 153
/content/XAI-course_2021/Tabular/externals/lore/lore/neighgen.py in fitness_equal(self, x, x1) 259 y1 = self.bb_predict(x1.reshape(1, -1))[0] 260 --> 261 target_similarity_score = 1.0 - hamming(y, y1) 262 # target_similarity = target_similarity_score if target_similarity_score >= self.eta2 else 0.0 263 target_similarity = sigmoid(target_similarity_score)
/usr/local/lib/python3.10/dist-packages/scipy/spatial/distance.py in hamming(u, v, w) 729 730 """ --> 731 u = _validate_vector(u) 732 v = _validate_vector(v) 733 if u.shape != v.shape:
/usr/local/lib/python3.10/dist-packages/scipy/spatial/distance.py in _validate_vector(u, dtype) 316 if u.ndim == 1: 317 return u --> 318 raise ValueError("Input vector should be 1-D.") 319 320
ValueError: Input vector should be 1-D.