def genotype(self): n, start = 3, 2 weightsr2 = [F.softmax(beta, dim=-1) for beta in self.beta_reduce[0:2]] weightsn2 = [F.softmax(beta, dim=-1) for beta in self.beta_normal[0:2]] for i in range(self.n_nodes): end = start + n tw2 = [ F.softmax(beta, dim=-1) for beta in self.beta_reduce[start:end] ] tn2 = [ F.softmax(beta, dim=-1) for beta in self.beta_normal[start:end] ] start = end n += 1 weightsr2 = weightsr2 + tw2 weightsn2 = weightsn2 + tn2 # normalize edge-leve beta parameters gene_normal = gt.parse(self.alpha_normal, weightsn2, k=2) gene_reduce = gt.parse(self.alpha_reduce, weightsr2, k=2) concat = range(2, 2 + self.n_nodes) # concat all intermediate nodes return gt.Genotype(normal=gene_normal, normal_concat=concat, reduce=gene_reduce, reduce_concat=concat)
def genotype(self): gene_normal = gt.parse(self.alpha_normal, k=2) gene_reduce = gt.parse(self.alpha_reduce, k=2) concat = range(2, 2+self.n_nodes) # concat all intermediate nodes return gt.Genotype(normal=gene_normal, normal_concat=concat, reduce=gene_reduce, reduce_concat=concat)
def genotype_cell(alphas_normal, alphas_reduce, primitives, steps=4, multiplier=4, skip_primitive='none'): # skip_primitive = 'none' is a hack in original DARTS, which removes a no-op primitive. # skip_primitive = None means no hack is applied # note the printed weights from a call to Network::arch_weights() in model_search.py # are already post-softmax, so we don't need to apply softmax again # alphas_normal = torch.FloatTensor(genotypes.SHARPER_SCALAR_WEIGHTS.normal) # alphas_reduce = torch.FloatTensor(genotypes.SHARPER_SCALAR_WEIGHTS.reduce) # F.softmax(alphas_normal, dim=-1).data.cpu().numpy() # F.softmax(alphas_reduce, dim=-1).data.cpu().numpy() gene_normal = parse_cell(alphas_normal, primitives, steps, skip_primitive=skip_primitive) gene_reduce = parse_cell(alphas_reduce, primitives, steps, skip_primitive=skip_primitive) concat = range(2 + steps - multiplier, steps + 2) genotype = genotypes.Genotype( normal=gene_normal, normal_concat=concat, reduce=gene_reduce, reduce_concat=concat, layout='cell', ) return genotype
def sample_arch_eval(self): n_nodes = genotypes.STEPS n_ops = len(genotypes.PRIMITIVES) arch = [] for i in range(n_nodes): op = np.random.choice(range(1, n_ops)) node_in = np.random.choice(range(i + 1)) arch.append((genotypes.PRIMITIVES[op], node_in)) concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) return genotype
def generate_random_structure(node = 4, k =2): total_edge = sum(list(range(2, node + 2))) * 2 cell_edge = int(total_edge / 2) num_ops = len(gt.PRIMITIVES) weight = np.random.randn(total_edge, num_ops) theta_norm = utils.darts_weight_unpack(weight[0:cell_edge], node) theta_reduce = utils.darts_weight_unpack(weight[cell_edge:], node) gene_normal = gt.parse_numpy(theta_norm, k=k) gene_reduce = gt.parse_numpy(theta_reduce, k=k) concat = range(2, 2+node) return gt.Genotype(normal=gene_normal, normal_concat=concat, reduce=gene_reduce, reduce_concat=concat)
def get_fitness(self, pop, n_nodes): num_pop = pop.shape[0] fitness = np.zeros((num_pop)) for m in range(num_pop): arch = [] for i in range(n_nodes): op = np.int(pop[m, 2 * i]) node_in = np.int(pop[m, 2 * i + 1]) arch.append((genotypes.PRIMITIVES[np.int(op)], node_in)) concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) fitness[m, ] = self.model.evaluate(genotype) return fitness
def get_param_range(self, n, stochastic=True): configs = [] for _ in range(n): n_nodes = genotypes.STEPS n_ops = len(genotypes.PRIMITIVES) arch = [] for i in range(n_nodes): op = np.random.choice(range(1, n_ops)) node_in = np.random.choice(range(i + 1)) arch.append((genotypes.PRIMITIVES[op], node_in)) #concat = [i for i in range(genotypes.STEPS) if i not in [j[1] for j in arch]] concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) configs.append(genotype) return configs
def get_performance(self, selec_arch): n_nodes = genotypes.STEPS n_ops = len(genotypes.PRIMITIVES) arch = [] performance = np.zeros((1)) # selec_arch=np.zeros((2*n_nodes,)) for i in range(n_nodes): op = np.int(selec_arch[2 * i, ]) node_in = np.int(selec_arch[2 * i + 1, ]) arch.append((genotypes.PRIMITIVES[op], node_in)) concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) performance[0, ] = self.evaluate(genotype) return performance[0, ]
def get_init_pop(self, num_pop, n_nodes): pop = np.empty((num_pop, 2 * n_nodes)) fitness = np.zeros((num_pop, )) for m in range(num_pop): num_ops = len(genotypes.PRIMITIVES) arch = [] for i in range(n_nodes): op = np.random.choice(range(1, num_ops)) node_in = np.random.choice(range(i + 1)) arch.append((genotypes.PRIMITIVES[op], node_in)) pop[m, 2 * i] = op pop[m, 2 * i + 1] = node_in concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) fitness[m, ] = self.model.evaluate(genotype) return pop, fitness
def EA_arch_search(self, num_pop, num_ite, num_cross, num_mutation): def get_init_pop(self, num_pop, n_nodes): pop = np.empty((num_pop, 8 * n_nodes)) fitness = np.zeros((num_pop, )) for m in range(num_pop): num_ops = len(genotypes.PRIMITIVES) normal = [] reduction = [] for i in range(n_nodes): ops = np.random.choice(range(num_ops), 4) nodes_in_normal = np.random.choice(range(i + 2), 2, replace=False) nodes_in_reduce = np.random.choice(range(i + 2), 2, replace=False) normal.extend([(nodes_in_normal[0], ops[0]), (nodes_in_normal[1], ops[1])]) reduction.extend([(nodes_in_reduce[0], ops[2]), (nodes_in_reduce[1], ops[3])]) pop[m, 4 * i] = nodes_in_normal[0] pop[m, 4 * i + 1] = ops[0] pop[m, 4 * i + 2] = nodes_in_normal[1] pop[m, 4 * i + 3] = ops[1] pop[m, 4 * i + 4 * n_nodes] = nodes_in_reduce[0] pop[m, 4 * i + 1 + 4 * n_nodes] = ops[2] pop[m, 4 * i + 2 + 4 * n_nodes] = nodes_in_reduce[1] pop[m, 4 * i + 3 + 4 * n_nodes] = ops[3] arch = (normal, reduction) fitness[m, ] = self.model.evaluate(arch) return pop, fitness def corssover(self, pop, fitness, num_cross): index = np.argsort(fitness) pop_select = pop[index[0:num_cross], ] inde_cross = np.arange(num_cross) np.random.shuffle(inde_cross) pop_select = pop_select[inde_cross, ] pop_cross = np.empty((num_cross, pop.shape[1])) for i in range(np.int(num_cross / 2)): cross1 = pop_select[2 * i, ] cross2 = pop_select[2 * i + 1, ] cross_points = np.arange(4 * self.model.model._steps) np.random.shuffle(cross_points) cross_points = cross_points[0:2] cross_points = np.sort(cross_points) p1 = 2 * cross_points[0] p2 = 2 * cross_points[1] cross1_ = cross1 cross2_ = cross2 cross1_[p1:p2] = cross2[p1:p2] cross2_[p1:p2] = cross1[p1:p2] pop_cross[2 * i, ] = cross1_ pop_cross[2 * i + 1, ] = cross2_ return pop_cross def mutation(self, pop, fitness, num_mutation): index = np.argsort(fitness) pop_select = pop[index[0:num_mutation], ] pop_mutation = np.empty((num_mutation, pop.shape[1])) num_ops = len(genotypes.PRIMITIVES) for i in range(num_mutation): pop_mutation[i, ] = pop_select[i, ] for j in range(pop.shape[1]): if j > ((pop.shape[1]) / 2 - 1): q = j - (pop.shape[1]) / 2 else: q = j m = q // 4 + 2 if np.random.rand( ) < 0.2: #################genes with mutation probability 0.2 if j % 2 == 0: pop_mutation[i, j] = np.random.randint(m) else: pop_mutation[i, j] = np.random.randint(num_ops) return pop_mutation def get_fitness(self, pop): num_pop = pop.shape[0] fitness = np.zeros((num_pop)) for m in range(num_pop): indiv = pop[m, ] normal = [] reduction = [] for i in range(self.model.model._steps): s1 = np.int(indiv[4 * i, ]) s2 = np.int(indiv[4 * i + 1, ]) s3 = np.int(indiv[4 * i + 2, ]) s4 = np.int(indiv[4 * i + 3, ]) s5 = np.int(indiv[4 * i + 16, ]) s6 = np.int(indiv[4 * i + 1 + 16, ]) s7 = np.int(indiv[4 * i + 2 + 16, ]) s8 = np.int(indiv[4 * i + 3 + 16, ]) normal.extend([(s1, s2), (s3, s4)]) reduction.extend([(s5, s6), (s7, s8)]) arch = (normal, reduction) fitness[m, ] = self.model.evaluate(arch) return fitness n_nodes = self.model.model._steps pop, fitness = get_init_pop(self, num_pop, n_nodes) for it in range(num_ite): pop_cross = corssover(self, pop, fitness, num_cross) fitness_cross = get_fitness(self, pop_cross) pop_mutate = mutation(self, pop, fitness, num_mutation) fitness_mutate = get_fitness(self, pop_mutate) pop_comb = np.concatenate((pop, pop_cross, pop_mutate), axis=0) fitness_comb = np.concatenate( (fitness, fitness_cross, fitness_mutate), axis=0) index = np.argsort(fitness_comb) pop_comb = pop_comb[index, ] pop = pop_comb[0:num_pop, ] fitness = fitness_comb[0:num_pop, ] index = np.argsort(fitness) indi_final = pop[index[0], ] normal = [] normal_struc = [] reduction = [] reduction_struc = [] for i in range(self.model.model._steps): s1 = np.int(indi_final[4 * i, ]) s2 = np.int(indi_final[4 * i + 1, ]) s3 = np.int(indi_final[4 * i + 2, ]) s4 = np.int(indi_final[4 * i + 3, ]) s5 = np.int(indi_final[4 * i + 16, ]) s6 = np.int(indi_final[4 * i + 1 + 16, ]) s7 = np.int(indi_final[4 * i + 2 + 16, ]) s8 = np.int(indi_final[4 * i + 3 + 16, ]) normal.extend([(s1, s2), (s3, s4)]) normal_struc.append((s1, genotypes.PRIMITIVES[s2])) normal_struc.append((s3, genotypes.PRIMITIVES[s4])) reduction.extend([(s5, s6), (s7, s8)]) reduction_struc.append((s5, genotypes.PRIMITIVES[s6])) reduction_struc.append((s7, genotypes.PRIMITIVES[s8])) concat = range(2, self.model.model._steps + 2) genotype = genotypes.Genotype(normal=normal_struc, normal_concat=concat, reduce=reduction_struc, reduce_concat=concat) best_arch = genotype return indi_final
def EA_arch_search(self, num_pop, num_ite, num_cross, num_mutation): def get_init_pop(self, num_pop, n_nodes): pop = np.empty((num_pop, 2 * n_nodes)) fitness = np.zeros((num_pop, )) for m in range(num_pop): num_ops = len(genotypes.PRIMITIVES) arch = [] for i in range(n_nodes): op = np.random.choice(range(1, num_ops)) node_in = np.random.choice(range(i + 1)) arch.append((genotypes.PRIMITIVES[op], node_in)) pop[m, 2 * i] = op pop[m, 2 * i + 1] = node_in concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) fitness[m, ] = self.model.evaluate(genotype) return pop, fitness def corssover(self, pop, fitness, num_cross): index = np.argsort(fitness) pop_select = pop[index[0:num_cross], ] inde_cross = np.arange(num_cross) np.random.shuffle(inde_cross) pop_select = pop_select[inde_cross, ] pop_cross = np.empty((num_cross, pop.shape[1])) for i in range(np.int(num_cross / 2)): cross1 = pop_select[2 * i, ] cross2 = pop_select[2 * i + 1, ] cross_points = np.arange(2 * genotypes.STEPS) np.random.shuffle(cross_points) cross_points = cross_points[0:2] cross_points = np.sort(cross_points) p1 = 2 * cross_points[0] p2 = 2 * cross_points[1] cross1_ = cross1 cross2_ = cross2 cross1_[p1:p2] = cross2[p1:p2] cross2_[p1:p2] = cross1[p1:p2] pop_cross[2 * i, ] = cross1_ pop_cross[2 * i + 1, ] = cross2_ return pop_cross def mutation(self, pop, fitness, num_mutation): index = np.argsort(fitness) pop_select = pop[index[0:num_mutation], ] pop_mutation = np.empty((num_mutation, pop.shape[1])) num_ops = len(genotypes.PRIMITIVES) for i in range(num_mutation): pop_mutation[i, ] = pop_select[i, ] for j in range(pop.shape[1]): if np.random.rand( ) < 0.2: #################genes with mutation probability 0.2np.random.choice(range(1,n_ops)) if j % 2 == 0: pop_mutation[i, j] = np.random.choice( range(1, num_ops)) else: pop_mutation[i, j] = np.random.choice( range(np.int(j / 2) + 1)) return pop_mutation def get_fitness(self, pop, n_nodes): num_pop = pop.shape[0] fitness = np.zeros((num_pop)) for m in range(num_pop): arch = [] for i in range(n_nodes): op = np.int(pop[m, 2 * i]) node_in = np.int(pop[m, 2 * i + 1]) arch.append((genotypes.PRIMITIVES[np.int(op)], node_in)) concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) fitness[m, ] = self.model.evaluate(genotype) return fitness k = sum(1 for i in range(genotypes.STEPS) for n in range(2 + i)) num_ops = len(genotypes.PRIMITIVES) n_nodes = genotypes.STEPS pop, fitness = get_init_pop(self, num_pop, n_nodes) for it in range(num_ite): pop_cross = corssover(self, pop, fitness, num_cross) fitness_cross = get_fitness(self, pop_cross, n_nodes) pop_mutate = mutation(self, pop, fitness, num_mutation) fitness_mutate = get_fitness(self, pop_mutate, n_nodes) pop_comb = np.concatenate((pop, pop_cross, pop_mutate), axis=0) fitness_comb = np.concatenate( (fitness, fitness_cross, fitness_mutate), axis=0) index = np.argsort(fitness_comb) pop_comb = pop_comb[index, ] pop = pop_comb[0:num_pop, ] fitness = fitness_comb[0:num_pop, ] index = np.argsort(fitness) indi_final = pop[index[0], ] sele_arch = [] for i in range(genotypes.STEPS): op = indi_final[2 * i] node_in = indi_final[2 * i + 1] sele_arch.append( (genotypes.PRIMITIVES[np.int(op)], np.int(node_in))) concat = range(1, 9) genotype = genotypes.Genotype(recurrent=sele_arch, concat=concat) return genotype
def sample_arch(self, node_id, store_arch): n_nodes = genotypes.STEPS n_ops = len(genotypes.PRIMITIVES) def limite_range(arch, n_ops, n_nodes): arch = np.int_(arch) for i in range(n_nodes): arch[2 * i, ] = np.max( (np.min((arch[2 * i, ], n_ops - 1)), 1)) ###############################why not conyain none arch[2 * i + 1, ] = np.max((np.min( (arch[2 * i + 1, ], (i))), 0)) return arch def get_performance(self, selec_arch): n_nodes = genotypes.STEPS n_ops = len(genotypes.PRIMITIVES) arch = [] performance = np.zeros((1)) # selec_arch=np.zeros((2*n_nodes,)) for i in range(n_nodes): op = np.int(selec_arch[2 * i, ]) node_in = np.int(selec_arch[2 * i + 1, ]) arch.append((genotypes.PRIMITIVES[op], node_in)) concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) performance[0, ] = self.evaluate(genotype) return performance[0, ] if node_id > 999: alfa = 0.01 n = 2 sigma = 1 mu = np.zeros((1, 2 * n_nodes)) Sigma = np.eye(2 * n_nodes) R = cholesky(Sigma) yita = np.dot(np.random.randn(n, 2 * n_nodes), R) + mu n_yita = np.empty((n, 2 * n_nodes)) n_yita1 = np.empty( (n, 2 * n_nodes) ) #############################store the performance###whether take the reward into consideration index0 = np.random.randint(1000) test_arch = store_arch[index0, ] for i in range(n): test_i = test_arch + yita[i, ] n_f = self.novelty_fitness(np.int_(np.round((test_i))), np.int_(np.round(store_arch)), 10) n_yita[i, ] = n_f * yita[i, ] # select_i=limite_range(test_i,n_ops,n_nodes)###whether take the reward into consideration # n_yita1[i,]=get_performance(self,select_i)###whether take the reward into consideration selec_arch = test_arch + alfa * (1 / (n * sigma)) * sum(n_yita) # selec_arch=test_arch+alfa*(1/(n*sigma))*(0.5*sum(n_yita)+0.5*sum(n_yita1))######whether take the reward into consideration store_arch[index0, ] = selec_arch selec_arch = np.int_(np.round(selec_arch)) selec_arch = limite_range(selec_arch, n_ops, n_nodes) arch = [] for i in range(n_nodes): op = np.int(selec_arch[2 * i, ]) node_in = np.int(selec_arch[2 * i + 1, ]) arch.append((genotypes.PRIMITIVES[np.int(op)], node_in)) index = index0 else: n_nodes = genotypes.STEPS n_ops = len(genotypes.PRIMITIVES) arch = [] selec_arch = np.zeros((2 * n_nodes, )) for i in range(n_nodes): op = np.random.choice(range(1, n_ops)) node_in = np.random.choice(range(i + 1)) arch.append((genotypes.PRIMITIVES[op], node_in)) selec_arch[2 * i, ] = op selec_arch[2 * i + 1, ] = node_in index = node_id concat = range(1, 9) genotype = genotypes.Genotype(recurrent=arch, concat=concat) ######the operations from two previous node are different return index, selec_arch, genotype
def genotype(self): gene_normal = gt.parse(self.alpha_normal, k=2) concat = range(1, 1 + self.n_nodes) # concat all intermediate nodes return gt.Genotype(normal=gene_normal, normal_concat=concat)
writer.add_scalar("pop_top5_{}".format(i + 1), p.top5.avg, epoch + 1) writer.add_scalar("pop_obj_valid_{}".format(i + 1), p.objs.avg, epoch + 1) with open(os.path.join(DIR, "population_{}.pickle".format(epoch + 1)), 'wb') as f: pickle.dump(population, f) if epoch == args.epochs - 1: normal_cell = utils.derive_architecture_topk( population.get_population()[0].arch_parameters[0]) reduction_cell = utils.derive_architecture_topk( population.get_population()[0].arch_parameters[1]) concat = [2, 3, 4, 5] genotype = genotypes.Genotype(normal=normal_cell, normal_concat=concat, reduce=reduction_cell, reduce_concat=concat) model.copy_arch_parameters( population.get_population()[0].arch_parameters) assert utils.check_equality( model, population.get_population()[0].arch_parameters) genotype2 = model.genotype() assert genotype2.normal == genotype.normal assert genotype2.reduce == genotype.reduce with open(os.path.join(DIR, "genotype.pickle"), 'wb') as f: pickle.dump(genotype, f) logging.info( "[INFO] Saving the best architecture after {} generation".format( epoch + 1)) population.print_population()
def get_genotype(genotype_path, name): genotype_file = os.path.join(genotype_path, '%s.txt'%name) tmp_dict = json.load(open(genotype_file,'r')) genotype = genotypes.Genotype(**tmp_dict) return genotype