Update REA, REINFORCE, and RANDOM
This commit is contained in:
		
							
								
								
									
										91
									
								
								exps/algos-v2/random_wo_share.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								exps/algos-v2/random_wo_share.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,91 @@ | ||||
| ################################################## | ||||
| # Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2020 # | ||||
| ############################################################################## | ||||
| # Random Search for Hyper-Parameter Optimization, JMLR 2012 ################## | ||||
| ############################################################################## | ||||
| # python ./exps/algos-v2/random_wo_share.py --dataset cifar10 --search_space tss | ||||
| ############################################################################## | ||||
| import os, sys, time, glob, random, argparse | ||||
| import numpy as np, collections | ||||
| from copy import deepcopy | ||||
| import torch | ||||
| import torch.nn as nn | ||||
| from pathlib import Path | ||||
| lib_dir = (Path(__file__).parent / '..' / '..' / 'lib').resolve() | ||||
| if str(lib_dir) not in sys.path: sys.path.insert(0, str(lib_dir)) | ||||
| from config_utils import load_config, dict2config, configure2str | ||||
| from datasets     import get_datasets, SearchDataset | ||||
| from procedures   import prepare_seed, prepare_logger, save_checkpoint, copy_checkpoint, get_optim_scheduler | ||||
| from utils        import get_model_infos, obtain_accuracy | ||||
| from log_utils    import AverageMeter, time_string, convert_secs2time | ||||
| from models       import get_search_spaces | ||||
| from nas_201_api  import NASBench201API, NASBench301API | ||||
| from .regularized_ea import random_topology_func, random_size_func | ||||
|  | ||||
|  | ||||
| def main(xargs, api): | ||||
|   torch.set_num_threads(4) | ||||
|   prepare_seed(xargs.rand_seed) | ||||
|   logger = prepare_logger(args) | ||||
|  | ||||
|   search_space = get_search_spaces(xargs.search_space, 'nas-bench-301') | ||||
|   if xargs.search_space == 'tss': | ||||
|     random_arch = random_topology_func(search_space) | ||||
|   else: | ||||
|     random_arch = random_size_func(search_space) | ||||
|  | ||||
|   x_start_time = time.time() | ||||
|   logger.log('{:} use nas_bench : {:}'.format(time_string(), nas_bench)) | ||||
|   best_arch, best_acc, total_time_cost, history = None, -1, [], [] | ||||
|   while total_time_cost[-1] < xargs.time_budget: | ||||
|     arch = random_arch() | ||||
|     accuracy, _, _, total_cost = api.simulate_train_eval(arch, xargs.dataset, '12') | ||||
|     total_time_cost.append(total_cost) | ||||
|     history.append(arch) | ||||
|     if best_arch is None or best_acc < accuracy: | ||||
|       best_acc, best_arch = accuracy, arch | ||||
|     logger.log('[{:03d}] : {:} : accuracy = {:.2f}%'.format(len(history), arch, accuracy)) | ||||
|   logger.log('{:} best arch is {:}, accuracy = {:.2f}%, visit {:} archs with {:.1f} s (real-cost = {:.3f} s).'.format(time_string(), best_arch, best_acc, len(history), total_time_cost, time.time()-x_start_time)) | ||||
|    | ||||
|   info = api.query_info_str_by_arch(best_arch, '200' if xargs.search_space == 'tss' else '90') | ||||
|   logger.log('{:}'.format(info)) | ||||
|   logger.log('-'*100) | ||||
|   logger.close() | ||||
|   return logger.log_dir, total_time_cost, history | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|   parser = argparse.ArgumentParser("Random NAS") | ||||
|   parser.add_argument('--dataset',            type=str,   choices=['cifar10', 'cifar100', 'ImageNet16-120'], help='Choose between Cifar10/100 and ImageNet-16.') | ||||
|   parser.add_argument('--search_space',       type=str,   choices=['tss', 'sss'], help='Choose the search space.') | ||||
|  | ||||
|   parser.add_argument('--time_budget',        type=int,   default=20000, help='The total time cost budge for searching (in seconds).') | ||||
|   parser.add_argument('--loops_if_rand',      type=int,   default=500,   help='The total runs for evaluation.') | ||||
|   # log | ||||
|   parser.add_argument('--save_dir',           type=str,   help='Folder to save checkpoints and log.') | ||||
|   parser.add_argument('--rand_seed',          type=int,   default=-1,    help='manual seed') | ||||
|   args = parser.parse_args() | ||||
|    | ||||
|   if args.search_space == 'tss': | ||||
|     api = NASBench201API(verbose=False) | ||||
|   elif args.search_space == 'sss': | ||||
|     api = NASBench301API(verbose=False) | ||||
|   else: | ||||
|     raise ValueError('Invalid search space : {:}'.format(args.search_space)) | ||||
|  | ||||
|   args.save_dir = os.path.join('{:}-{:}'.format(args.save_dir, args.search_space), args.dataset, 'RANDOM') | ||||
|   print('save-dir : {:}'.format(args.save_dir)) | ||||
|  | ||||
|   if args.rand_seed < 0: | ||||
|     save_dir, all_info = None, {} | ||||
|     for i in range(args.loops_if_rand): | ||||
|       print ('{:} : {:03d}/{:03d}'.format(time_string(), i, args.loops_if_rand)) | ||||
|       args.rand_seed = random.randint(1, 100000) | ||||
|       save_dir, all_archs, all_total_times = main(args, api) | ||||
|       all_info[i] = {'all_archs': all_archs, | ||||
|                      'all_total_times': all_total_times} | ||||
|     save_path = save_dir / 'results.pth' | ||||
|     print('save into {:}'.format(save_path)) | ||||
|     torch.save(all_info, save_path) | ||||
|   else: | ||||
|     main(args, api) | ||||
| @@ -3,12 +3,12 @@ | ||||
| ################################################################## | ||||
| # Regularized Evolution for Image Classifier Architecture Search # | ||||
| ################################################################## | ||||
| # python ./exps/algos-v2/REA.py --dataset cifar10 --search_space tss --time_budget 12000 --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/REA.py --dataset cifar100 --search_space tss --time_budget 12000 --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/REA.py --dataset ImageNet16-120 --search_space tss --time_budget 12000 --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/REA.py --dataset cifar10 --search_space sss --time_budget 12000 --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/REA.py --dataset cifar100 --search_space sss --time_budget 12000 --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/REA.py --dataset ImageNet16-120 --search_space sss --time_budget 12000 --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/regularized_ea.py --dataset cifar10 --search_space tss --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/regularized_ea.py --dataset cifar100 --search_space tss --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/regularized_ea.py --dataset ImageNet16-120 --search_space tss --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/regularized_ea.py --dataset cifar10 --search_space sss --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/regularized_ea.py --dataset cifar100 --search_space sss --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| # python ./exps/algos-v2/regularized_ea.py --dataset ImageNet16-120 --search_space sss --ea_cycles 200 --ea_population 10 --ea_sample_size 3 --rand_seed 1 | ||||
| ################################################################## | ||||
| import os, sys, time, glob, random, argparse | ||||
| import numpy as np, collections | ||||
| @@ -160,7 +160,7 @@ def regularized_evolution(cycles, population_size, sample_size, time_budget, ran | ||||
|   while len(population) < population_size: | ||||
|     model = Model() | ||||
|     model.arch = random_arch() | ||||
|     model.accuracy, time_cost, total_cost = api.simulate_train_eval(model.arch, dataset, '12') | ||||
|     model.accuracy, _, _, total_cost = api.simulate_train_eval(model.arch, dataset, '12') | ||||
|     # Append the info | ||||
|     population.append(model) | ||||
|     history.append(model) | ||||
| @@ -183,7 +183,7 @@ def regularized_evolution(cycles, population_size, sample_size, time_budget, ran | ||||
|     # Create the child model and store it. | ||||
|     child = Model() | ||||
|     child.arch = mutate_arch(parent.arch) | ||||
|     child.accuracy, time_cost, total_cost = api.simulate_train_eval(model.arch, dataset, '12') | ||||
|     child.accuracy, _, _, total_cost = api.simulate_train_eval(model.arch, dataset, '12') | ||||
|     # Append the info | ||||
|     population.append(child) | ||||
|     history.append(child) | ||||
| @@ -195,11 +195,7 @@ def regularized_evolution(cycles, population_size, sample_size, time_budget, ran | ||||
| 
 | ||||
| 
 | ||||
| def main(xargs, api): | ||||
|   assert torch.cuda.is_available(), 'CUDA is not available.' | ||||
|   torch.backends.cudnn.enabled   = True | ||||
|   torch.backends.cudnn.benchmark = False | ||||
|   torch.backends.cudnn.deterministic = True | ||||
|   torch.set_num_threads(xargs.workers) | ||||
|   torch.set_num_threads(4) | ||||
|   prepare_seed(xargs.rand_seed) | ||||
|   logger = prepare_logger(args) | ||||
| 
 | ||||
| @@ -235,12 +231,11 @@ if __name__ == '__main__': | ||||
|   parser.add_argument('--ea_cycles',          type=int,   help='The number of cycles in EA.') | ||||
|   parser.add_argument('--ea_population',      type=int,   help='The population size in EA.') | ||||
|   parser.add_argument('--ea_sample_size',     type=int,   help='The sample size in EA.') | ||||
|   parser.add_argument('--time_budget',        type=int,   help='The total time cost budge for searching (in seconds).') | ||||
|   parser.add_argument('--loops_if_rand',      type=int,   default=500, help='The total runs for evaluation.') | ||||
|   parser.add_argument('--time_budget',        type=int,   default=20000, help='The total time cost budge for searching (in seconds).') | ||||
|   parser.add_argument('--loops_if_rand',      type=int,   default=500,   help='The total runs for evaluation.') | ||||
|   # log | ||||
|   parser.add_argument('--workers',            type=int,   default=2,    help='number of data loading workers (default: 2)') | ||||
|   parser.add_argument('--save_dir',           type=str,   default='./output/search', help='Folder to save checkpoints and log.') | ||||
|   parser.add_argument('--rand_seed',          type=int,   default=-1,   help='manual seed') | ||||
|   parser.add_argument('--rand_seed',          type=int,   default=-1,    help='manual seed') | ||||
|   args = parser.parse_args() | ||||
| 
 | ||||
|   if args.search_space == 'tss': | ||||
| @@ -3,12 +3,12 @@ | ||||
| ##################################################################################################### | ||||
| # modified from https://github.com/pytorch/examples/blob/master/reinforcement_learning/reinforce.py # | ||||
| ##################################################################################################### | ||||
| # python ./exps/algos-v2/reinforce.py --dataset cifar10 --search_space tss --time_budget 12000 --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset cifar100 --search_space tss --time_budget 12000 --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset ImageNet16-120 --search_space tss --time_budget 12000 --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset cifar10 --search_space sss --time_budget 12000 --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset cifar100 --search_space sss --time_budget 12000 --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset ImageNet16-120 --search_space sss --time_budget 12000 --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset cifar10 --search_space tss --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset cifar100 --search_space tss --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset ImageNet16-120 --search_space tss --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset cifar10 --search_space sss --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset cifar100 --search_space sss --learning_rate 0.001  | ||||
| # python ./exps/algos-v2/reinforce.py --dataset ImageNet16-120 --search_space sss --learning_rate 0.001  | ||||
| ##################################################################################################### | ||||
| import os, sys, time, glob, random, argparse | ||||
| import numpy as np, collections | ||||
| @@ -120,15 +120,10 @@ def select_action(policy): | ||||
|  | ||||
|  | ||||
| def main(xargs, api): | ||||
|   assert torch.cuda.is_available(), 'CUDA is not available.' | ||||
|   torch.backends.cudnn.enabled   = True | ||||
|   torch.backends.cudnn.benchmark = False | ||||
|   torch.backends.cudnn.deterministic = True | ||||
|   torch.set_num_threads(xargs.workers) | ||||
|   torch.set_num_threads(4) | ||||
|   prepare_seed(xargs.rand_seed) | ||||
|   logger = prepare_logger(args) | ||||
|    | ||||
|    | ||||
|   search_space = get_search_spaces(xargs.search_space, 'nas-bench-301') | ||||
|   if xargs.search_space == 'tss': | ||||
|     policy = PolicyTopology(search_space) | ||||
| @@ -144,6 +139,7 @@ def main(xargs, api): | ||||
|  | ||||
|   # nas dataset load | ||||
|   logger.log('{:} use api : {:}'.format(time_string(), api)) | ||||
|   api.reset_time() | ||||
|  | ||||
|   # REINFORCE | ||||
|   x_start_time = time.time() | ||||
| @@ -153,7 +149,7 @@ def main(xargs, api): | ||||
|     start_time = time.time() | ||||
|     log_prob, action = select_action( policy ) | ||||
|     arch   = policy.generate_arch( action ) | ||||
|     reward, _, current_total_cost = api.simulate_train_eval(arch, xargs.dataset, '12') | ||||
|     reward, _, _, current_total_cost = api.simulate_train_eval(arch, xargs.dataset, '12') | ||||
|     trace.append((reward, arch)) | ||||
|     total_costs.append(current_total_cost) | ||||
|  | ||||
| @@ -177,7 +173,7 @@ def main(xargs, api): | ||||
|   logger.log('-'*100) | ||||
|   logger.close() | ||||
|  | ||||
|   return logger.log_dir, [api.query_index_by_arch(x[0]) for x in trace], total_costs | ||||
|   return logger.log_dir, [api.query_index_by_arch(x[1]) for x in trace], total_costs | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
| @@ -186,15 +182,14 @@ if __name__ == '__main__': | ||||
|   parser.add_argument('--dataset',            type=str,   choices=['cifar10', 'cifar100', 'ImageNet16-120'], help='Choose between Cifar10/100 and ImageNet-16.') | ||||
|   parser.add_argument('--search_space',       type=str,   choices=['tss', 'sss'], help='Choose the search space.') | ||||
|   parser.add_argument('--learning_rate',      type=float, help='The learning rate for REINFORCE.') | ||||
|   parser.add_argument('--EMA_momentum',       type=float, default=0.9, help='The momentum value for EMA.') | ||||
|   parser.add_argument('--time_budget',        type=int,   help='The total time cost budge for searching (in seconds).') | ||||
|   parser.add_argument('--loops_if_rand',      type=int,   default=500, help='The total runs for evaluation.') | ||||
|   parser.add_argument('--EMA_momentum',       type=float, default=0.9,   help='The momentum value for EMA.') | ||||
|   parser.add_argument('--time_budget',        type=int,   default=20000, help='The total time cost budge for searching (in seconds).') | ||||
|   parser.add_argument('--loops_if_rand',      type=int,   default=500,   help='The total runs for evaluation.') | ||||
|   # log | ||||
|   parser.add_argument('--workers',            type=int,   default=2,   help='number of data loading workers (default: 2)') | ||||
|   parser.add_argument('--save_dir',           type=str,   default='./output/search', help='Folder to save checkpoints and log.') | ||||
|   parser.add_argument('--arch_nas_dataset',   type=str,   help='The path to load the architecture dataset (tiny-nas-benchmark).') | ||||
|   parser.add_argument('--print_freq',         type=int,   help='print frequency (default: 200)') | ||||
|   parser.add_argument('--rand_seed',          type=int,   default=-1,  help='manual seed') | ||||
|   parser.add_argument('--rand_seed',          type=int,   default=-1,    help='manual seed') | ||||
|   args = parser.parse_args() | ||||
|  | ||||
|   if args.search_space == 'tss': | ||||
|   | ||||
							
								
								
									
										17
									
								
								exps/algos-v2/run-all.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								exps/algos-v2/run-all.sh
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| #!/bin/bash | ||||
| # bash ./exps/algos-v2/run-all.sh | ||||
| echo script name: $0 | ||||
| echo $# arguments | ||||
|  | ||||
| datasets="cifar10 cifar100 ImageNet16-120" | ||||
| search_spaces="tss sss" | ||||
|  | ||||
|  | ||||
| for dataset in ${datasets} | ||||
| do | ||||
|   for search_space in ${search_spaces} | ||||
|   do | ||||
|     python ./exps/algos-v2/reinforce.py --dataset ${dataset} --search_space ${search_space} --learning_rate 0.001 | ||||
|     python ./exps/algos-v2/regularized_ea.py --dataset ${dataset} --search_space ${search_space} --ea_cycles 200 --ea_population 10 --ea_sample_size 3 | ||||
|   done | ||||
| done | ||||
		Reference in New Issue
	
	Block a user