-
Notifications
You must be signed in to change notification settings - Fork 0
/
gru.py
211 lines (154 loc) · 7.98 KB
/
gru.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
from sqlalchemy import true
import torch.cuda
import torch
import numpy as np
import sys
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
from distutils import util
import matplotlib.pyplot as plt
import tensorflow as tf
import os
import time
#-------------------------------------------------------------------------
torch.cuda.set_device(0)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
np.set_printoptions(threshold=sys.maxsize)
torch.set_printoptions(threshold=10_000)
#-------------------------------------------------------------------------
#input_data = torch.Tensor(np.load("1inputData.npy", allow_pickle=True))
#predict_data = torch.Tensor(np.load("1predict.npy", allow_pickle=True))
input_data = torch.Tensor(np.load("biginputdata.npy", allow_pickle=True))
predict_data = torch.Tensor(np.load("bigpredictdata.npy", allow_pickle=True))
#testingdata_x = torch.Tensor(np.load("1testingdata_x.npy", allow_pickle=True))
#testingdata_y = torch.Tensor(np.load("1testingdata_y.npy", allow_pickle=True))
#testingdata_x = testingdata_x.type(torch.FloatTensor)
#testingdata_y = testingdata_y.type(torch.LongTensor)
input_data = input_data.type(torch.FloatTensor)
predict_data = predict_data.type(torch.LongTensor)
#testingdata_x = torch.Tensor(np.load("1inputData.npy", allow_pickle=True))
#testingdata_y = torch.Tensor(np.load("1predict.npy", allow_pickle=True))
#testingdata_x = testingdata_x.type(torch.FloatTensor)
#testingdata_y = testingdata_y.type(torch.LongTensor)
#print(predict_data)
#print(type(input_data))
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(RNN, self).__init__()
self.num_layers = num_layers
self.hidden_size = hidden_size
self.gru = nn.GRU(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
out, _ = self.gru(x, (h0))
out = out[:, -1, :]
out = self.fc(out)
return out
learning_rate = 0.001######################################################################################
input_size = 248
num_layers = 2
hidden_size = 248
num_classes = 2
lr = .001
wd = 0
# 3 lrs
# 1 wd
# 3 eps
# 4 betas
wd = [ 1e-11, 1e-14, 1e-15]
eps = [0.000005, 0.000001, 0.0000005] #
lr = [0.0002, 0.0005, 0.0009]
betasleft = [0.65, 0.7, 0.75]
betasright = [0.9999999, 0.9999, 0.999 ]
best = np.float64([99]) #antioverfit
# hint just take the most wining and put parameters on both sides of it
model = RNN(input_size, hidden_size, num_layers, num_classes).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.RAdam(model.parameters(), lr=0.0005, betas=(0.8, 0.999), eps=1e-07, weight_decay=1e-11)
BATCH_SIZE = 100
num_epochs = 5000000
print_interval = 3000
a = np.float64([99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99]) #antioverfit
testing_loss = 0.0
model.train()
model.to(device)
input_data.to(device)
predict_data.to(device)
PATH = "model.pt"
torch.save({
'model_state_dict': model.state_dict(),
}, PATH)
start_time = time.time()
counter = 0
for WD in wd:
for EPS in eps:
for LR in lr:
for BETASRIGHT in betasright:
for BETASLEFT in betasleft:
counter = counter + 1
checkpoint = torch.load(PATH)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer = torch.optim.RAdam(model.parameters(), lr=LR, betas=(BETASLEFT, BETASRIGHT), eps=EPS, weight_decay=WD )
a = np.float64([99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99]) #antioverfit
testing_loss = 0.0
model.train()
for epoch in range(num_epochs):
start_time = time.time()
if(testing_loss < a[4]): # part of anti overfit
train_loss = 0.0
testing_loss = 0.0
model.train()
for i in (range(0, len(input_data), BATCH_SIZE)):
batch_X = input_data[i:i+BATCH_SIZE]
batch_y = predict_data[i:i+BATCH_SIZE]
batch_X = batch_X.to(device) #gpu # input data here!!!!!!!!!!!!!!!!!!!!!!!!!!
batch_y = batch_y.to(device) #gpu # larget data here!!!!!!!!!!!!!!!!!!!!!!!!!!
batch_X = batch_X.reshape(-1, 1, input_size).to(device)
output = model(batch_X)
loss = criterion(output, batch_y).to(device)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(
f"Epoch [{epoch + 1}/{num_epochs}], "
f"Step [{i + 1}/{len(input_data)}], "
f"Loss: {loss.item():.4f}"
)
secondTime = time. time()
print("total time for 1 epoch: ", secondTime-start_time)
if(epoch%10 ==0):
#val loss calc below
model.eval()
correctCount =0
wrongCount =0
with torch.no_grad():
for i in (range(0, len(input_data), BATCH_SIZE)):
batch_X = input_data[i:i+BATCH_SIZE]
batch_y = predict_data[i:i+BATCH_SIZE]
batch_X = batch_X.to(device) #gpu # input data here!!!!!!!!!!!!!!!!!!!!!!!!!!
batch_y = batch_y.to(device) #gpu # larget data here!!!!!!!!!!!!!!!!!!!!!!!!!!
batch_X = batch_X.reshape(-1, 1, input_size).to(device)
output = model(batch_X)
_, pred = torch.max(output, dim=1)
correct = np.squeeze(pred.eq(batch_y.data.view_as(pred)))
for i in correct:
if i == True:
correctCount = correctCount+1
else:
wrongCount = wrongCount +1
#print(f"Accuracy: {wrongCount / 90000 * 100:.10f}%")
accuracy = wrongCount / (correctCount+wrongCount) * 100
a = np.insert(a,0,accuracy) # part of anti overfit
a = np.delete(a,22)
testing_loss = accuracy
print("Accuracy: " ,accuracy)
secondTime = time. time()
print("total time for 1 epoch: ", secondTime-start_time)
torch.save(model, "models/GRUModel.pth")
print(optimizer)
print("lr= ", LR, "betaright= ", BETASRIGHT, "betaleft= ", BETASLEFT, " wd= ", WD, "eps= ", EPS)
print("round: ", counter, " out of 243")
best = np.append(best, accuracy)
print(best)