Update REA, REINFORCE, and RANDOM
This commit is contained in:
parent
6dc494be08
commit
ebad9197f7
@ -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
|
||||
|
@ -3,7 +3,7 @@
|
||||
###############################################################
|
||||
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2020.06 #
|
||||
###############################################################
|
||||
# Usage: python exps/experimental/vis-bench-algos.py
|
||||
# Usage: python exps/experimental/vis-bench-algos.py #
|
||||
###############################################################
|
||||
import os, sys, time, torch, argparse
|
||||
import numpy as np
|
||||
@ -30,6 +30,7 @@ def fetch_data(root_dir='./output/search', search_space='tss', dataset=None):
|
||||
alg2name, alg2path = OrderedDict(), OrderedDict()
|
||||
alg2name['REA'] = 'R-EA-SS3'
|
||||
alg2name['REINFORCE'] = 'REINFORCE-0.001'
|
||||
# alg2name['RANDOM'] = 'RANDOM'
|
||||
for alg, name in alg2name.items():
|
||||
alg2path[alg] = os.path.join(ss_dir, dataset, name, 'results.pth')
|
||||
assert os.path.isfile(alg2path[alg])
|
||||
@ -62,14 +63,15 @@ def visualize_curve(api, vis_save_dir, search_space, max_time):
|
||||
vis_save_dir = vis_save_dir.resolve()
|
||||
vis_save_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
dpi, width, height = 250, 4700, 1500
|
||||
dpi, width, height = 250, 5100, 1500
|
||||
figsize = width / float(dpi), height / float(dpi)
|
||||
LabelSize, LegendFontsize = 14, 14
|
||||
|
||||
def sub_plot_fn(ax, dataset):
|
||||
alg2data = fetch_data(search_space=search_space, dataset=dataset)
|
||||
alg2accuracies = OrderedDict()
|
||||
time_tickets = [float(i) / 100 * max_time for i in range(100)]
|
||||
total_tickets = 150
|
||||
time_tickets = [float(i) / total_tickets * max_time for i in range(total_tickets)]
|
||||
colors = ['b', 'g', 'c', 'm', 'y']
|
||||
for idx, (alg, data) in enumerate(alg2data.items()):
|
||||
print('plot alg : {:}'.format(alg))
|
||||
@ -78,7 +80,10 @@ def visualize_curve(api, vis_save_dir, search_space, max_time):
|
||||
accuracy = query_performance(api, data, dataset, ticket)
|
||||
accuracies.append(accuracy)
|
||||
alg2accuracies[alg] = accuracies
|
||||
ax.plot(time_tickets, accuracies, c=colors[idx], label='{:}'.format(alg))
|
||||
ax.plot([x/100 for x in time_tickets], accuracies, c=colors[idx], label='{:}'.format(alg))
|
||||
ax.set_xlabel('Estimated wall-clock time (1e2 seconds)', fontsize=LabelSize)
|
||||
ax.set_ylabel('Test accuracy on {:}'.format(dataset), fontsize=LabelSize)
|
||||
ax.set_title('Searching results on {:}'.format(dataset), fontsize=LabelSize+4)
|
||||
ax.legend(loc=4, fontsize=LegendFontsize)
|
||||
|
||||
fig, axs = plt.subplots(1, 3, figsize=figsize)
|
||||
@ -104,4 +109,3 @@ if __name__ == '__main__':
|
||||
visualize_curve(api201, save_dir, 'tss', args.max_time)
|
||||
api301 = NASBench301API(verbose=False)
|
||||
visualize_curve(api301, save_dir, 'sss', args.max_time)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user