def trainab(model_name, dataset_name, iter_num): # init T,R,SP T = getSGGRes(model_name, dataset_name, "train", tasktype) R = getGT(model_name, dataset_name, "train", tasktype) SP, N = getPfromR(R, dataset_name) print("T info: ", len(T), len(T[0]), T[0][0]) print("R info: ", len(R), len(R[0]), R[0][0]) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") dd = 1e23 # start iteration for i in range(iter_num): ga = GA(func=fitness, n_dim=2, size_pop=10, max_iter=50, \ lb=[0.0, 0.0], ub=[dd, dd], precision=1e-7) #ga = GA(func=fitness, n_dim=2, size_pop=10, max_iter=4, \ # lb=[0.0, 0.0], ub=[dd, dd], precision=1e-7) ga.to(device=device) #GPU best_x, best_y = ga.run() # best_x={alpha,beta},best_y=Q-R alpha, beta = best_x print('iteration ', i, ': best alpha and beta: ', alpha, beta) print('best_y:', best_y) if best_y == 0: print('best {alpha,beta}:', best_x, 'best_y', best_y) end_cnt = fitness(best_x) print("detect if zeros: ", end_cnt) if end_cnt == 0: break else: R = ReSet(alpha, beta) SP, _ = getPfromR(R, dataset_name) return best_x
def trainabGA(model_name,dataset_name,iter_num): # init T,R,SP T = getSGGRes(model_name,dataset_name,"train",tasktype) R = getGT(model_name,dataset_name,"train",tasktype) SP,N = getPfromR(R,dataset_name) print("T info: ",len(T),len(T[0]),T[0][0]) print("R info: ",len(R),len(R[0]),R[0][0]) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # start iteration low = [0.0,0.0] high = [1e6,1e6] for i in range(iter_num): #ga = GA(func=fitness, n_dim=2, size_pop=4, max_iter=10, \ # lb=[0.0, 0.0], ub=[1.0, 1.0], precision=1e-7) #ga = GA(func=fitness, n_dim=2, size_pop=6, max_iter=50, \ # lb=low, ub=high, precision=1e-7) #ga = GA(func=fitness, n_dim=2, size_pop=50, max_iter=50, \ # lb=low, ub=high, precision=1e-7) ga = GA(func=fitnessNDCG, n_dim=2, size_pop=100, max_iter=100, \ lb=low, ub=high, precision=1e-7) ga.register(operator_name='selection', operator=selection_tournament, tourn_size=3).\ register(operator_name='ranking', operator=ranking.ranking). \ register(operator_name='crossover', operator=crossover.crossover_2point). \ register(operator_name='mutation', operator=mutation.mutation) ga.to(device=device) #GPU start_time = time.time() best_x, best_y = ga.run()# best_x={alpha,beta},best_y=Q-R print("run time: ",time.time() - start_time) alpha, beta = best_x print('iteration ',i,': best alpha and beta: ', alpha,beta) print('best_y:', best_y) print("sum_ndcg: ",-best_y-0.5*(alpha**2+beta**2)) if best_y==0: print('best {alpha,beta}:',best_x,'best_y',best_y) end_cnt = fitnessNDCG(best_x) print("detect if zeros: ",end_cnt) if end_cnt==0: break else: #R = ReSet(alpha,beta) R = addsmallNDCG(alpha,beta,R) SP,_ = getPfromR(R,dataset_name) cnt_now=fitness(best_x) print(i," iter :", cnt_now) return best_x
return 0.5 + (np.sin(x) - 0.5) / np.square(1 + 0.001 * x) import torch from sko.GA import GA device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") ga = GA(func=schaffer, n_dim=2, size_pop=50, max_iter=800, lb=[-1, -1], ub=[1, 1], precision=1e-7) ga.to(device=device) start_time = time.time() best_x, best_y = ga.run() print(time.time() - start_time) print('best_x:', best_x, '\n', 'best_y:', best_y) ga = GA(func=schaffer, n_dim=2, size_pop=50, max_iter=800, lb=[-1, -1], ub=[1, 1], precision=1e-7) start_time = time.time() best_x, best_y = ga.run() print(time.time() - start_time)