def main(): input_file = "files/readyToGo.csv" data_file = "files/cache/data.pkl" labels_file = "files/cache/labels.pkl" features_file = "files/cache/features.pkl" model_file = "files/cache/model.pkl" try: data = pickle.load(open(data_file, "rb")) labels = pickle.load(open(labels_file, "rb")) features = pickle.load(open(features_file, "rb")) except IOError: data, labels, features = read_csv(input_file) pickle.dump(data, open(data_file, "wb")) pickle.dump(labels, open(labels_file, "wb")) pickle.dump(features, open(features_file, "wb")) try: model = pickle.load(open(model_file, "rb")) except IOError: print("Start fit") # model = LogisticRegression() model = RandomForestClassifier(random_state=0) model.fit(data, labels) print("Finish fitting") pickle.dump(model, open(model_file, "wb")) top_obs = 1000 data = data[:top_obs, :] labels = labels[:top_obs] explainer = Explainer(model.predict_proba, 0.5) explanations = explainer.explain(data) scores = model.predict_proba(data)[:, 1] export_explanations(explanations, labels, scores, features)
def _create_explainer(self, features, df, target_type): feature_names = list(features.keys()) train_dataset, training_labels = self.train_csv_reader.make_numpy_array(self.train_csv_reader.label_name, include_features=feature_names, numerical_labels=False) df = df[feature_names] categorical_features = [i for i in range(len(df.columns)) if df[df.columns[i]].dtype == 'object'] mode = 'regression' if target_type == 'numerical' else 'classification' categorical_names = {k: df[feature_names[k]].unique() for k in categorical_features} return Explainer(train_dataset, training_labels, feature_names, self.label_unique_values, categorical_features, categorical_names, mode)
def run_main(): global is_running if not is_running: is_running = True image_path = '' if bool(is_url.get()): image_path = url_entry.get() else: image_path = askopenfilename() if image_path != '' and type(image_path) != tuple: image_path = image_path.strip() labelTop.configure(text="Running ...") labelTop.update_idletasks() prediction = Explainer(comboExample.get(), models).main(image_path, bool(is_url.get())) labelTop.configure(text=prediction) is_running = False
def q_learning_table_agent(env, num_episodes=500, learning_rate=0.9, discount=0.95): # remember rewards for each step of each episode explainer = Explainer() # create reward table # that associates conditions, actions and rewards reward_table = np.zeros((5, 2)) # run episodes for episode in range(num_episodes): # create new explainer episode explainer.new_episode(episode) # reset env and get starting state state = env.reset() # run until env is finished done = False while not done: # get previous action prev_reward = reward_table[state, :] # if no previous record, choose random action if sum(prev_reward) == 0: action = np.random.randint(0, 2) # choose action with best reward else: action = np.argmax(prev_reward) # perform step new_state, reward, done, _ = env.step(action) # save reward # reward_table[state, action] += reward # Q(s, a) = Q(s, a) + a*(r + y * max(Q(s', a')) - Q(s, a)) reward_table[state, action] += \ reward + learning_rate * \ (discount * max(reward_table[new_state, :]) - reward_table[state, action]) # update state state = new_state # save stats explainer.save(episode, reward) return reward_table, explainer
def naive_sum_reward_agent(env, num_episodes=500): # remember rewards for each step of each episode explainer = Explainer() # create reward table # that associates conditions, actions and rewards reward_table = np.zeros((5, 2)) # run episodes for episode in range(num_episodes): # create new explainer episode explainer.new_episode(episode) # reset env and get starting state state = env.reset() # run until env is finished done = False while not done: # get previous action prev_reward = reward_table[state] # if no previous record, choose random action if sum(prev_reward) == 0: action = np.random.randint(0, 2) # choose action with best reward else: action = np.argmax(prev_reward) # perform step new_state, reward, done, _ = env.step(action) # save reward reward_table[state, action] += reward # update state state = new_state # save stats explainer.save(episode, reward) return reward_table, explainer
logging.info("Loaded rule predictions from %s" % (args.rule_pred)) if (len(rule_predictions) != len(mapped_data)): logging.error("Unequal length of rule predictions and data") exit(-1) entity_inverse_map = utils.get_inverse_dict(distmult_dump['entity_to_id']) relation_inverse_map = utils.get_inverse_dict( distmult_dump['relation_to_id']) template_objs = template_builder.template_obj_builder( data_root, args.model_weights, args.template_load_dir, None, "distmult", args.t_ids, True) explainer = Explainer(data_root, template_objs[0].kb, template_objs[0].base_model, entity_inverse_map, relation_inverse_map, list_of_different_template_files=['template1.txt']) if (args.template_pred is not None): template_exps = english_exp_template(mapped_data, template_predictions, template_objs, explainer) else: template_exps = [ explainer.NO_EXPLANATION for _ in range(len(mapped_data)) ] if (args.rule_pred is not None): rule_exps = english_exp_rules(mapped_data, rule_predictions, explainer) else: rule_exps = [explainer.NO_EXPLANATION for _ in range(len(mapped_data))]
hardModel = wrn28_10_cifar10() hardModel = HardNBDT(pretrained=True, dataset='CIFAR10', arch='wrn28_10_cifar10', model=hardModel) hardModel = hardModel.cuda() softModel = wrn28_10_cifar10() softModel = SoftNBDT(pretrained=True, dataset='CIFAR10', arch='wrn28_10_cifar10', model=softModel) softModel = softModel.cuda() explainer = Explainer(hardModel, (32, 32), 500) explainer.generate_masks(1000, 8, 0.1, 'temp.npy') klen = 11 ksig = 5 kern = gkern(klen, ksig) blur = lambda x: nn.functional.conv2d(x, kern, padding=klen // 2) insertion = CausalMetric(softModel, 'ins', 32 * 8, substrate_fn=blur, n_classes=10, device=torch.device("cuda")) deletion = CausalMetric(softModel,
count += 1 def print_proof(proof): #print the proove for line in proof: print('forgetting ' + str(line[0])) print(str(line[1])) ############# SCRIPT ################# # set up things ontology = 'datasets/snomed_organism_1.1.owl' all_subclasses_file = "datasets/snomed_orga_all_subClasses.nt" heuristic = StandardHeuristics(ontology, ('dummy', 'dummy')) explainer = Explainer(ontology, heuristic) # container for amount of differences for each subsumption. 100 if lines of proofs differ. -1 if there was an error differences = [] processed_proofs = 0 num_samples = 50 # loop over all subclasses entailed by the ontology with open(all_subclasses_file) as f: for subclass in f.readlines(): # Load the current subsumption for heuristics and explainer subsumbtion_signatures = get_sub_from_nt(subclass) heuristic.set_subsumption(subsumbtion_signatures) with open('datasets/subClasses.nt', 'w+') as curr_sub_f: curr_sub_f.write(subclass)
rule_predictions = utils.read_pkl(args.rule_pred) logging.info("Loaded rule predictions from %s" % (args.rule_pred)) if (len(rule_predictions) != len(mapped_data)): logging.error("Unequal length of rule predictions and data") exit(-1) entity_inverse_map = utils.get_inverse_dict(distmult_dump['entity_to_id']) relation_inverse_map = utils.get_inverse_dict( distmult_dump['relation_to_id']) template_objs = template_builder.template_obj_builder( data_root, args.model_weights, args.template_load_dir, None, "distmult", args.t_ids, True) explainer = Explainer(data_root, template_objs[0].kb, template_objs[0].base_model, entity_inverse_map, relation_inverse_map) if (args.template_pred is not None): exps = english_exp_template(mapped_data, template_predictions, template_objs, explainer) elif (args.rule_pred is not None): exps = english_exp_rules(mapped_data, rule_predictions, explainer) logging.info("Generated explanations") os.makedirs(args.output_path, exist_ok=True) y_labels = ['na' for _ in range(len(mapped_data))] if (args.y_label is not None): y_labels = np.loadtxt(args.y_label)
f.write(line[1]) count += 1 def print_proof(proof): #print the proove for line in proof: print('forgetting ' + str(line[0])) print(str(line[1])) # Pizza example heur = ForgetFromList('datasets/pizza.owl', ["http://www.co-ode.org/ontologies/pizza/pizza.owl#SundriedTomatoTopping"]) explainer = Explainer("datasets/pizza.owl", heur) # university example #heur = ForgetFromList('datasets/university-example.owl', ["http://example.com/myOntology/Professor"]) #explainer = Explainer('datasets/university-example.owl', heur) #explainer.print_all_subclasses() #explainer.save_all_subclasses() #explainer.print_all_explanations('datasets/subClasses.nt') #explainer.save_all_explanations('datasets/subClasses.nt') #explanations = explainer.load_all_explanations() #explanations = explainer.get_all_explanations('datasets/subClasses.nt') #print(explanations) # testing heuristic heur = StandardHeuristics('datasets/pizza.owl', ("http://www.co-ode.org/ontologies/pizza/pizza.owl#Siciliana", "http://www.co-ode.org/ontologies/pizza/pizza.owl#Food"))
else: with open(latent_x_file, 'rb') as handle: latent_X, latent_Y, latent_Y_centers = pickle.load(handle) print("Loading pre-saved latent data for dataset %s on %s" % (dataset, algo)) if k is None: kf = original_X.shape[1] if original_X.shape[1] <= 5 else (original_X.shape[1] if original_X.shape[1] % 2 == 1 else original_X.shape[1]-1) else: kf = k save_file_name = global_dir + "/results/deltas/" + dataset + "_" + algo + "_" + str(variance_adjust) + "_k" + str(kf) + ".npy" if not os.path.isfile(save_file_name): print('The explanation for this settings has not been learnt yet!') else: E = Explainer(original_X, latent_Y, transformer, num_clusters, 0.5, global_dir, latent_X) E.set_delta(np.load(save_file_name)) cr, cv = E.metrics(epsilon=fixed_epsilon, k=k) print('Average correctness: %.3f | Average coverage: %.3f' % (np.mean(cr), np.mean(cv))) # First, we plot the metrics for this K E.plot_metrics() if k is not None: # Now, we plot the explanation E.plot_explanation(c1, c2, k) quit()
def q_learning_table_rand_greedy_agent(num_episodes=500, learning_rate=0.8, discount=0.95, rand=0.5, rand_from=0, rand_to=250, show_every=False): opts = [learning_rate, discount, rand, rand_from, rand_to].copy() # create env env = gym.make('NChain-v0') # get rand decay vlaue rand_decay = rand / (rand_to - rand_from) # remember rewards for each step of each episode explainer = Explainer() # create reward table # that associates conditions, actions and rewards reward_table = np.random.uniform(low=-2, high=0, size=(5, 2)) # run episodes for episode in range(num_episodes): # create new explainer episode explainer.new_episode(episode) # reset env and get starting state state = env.reset() # decay rand if episode >= rand_from and episode < rand_to: rand -= rand_decay else: rand = 0 # show episode number if show_every and episode % show_every == 0: print(episode) # run until env is finished done = False while not done: if np.random.random() < rand: # if no previous record, choose random action action = np.random.randint(0, 2) else: # choose previous action with best reward action = np.argmax(reward_table[state]) # perform step new_state, reward, done, _ = env.step(action) # save reward reward_table[state, action] += \ reward + learning_rate * \ (discount * np.max(reward_table[new_state, :]) - reward_table[state, action]) # update state state = new_state # save stats explainer.save(episode, reward) # if done and new_state_n[0] >= env.goal_position: # print('reach!') return reward_table, explainer, opts
from explainer import Explainer from forgetHeuristics import StandardHeuristics import sys ontology_file = sys.argv[2] subsumption_file = sys.argv[4] justification_step = sys.argv[6] if justification_step == 'False': justification_step = False else: justification_step = True # load subsumption with open(subsumption_file) as f: nt_string = f.readline() comp = nt_string.split() subsumption = (comp[0][1:-1], comp[2][1:-1]) heuristic = StandardHeuristics(ontology_file, subsumption) explainer = Explainer(ontology_file, heuristic) proof = explainer.get_proove(subsumption_file, justification_step) for line in proof: print("justification after forgetting " + str(line[0])) print(str(line[1]))