-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
model.py
350 lines (299 loc) · 11.3 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
PyTorch GRU-D model.
"""
# Created by Wenjie Du <[email protected]>
# License: GLP-v3
from typing import Union, Optional
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from pypots.classification.base import BaseNNClassifier
from pypots.classification.grud.dataset import DatasetForGRUD
from pypots.imputation.brits.module import TemporalDecay
class _GRUD(nn.Module):
def __init__(
self,
n_steps: int,
n_features: int,
rnn_hidden_size: int,
n_classes: int,
device: Union[str, torch.device],
):
super().__init__()
self.n_steps = n_steps
self.n_features = n_features
self.rnn_hidden_size = rnn_hidden_size
self.n_classes = n_classes
self.device = device
# create models
self.rnn_cell = nn.GRUCell(
self.n_features * 2 + self.rnn_hidden_size, self.rnn_hidden_size
)
self.temp_decay_h = TemporalDecay(
input_size=self.n_features, output_size=self.rnn_hidden_size, diag=False
)
self.temp_decay_x = TemporalDecay(
input_size=self.n_features, output_size=self.n_features, diag=True
)
self.classifier = nn.Linear(self.rnn_hidden_size, self.n_classes)
def classify(self, inputs: dict) -> torch.Tensor:
values = inputs["X"]
masks = inputs["missing_mask"]
deltas = inputs["deltas"]
empirical_mean = inputs["empirical_mean"]
X_filledLOCF = inputs["X_filledLOCF"]
hidden_state = torch.zeros(
(values.size()[0], self.rnn_hidden_size), device=self.device
)
for t in range(self.n_steps):
# for data, [batch, time, features]
x = values[:, t, :] # values
m = masks[:, t, :] # mask
d = deltas[:, t, :] # delta, time gap
x_filledLOCF = X_filledLOCF[:, t, :]
gamma_h = self.temp_decay_h(d)
gamma_x = self.temp_decay_x(d)
hidden_state = hidden_state * gamma_h
x_h = gamma_x * x_filledLOCF + (1 - gamma_x) * empirical_mean
x_replaced = m * x + (1 - m) * x_h
inputs = torch.cat([x_replaced, hidden_state, m], dim=1)
hidden_state = self.rnn_cell(inputs, hidden_state)
logits = self.classifier(hidden_state)
prediction = torch.softmax(logits, dim=1)
return prediction
def forward(self, inputs: dict) -> dict:
"""Forward processing of GRU-D.
Parameters
----------
inputs : dict,
The input data.
Returns
-------
dict,
A dictionary includes all results.
"""
prediction = self.classify(inputs)
classification_loss = F.nll_loss(torch.log(prediction), inputs["label"])
results = {"prediction": prediction, "loss": classification_loss}
return results
class GRUD(BaseNNClassifier):
"""GRU-D implementation of BaseClassifier.
Attributes
----------
model : object,
The underlying GRU-D model.
optimizer : object,
The optimizer for model training.
data_loader : object,
The data loader for dataset loading.
Parameters
----------
rnn_hidden_size : int,
The size of the RNN hidden state.
learning_rate : float (0,1),
The learning rate parameter for the optimizer.
weight_decay : float in (0,1),
The weight decay parameter for the optimizer.
epochs : int,
The number of training epochs.
patience : int,
The number of epochs with loss non-decreasing before early stopping the training.
batch_size : int,
The batch size of the training input.
device :
Run the model on which device.
"""
def __init__(
self,
n_steps: int,
n_features: int,
rnn_hidden_size: int,
n_classes: int,
batch_size: int = 32,
epochs: int = 100,
patience: int = None,
learning_rate: float = 1e-3,
weight_decay: float = 1e-5,
num_workers: int = 0,
device: Optional[Union[str, torch.device]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
):
super().__init__(
n_classes,
batch_size,
epochs,
patience,
learning_rate,
weight_decay,
num_workers,
device,
saving_path,
model_saving_strategy,
)
self.n_steps = n_steps
self.n_features = n_features
self.rnn_hidden_size = rnn_hidden_size
self.model = _GRUD(
self.n_steps,
self.n_features,
self.rnn_hidden_size,
self.n_classes,
self.device,
)
self.model = self.model.to(self.device)
self._print_model_size()
def _assemble_input_for_training(self, data: dict) -> dict:
"""Assemble the input data into a dictionary.
Parameters
----------
data : list
A list containing data fetched from Dataset by Dataload.
Returns
-------
inputs : dict
A dictionary with data assembled.
"""
# fetch data
indices, X, X_filledLOCF, missing_mask, deltas, empirical_mean, label = map(
lambda x: x.to(self.device), data
)
# assemble input data
inputs = {
"indices": indices,
"X": X,
"X_filledLOCF": X_filledLOCF,
"missing_mask": missing_mask,
"deltas": deltas,
"empirical_mean": empirical_mean,
"label": label,
}
return inputs
def _assemble_input_for_validating(self, data: dict) -> dict:
"""Assemble the given data into a dictionary for validating input.
Notes
-----
The validating data assembling processing is the same as training data assembling.
Parameters
----------
data : list,
A list containing data fetched from Dataset by Dataloader.
Returns
-------
inputs : dict,
A python dictionary contains the input data for model validating.
"""
return self._assemble_input_for_training(data)
def _assemble_input_for_testing(self, data: dict) -> dict:
"""Assemble the given data into a dictionary for testing input.
Notes
-----
The testing data assembling processing is the same as training data assembling.
Parameters
----------
data : list,
A list containing data fetched from Dataset by Dataloader.
Returns
-------
inputs : dict,
A python dictionary contains the input data for model testing.
"""
indices, X, X_filledLOCF, missing_mask, deltas, empirical_mean = map(
lambda x: x.to(self.device), data
)
inputs = {
"indices": indices,
"X": X,
"X_filledLOCF": X_filledLOCF,
"missing_mask": missing_mask,
"deltas": deltas,
"empirical_mean": empirical_mean,
}
return inputs
def fit(
self,
train_set: Union[dict, str],
val_set: Optional[Union[dict, str]] = None,
file_type: str = "h5py",
) -> None:
"""Train the classifier on the given data.
Parameters
----------
train_set : dict or str,
The dataset for model training, should be a dictionary including keys as 'X' and 'y',
or a path string locating a data file.
If it is a dict, X should be array-like of shape [n_samples, sequence length (time steps), n_features],
which is time-series data for training, can contain missing values, and y should be array-like of shape
[n_samples], which is classification labels of X.
If it is a path string, the path should point to a data file, e.g. a h5 file, which contains
key-value pairs like a dict, and it has to include keys as 'X' and 'y'.
val_set : dict or str,
The dataset for model validating, should be a dictionary including keys as 'X' and 'y',
or a path string locating a data file.
If it is a dict, X should be array-like of shape [n_samples, sequence length (time steps), n_features],
which is time-series data for validating, can contain missing values, and y should be array-like of shape
[n_samples], which is classification labels of X.
If it is a path string, the path should point to a data file, e.g. a h5 file, which contains
key-value pairs like a dict, and it has to include keys as 'X' and 'y'.
file_type : str, default = "h5py"
The type of the given file if train_set and val_set are path strings.
Returns
-------
self : object,
Trained classifier.
"""
# Step 1: wrap the input data with classes Dataset and DataLoader
training_set = DatasetForGRUD(train_set, file_type=file_type)
training_loader = DataLoader(
training_set,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
)
val_loader = None
if val_set is not None:
val_set = DatasetForGRUD(val_set, file_type=file_type)
val_loader = DataLoader(
val_set,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_workers,
)
# Step 2: train the model and freeze it
self._train_model(training_loader, val_loader)
self.model.load_state_dict(self.best_model_dict)
self.model.eval() # set the model as eval status to freeze it.
# Step 3: save the model if necessary
self.auto_save_model_if_necessary(training_finished=True)
def classify(self, X: Union[dict, str], file_type: str = "h5py") -> np.ndarray:
"""Classify the input data with the trained model.
Parameters
----------
X : array-like or str,
The data samples for testing, should be array-like of shape [n_samples, sequence length (time steps),
n_features], or a path string locating a data file, e.g. h5 file.
file_type : str, default = "h5py",
The type of the given file if X is a path string.
Returns
-------
array-like, shape [n_samples],
Classification results of the given samples.
"""
self.model.eval() # set the model as eval status to freeze it.
test_set = DatasetForGRUD(X, return_labels=False, file_type=file_type)
test_loader = DataLoader(
test_set,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_workers,
)
prediction_collector = []
with torch.no_grad():
for idx, data in enumerate(test_loader):
inputs = self._assemble_input_for_testing(data)
prediction = self.model.classify(inputs)
prediction_collector.append(prediction)
predictions = torch.cat(prediction_collector)
return predictions.cpu().detach().numpy()