forked from DHDev0/Muzero-unplugged
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_network_mlp_model.py
298 lines (236 loc) · 11.7 KB
/
neural_network_mlp_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import torch
import torch.nn as nn
import math
# # # https://arxiv.org/pdf/1911.08265.pdf [page: 3 and 4] for the structure
# # # Multilayer perceptron (MLP) for muzero with 1D observation and discrete action
class Representation_function(nn.Module):
def __init__(self,
observation_space_dimensions,
state_dimension,
action_dimension,
hidden_layer_dimensions,
number_of_hidden_layer):
super().__init__()
self.action_space = action_dimension
# # # add to sequence|first and recursive|,, whatever you need
linear_in = nn.Linear(observation_space_dimensions, hidden_layer_dimensions)
linear_mid = nn.Linear(hidden_layer_dimensions, hidden_layer_dimensions)
linear_out = nn.Linear(hidden_layer_dimensions, state_dimension)
self.scale = nn.Tanh()
layernom_init = nn.BatchNorm1d(observation_space_dimensions)
layernorm_recur = nn.BatchNorm1d(hidden_layer_dimensions)
# 0.1, 0.2 , 0.25 , 0.5 parameter (first two more recommended for rl)
dropout = nn.Dropout(0.1)
activation = nn.ELU() # , nn.ELU() , nn.GELU, nn.ELU() , nn.ELU
first_layer_sequence = [
linear_in,
activation
]
recursive_layer_sequence = [
linear_mid,
activation
]
sequence = first_layer_sequence + \
(recursive_layer_sequence*number_of_hidden_layer)
self.state_norm = nn.Sequential(*tuple(sequence+[nn.Linear(hidden_layer_dimensions, state_dimension)]))
# self.state_norm = nn.Linear(observation_space_dimensions, state_dimension)
def forward(self, state):
return scale_to_bound_action(self.state_norm(state))
# # # https://arxiv.org/pdf/1911.08265.pdf [page: 3 and 4] for the structure
# # # Multilayer perceptron (MLP) for muzero with 1D observation and discrete action
class Dynamics_function(nn.Module):
def __init__(self,
state_dimension,
action_dimension,
observation_space_dimensions,
hidden_layer_dimensions,
number_of_hidden_layer):
super().__init__()
self.action_space = action_dimension
# # # add to sequence|first and recursive|, whatever you need
linear_in = nn.Linear(state_dimension + action_dimension,hidden_layer_dimensions)
linear_mid = nn.Linear(hidden_layer_dimensions, hidden_layer_dimensions)
linear_out_reward = nn.Linear(hidden_layer_dimensions,state_dimension)
linear_out_state = nn.Linear(hidden_layer_dimensions, state_dimension)
layernom_init = nn.BatchNorm1d(state_dimension + action_dimension)
layernorm_recur = nn.BatchNorm1d(hidden_layer_dimensions)
dropout = nn.Dropout(0.1)
activation = nn.ELU()
first_layer_sequence = [
linear_in,
activation
]
recursive_layer_sequence = [
linear_mid,
activation
]
sequence = first_layer_sequence + \
(recursive_layer_sequence*number_of_hidden_layer)
self.reward = nn.Sequential(*tuple(sequence +[linear_out_reward]))
self.next_state_normalized = nn.Sequential(*tuple(sequence +[linear_out_state]))
def forward(self, state_normalized, action):
x = torch.cat([state_normalized.T, action.T]).T
return self.reward(x), scale_to_bound_action(self.next_state_normalized(x))
# # # https://arxiv.org/pdf/1911.08265.pdf [page: 3 and 4] for the structure
# # # Multilayer perceptron (MLP) for muzero with 1D observation and discrete action
class Prediction_function(nn.Module):
def __init__(self,
state_dimension,
action_dimension,
observation_space_dimensions,
hidden_layer_dimensions,
number_of_hidden_layer):
super().__init__()
linear_in = nn.Linear(state_dimension, hidden_layer_dimensions)
linear_mid = nn.Linear(hidden_layer_dimensions, hidden_layer_dimensions)
linear_out_policy = nn.Linear(hidden_layer_dimensions,action_dimension)
linear_out_value = nn.Linear(hidden_layer_dimensions,state_dimension)
layernom_init = nn.BatchNorm1d(state_dimension)
layernorm_recur = nn.BatchNorm1d(hidden_layer_dimensions)
dropout = nn.Dropout(0.5)
activation = nn.ELU()
first_layer_sequence = [
linear_in,
activation
]
recursive_layer_sequence = [
linear_mid,
activation
]
sequence = first_layer_sequence + \
(recursive_layer_sequence*number_of_hidden_layer)
self.policy = nn.Sequential(*tuple(sequence + [linear_out_policy]))
self.value = nn.Sequential(*tuple(sequence + [linear_out_value]))
def forward(self, state_normalized):
return self.policy(state_normalized), self.value(state_normalized)
# # # https://arxiv.org/pdf/1911.08265.pdf [page: 15]
# # # To improve the learning process and bound the activations,
# # # we also scale the hidden state to the same range as
# # # the action input
def scale_to_bound_action(x):
min_next_encoded_state = x.min(1, keepdim=True)[0]
max_next_encoded_state = x.max(1, keepdim=True)[0]
scale_next_encoded_state = max_next_encoded_state - min_next_encoded_state
scale_next_encoded_state[scale_next_encoded_state < 1e-5] += 1e-5
next_encoded_state_normalized = (
x - min_next_encoded_state
) / scale_next_encoded_state
return next_encoded_state_normalized
import numpy as np
class Loss_function:
def __init__(self, parameter = (0), prediction = "no_transform",label = "no_transform"):
"""_
Loss function and pre-transform.
Example
-------
init class:
loss = Loss_function(prediction = "no_transform",
label = "no_transform")
You could use a list of transform to apply such as ["softmax_softmax","clamp_softmax"]
ps: if you add transform just be carefull to not add transform which break the gradient graph of pytorch
Parameters
----------
Transform
---------
"no_transform" : return the input
"softmax_transform" : softmax the input
"zero_clamp_transform" : to solve log(0)
refer to : https://github.com/pytorch/pytorch/blob/949559552004db317bc5ca53d67f2c62a54383f5/aten/src/THNN/generic/BCECriterion.c#L27
"clamp_transform" : bound value betwen 0.01 to 0.99
Loss function
-------------
https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
loss.kldiv
https://en.wikipedia.org/wiki/Cross_entropy
loss.cross_entropy
https://en.wikipedia.org/wiki/Mean_squared_error
loss.mse
https://en.wikipedia.org/wiki/Root-mean-square_deviation
loss.rmse
https://en.wikipedia.org/wiki/Residual_sum_of_squares
loss.square_error
zero loss (set loss to 0)
loss.zero_loss
"""
self.transform = {
"no_transform" : lambda x : x ,
"softmax_transform" : lambda x : torch.nn.Softmax(dim=1)(x),
"zero_clamp_transform" : lambda x : x + 1e-9,
"sigmoid_transform": lambda x : torch.nn.Sigmoid()(x),
"tanh_transform": lambda x : torch.nn.Tanh()(x),
"relu_transform": lambda x : torch.nn.ELU() (x),
"shrink_transform": lambda x : torch.nn.Softshrink(lambd=1e-3)(x),
}
if isinstance(prediction,str):
self.prediction_transform = self.transform[prediction]
if isinstance(label,str):
self.label_transform = self.transform[label]
if isinstance(prediction,list):
self.prediction = prediction
self.prediction_transform = lambda x : self.multiple_transform(x,"pred")
if isinstance(label,list):
self.label = label
self.label_transform = lambda x : self.multiple_transform(x,"lab")
self.parameter = parameter
def multiple_transform(self,x,dict_transform):
if dict_transform == "pred":
dict_transform = self.prediction
else:
dict_transform = self.label
for i in dict_transform:
x = self.transform[i](x)
return x
def kldiv(self, input, target):
p = self.label_transform(target)
q = self.prediction_transform(input)
return (p*(torch.log(p)-torch.log(q))).sum(1)
def cross_entropy(self, input, target):
p = self.label_transform(target)
q = self.prediction_transform(input)
return (-p*torch.log(q)).sum(1)
def square_error(self, input, target):
p = self.label_transform(target)
q = self.prediction_transform(input)
return ((p-q)**(1/2)).sum(1)
def mse(self, input, target):
p = self.label_transform(target)
q = self.prediction_transform(input)
return ((p-q)**2).mean(1)
def rmse(self, input, target):
p = self.label_transform(target)
q = self.prediction_transform(input)
return torch.sqrt(((p-q)**2).mean(1))
def zero_loss(self, input, target):
return(input+target).sum(1)*0
# # # L1 Regularization
# # # Explain at : https://paperswithcode.com/method/l1-regularization
def l1(models, l1_weight_decay=0.0001):
l1_parameters = []
for parameter_1, parameter_2, parameter_3 in zip(models[0].parameters(), models[1].parameters(), models[2].parameters()):
l1_parameters.extend(
(parameter_1.view(-1), parameter_2.view(-1), parameter_3.view(-1)))
return l1_weight_decay * torch.abs(torch.cat(l1_parameters)).sum()
# # # https://arxiv.org/pdf/1911.08265.pdf [page: 4]
# # # L2 Regularization manually
# # # or can be done using weight_decay from ADAM or SGD
# # # Explain at : https://paperswithcode.com/task/l2-regularization
def l2(models, l2_weight_decay=0.0001):
l2_parameters = []
for parameter_1, parameter_2, parameter_3 in zip(models[0].parameters(), models[1].parameters(), models[2].parameters()):
l2_parameters.extend(
(parameter_1.view(-1), parameter_2.view(-1), parameter_3.view(-1)))
return l2_weight_decay * torch.square(torch.cat(l2_parameters)).sum()
def weights_init(m):
# # # std constant :
# # https://en.wikipedia.org/wiki/Fine-structure_constant
# # https://en.wikipedia.org/wiki/Dimensionless_physical_constant
if isinstance(m, nn.Linear):
torch.nn.init.zeros_(m.weight)
torch.nn.init.zeros_(m.bias)
torch.nn.init.normal_(m.weight, mean=0.0, std=1/137.035999)
torch.nn.init.normal_(m.bias, mean=0.0, std=1/137.035999)
if isinstance(m, nn.Conv2d):
torch.nn.init.zeros_(m.weight)
torch.nn.init.zeros_(m.bias)
torch.nn.init.normal_(m.weight, mean=0.0, std=1/137.035999)
torch.nn.init.normal_(m.bias, mean=0.0, std=1/137.035999)