-
Notifications
You must be signed in to change notification settings - Fork 13
/
random_search.py
82 lines (67 loc) · 2.87 KB
/
random_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from Worker import Worker, get_acc
import numpy as np
import logging
from multiprocessing import Process, Queue
import random
from operations import *
import multiprocessing
multiprocessing.set_start_method('spawn', force=True)
def consume(worker, results_queue):
get_acc(worker)
results_queue.put(worker)
class RandomSearch(object):
def __init__(self, args):
self.args = args
self.arch_epochs = args.arch_epochs
self.episodes = args.episodes
def random_sample(self):
steps = 4
len_nodes = steps + 1
len_OPS = len(OP_NAME)
len_combs = len(COMB_NAME)
nodes = list(range(len_nodes))
OPS = list(range(len_OPS))
combs = list(range(len_combs))
actions_index = []
for type in range(2):
for node in range(steps):
actions_index.append(random.choice(nodes[:node+2]))
actions_index.append(random.choice(nodes[:node+2]))
actions_index.append(random.choice(OPS))
actions_index.append(random.choice(OPS))
actions_index.append(random.choice(combs))
return actions_index
def multi_solve_environment(self):
workers_top20 = []
for arch_epoch in range(self.arch_epochs):
results_queue = Queue()
processes = []
for episode in range(self.episodes):
actions_index = self.random_sample()
if episode < self.episodes // 3:
worker = Worker(None, None, actions_index, self.args, 'cuda:0')
elif self.episodes // 3 <= episode < 2 * self.episodes // 3:
worker = Worker(None, None, actions_index, self.args, 'cuda:1')
else:
worker = Worker(None, None, actions_index, self.args, 'cuda:3')
process = Process(target=consume, args=(worker, results_queue))
process.start()
processes.append(process)
for process in processes:
process.join()
workers = []
for episode in range(self.episodes):
worker = results_queue.get()
workers.append(worker)
# sort worker retain top20
workers_total = workers_top20 + workers
workers_total.sort(key=lambda worker: worker.acc, reverse=True)
workers_top20 = workers_total[:20]
top1_acc = workers_top20[0].acc
top5_avg_acc = np.mean([worker.acc for worker in workers_top20[:5]])
top20_avg_acc = np.mean([worker.acc for worker in workers_top20])
logging.info(
'arch_epoch {:0>3d} top1_acc {:.4f} top5_avg_acc {:.4f} top20_avg_acc {:.4f}'.format(
arch_epoch, top1_acc, top5_avg_acc, top20_avg_acc))
for i in range(5):
print(workers_top20[i].genotype)