-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_supervised_models.py
281 lines (230 loc) · 10.7 KB
/
run_supervised_models.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
import numpy as np
import torch
import torch.nn as nn
import utilities as u
import supervised_models as super_models # ;)
class baseline_model():
'''
class for training and testing basic RNN model
'''
def __init__(self,inp_size=3,n_neurons=128,out_size=6,rnn_nonlinearity = "relu",
dropout_p=.3,curl=False,lr = 3E-4,BATCH_SIZE=32):
'''
initialize model and Parameters
----------
Parameters
----------
inp_size : int
input size, defaults to 3 (target position and go cue)
n_neurons: int
number of hidden units in each layer
out_size: int
number of outputs, defaults to 6 (target kinematics in 2D)
rnn_nonlinearity: string ['relu' or 'tanh']
gru and lstm didn't improve performance so removed from class for now
dropout_p: float [0-1]
dropout percentage for fully connected layers
curl: bool
whether to include curl field
to edit curl field parameters see utilities.RandomTargetTimeseries docstring
lr : float
learning rate for Adam optimizer
BATCH_SIZE : int
number of training examples in each mini-batch
'''
if torch.cuda.is_available():
self.device = torch.device("cuda")
else:
self.device = torch.device("cpu")
self.losses = []
self.model = super_models.BaselineRNN(inp_size=inp_size,n_neurons=n_neurons,out_size=out_size,
rnn_nonlinearity=rnn_nonlinearity,
dropout_p=dropout_p).to(self.device)
self.data_gen = u.RandomTargetTimeseries(curl=curl) # data generator
self.optimizer = torch.optim.Adam(self.model.parameters(),lr=lr)
self.BATCH_SIZE = BATCH_SIZE
def train(self,N_TRAIN_EPOCHS,verbose=True):
'''
run backprop on N_TRAIN_EPOCHS mini-batches
'''
for epoch in range(int(N_TRAIN_EPOCHS)):
X,Y,CURL = self.data_gen.get_minibatch(self.BATCH_SIZE) # get minibatch
self.train_epoch(X.to(self.device),Y.to(self.device),CURL.to(self.device)) # push to GPU if available and run backprop
if verbose:
if epoch%10 == 0:
print("epoch",epoch,"loss",self.losses[-1])
def train_epoch(self,X,Y,CURL):
'''
train a single epoch
----------
Parameters
----------
X : torch.tensor, size [batch size, number of inputs, timepoints]
inputs
Y: torch.tensor, size [batch size, number of outputs, timepoints]
target timeseries
CURL: torch.tensor, size [batch size, number of outputs, timepoints]
curl forces
'''
self.model.train() # ensure dropout layers are turned on
YHAT = torch.zeros(*Y.shape).to(self.device) # init estimates
h= torch.zeros(self.BATCH_SIZE,self.model.n_neurons,dtype=torch.float).to(self.device) # init hidden state
self.optimizer.zero_grad()
for t in range(self.data_gen.Tx): # for each timepoint
kin, h = self.model.forward(X[:,:,t],h) # forward prop
YHAT[:,:,t]=kin + CURL[:,:,t]
loss = (Y - YHAT).pow(2).mean() + (Y-YHAT).abs().mean() # elastic net like loss to get steeper gradient near zero
loss.backward() # compute gradients
self.optimizer.step() # backprop
self.losses.append(loss.item()) # save losses
def test_batch(self,m,to_cpu_return=True):
'''
evaluate model on "m" samples
to_cpu_return : bool
return outputs to cpu, if not using gpu, does nothing
'''
X,Y,CURL = self.data_gen.get_minibatch(m)
X,Y,CURL = X.to(self.device),Y.to(self.device),CURL.to(self.device)
YHAT = torch.zeros(*Y.shape).to(self.device)
h= torch.zeros(m,self.model.n_neurons,dtype=torch.float).to(self.device)
self.model.eval()
for t in range(self.data_gen.Tx):
with torch.no_grad():
kin, h = self.model.forward(X[:,:,t],h)
YHAT[:,:,t]=kin + CURL[:,:,t]
if to_cpu_return:
return X.to("cpu"),Y.to("cpu"),YHAT.to("cpu"),CURL.to("cpu")
else:
return X,Y,YHAT,CURL
class recursive_model():
'''
class for training and testing recursive RNN model for cursor control.
This network outputs x and y cursor accelerations.
The input to this network is target location, go cue, and a buffer of recent
outputs from the network.
'''
def __init__(self,inp_size=3,n_neurons=128,out_size=2,rnn_type = "gru",
dropout_p=.3,curl=False,lr = 3E-4,BATCH_SIZE=32,BUFFER_CAP=5):
'''
initialize model and Parameters
----------
Parameters
----------
inp_size : int
input size without buffer, defaults to 3 (target position and go cue)
n_neurons: int
number of hidden units in each layer
out_size: int
number of outputs, defaults to 6 (target kinematics in 2D)
rnn_nonlinearity: string ['relu', 'tanh', 'gru']
lstm not implemented yet
dropout_p: float [0-1]
dropout percentage for fully connected layers
curl: bool
whether to include curl field
to edit curl field parameters see utilities.RandomTargetTimeseries docstring
lr : float
learning rate for Adam optimizer
BATCH_SIZE : int
number of training examples in each mini-batch
BUFFER_CAP : int
capacity of recent output buffer, determines input size to network
'''
if torch.cuda.is_available():
self.device = torch.device("cuda")
else:
self.device = torch.device("cpu")
self.losses = []
self.model = super_models.RNN_Recursive(inp_size=inp_size + 2*BUFFER_CAP,n_neurons=n_neurons,out_size=out_size,
rnn_type=rnn_type,
dropout_p=dropout_p).to(self.device)
self.data_gen = u.RandomTargetTimeseries(curl=curl)
self.optimizer = torch.optim.Adam(self.model.parameters(),lr=lr)
self.BATCH_SIZE = BATCH_SIZE
self.BUFFER_CAP = BUFFER_CAP
def train(self,N_TRAIN_EPOCHS,verbose=True,elastic_net_lamb=.5):
'''
run backprop on N_TRAIN_EPOCHS mini-batches
elastic_net_lamb : float
mixing parameter for l1 and l2 loss
'''
for epoch in range(int(N_TRAIN_EPOCHS)):
X,Y,_ = self.data_gen.get_minibatch(self.BATCH_SIZE) # generate data
# train epoch (keep only acceleration infor for training)
self.train_epoch(X.to(self.device),Y[:,:2,:].to(self.device),elastic_net_lamb)
if verbose:
if epoch%10 == 0:
print("epoch",epoch,"loss",self.losses[-1])
def train_epoch(self,X,Y,elastic_net_lamb):
'''
train a single epoch
----------
Parameters
----------
X : torch.tensor, size [batch size, number of inputs, timepoints]
inputs
Y: torch.tensor, size [batch size, number of outputs, timepoints]
target timeseries
elastic_net_lamb : float
mixing parameter for l1 and l2 loss
'''
self.model.train() # ensure dropout is turned on
YHAT = torch.zeros(*Y.shape).to(self.device) # init estimates
h= torch.zeros(self.BATCH_SIZE,self.model.n_neurons,dtype=torch.float).to(self.device) # init hidden state
self.optimizer.zero_grad() # clear gradients
kin_buff = u.data_buffer(self.BUFFER_CAP) # fill acceleartion buffer with zeros
for b in range(self.BUFFER_CAP):
kin_buff.push(torch.zeros(self.BATCH_SIZE,2,dtype=torch.float).to(self.device))
for t in range(self.data_gen.Tx):
INPUT = torch.cat([X[:,:,t],*kin_buff.buffer],dim=1) # concatenate targets and buffer
kin,h = self.model.forward(INPUT,h) # forward pass
kin = self.rt_curl(kin) # apply curl field
kin_buff.push(kin) # push outputs to buffer
YHAT[:,:,t]=kin # save outputs
V,VHAT = torch.cumsum(Y,dim=2),torch.cumsum(YHAT,dim=2) # estimated velocity and target velocity
D,DHAT = torch.cumsum(V,dim=2),torch.cumsum(VHAT,dim=2) # estimated position and target position
# loss is on position from acceleration output
loss = 1./self.data_gen.Tx/self.data_gen.Tx*((D-DHAT).pow(2).mean()+(D-DHAT).abs().mean()) #+
loss.backward() # calculate gradients
self.optimizer.step() # backprop
self.losses.append(loss.item()) # save losses
def test_batch(self,m,to_cpu_return=True):
'''
evaluate model on "m" samples
to_cpu_return : bool
return outputs to cpu, if not using gpu, does nothing
'''
self.model.eval() # turn off dropout
X,Y,_ = self.data_gen.get_minibatch(m) # get test data
X,Y = X.to(self.device),Y[:,:2,:].to(self.device)
YHAT = torch.zeros(*Y.shape).to(self.device)
h= torch.zeros(m,self.model.n_neurons,dtype=torch.float).to(self.device)
kin_buff = u.data_buffer(self.BUFFER_CAP) # fill acceleration buffer
for b in range(self.BUFFER_CAP):
kin_buff.push(torch.zeros(m,2,dtype=torch.float).to(self.device))
for t in range(self.data_gen.Tx):
with torch.no_grad(): # don't track gradients
INPUT = torch.cat([X[:,:,t],*kin_buff.buffer],dim=1) # concat targets and buffer
kin,h = self.model.forward(INPUT,h) # forward prop
kin = self.rt_curl(kin)
kin_buff.push(kin) # push outputs to buffer
YHAT[:,:,t]=kin
if to_cpu_return:
return X.to("cpu"),Y.to("cpu"),YHAT.to("cpu")
else:
return X,Y,YHAT
def rt_curl(self,acc):
'''
Apply curl field to accelerations for models that get state information
----------
Parameters
----------
acc - torch.tensor
has shape (batch_size,2). Accelerations for given timepoints
'''
if self.data_gen.curl:
# if cursor location in curl field
mask = ((acc[:,-2]>=self.data_gen.curl_xlims[0]) & (acc[:,-2]<=self.data_gen.curl_xlims[1])) | ((acc[:,-1]>=self.data_gen.curl_ylims[0]) & (acc[:,-1]<=self.data_gen.curl_ylims[1]))
# apply field
acc[mask,:] += torch.from_numpy(np.array([self.data_gen.curl_xmag, self.data_gen.curl_ymag])[np.newaxis,:]).to(self.device)
return acc