Update REA, REINFORCE, and RANDOM
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
# Random Search for Hyper-Parameter Optimization, JMLR 2012 ##################
|
||||
##############################################################################
|
||||
# python ./exps/algos-v2/random_wo_share.py --dataset cifar10 --search_space tss
|
||||
# python ./exps/algos-v2/random_wo_share.py --dataset cifar100 --search_space tss
|
||||
# python ./exps/algos-v2/random_wo_share.py --dataset ImageNet16-120 --search_space tss
|
||||
##############################################################################
|
||||
import os, sys, time, glob, random, argparse
|
||||
import numpy as np, collections
|
||||
@@ -20,7 +22,7 @@ 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
|
||||
from regularized_ea import random_topology_func, random_size_func
|
||||
|
||||
|
||||
def main(xargs, api):
|
||||
@@ -28,16 +30,18 @@ def main(xargs, api):
|
||||
prepare_seed(xargs.rand_seed)
|
||||
logger = prepare_logger(args)
|
||||
|
||||
logger.log('{:} use api : {:}'.format(time_string(), api))
|
||||
api.reset_time()
|
||||
|
||||
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:
|
||||
current_best_index = []
|
||||
while len(total_time_cost) == 0 or 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)
|
||||
@@ -45,13 +49,14 @@ def main(xargs, api):
|
||||
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))
|
||||
current_best_index.append(api.query_index_by_arch(best_arch))
|
||||
logger.log('{:} best arch is {:}, accuracy = {:.2f}%, visit {:} archs with {:.1f} s.'.format(time_string(), best_arch, best_acc, len(history), total_time_cost[-1]))
|
||||
|
||||
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
|
||||
return logger.log_dir, current_best_index, total_time_cost
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -62,7 +67,7 @@ if __name__ == '__main__':
|
||||
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('--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')
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -77,7 +82,7 @@ if __name__ == '__main__':
|
||||
print('save-dir : {:}'.format(args.save_dir))
|
||||
|
||||
if args.rand_seed < 0:
|
||||
save_dir, all_info = None, {}
|
||||
save_dir, all_info = None, collections.OrderedDict()
|
||||
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)
|
||||
|
@@ -155,7 +155,7 @@ def regularized_evolution(cycles, population_size, sample_size, time_budget, ran
|
||||
population = collections.deque()
|
||||
api.reset_time()
|
||||
history, total_time_cost = [], [] # Not used by the algorithm, only used to report results.
|
||||
|
||||
current_best_index = []
|
||||
# Initialize the population with random models.
|
||||
while len(population) < population_size:
|
||||
model = Model()
|
||||
@@ -163,8 +163,9 @@ def regularized_evolution(cycles, population_size, sample_size, time_budget, ran
|
||||
model.accuracy, _, _, total_cost = api.simulate_train_eval(model.arch, dataset, '12')
|
||||
# Append the info
|
||||
population.append(model)
|
||||
history.append(model)
|
||||
history.append((model.accuracy, model.arch))
|
||||
total_time_cost.append(total_cost)
|
||||
current_best_index.append(api.query_index_by_arch(max(history, key=lambda x: x[0])[1]))
|
||||
|
||||
# Carry out evolution in cycles. Each cycle produces a model and removes another.
|
||||
while total_time_cost[-1] < time_budget:
|
||||
@@ -183,15 +184,16 @@ 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, _, _, total_cost = api.simulate_train_eval(model.arch, dataset, '12')
|
||||
child.accuracy, _, _, total_cost = api.simulate_train_eval(child.arch, dataset, '12')
|
||||
# Append the info
|
||||
population.append(child)
|
||||
history.append(child)
|
||||
history.append((child.accuracy, child.arch))
|
||||
current_best_index.append(api.query_index_by_arch(max(history, key=lambda x: x[0])[1]))
|
||||
total_time_cost.append(total_cost)
|
||||
|
||||
# Remove the oldest model.
|
||||
population.popleft()
|
||||
return history, total_time_cost
|
||||
return history, current_best_index, total_time_cost
|
||||
|
||||
|
||||
def main(xargs, api):
|
||||
@@ -210,7 +212,7 @@ def main(xargs, api):
|
||||
x_start_time = time.time()
|
||||
logger.log('{:} use api : {:}'.format(time_string(), api))
|
||||
logger.log('-'*30 + ' start searching with the time budget of {:} s'.format(xargs.time_budget))
|
||||
history, total_times = regularized_evolution(xargs.ea_cycles, xargs.ea_population, xargs.ea_sample_size, xargs.time_budget, random_arch, mutate_arch, api, xargs.dataset)
|
||||
history, current_best_index, total_times = regularized_evolution(xargs.ea_cycles, xargs.ea_population, xargs.ea_sample_size, xargs.time_budget, random_arch, mutate_arch, api, xargs.dataset)
|
||||
logger.log('{:} regularized_evolution finish with history of {:} arch with {:.1f} s (real-cost={:.2f} s).'.format(time_string(), len(history), total_times[-1], time.time()-x_start_time))
|
||||
best_arch = max(history, key=lambda i: i.accuracy)
|
||||
best_arch = best_arch.arch
|
||||
@@ -220,7 +222,7 @@ def main(xargs, api):
|
||||
logger.log('{:}'.format(info))
|
||||
logger.log('-'*100)
|
||||
logger.close()
|
||||
return logger.log_dir, [api.query_index_by_arch(x.arch) for x in history], total_times
|
||||
return logger.log_dir, current_best_index, total_times
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -249,7 +251,7 @@ if __name__ == '__main__':
|
||||
print('save-dir : {:}'.format(args.save_dir))
|
||||
|
||||
if args.rand_seed < 0:
|
||||
save_dir, all_info = None, {}
|
||||
save_dir, all_info = None, collections.OrderedDict()
|
||||
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)
|
||||
|
@@ -145,6 +145,7 @@ def main(xargs, api):
|
||||
x_start_time = time.time()
|
||||
logger.log('Will start searching with time budget of {:} s.'.format(xargs.time_budget))
|
||||
total_steps, total_costs, trace = 0, [], []
|
||||
current_best_index = []
|
||||
while len(total_costs) == 0 or total_costs[-1] < xargs.time_budget:
|
||||
start_time = time.time()
|
||||
log_prob, action = select_action( policy )
|
||||
@@ -162,9 +163,8 @@ def main(xargs, api):
|
||||
# accumulate time
|
||||
total_steps += 1
|
||||
logger.log('step [{:3d}] : average-reward={:.3f} : policy_loss={:.4f} : {:}'.format(total_steps, baseline.value(), policy_loss.item(), policy.genotype()))
|
||||
#logger.log('----> {:}'.format(policy.arch_parameters))
|
||||
#logger.log('')
|
||||
|
||||
# to analyze
|
||||
current_best_index.append(api.query_index_by_arch(max(trace, key=lambda x: x[0])[1]))
|
||||
# best_arch = policy.genotype() # first version
|
||||
best_arch = max(trace, key=lambda x: x[0])[1]
|
||||
logger.log('REINFORCE finish with {:} steps and {:.1f} s (real cost={:.3f}).'.format(total_steps, total_costs[-1], time.time()-x_start_time))
|
||||
@@ -173,7 +173,7 @@ def main(xargs, api):
|
||||
logger.log('-'*100)
|
||||
logger.close()
|
||||
|
||||
return logger.log_dir, [api.query_index_by_arch(x[1]) for x in trace], total_costs
|
||||
return logger.log_dir, current_best_index, total_costs
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -203,7 +203,7 @@ if __name__ == '__main__':
|
||||
print('save-dir : {:}'.format(args.save_dir))
|
||||
|
||||
if args.rand_seed < 0:
|
||||
save_dir, all_info = None, {}
|
||||
save_dir, all_info = None, collections.OrderedDict()
|
||||
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)
|
||||
|
@@ -13,5 +13,6 @@ do
|
||||
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
|
||||
python ./exps/algos-v2/random_wo_share.py --dataset ${dataset} --search_space ${search_space}
|
||||
done
|
||||
done
|
||||
|
Reference in New Issue
Block a user