def test(self, test_inputs, with_implicit_reward=False): outs = get_layer_outs_new(self.model, test_inputs, skip=self.skip_layers) for layer_index, layer_out in enumerate( outs): # layer_out is output of layer for all inputs for out_for_input in layer_out: # out_for_input is output of layer for single input for neuron_index in range(out_for_input.shape[-1]): neuron_out = np.mean(out_for_input[..., neuron_index]) global_neuron_index = (layer_index, neuron_index) self.neuron_set.add(global_neuron_index) neuron_low = self.major_func_regions[layer_index][0][ neuron_index] neuron_high = self.major_func_regions[layer_index][1][ neuron_index] section_length = (neuron_high - neuron_low) / self.k section_index = floor( (neuron_out - neuron_low) / section_length) if section_length > 0 else 0 self.activation_table_by_section[(global_neuron_index, section_index)] = True if neuron_out < neuron_low: self.lower_activation_table[global_neuron_index] = True elif neuron_out > neuron_high: self.upper_activation_table[global_neuron_index] = True multisection_activated = len(self.activation_table_by_section.keys()) lower_activated = len(self.lower_activation_table.keys()) upper_activated = len(self.upper_activation_table.keys()) total = len(self.neuron_set) return ( percent_str(multisection_activated, self.k * total), # kmn multisection_activated, percent_str(upper_activated + lower_activated, 2 * total), # nbc percent_str(upper_activated, total), # snac lower_activated, upper_activated, total, multisection_activated, upper_activated, lower_activated, total, outs)
def measure_neuron_cov(model, test_inputs, scaler, threshold=0, skip_layers=None, outs=None): if outs is None: outs = get_layer_outs_new(model, test_inputs, skip_layers) activation_table = defaultdict(bool) for layer_index, layer_out in enumerate(outs): # layer_out is output of layer for all inputs for out_for_input in layer_out: # out_for_input is output of layer for single input out_for_input = scaler(out_for_input) for neuron_index in range(out_for_input.shape[-1]): activation_table[(layer_index, neuron_index)] = activation_table[(layer_index, neuron_index)] or\ np.mean(out_for_input[..., neuron_index]) > threshold covered = len([1 for c in activation_table.values() if c]) total = len(activation_table.keys()) return percent_str(covered, total), covered, total, outs
def test(self, test_inputs): outs = get_layer_outs_new(self.model, test_inputs, self.skip_layers) neuron_count_by_layer = {} layer_count = len(outs) for input_index in range( len(test_inputs )): # out_for_input is output of layer for single input pattern = [] for layer_index in range( layer_count ): # layer_out is output of layer for all inputs out_for_input = outs[layer_index][input_index] neuron_outs = np.zeros((out_for_input.shape[-1], )) neuron_count_by_layer[layer_index] = len(neuron_outs) for i in range(out_for_input.shape[-1]): neuron_outs[i] = np.mean(out_for_input[..., i]) top_k_neuron_indexes = (np.argsort( neuron_outs, axis=None)[-self.k:len(neuron_outs)]) pattern.append(tuple(top_k_neuron_indexes)) for neuron_index in top_k_neuron_indexes: self.activation_table[(layer_index, neuron_index)] = True if layer_index + 1 == layer_count: self.pattern_set.add(tuple(pattern)) neuron_count = sum(neuron_count_by_layer.values()) covered = len(self.activation_table.keys()) return ( percent_str(covered, neuron_count), # tknc covered, neuron_count, len(self.pattern_set), # tknp outs)
def test(self, test_inputs, with_implicit_reward=False): outs = get_layer_outs_new(self.model, test_inputs, self.skip_layers) for layer_index, layer_out in enumerate(outs): # layer_out is output of layer for all inputs for out_for_input in layer_out: # out_for_input is output of layer for single input out_for_input = self.scaler(out_for_input) for neuron_index in range(out_for_input.shape[-1]): if self.activation_table[(layer_index, neuron_index)] == 1: pass elif np.mean(out_for_input[..., neuron_index]) > self.threshold: self.activation_table[(layer_index, neuron_index)] = 1 elif self.calc_implicit_reward_neuron: p1 = np.mean(out_for_input[..., neuron_index]) p2 = self.threshold r = self.calc_implicit_reward_neuron(p1, p2) self.activation_table[(layer_index, neuron_index)] = r reward, covered, implicit_reward = self.calc_reward(self.activation_table, with_implicit_reward=with_implicit_reward) total = len(self.activation_table.keys()) return percent_str(reward, total), reward, total, outs
def test(self, test_inputs, with_implicit_reward=False): pen_layer_outs = get_layer_outs_new(self.model, test_inputs)[self.subject_layer] for plo in pen_layer_outs: if len(self.distant_vectors) > 0: _, approx_distances = self.flann.nn_index(plo, 1) exact_distances = [ np.sum(np.square(plo - distant_vec)) for distant_vec in self.distant_vectors_buffer ] nearest_distance = min(exact_distances + approx_distances.tolist()) if nearest_distance > self.distance_threshold: self.distant_vectors_buffer.append(plo) self.distant_vectors.append(plo) else: self.flann.build_index(plo) self.distant_vectors.append(plo) return len(self.distant_vectors), self.distant_vectors