-
Notifications
You must be signed in to change notification settings - Fork 5
/
softmax.py
300 lines (199 loc) · 9.02 KB
/
softmax.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
# Angus Dempster, Daniel F Schmidt, Geoffrey I Webb
# HYDRA: Competing Convolutional Kernels for Fast and Accurate Time Series Classification
# https://arxiv.org/abs/2203.13652
import copy
import numpy as np
import pandas as pd
import torch, torch.nn as nn, torch.optim as optim
from hydra import Hydra
def train(path, num_classes, training_size, **kwargs):
# -- init ------------------------------------------------------------------
args = \
{
"validation_proportion" : 0.1,
"validation_min" : 1_024,
"chunk_size" : 2 ** 12,
"chunk_size_sgd" : 2 ** 12,
"minibatch_size" : 256,
"max_epochs" : 200,
"patience_lr" : 10,
"patience" : 20,
"threshold" : 1e-4,
"k" : 8,
"g" : 64,
"seed" : None,
"validate" : True
}
args = {**args, **kwargs}
if args["seed"] is not None:
torch.manual_seed(args["seed"])
random_state = torch.random.get_rng_state()
data = np.load(path, mmap_mode = "r")
max_size = data.shape[0]
_validation_size = max(np.int32(max_size * args["validation_proportion"]), args["validation_min"])
# -- validation data -------------------------------------------------------
indices = torch.randperm(max_size)
validation_indices, training_indices = indices[:_validation_size], indices[_validation_size:]
validation_data = torch.tensor(data[validation_indices])
X_validation, Y_validation = validation_data[:, :-1].float().unsqueeze(1), validation_data[:, -1].long()
transform = Hydra(X_validation.shape[-1], k = args["k"], g = args["g"], seed = None)
X_validation_transform = transform.batch(X_validation).clamp(0).sqrt()
validation_mask = X_validation_transform != 0
# -- init (cont) -----------------------------------------------------------
exponent = np.log2((X_validation.shape[-1] - 1) / (9 - 1))
num_dilations = int(exponent) + 1
_num_features = num_dilations * 2 * 512
def init(layer):
if isinstance(layer, nn.Linear):
nn.init.constant_(layer.weight.data, 0)
nn.init.constant_(layer.bias.data, 0)
# -- cache -----------------------------------------------------------------
cache_Y = torch.zeros(training_size, dtype = torch.long)
cache_X = torch.zeros((training_size, _num_features))
cache_map = torch.zeros(max_size).long()
torch.random.set_rng_state(random_state)
chunks = training_indices[torch.randperm(training_size)].split(args["chunk_size"])
sequences = torch.arange(training_size).split(args["chunk_size"])
f_mean = 0
f_std = 0
est_size = 0
for chunk_index, chunk in enumerate(chunks):
chunk_size = len(chunk)
training_data = np.array(data[chunk])
X_training, Y_training = torch.FloatTensor(training_data[:, :-1]).unsqueeze(1), torch.LongTensor(training_data[:, -1])
X_training_transform = transform.batch(X_training).clamp(0).sqrt()
s = (X_training_transform == 0).float().mean(0) ** 4 + 1e-8
cache_map.scatter_(-1, chunk, sequences[chunk_index])
cache_indices = cache_map.gather(-1, chunk)
cache_X[cache_indices] = X_training_transform
cache_Y[cache_indices] = Y_training
_f_mean = X_training_transform.mean(0)
_f_std = X_training_transform.std(0) + s
f_mean = ((f_mean * est_size) + (_f_mean * chunk_size)) / (est_size + chunk_size)
f_std = ((f_std * est_size) + (_f_std * chunk_size)) / (est_size + chunk_size)
est_size = est_size + chunk_size
data._mmap.close()
del data
print(f"Training...", flush = True)
stage = 0
lr = 1e-6
factor = 1.1
interval = 10
model = nn.Sequential(nn.Linear(_num_features, num_classes))
loss_function = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr = lr, momentum = 0.9, nesterov = True)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor = 0.1, min_lr = 1e-8, patience = args["patience_lr"] - 2, cooldown = 1)
model.apply(init)
model.train()
stall_count = 0
minibatch_count = 0
best_validation_loss = np.inf
stop = False
best_loss_VA = np.inf
state = {}
for epoch in range(args["max_epochs"]):
if epoch > 0 and stop:
break
chunks = torch.arange(training_size).split(args["chunk_size_sgd"])
chunks = [chunks[_] for _ in torch.randperm(len(chunks))] # shuffle chunks
for chunk_index, chunk in enumerate(chunks):
if epoch > 0 and stop:
break
chunk_size = len(chunk)
X_training_transform = cache_X[chunk]
Y_training = cache_Y[chunk]
minibatches = torch.randperm(chunk_size).split(args["minibatch_size"]) # shuffle within chunk
for minibatch_index, minibatch in enumerate(minibatches):
if epoch > 0 and stop:
break
if minibatch_index > 0 and len(minibatch) < args["minibatch_size"]:
break
X_mask = X_training_transform[minibatch] != 0
optimizer.zero_grad()
_Y_training = model(((X_training_transform[minibatch] - f_mean) * X_mask) / f_std)
training_loss = loss_function(_Y_training, Y_training[minibatch])
training_loss.backward()
optimizer.step()
minibatch_count += 1
if stage == 0:
if minibatch_count % interval == 0:
with torch.no_grad():
model.eval()
_Y_validation = model(((X_validation_transform - f_mean) * validation_mask) / f_std)
validation_loss = loss_function(_Y_validation, Y_validation)
model.train()
if validation_loss.item() < best_loss_VA:
best_loss_VA = validation_loss.item()
state["model"] = copy.deepcopy(model.state_dict())
state["optim"] = copy.deepcopy(optimizer.state_dict())
elif validation_loss.item() > best_loss_VA:
stage = 1
model.load_state_dict(state["model"])
optimizer.load_state_dict(state["optim"])
if stage == 0:
lr *= factor
for group in optimizer.param_groups:
group["lr"] = lr
if stage == 1:
with torch.no_grad():
model.eval()
_Y_validation = model(((X_validation_transform - f_mean) * validation_mask) / f_std)
validation_loss = loss_function(_Y_validation, Y_validation)
model.train()
scheduler.step(validation_loss)
if validation_loss.item() < best_validation_loss - args["threshold"]:
best_validation_loss = validation_loss.item()
best_model = copy.deepcopy(model)
if not stop:
stall_count = 0
else:
stall_count += 1
if stall_count >= args["patience"]:
stop = True
print(f"\n<Stopped at Epoch {epoch + 1}>")
validation_accuracy = 0
best_model.eval()
if args["validate"]:
with torch.no_grad():
_Y_validation = best_model(((X_validation_transform - f_mean) * validation_mask) / f_std)
validation_accuracy = (_Y_validation.argmax(-1) == Y_validation).numpy().mean()
return transform, best_model, f_mean, f_std, validation_accuracy
def predict(path,
transform,
model,
f_mean,
f_std,
**kwargs):
args = \
{
"score" : True,
"batch_size" : 256,
"test_size" : None,
}
args = {**args, **kwargs}
model.eval()
data = np.load(path, mmap_mode = "r")
max_size = data.shape[0]
indices = torch.arange(max_size)
batches = indices.split(args["batch_size"])
predictions = []
correct = 0
total = 0
print("Predicting...")
for batch_index, batch in enumerate(batches):
test_data = torch.tensor(data[batch])
X_test, Y_test = test_data[:, :-1].float().unsqueeze(1), test_data[:, -1].long()
X_test_transform = transform(X_test).clamp(0).sqrt()
X_mask = X_test_transform != 0
X_test_transform = ((X_test_transform - f_mean) * X_mask) / f_std
with torch.no_grad():
_predictions = model(X_test_transform).argmax(1)
predictions.append(_predictions)
total += len(test_data)
correct += (_predictions == Y_test).long().sum()
data._mmap.close()
del data
if args["score"]:
return np.concatenate(predictions), correct / total
else:
return np.concatenate(predictions)