-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnets.py
251 lines (218 loc) · 7.22 KB
/
nets.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
import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.distributions import Normal, MultivariateNormal
###########################################################################
#
# Classes
#
###########################################################################
class Memory:
'''
Description:
The Memory class allows to store and sample events
Attributes:
capacity -- max amount of events stored
data -- list with events memorized
pointer -- position of the list in which an event will be registered
Methods:
store -- save one event in "data" in the position indicated by "pointer"
sample -- returns a uniformly sampled batch of stored events
retrieve -- returns the whole information memorized
forget -- elliminates all data stored
'''
def __init__(self, capacity=50000):
'''
Description:
Initializes an empty data list and a pointer located at 0.
Also determines the capacity of the data list.
Arguments:
capacity -- positive int number
'''
self.capacity = capacity
self.data = []
self.pointer = 0
def store(self, event):
'''
Description:
Stores the input event in the location designated by the pointer.
The pointer is increased by one modulo the capacity.
Arguments:
event -- tuple to be stored
'''
if len(self.data) < self.capacity:
self.data.append(None)
self.data[self.pointer] = event
self.pointer = (self.pointer + 1) % self.capacity
def sample(self, batch_size):
'''
Description:
Samples a specified number of events
Arguments:
batch_size -- int number that determines the amount of events to be sampled
Outputs:
random list with stored events
'''
return random.sample(self.data, batch_size)
def retrieve(self):
'''
Description:
Returns the whole stored data
Outputs:
data
'''
return (self.data)
def forget(self):
'''
Description:
Restarts the stored data and the pointer
'''
self.data = []
self.pointer = 0
# -------------------------------------------------------------
#
# Value network
#
# -------------------------------------------------------------
class v_valueNet(nn.Module):
'''
Description:
The valueNet is a standard fully connected NN with ReLU activation functions
and 3 linear layers that approximates the value function
Attributes:
l1,l2,l3 -- linear layers
Methods:
forward -- calculates otput of network
'''
def __init__(self, input_dim):
'''
Description:
Creates the three linear layers of the net
Arguments:
input_dim -- int that specifies the size of input
'''
super().__init__()
self.l1 = nn.Linear(input_dim, 256)
self.l2 = nn.Linear(256, 256)
self.l3 = nn.Linear(256, 1)
self.l3.weight.data.uniform_(-3e-3, 3e-3)
self.l3.bias.data.uniform_(-3e-3, 3e-3)
self.loss_func = nn.MSELoss()
self.optimizer = optim.Adam(self.parameters(), lr=3e-4)
def forward(self, s):
'''
Descrption:
Calculates output for the given input
Arguments:
x -- input to be propagated through the net
Outputs:
x -- number that represents the approximate value of the input
'''
x = F.relu(self.l1(s))
x = F.relu(self.l2(x))
x = self.l3(x)
return (x)
class q_valueNet(nn.Module):
'''
Description:
The valueNet is a standard fully connected NN with ReLU activation functions
and 3 linear layers that approximates the value function
Attributes:
l1,l2,l3 -- linear layers
Methods:
forward -- calculates otput of network
'''
def __init__(self, s_dim, a_dim):
'''
Descrption:
Creates the three linear layers of the net
Arguments:
input_dim -- int that specifies the size of input
'''
super().__init__()
self.l1 = nn.Linear(s_dim + a_dim, 256)
self.l2 = nn.Linear(256, 256)
self.l3 = nn.Linear(256, 1)
self.l3.weight.data.uniform_(-3e-3, 3e-3)
self.l3.bias.data.uniform_(-3e-3, 3e-3)
self.loss_func = nn.MSELoss()
self.optimizer = optim.Adam(self.parameters(), lr=3e-4)
def forward(self, s, a):
'''
Descrption:
Calculates output for the given input
Arguments:
x -- input to be propagated through the net
Outputs:
x -- number that represents the approximate value of the input
'''
x = torch.cat([s, a], 1)
x = F.relu(self.l1(x))
x = F.relu(self.l2(x))
x = self.l3(x)
return (x)
# -------------------------------------------------------------
#
# Policy network
#
# -------------------------------------------------------------
class policyNet(nn.Module):
'''
Description:
The policyNet is a standard fully connected NN with ReLU and sigmoid activation
functions and 3 linear layers. This net determines the action for a given state.
Attributes:
l1,l2,l3 -- linear layers
Methods:
forward -- calculates otput of network
'''
def __init__(self, input_dim, output_dim, min_log_stdev=-30, max_log_stdev=30):
'''
Descrption:
Creates the three linear layers of the net
Arguments:
input_dim -- int that specifies the size of input
'''
super().__init__()
self.min_log_stdev = min_log_stdev
self.max_log_stdev = max_log_stdev
self.l1 = nn.Linear(input_dim, 256)
self.l2 = nn.Linear(256, 256)
self.l31 = nn.Linear(256, output_dim)
self.l32 = nn.Linear(256, output_dim)
self.l31.weight.data.uniform_(-3e-3, 3e-3)
self.l32.weight.data.uniform_(-3e-3, 3e-3)
self.l31.bias.data.uniform_(-3e-3, 3e-3)
self.l32.bias.data.uniform_(-3e-3, 3e-3)
self.optimizer = optim.Adam(self.parameters(), lr=3e-4)
def forward(self, s):
x = F.relu(self.l1(s))
x = F.relu(self.l2(x))
m = self.l31(x)
log_stdev = self.l32(x)
log_stdev = torch.clamp(log_stdev, self.min_log_stdev, self.max_log_stdev)
return m, log_stdev
def sample_action(self, s):
'''
Description:
Calculates output for the given input
Arguments:
x -- input to be propagated through the net
Outputs:
a --
'''
m, log_stdev = self(s)
u = m + log_stdev.exp() * torch.randn_like(m)
a = torch.tanh(u).cpu()
return a
def sample_action_and_llhood(self, s):
m, log_stdev = self(s)
stdev = log_stdev.exp()
u = m + stdev * torch.randn_like(m)
a = torch.tanh(u)
llhood = (Normal(m, stdev).log_prob(u) - torch.log(torch.clamp(1 - a.pow(2), 1e-6, 1.0))).sum(dim=1,
keepdim=True)
return a, llhood