def test_get_or_train_cav_save_test(self): cav_instance = get_or_train_cav(self.concepts, self.bottleneck, self.acts, cav_dir=self.cav_dir, cav_hparams=self.hparams) # check values of some elements. self.assertEqual(cav_instance.cavs[0][0], self.cav_vecs[0][0]) self.assertEqual(cav_instance.cavs[1][2], self.cav_vecs[1][2])
def _run_single_set(self, param): """Run TCAV with provided for one set of (target, concepts). Args: param: parameters to run Returns: a dictionary of results (panda frame) """ bottleneck = param.bottleneck concepts = param.concepts target_class = param.target_class activation_generator = param.activation_generator alpha = param.alpha mymodel = param.model cav_dir = param.cav_dir # Get acts acts = activation_generator.process_and_load_activations( [bottleneck], concepts + [target_class]) # Get CAVs cav_hparams = CAV.default_hparams() cav_hparams.alpha = alpha cav_instance = get_or_train_cav( concepts, bottleneck, acts, cav_dir=cav_dir, cav_hparams=cav_hparams) # clean up for c in concepts: del acts[c] # Hypo testing a_cav_key = CAV.cav_key(concepts, bottleneck, cav_hparams.model_type, cav_hparams.alpha) target_class_for_compute_tcav_score = target_class for cav_concept in concepts: if cav_concept is self.random_counterpart or 'random' not in cav_concept: i_up = self.compute_tcav_score( mymodel, target_class_for_compute_tcav_score, cav_concept, cav_instance, acts[target_class][cav_instance.bottleneck]) val_directional_dirs = self.get_directional_dir( mymodel, target_class_for_compute_tcav_score, cav_concept, cav_instance, acts[target_class][cav_instance.bottleneck]) result = {'cav_key' : a_cav_key, 'cav_concept' : cav_concept, 'target_class' : target_class, 'i_up' : i_up, 'val_directional_dirs_abs_mean' : np.mean(np.abs(val_directional_dirs)), 'val_directional_dirs_mean' : np.mean(val_directional_dirs), 'val_directional_dirs_std' : np.std(val_directional_dirs), 'note' : 'alpha_%s ' % (alpha), 'alpha' : alpha, 'bottleneck' : bottleneck} del acts return result
def _run_single_set(self, params): """ Run TCAV with provided for one set of (target, concepts). :param params: parameters to run :return: a dict of results (pandas dataframe) """ bottleneck = params.bottleneck concepts = params.concepts target_class = params.target_class activation_generator = params.activation_generator alpha = params.alpha black_box = params.black_box cav_dir = params.cav_dir tf.logging.info('running %s %s' % (target_class, concepts)) acts = activation_generator(black_box, bottlenecks, concepts + [target_class]) cav_hparams = CAV.default_hparams() cav_hparams.alpha = alpha cav_instance = get_or_train_cav(concepts, bottlenecks, acts, cav_dir=cav_dir, cav_hparams=cav_hparams) for concept in concepts: del acts[concept] a_cav_key = CAV.cav_key(concepts, bottlenecks, cav_hparams.model_type, cav_hparams.alpha) target_class_for_compute_tcav_score = target_class for cav_concept in concepts: if cav_concept is self.random_counterpart or 'random' not in cav_concept: i_up = self.compute_tcav_score( black_box, target_class_for_compute_tcav_score, cav_concept.cav_instance, acts[target_class][cav_instance.bottleneck]) val_dir_derivatives = self.get_dir_derivative( black_box, target_class_for_compute_tcav_score, cav_concept.cav_instance, acts[target_class][cav_instance.bottleneck]) result = { 'cav_key': a_cav_key, 'cav_concept': cav_concept, 'target_class': target_class, 'i_up': i_up, 'val_dir_derivatives_abs_mean': np.mean(np.abs(val_dir_derivatives)), 'val_dir_derivatives_mean': np.mean(val_dir_derivatives), 'val_dir_derivatives_std': np.std(val_dir_derivatives), 'note': 'alpha_%s' % (alpha), 'alpha': alpha, 'bottleneck': bottlenecks } del acts return result