def eval_genome(genome, config, batch_data): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) :param config: config from the config file :param genome: one genome to get evaluated :param batch_data: data to use to evaluate the genomes :return fitness: returns the fitness of the genome this version is intented to use ParallelEvaluator and should be much faster """ net = neat.nn.RecurrentNetwork.create(genome, config) mse = 0 for data in batch_data: inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() mask, score = gate_activation(net, inputs) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size mse += (xo - output)**2 return 1 / (1 + mse)
def eval_genomes(genomes, config_): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) :param config_: config from the config file :param genomes: list of all the genomes to get evaluated """ for _, genome in tqdm(genomes): net = neat.nn.RecurrentNetwork.create(genome, config_) target_scores = [] non_target_scores = [] for data in trainloader: inputs, output = data[0], data[1] net.reset() mask, score = gate_activation(net, inputs) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size if output == 1: target_scores.append(xo) else: non_target_scores.append(xo) target_scores = np.array(target_scores) non_target_scores = np.array(non_target_scores) pmiss, pfa = rocch(target_scores, non_target_scores) eer = rocch2eer(pmiss, pfa) genome.fitness = 2 * (.5 - eer)
def evaluate(net, data_loader): correct = 0 total = 0 net.reset() target_scores = [] non_target_scores = [] for data in tqdm(data_loader): inputs, output = data[0], data[1] mask, score = gate_activation(net, inputs) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size total += 1 correct += ((xo > 0.5) == output) if output == 1: target_scores.append(xo) else: non_target_scores.append(xo) target_scores = np.array(target_scores) non_target_scores = np.array(non_target_scores) pmiss, pfa = rocch(target_scores, non_target_scores) eer = rocch2eer(pmiss, pfa) return target_scores, non_target_scores, float(correct) / total, eer
def eval_genome(genome, config, batch_data): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) :param config: config from the config file :param genome: one genome to get evaluated :param batch_data: data to use to evaluate the genomes :return fitness: returns the fitness of the genome this version is intented to use ParallelEvaluator and should be much faster """ net = neat.nn.RecurrentNetwork.create(genome, config) target_scores = [] non_target_scores = [] l_s_n = np.zeros(batch_size) for data in batch_data: inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() mask, score = gate_activation(net, inputs) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size if output == 1: target_scores.append(xo) else: non_target_scores.append(xo) target_scores = np.array(target_scores) non_target_scores = np.array(non_target_scores) for i in range(batch_size // 2): l_s_n[i] = (non_target_scores >= target_scores[i]).sum() / (batch_size // 2) for i in range(batch_size // 2): l_s_n[i + batch_size // 2] = ( target_scores <= non_target_scores[i]).sum() / (batch_size // 2) return 1 - l_s_n
def eval_genomes(genomes, config_, batch_data): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) Used for single processing :param config_: config from the config file :param genomes: list of all the genomes to get evaluated :param batch_data: data to use to evaluate the genomes """ for _, genome in tqdm(genomes): net = neat.nn.RecurrentNet.create(genome, config_) mse = 0 for data in batch_data: inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() mask, score = gate_activation(net, inputs[0]) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size mse += (xo - output)**2 genome.fitness = 1 / (1 + mse)
def eer_gc(genome, config, validation_set): """ function to use for selecting the grand xhampion of each generation :param genome: genome one genome to get evaluated :param config: file configuration file :param validation_set: ASVDataset data use for validation :return: """ net = neat.nn.RecurrentNetwork.create(genome, config) target_scores = [] non_target_scores = [] for data in tqdm(validation_set): inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() mask, score = gate_activation(net, inputs) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size if output == 1: target_scores.append(xo) else: non_target_scores.append(xo) target_scores = np.array(target_scores) non_target_scores = np.array(non_target_scores) pmiss, pfa = rocch(target_scores, non_target_scores) eer = rocch2eer(pmiss, pfa) return eer