-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNN.py
797 lines (600 loc) · 25.3 KB
/
RNN.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# Todoes :
'''
Voire des modèles simples (Time warmping + KNN)
donner la classe en meme temps
apprendre a plusieurs endroits de la série
----------------------
Faire une moyenne pour le KNN
Correlation avec la prédiction (en fonction de h)
Tester plusieurs h
'''
import numpy as np
import torch
import torchvision
import matplotlib.pyplot as plt
import tslearn.datasets
import seaborn as sns
from sklearn.manifold import TSNE
from time import time
from torchvision import datasets, transforms
from torch import nn, optim
from numba import jit
#################### Fonctions Diverses ###############################
@jit
def shuffle_in_unison(a, b, shuffle_arg = False):
n_elem = len(a)
indeces = np.random.choice(n_elem, size = n_elem, replace = False)
if shuffle_arg:
return a[indeces], b[indeces], indeces
else :
return a[indeces], b[indeces]
@jit
def cleanse(x,y):
l = []
for e,elm in enumerate(y):
if elm == 3 or elm == 5: l.append(e)
return np.delete(x,l,axis =0), np.delete(y,l)
@jit
def repartition(x,y):
l = [ [], [], [] ]
for e,elm in enumerate(y):
if elm == 4 : l[elm - 2].append(e)
else : l[elm - 1].append(e)
n = min(len(l[2]),100)
l=np.array(l)
print(n)
a = np.concatenate((x[l[0][:n]],x[l[1][:n]],x[l[2][:n]]),axis =0)
b = np.concatenate((y[l[0][:n]],y[l[1][:n]],y[l[2][:n]]))
return(a,b)
################################# Dataset #################################
class Dataset(torch.utils.data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, series, labels):
'Initialization'
self.labels = labels
self.series = series
def __len__(self):
'Denotes the total number of samples'
return len(self.series)
def __getitem__(self, index):
'Generates one sample of data'
return self.series[index], self.labels[index]
def get_random(self):
'Generates a random element of the dataset'
n = self.__len__()
i = np.random.randint(n)
return self.series[i], self.labels[i], i
################################# RNNPred #################################
class RNNPred(torch.nn.Module):
def __init__(self, ins, hs, os,length = 140, lstms = 1):
'''
length = longueur des time series
'''
super(RNNPred, self).__init__()
self.ins = ins
self.length = length
self.relu = torch.nn.ReLU()
self.rnn = nn.LSTM(1, hs[0], lstms, batch_first = True)
self.linear1 = torch.nn.Linear(ins*hs[0], hs[0])
self.linear2 = torch.nn.Linear(hs[0], hs[2])
self.linear3 = torch.nn.Linear(hs[2], os)
def forward(self, x, hidd = False):
"""
In the forward function we accept a Tensor of input data and we must return
a Tensor of output data. We can use Modules defined in the constructor as
well as arbitrary operators on Tensors.
"""
a = x.shape
x = x.view(a[0],a[2],a[1])
hidden = None
for i in range(a[1]):
out, hidden = self.rnn(x[:, :, i:i+1], hidden)
out = out.contiguous()
res1 = self.relu(self.linear1(out.view(out.shape[0], -1)))
res2 = self.relu(self.linear2(res1))
res = self.linear3(res2)
if hidd:
return hidden[0].view(-1)
return res
def fit(self, data, epochs, optimizer, criterion):
time0 = time()
for e in range(epochs):
running_loss = 0
for entries, labels in data:
entries = entries.reshape(entries.shape[0], 1, -1).float()
# Training pass
optimizer.zero_grad()
output = self(entries)
output = output.view(output.shape[0], -1, 1)
loss = criterion(output, labels.float())
# This is where the model learns by backpropagating
loss.backward()
# And optimizes its weights here
optimizer.step()
running_loss += loss.item()
if e % 5 == 0: print("Epoch {} - Training loss: {}".format(e, running_loss / len(data)))
timet = time() - time0
print("Fin de l'étape d'apprentissage en {} min et {} sec".format(timet // 60, timet % 60))
def test_acc(self, data, perc):
loss, zoloss, all_count = 0, 0, 0
for entries, labels in data:
for i in range(len(labels)):
entry = entries[i].reshape(1, 1, self.ins).float()
with torch.no_grad():
pred = self(entry)
true = labels.numpy()[i]
# if true_label == pred_label : print(true_label)
loss += np.abs(true.reshape(5) - pred.view(5).numpy())
#zoloss += np.abs(true-pred.item()) > epsi.item()
all_count += 1
print("Number Of Images Tested =", all_count)
print("Model Accuracy (L1-loss) =", (loss / all_count))
def test_tab(self,data):
l = [0]*4500
for entries, labels in data:
j = 0
for i in range(len(labels)):
entry = entries[i].reshape(1, 1, self.ins).float()
with torch.no_grad():
pred = self(entry)
true = labels.numpy()[i]
l[j] = np.sum(np.abs(true.reshape(5) - pred.view(5).numpy()))
j += 1
return np.array(l)/4500
def test_img(self,x, x_true,i):
n = len(x) + len(x_true)
y1 = np.append(x,x_true)
res = (self.forward(torch.tensor(x).view(1,self.ins,-1).float())).detach().numpy()
y2 = np.append(x,res)
n = len(y1)
loss = (np.abs(res.reshape(5)-x_true.reshape(5)))
abc = range(n)
plt.plot(abc, y1,)
plt.plot(abc, y2,)
plt.title("The {}_th element ins the shuffled array. \n Loss is : {}".format(i,np.sum(loss)))
plt.show()
def sing_loss(self,entry,true):
with torch.no_grad():
entry = entry.reshape(1, 1, self.ins).float()
pred = model(entry)
loss = np.abs(true.numpy().reshape(5) - pred.numpy().reshape(5))
return loss
################################# RNNClass ################################
class RNNClass(torch.nn.Module):
def __init__(self, ins, hs, os, length = 140, lstms = 1):
'''
length = longueur des time series
'''
super(RNNClass, self).__init__()
self.ins = ins
self.length = length
self.crit = nn.NLLLoss()
self.relu = torch.nn.ReLU()
self.rnn = nn.LSTM(1, hs[0], lstms, batch_first = True)
self.linear1 = torch.nn.Linear(ins*hs[0], hs[0])
self.linear2 = torch.nn.Linear(hs[0], 5)
self.maxi = torch.nn.LogSoftmax(dim=1)
def forward(self, x, hidd = False):
"""
In the forward function we accept a Tensor of input data and we must return
a Tensor of output data. We can use Modules defined in the constructor as
well as arbitrary operators on Tensors.
"""
a = x.shape
x = x.view(a[0],a[2],a[1])
hidden = None
for i in range(a[1]):
out, hidden = self.rnn(x[:, :, i:i+1], hidden)
out = out.contiguous()
res1 = self.relu(self.linear1(out.view(out.shape[0], -1)))
res2 = (self.linear2(res1))
res = self.maxi(res2)
return res
def fit(self, data, epochs, optimizer, prnt = False ):
time0 = time()
for e in range(epochs):
running_loss = 0
for entries, labels in data:
entries = entries.reshape(entries.shape[0], 1, -1).float()
# Training pass
optimizer.zero_grad()
output = self.forward(entries)
output = output.view(output.shape[0], -1)
loss = self.crit(output, labels.long())
# This is where the model learns by backpropagating
loss.backward()
# And optimizes its weights here
optimizer.step()
running_loss += loss.item()
if e % 5 == 0 and prnt: print("Epoch {} - Training loss: {}".format(e, running_loss / len(data)))
timet = time() - time0
print("Fin de l'étape d'apprentissage en {} min et {} sec".format(timet // 60, timet % 60))
def test_acc(self, data, perc, prnt = False):
guesses = np.zeros(5)
total_labels = np.zeros(5)
loss, all_count = 0, 0
for entries, labels in data:
for i in range(len(labels)):
entry = entries[i].reshape(1,1,self.ins).float()
with torch.no_grad():
aux = self.forward(entry)
res = list(torch.exp(aux).numpy()[0])
pred_label = res.index(max(res))
true_label = labels.numpy()[i]
guesses[pred_label] += 1
total_labels[true_label] += 1
#if true_label == pred_label : print(true_label)
loss += true_label != pred_label
all_count += 1
if prnt :
print("Number Of Images Tested =", all_count)
print("Model Accuracy =", 1 - (loss / all_count).item())
print("Guesses overall : ", guesses)
print("Actual numbers :", total_labels)
return (loss / all_count).item()
################################# RNNDBPred #################################
class RNNdoubleheadPred(torch.nn.Module):
def __init__(self, ins, hs, os,modul, length = 140, lstms = 1):
'''
length = longueur des time series
'''
super(RNNdoubleheadPred, self).__init__()
self.ins = ins
self.os = os
self.length = length
self.relu = torch.nn.ReLU()
self.crit1 = nn.MSELoss()
self.crit2 = nn.NLLLoss()
self.rnn = nn.LSTM(1, hs[0], lstms, batch_first = True)
self.linear1 = torch.nn.Linear(ins*hs[0], hs[0])
self.linear2 = torch.nn.Linear(hs[0], hs[2])
self.linear3 = torch.nn.Linear(hs[2], os)
self.linear4 = torch.nn.Linear(hs[2],5)
self.modul = modul
self.maxi = torch.nn.LogSoftmax(dim = 1)
def forward(self, x, sel = True ,hidd = False):
"""
In the forward function we accept a Tensor of input data and we must return
a Tensor of output data. We can use Modules defined in the constructor as
well as arbitrary operators on Tensors.
"""
a = x.shape
x = x.view(a[0],a[2],a[1])
hidden = None
for i in range(a[1]):
out, hidden = self.rnn(x[:, :, i:i+1], hidden)
if hidd:
return hidden[0].view(-1)
out = out.contiguous()
res1 = self.relu(self.linear1(out.view(out.shape[0], -1)))
res2 = (self.linear2(res1))
if sel :
res3 = self.linear3(res2)
return res3
else :
res = self.maxi(self.linear4(res2))
return res
def fit(self, data, epochs, opti, prnt = False):
time0 = time()
i = 0
for e in range(epochs):
running_loss = 0
for entries, labels in data:
if i % self.modul != 0 :
labl = labels[:,:self.os,0]
entries = entries.reshape(entries.shape[0], 1, -1).float()
# Training pass
opti.zero_grad()
output = self(entries)
output = output.view(output.shape[0], -1)
loss = self.crit1(output, labl.float())
# This is where the model learns by backpropagating
loss.backward()
# And optimizes its weights here
opti.step()
running_loss += loss.item()
else:
labl = labels[:,self.os,0]
entries = entries.reshape(entries.shape[0], 1, -1).float()
# Training pass
opti.zero_grad()
output = self(entries, sel = False)
output = output.view(output.shape[0], -1)
loss = self.crit2(output, labl.long())
# This is where the model learns by backpropagating
loss.backward()
# And optimizes its weights here
opti.step()
running_loss += loss.item()
if e % 5 == 0 and prnt: print("Epoch {} - Training loss: {}".format(e, running_loss / len(data)))
timet = time() - time0
print("Fin de l'étape d'apprentissage en {} min et {} sec".format(timet // 60, timet % 60))
def test_acc(self, data, perc ,prnt = False):
guesses = np.zeros(5)
total_labels = np.zeros(5)
loss, all_count = 0, 0
for entries, labels in data:
for i in range(len(labels)):
entry = entries[i].reshape(1,1,self.ins).float()
with torch.no_grad():
aux = self.forward(entry)
res = list(torch.exp(aux).numpy()[0])
pred_label = res.index(max(res))
true_label = labels.numpy()[i]
guesses[pred_label] += 1
total_labels[true_label] += 1
#if true_label == pred_label : print(true_label)
loss += true_label != pred_label
all_count += 1
if prnt:
print("Number Of Images Tested =", all_count)
print("Model Accuracy =", 1 - (loss / all_count).item())
print("Guesses overall : ", guesses)
print("Actual numbers :", total_labels)
################################# RNNDBClass #################################
class RNNdoubleheadClass(torch.nn.Module):
def __init__(self, ins, hs, os,modul, length = 140, lstms = 1):
'''
length = longueur des time series
'''
super(RNNdoubleheadClass, self).__init__()
self.ins = ins
self.os = os
self.length = length
self.relu = torch.nn.ReLU()
self.crit1 = nn.NLLLoss()
self.crit2 = nn.MSELoss()
self.modul = modul
self.rnn = nn.LSTM(1, hs[0], lstms, batch_first = True)
self.linear1 = torch.nn.Linear(ins*hs[0], hs[0])
self.linear2 = torch.nn.Linear(hs[0], hs[2])
self.linear3 = torch.nn.Linear(hs[2],os)
self.linear4 = torch.nn.Linear(hs[2],5)
self.maxi = torch.nn.LogSoftmax(dim =1)
def forward(self, x, sel = True ,hidd = False):
"""
In the forward function we accept a Tensor of input data and we must return
a Tensor of output data. We can use Modules defined in the constructor as
well as arbitrary operators on Tensors.
"""
a = x.shape
x = x.view(a[0],a[2],a[1])
hidden = None
for i in range(a[1]):
out, hidden = self.rnn(x[:, :, i:i+1], hidden)
out = out.contiguous()
res1 = self.relu(self.linear1(out.view(out.shape[0], -1)))
res2 = (self.linear2(res1))
if sel :
res = self.maxi(self.linear4(res2))
return res
else :
res3 = self.linear3(res2)
return res3
def fit(self, data, epochs, opti, prnt = False):
time0 = time()
i = 0
for e in range(epochs):
running_loss = 0
for entries, labels in data:
if i % self.modul != 0 :
entries = entries.reshape(entries.shape[0], 1, -1).float()
# Training pass
labl = labels[:,self.os,0]
opti.zero_grad()
output = self.forward(entries)
output = output.view(output.shape[0], -1)
loss = self.crit1(output, labl.long())
# This is where the model learns by backpropagating
loss.backward()
# And optimizes its weights here
opti.step()
running_loss += loss.item()
else:
labl = labels[:,:self.os,0]
entries = entries.reshape(entries.shape[0], 1, -1).float()
# Training pass
opti.zero_grad()
output = self(entries, sel = False)
output = output.view(output.shape[0], -1)
loss = self.crit2(output, labl.float())
# This is where the model learns by backpropagating
loss.backward()
# And optimizes its weights here
opti.step()
running_loss += loss.item()
i+= 1
if e % 5 == 0 and prnt: print("Epoch {} - Training loss: {}".format(e, running_loss / len(data)))
timet = time() - time0
print("Fin de l'étape d'apprentissage en {} min et {} sec".format(timet // 60, timet % 60))
def test_acc(self, data, perc, prnt = False):
guesses = np.zeros(5)
total_labels = np.zeros(5)
loss, all_count = 0, 0
for entries, labels in data:
for i in range(len(labels)):
entry = entries[i].reshape(1, 1, self.ins).float()
with torch.no_grad():
aux = self.forward(entry)
res = list(torch.exp(aux).numpy()[0])
pred_label = res.index(max(res))
true_label = labels.numpy()[i]
guesses[pred_label] += 1
total_labels[true_label] += 1
# if true_label == pred_label : print(true_label)
loss += true_label == pred_label
all_count += 1
if prnt:
print("Number Of Images Tested =", all_count)
print("Model Accuracy =", (loss / all_count).item())
print("Guesses overall : ", guesses)
print("Actual numbers :", total_labels)
return (loss / all_count).item()
#Data Coding
X_train, y_train, X_test, y_test = tslearn.datasets.UCR_UEA_datasets().load_dataset("ECG5000")
Cleanup = False
plotter = False
if Cleanup:
a,b = cleanse(np.append(X_train,X_test,axis = 0),np.append(y_train,y_test))
print(a.shape,b.shape)
a,b = shuffle_in_unison(a,b)
xe,xr = a[:1000], a[1000:]
ye,yr = b[:1000], b[1000:]
xsp,ysp = repartition(xr,yr)
X_train, X_test, y_train, y_test = xr, xe, yr,ye
else:
xsp = X_train
ysp = y_train
if plotter:
res1 = np.bincount(y_train)[1:]
res2 = np.bincount(y_test)[1:]
plt.figure()
ntr = len(y_train)
pertr = res1*100/ntr
labtr = ["{}%".format(i) for i in pertr]
sns.set(style = "darkgrid")
plt.bar(range(1,len(res1)+1),res1)
xlocs, xlabs = plt.xticks()
for i, v in enumerate(res1):
plt.text(xlocs[i+1]-0.2, v + 0.01, labtr[i])
plt.title("Repartition of Train Data")
plt.xlabel("Labels")
plt.ylabel("Number of elements")
#plt.legend(loc = 'best')
nte = len(y_test)
pertr = np.floor(res2 * 10000 / nte)/100
labtr = ["{}%".format(i) for i in pertr]
plt.figure()
sns.set(style = "darkgrid")
plt.bar(range(1, len(res2)+1), res2)
xlocs, xlabs = plt.xticks()
for i, v in enumerate(res2):
plt.text(xlocs[i + 1] - 0.24, v + 0.01, labtr[i])
plt.title("Repartition of Test Data")
plt.xlabel("Labels")
plt.ylabel("Number of elements")
#plt.legend(loc = 'best')
n = X_train.shape[1]
testsize = X_test.shape[0]
length = 5
start = 0
seq_len = 135
end = start + seq_len
############################## Data for RNNPred #########################
# Training Data
data_tr = Dataset(X_train[:,start:end,:], X_train[:,end:end + length,:])
data_r = torch.utils.data.DataLoader(data_tr, batch_size = 24, shuffle = True)
# Test Data
data_te = Dataset(X_test[:,start:end,:], X_test[:,end:end + length,:])
data_e = torch.utils.data.DataLoader(data_te, batch_size = 24, shuffle = False)
############################## Data for RNNClass #########################
# Training Data
data_tr = Dataset(X_train,y_train-1)
data_r_class = torch.utils.data.DataLoader(data_tr, batch_size = 24, shuffle = True)
# Test Data
data_te = Dataset(X_test,y_test-1)
data_e_class = torch.utils.data.DataLoader(data_te, batch_size = 24, shuffle = False)
############################## Data for DBhead #########################
# Training Data
au1 = X_train[:,end:end + length,:]
au2 = (y_train-1).reshape(y_train.shape[0],1,1)
au = np.append(au1,au2,axis=1)
data_tr = Dataset(X_train[:,start:end,:],au)
data_r_db = torch.utils.data.DataLoader(data_tr, batch_size = 24, shuffle = True)
# Test Data
au1 = X_test[:,end:end + length,:]
au2 = (y_test-1).reshape(y_test.shape[0],1,1)
au = np.append(au1,au2,axis=1)
data_te = Dataset(X_test[:,start:end,:],y_test-1)
data_e_db = torch.utils.data.DataLoader(data_te, batch_size = 24, shuffle = False)
'''
utiliser les implems
'''
#Parameters
input_size = seq_len
hidden_sizes = [32, 128, 128]
output_size = length
output_channels = 20
hdim = 1
# On regarde le résultat du modèle avant de l'entrainer
def test(model,data):
return model.test_acc(data,0.1)
def train(epochs,model,optimizer,criterion):
model.fit(data_r, epochs, optimizer, criterion)
def ts_test(model):
x, xt, i = data_te.get_random()
print(y_test[i])
model.test_img(x, xt, i)
def histo(num_class, longe, model):
'''
à modifier pour une version plus générale pour une sortie différente de 5/ plus que 5 types
Memes axes , ajouter des titres et descriptions pour que ce soit plus compréhensible
'''
indx = 0
tots = np.zeros(num_class)
totloss = np.zeros((num_class,longe))
for entries, labels in data_e:
for i in range(len(labels)):
totloss[y_test[indx]-1] += model.sing_loss(entries[i],labels[i])
tots[y_test[indx]-1] += 1
indx +=1
fig,ax = plt.subplots(1,num_class,sharey = True)
fig.add_subplot(111, frameon = False)
# hide tick and tick label of the big axis
plt.tick_params(labelcolor = 'none', top = False, bottom = False, left = False, right = False)
plt.xlabel("common X")
plt.ylabel("common Y")
for i in range(longe):
totloss[:,i] /= tots
for i in range(num_class):
ax[i].bar(np.arange(longe),totloss[i])
ax[i].set_title("Classe {}".format(i))
plt.show()
return totloss,tots
def code():
model = RNNPred(input_size, hidden_sizes, output_size)
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr = 0.03)
#test(model)
model.fit(data_r, 50, optimizer, criterion)
test(model,data_e)
#totloss,tots = histo(5,5,model)
#ts_test(model)
return model
def codeclass(ft = False):
model = RNNClass(140, hidden_sizes, 5)
optimizer = optim.SGD(model.parameters(), lr = 0.03)
if ft : model.fit(data_r_class, 50, optimizer)
#test(model,data_e_class)
return model
def codedbheadclass(ft= False):
model = RNNdoubleheadClass(input_size, hidden_sizes, output_size, 10)
optimizer = optim.SGD(model.parameters(), lr = 0.03)
if ft : model.fit(data_r_db, 50, optimizer)
return model
def codedbheadpred():
model = RNNdoubleheadPred(input_size, hidden_sizes, output_size, 10)
optimizer = optim.SGD(model.parameters(), lr = 0.03)
model.fit(data_r_db, 50, optimizer)
return model
'''
--> Réseau double sortie
-----------------------------------------------------
OLD COMMENTS :
l1 loss , training pendant 2XX epochs , 5 prédictions, lstm =2, 2 linear (128,128):
[0.20496206 0.31164772 0.4228258 0.54931241 0.67645596]
Plus on séloigne du point le plus proche plus on a du mal a deviner ce qu'il faut faire ce qui est plus ou moins a quoi on s'attends
---------------------
l1 loss , 5 prédictions, lstm =1, 3 linear(128,64,64) :
75 Epochs : Model Accuracy (l1-loss) = [0.32147136 0.48832314 0.65492892 0.78805073 0.89032827]
Stable vers 200 Eporhs : Model Accuracy (l1-loss) = [0.20402207 0.33185193 0.44552198 0.57691824 0.6950413 ]
Pas vraiment mieux
---------------------
epochs = 75, lstm = 0, linear = 3 (128,64,64):
Model Accuracy (l1-loss) = [0.16622388 0.2063745 0.24493237 0.27409063 0.41701113]
On apprend mieux, mais ce n'est toujours pas convaincant.
--------------------------------------------------------------------------------------
On peut maintenant tester pour voir ce que cela donne, et les résultats sont plutôt convaincants.
Pour l'instant on est sur de l'apprentissage sur toute la série temporelle. On va essayer de prendre des petits bouts maintenant.
---------------------------------------------------------------------------------------
'''