-
Notifications
You must be signed in to change notification settings - Fork 14
/
controller.py
185 lines (154 loc) · 7.43 KB
/
controller.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import torch.nn.functional as F
from torch.distributions import Categorical
from operations import *
class Controller(nn.Module):
def __init__(self, args, hidden_size=100, steps=4, device='cpu'):
super(Controller, self).__init__()
self.embedding_size = args.embedding_size
self.len_nodes = steps + 1
self.len_OPS = len(OP_NAME)
self.len_combs = len(COMB_NAME)
self.hidden_size = hidden_size
self.steps = steps
self.device = device
len_action = self.len_nodes + self.len_OPS + self.len_combs
self.embedding = nn.Embedding(len_action, self.embedding_size)
self.node_decoders = nn.ModuleList()
for step in range(steps):
self.node_decoders.append(nn.Linear(hidden_size, step+2))
#operations: identity, 3x3 conv, 3x3 maxpool
self.op_decoder = nn.Linear(hidden_size, self.len_OPS)
#combine: add, concat
self.comb_decoder = nn.Linear(hidden_size, self.len_combs)
self.rnn = nn.LSTMCell(self.embedding_size, hidden_size)
self.init_parameters()
def forward(self, input, h_t, c_t, decoder):
input = self.embedding(input)
h_t, c_t = self.rnn(input, (h_t, c_t))
logits = decoder(h_t)
return h_t, c_t, logits
def sample(self):
input = torch.LongTensor([self.len_nodes + self.len_OPS]).to(self.device)
h_t, c_t = self.init_hidden()
actions_p = []
actions_log_p = []
actions_index = []
for type in range(2):
for node in range(self.steps):
#node1
h_t, c_t, logits = self.forward(input, h_t, c_t, self.node_decoders[node])
action_index = Categorical(logits=logits).sample()
p = F.softmax(logits, dim=-1)[0,action_index]
log_p =F.log_softmax(logits, dim=-1)[0,action_index]
actions_p.append(p.detach())
actions_log_p.append(log_p.detach())
actions_index.append(action_index)
#node2
input = action_index
h_t, c_t, logits = self.forward(input, h_t, c_t, self.node_decoders[node])
action_index = Categorical(logits=logits).sample()
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p.detach())
actions_log_p.append(log_p.detach())
actions_index.append(action_index)
#op1
input = action_index
h_t, c_t, logits = self.forward(input, h_t, c_t, self.op_decoder)
action_index = Categorical(logits=logits).sample()
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p.detach())
actions_log_p.append(log_p.detach())
actions_index.append(action_index)
#op2
input = action_index + self.len_nodes
h_t, c_t, logits = self.forward(input, h_t, c_t, self.op_decoder)
action_index = Categorical(logits=logits).sample()
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p.detach())
actions_log_p.append(log_p.detach())
actions_index.append(action_index)
#comb
input = action_index + self.len_nodes
h_t, c_t, logits = self.forward(input, h_t, c_t, self.comb_decoder)
action_index = Categorical(logits=logits).sample()
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p.detach())
actions_log_p.append(log_p.detach())
actions_index.append(action_index)
input = action_index + self.len_nodes + self.len_OPS
actions_p = torch.cat(actions_p)
actions_log_p = torch.cat(actions_log_p)
actions_index = torch.cat(actions_index)
return actions_p, actions_log_p, actions_index
def get_p(self, actions_index):
input = torch.LongTensor([self.len_nodes + self.len_OPS]).to(self.device)
h_t, c_t = self.init_hidden()
t = 0
actions_p = []
actions_log_p = []
for type in range(2):
for node in range(self.steps):
# node1
h_t, c_t, logits = self.forward(input, h_t, c_t, self.node_decoders[node])
action_index = actions_index[t].unsqueeze(0)
t += 1
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p)
actions_log_p.append(log_p)
# node2
input = action_index
h_t, c_t, logits = self.forward(input, h_t, c_t, self.node_decoders[node])
action_index = actions_index[t].unsqueeze(0)
t += 1
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p)
actions_log_p.append(log_p)
# op1
input = action_index
h_t, c_t, logits = self.forward(input, h_t, c_t, self.op_decoder)
action_index = actions_index[t].unsqueeze(0)
t += 1
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p)
actions_log_p.append(log_p)
# op2
input = action_index + self.len_nodes
h_t, c_t, logits = self.forward(input, h_t, c_t, self.op_decoder)
action_index = actions_index[t].unsqueeze(0)
t += 1
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p)
actions_log_p.append(log_p)
# comb
input = action_index + self.len_nodes
h_t, c_t, logits = self.forward(input, h_t, c_t, self.comb_decoder)
action_index = actions_index[t].unsqueeze(0)
t += 1
p = F.softmax(logits, dim=-1)[0, action_index]
log_p = F.log_softmax(logits, dim=-1)[0, action_index]
actions_p.append(p)
actions_log_p.append(log_p)
input = action_index + self.len_nodes + self.len_OPS
actions_p = torch.cat(actions_p)
actions_log_p = torch.cat(actions_log_p)
return actions_p, actions_log_p
def init_hidden(self):
h_t = torch.zeros(1, self.hidden_size, dtype=torch.float, device=self.device)
c_t = torch.zeros(1, self.hidden_size, dtype=torch.float, device=self.device)
return (h_t, c_t)
def init_parameters(self):
init_range = 0.1
for param in self.parameters():
param.data.uniform_(-init_range, init_range)
for decoder in self.node_decoders:
decoder.bias.data.fill_(0)
self.op_decoder.bias.data.fill_(0)
self.comb_decoder.bias.data.fill_(0)