-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
280 lines (219 loc) · 9.23 KB
/
model.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
""" Models used in the experiments """
import math
import torch
import torch.nn as nn
def weight_init(module):
if isinstance(module, nn.Linear):
nn.init.xavier_uniform_(module.weight)
module.bias.data.zero_()
class ControllerMonolithic(torch.nn.Module):
"""Class that represents the monolithic network"""
def __init__(self, D_in, H, D_out, min_std=1e-6, init_std=1.0, det=False):
super(ControllerMonolithic, self).__init__()
self.din = D_in
self.dout = D_out
self.controller = nn.Sequential(
nn.Linear(D_in, H),
nn.ReLU(),
nn.Linear(H, H),
nn.ReLU(),
nn.Linear(H, H),
nn.ReLU(),
nn.Linear(H, D_out),
)
self.sigma = nn.Parameter(torch.Tensor(D_out))
self.sigma.data.fill_(math.log(init_std))
self.min_log_std = math.log(min_std)
self.saved_log_probs = []
self.rewards = []
self.deterministic = det
def forward(self, x):
means = self.controller(x)
scales = torch.exp(torch.clamp(self.sigma, min=self.min_log_std))
dist = torch.distributions.Normal(means, scales)
action = dist.mean if self.deterministic else dist.sample()
log_prob = 0 if self.deterministic else dist.log_prob(action)
return action, log_prob, None
class ControllerInstinct(torch.nn.Module):
"""Single element of the modular network"""
def __init__(self, D_in, H, D_out):
super(ControllerInstinct, self).__init__()
self.din = D_in
self.dout = D_out
self.controller = nn.Sequential(
nn.Linear(D_in, H), nn.ReLU(), nn.Linear(H, H), nn.ReLU()
)
self.last_layer = nn.Sequential(nn.Linear(H, D_out), nn.Tanh())
self.strength_signal = nn.Sequential(nn.Linear(H, D_out), nn.Sigmoid())
def forward(self, x):
inter = self.controller(x)
means = self.last_layer(inter)
str_sig = self.strength_signal(inter)
return means, str_sig
class Controller(torch.nn.Module):
"""Single element of the modular network"""
def __init__(self, D_in, H, D_out, min_std=1e-6, init_std=1.0):
super(Controller, self).__init__()
self.din = D_in
self.dout = D_out
self.controller = nn.Sequential(
nn.Linear(D_in, H),
nn.ReLU(),
nn.Linear(H, H),
nn.ReLU(),
nn.Linear(H, D_out),
nn.Tanh(),
)
self.sigma = nn.Parameter(torch.Tensor(D_out))
self.sigma.data.fill_(math.log(init_std))
self.min_log_std = math.log(min_std)
self.deterministic = False
def forward(self, x):
means = self.controller(x)
scales = torch.exp(torch.clamp(self.sigma, min=self.min_log_std))
dist = torch.distributions.Normal(means, scales)
action = dist.mean if self.deterministic else dist.sample()
log_prob = 0 if self.deterministic else dist.log_prob(action)
return action, log_prob
class ControllerInstinctSigma(torch.nn.Module):
"""Single element of the modular network"""
def __init__(self, D_in, H, D_out):
super(ControllerInstinctSigma, self).__init__()
D_in += 1
self.din = D_in
self.controller = nn.Sequential(
nn.Linear(D_in, H),
nn.ReLU(),
nn.Linear(H, H),
nn.ReLU(),
nn.Linear(H, D_out),
nn.Tanh(),
)
self.sigma_controller = nn.Sequential(
nn.Linear(D_in, H),
nn.ReLU(),
nn.Linear(H, H),
nn.ReLU(),
nn.Linear(H, D_out),
nn.Sigmoid(),
)
self.controller.deterministic = False
self.apply(weight_init)
def forward(self, x):
means = self.controller(x)
sigmas = self.sigma_controller(x)
dist = torch.distributions.Normal(means, sigmas)
action = dist.mean if self.controller.deterministic else dist.sample()
log_prob = 0 if self.controller.deterministic else dist.log_prob(action)
return action, log_prob, sigmas
def get_combinator_params(self):
# comb_params = []
# dct = self.named_parameters()
# for pkey, ptensor in dct:
# if "combinator" in pkey or pkey == "sigma":
# comb_params.append(ptensor)
# return comb_params
# return self.controller.parameters()
return super(ControllerInstinctSigma, self).parameters()
def get_evolvable_params(self):
return super(ControllerInstinctSigma, self).parameters()
def parameters(self):
return super(ControllerInstinctSigma, self).parameters()
class ControllerCombinator(torch.nn.Module):
""" The combinator that is modified during lifetime"""
def __init__(self, D_in, H, D_out, min_std=1e-6, init_std=0.1, load_instinct=False):
super(ControllerCombinator, self).__init__()
# Initialize the modules
self.controller = Controller(D_in + 1, H, D_out, init_std=init_std)
self.instinct = ControllerInstinct(D_in + 1, H, D_out)
if load_instinct:
pretrained_inst = torch.load("instinct.pt")
self.instinct.load_state_dict(pretrained_inst.state_dict())
# Initialize the combinator dimensions and the combinator
combinator_input_size = 2
self.combinators = nn.ModuleList()
for _ in range(D_out):
self.combinators.append(nn.Linear(combinator_input_size, 1))
# self.combinator = nn.Sequential(nn.Linear(combinator_input_size, D_out))
self.sigma = nn.Parameter(torch.Tensor(D_out))
self.apply(weight_init)
self.sigma.data.fill_(math.log(init_std))
self.min_log_std = math.log(min_std)
self.freeze_instinct = load_instinct
def forward(self, x):
# Pass the input to the submodules
stoch_action, log_prob = self.controller(x)
alt_action, control = self.instinct(x)
controlled_stoch_action = stoch_action * control
# Separate the output dimensions
output_dims = [
torch.tensor(ins) for ins in zip(controlled_stoch_action, alt_action)
]
# comb_x = torch.cat((controlled_stoch_action, alt_action))
# Combine actions into the final action
output_list = [
combinator(comb_x)
for combinator, comb_x in zip(self.combinators, output_dims)
]
final_action = torch.cat(output_list)
return final_action, log_prob + torch.log(control), control
def get_combinator_params(self):
# comb_params = []
# dct = self.named_parameters()
# for pkey, ptensor in dct:
# if "combinator" in pkey or pkey == "sigma":
# comb_params.append(ptensor)
# return comb_params
return self.controller.parameters()
def get_evolvable_params(self):
comb_params = []
dct = self.named_parameters()
for pkey, ptensor in dct:
if not ("instinct" in pkey and self.freeze_instinct):
comb_params.append(ptensor)
return comb_params
def parameters(self):
return super.parameters()
class ControllerNonParametricCombinator(torch.nn.Module):
""" The combinator that is modified during lifetime"""
def __init__(self, D_in, H, D_out, min_std=1e-6, init_std=0.1, load_instinct=False):
super(ControllerNonParametricCombinator, self).__init__()
# Initialize the modules
self.controller = Controller(D_in, H, D_out, init_std=init_std)
self.instinct = ControllerInstinct(D_in, H, D_out)
if load_instinct:
loaded_instinct = torch.load("instinct.pt")
self.instinct.load_state_dict(self.instinct.state_dict())
# Initialize the combinator dimensions and the combinator
# combinator_input_size = 2
# self.combinators = nn.ModuleList()
# for _ in range(D_out):
# self.combinators.append(nn.Linear(combinator_input_size, 1))
# self.combinator = nn.Sequential(nn.Linear(combinator_input_size, D_out))
self.apply(weight_init)
self.freeze_instinct = load_instinct
def forward(self, x):
# Pass the input to the submodules
stoch_action, log_prob = self.controller(x)
instinct_action, control = self.instinct(x)
controlled_stoch_action = stoch_action * control
controlled_instinct_action = instinct_action * (1 - control)
final_action = controlled_stoch_action + controlled_instinct_action
return final_action, log_prob + torch.log(control), control
def get_combinator_params(self):
# comb_params = []
# dct = self.named_parameters()
# for pkey, ptensor in dct:
# if "combinator" in pkey or pkey == "sigma":
# comb_params.append(ptensor)
# return comb_params
return self.controller.parameters()
def get_evolvable_params(self):
comb_params = []
dct = self.named_parameters()
for pkey, ptensor in dct:
if not ("instinct" in pkey and self.freeze_instinct):
comb_params.append(ptensor)
return comb_params
def parameters(self):
return super(ControllerNonParametricCombinator, self).parameters()