-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
374 lines (277 loc) · 12 KB
/
helpers.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
import pandas as pd
import numpy as np
from tqdm.notebook import tqdm
import seaborn as sns
import sklearn
import sklearn.metrics as metrics
from sklearn import preprocessing
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn import linear_model
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import SGDRegressor
from sklearn.svm import SVR
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import matplotlib.pyplot as plt
class NN_Regressor(torch.nn.Module):
def __init__(self):
super(NN_Regressor, self).__init__()
self.hid1 = torch.nn.Linear(16, 32) # 16-(32-10)-1
self.hid2 = torch.nn.Linear(32, 10)
self.oupt = torch.nn.Linear(10, 1)
def forward(self, x):
x = torch.relu(self.hid1(x))
x = torch.relu(self.hid2(x))
x = self.oupt(x) # no activation
return x
class Optimized_NN_Regressor(torch.nn.Module):
def __init__(self):
super(Optimized_NN_Regressor, self).__init__()
self.hid1 = torch.nn.Linear(16, 24) # 16-(32-10)-1
self.hid2 = torch.nn.Linear(24, 24)
self.dropout = torch.nn.Dropout(p=0.1, inplace=False)
self.oupt = torch.nn.Linear(24, 1);
def forward(self, x):
x = torch.relu(self.hid1(x))
x = torch.relu(self.hid2(x))
x = self.dropout(x)
x = self.oupt(x) # no activation
return x
class RegressionDataset(Dataset):
def __init__(self, X_data, y_data):
self.X_data = X_data
self.y_data = y_data
def __getitem__(self, index):
return self.X_data[index], self.y_data[index]
def __len__ (self):
return len(self.X_data)
def heatmap(X, Y, title = '', bin_num = 150, log_space=False, x_lim=False, y_lim=False, reverse = True):
plt.rcParams["axes.grid"] = False
x_lim = x_lim if x_lim else [X.min(), X.max()]
y_lim = y_lim if y_lim else [Y.min(), Y.max()]
if log_space:
xedges = 10 ** np.linspace(np.log10(x_lim[0]), np.log10(x_lim[1]), bin_num + 1)
else:
xedges = np.linspace(x_lim[0], x_lim[1], bin_num + 1)
yedges = np.linspace(y_lim[0],y_lim[1], bin_num + 1)
H, xedges, yedges = np.histogram2d(X, Y, bins=(xedges, yedges))
H = H[:,::-1] if reverse else H
plt.axis('off')
plt.title(title);
plt.imshow(H.T, cmap=plt.get_cmap("turbo"));
def linear_regressor(X_train, y_train, X_test, y_test):
lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
# Calculate R2 for the regression problem
R_2 = lin_reg.score(X_test, y_test)
# Calculate MSE and MAE for the regression problem
MAE = metrics.mean_absolute_error(y_test, lin_reg.predict(X_test))
MSE = metrics.mean_squared_error(y_test, lin_reg.predict(X_test))
# print(f"R_2 for the regression problem is: {R_2:.2f}")
# print(f"MSE for the regression problem is: {MSE:.2f}")
# print(f"MAE for the regression problem is: {MAE:.2f}")
return lin_reg, [R_2, MAE, MSE]
def polynomial_regressor(X_train, y_train, X_test, y_test, degree=3):
poly = PolynomialFeatures(degree=degree)
X_ = poly.fit_transform(X_train)
X_Test = poly.fit_transform(X_test)
clf = linear_model.LinearRegression()
clf.fit(X_, y_train)
# Calculate R2 for the regression problem
R_2 = clf.score(X_Test, y_test)
# Calculate MSE and MAE for the regression problem
MAE = metrics.mean_absolute_error(y_test, clf.predict(X_Test))
MSE = metrics.mean_squared_error(y_test, clf.predict(X_Test))
# print(f"R_2 for the regression problem is: {R_2:.2f}")
# print(f"MSE for the regression problem is: {MSE:.2f}")
# print(f"MAE for the regression problem is: {MAE:.2f}")
return clf, poly, [R_2, MAE, MSE]
def lasso_regressor(X_train, y_train, X_test, y_test, alpha=0.1):
lasso_regr = Lasso(alpha=alpha)
lasso_regr.fit(X_train, y_train)
# Calculate R2 for the regression problem
R_2 = lasso_regr.score(X_test, y_test)
# Calculate MSE and MAE for the regression problem
MAE = metrics.mean_absolute_error(y_test, lasso_regr.predict(X_test))
MSE = metrics.mean_squared_error(y_test, lasso_regr.predict(X_test))
# print(f"R_2 for the regression problem is: {R_2:.2f}")
# print(f"MSE for the regression problem is: {MSE:.2f}")
# print(f"MAE for the regression problem is: {MAE:.2f}")
return lasso_regr, [R_2, MAE, MSE]
def sgd_regressor(X_train, y_train, X_test, y_test, max_iter=100000, tol=0.01):
sgdـregr = SGDRegressor(max_iter=max_iter, tol=tol)
sgdـregr.fit(X_train, y_train)
# Calculate R2 for the regression problem
R_2 = sgdـregr.score(X_test, y_test)
# Calculate MSE and MAE for the regression problem
MAE = metrics.mean_absolute_error(y_test, sgdـregr.predict(X_test))
MSE = metrics.mean_squared_error(y_test, sgdـregr.predict(X_test))
# print(f"R_2 for the regression problem is: {R_2:.2f}")
# print(f"MSE for the regression problem is: {MSE:.2f}")
# print(f"MAE for the regression problem is: {MAE:.2f}")
return sgdـregr, [R_2, MAE, MSE]
def svr_regressor(X_train, y_train, X_test, y_test,C=1.0, epsilon=0.2):
regr = SVR(C=C, epsilon=epsilon)
regr.fit(X_train, y_train)
# Calculate R2 for the regression problem
R_2 = regr.score(X_test, y_test)
# Calculate MSE and MAE for the regression problem
MAE = metrics.mean_absolute_error(y_test, regr.predict(X_test))
MSE = metrics.mean_squared_error(y_test, regr.predict(X_test))
# print(f"R_2 for the regression problem is: {R_2:.2f}")
# print(f"MSE for the regression problem is: {MSE:.2f}")
# print(f"MAE for the regression problem is: {MAE:.2f}")
return regr, [R_2, MAE, MSE]
def random_forest_regressor(X_train, y_train, X_test, y_test,max_depth=20, random_state=0 ):
#Random Forest
regr = RandomForestRegressor(max_depth=max_depth, random_state=random_state)
regr.fit(X_train, y_train)
# Calculate R2 for the regression problem
R_2 = regr.score(X_test, y_test)
# Calculate MSE and MAE for the regression problem
MAE = metrics.mean_absolute_error(y_test, regr.predict(X_test))
MSE = metrics.mean_squared_error(y_test, regr.predict(X_test))
# print(f"R_2 for the regression problem is: {R_2:.2f}")
# print(f"MSE for the regression problem is: {MSE:.2f}")
# print(f"MAE for the regression problem is: {MAE:.2f}")
return regr, [R_2, MAE, MSE]
def neural_network_regressor(X_train, y_train, X_val, y_val, epochs = 150):
train_dataset = RegressionDataset(torch.from_numpy(X_train).float(), torch.from_numpy(y_train).float())
val_dataset = RegressionDataset(torch.from_numpy(X_val).float(), torch.from_numpy(y_val).float())
BATCH_SIZE = 64
LEARNING_RATE = 0.001
train_loader = DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True)
val_loader = DataLoader(dataset=val_dataset, batch_size=BATCH_SIZE)
test_loader = DataLoader(dataset=val_dataset, batch_size=1)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(f"Running on: {device}")
model = NN_Regressor()
model.to(device)
print(model)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
print("Begin training.")
loss_stats = {
'train': [],
"val": []
}
for e in tqdm(range(1, epochs+1)):
# TRAINING
train_epoch_loss = 0
model.train()
for X_train_batch, y_train_batch in train_loader:
X_train_batch, y_train_batch = X_train_batch.to(device), y_train_batch.to(device)
optimizer.zero_grad()
y_train_pred = model(X_train_batch)
train_loss = criterion(y_train_pred, y_train_batch.unsqueeze(1))
train_loss.backward()
optimizer.step()
train_epoch_loss += train_loss.item()
# VALIDATION
with torch.no_grad():
val_epoch_loss = 0
model.eval()
for X_val_batch, y_val_batch in val_loader:
X_val_batch, y_val_batch = X_val_batch.to(device), y_val_batch.to(device)
y_val_pred = model(X_val_batch)
val_loss = criterion(y_val_pred, y_val_batch.unsqueeze(1))
val_epoch_loss += val_loss.item()
loss_stats['train'].append(train_epoch_loss/len(train_loader))
loss_stats['val'].append(val_epoch_loss/len(val_loader))
# print(f'Epoch {e+0:03}: | Train Loss: {train_epoch_loss/len(train_loader):.5f} | Val Loss: {val_epoch_loss/len(val_loader):.5f}')
train_val_loss_df = pd.DataFrame.from_dict(loss_stats).reset_index().melt(id_vars=['index']).rename(columns={"index":"epochs"})
y_pred_list = []
with torch.no_grad():
model.eval()
for X_batch, _ in test_loader:
X_batch = X_batch.to(device)
y_test_pred = model(X_batch)
y_pred_list.append(y_test_pred.cpu().numpy())
y_pred_list = [a.squeeze() for a in y_pred_list]
y_pred_list = [a.squeeze().tolist() for a in y_pred_list]
MAE = metrics.mean_absolute_error(y_val, y_pred_list)
MSE = metrics.mean_squared_error(y_val, y_pred_list)
R_2 = metrics.r2_score(y_val, y_pred_list)
# print(f"R_2 for the regression problem is: {R_2:.2f}")
# print(f"MSE for the regression problem is: {MSE:.2f}")
# print(f"MAE for the regression problem is: {MAE:.2f}")
return model.cpu(),train_val_loss_df , [R_2, MAE, MSE]
def calculate_performance(perf_array):
mean = np.array(perf_array).mean(axis=0);
std = np.array(perf_array).std(axis=0);
R_2_mean = mean[0];
R_2_std = std[0];
MSE_mean = mean[1];
MSE_std = std[1];
MAE_mean = mean[2];
MAE_std = std[2];
print(f"R_2: {R_2_mean:.2f} ±{R_2_std:.2f}")
print(f"MSE: {MSE_mean:.2f} ±{MSE_std:.2f}")
print(f"MAE: {MAE_mean:.2f} ±{MAE_std:.2f}")
return
def optimized_neural_network_regressor(X_train, y_train, X_val, y_val, epochs = 150):
train_dataset = RegressionDataset(torch.from_numpy(X_train).float(), torch.from_numpy(y_train).float())
val_dataset = RegressionDataset(torch.from_numpy(X_val).float(), torch.from_numpy(y_val).float())
BATCH_SIZE = 64
LEARNING_RATE = 0.01
train_loader = DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True)
val_loader = DataLoader(dataset=val_dataset, batch_size=BATCH_SIZE)
test_loader = DataLoader(dataset=val_dataset, batch_size=1)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(f"Running on: {device}")
model = Optimized_NN_Regressor()
model.to(device)
print(model)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
print("Begin training.")
loss_stats = {
'train': [],
"val": []
}
for e in tqdm(range(1, epochs+1)):
# TRAINING
train_epoch_loss = 0
model.train()
for X_train_batch, y_train_batch in train_loader:
X_train_batch, y_train_batch = X_train_batch.to(device), y_train_batch.to(device)
optimizer.zero_grad()
y_train_pred = model(X_train_batch)
train_loss = criterion(y_train_pred, y_train_batch.unsqueeze(1))
train_loss.backward()
optimizer.step()
train_epoch_loss += train_loss.item()
# VALIDATION
with torch.no_grad():
val_epoch_loss = 0
model.eval()
for X_val_batch, y_val_batch in val_loader:
X_val_batch, y_val_batch = X_val_batch.to(device), y_val_batch.to(device)
y_val_pred = model(X_val_batch)
val_loss = criterion(y_val_pred, y_val_batch.unsqueeze(1))
val_epoch_loss += val_loss.item()
loss_stats['train'].append(train_epoch_loss/len(train_loader))
loss_stats['val'].append(val_epoch_loss/len(val_loader))
# print(f'Epoch {e+0:03}: | Train Loss: {train_epoch_loss/len(train_loader):.5f} | Val Loss: {val_epoch_loss/len(val_loader):.5f}')
train_val_loss_df = pd.DataFrame.from_dict(loss_stats).reset_index().melt(id_vars=['index']).rename(columns={"index":"epochs"})
y_pred_list = []
with torch.no_grad():
model.eval()
for X_batch, _ in test_loader:
X_batch = X_batch.to(device)
y_test_pred = model(X_batch)
y_pred_list.append(y_test_pred.cpu().numpy())
y_pred_list = [a.squeeze() for a in y_pred_list]
y_pred_list = [a.squeeze().tolist() for a in y_pred_list]
MAE = metrics.mean_absolute_error(y_val, y_pred_list)
MSE = metrics.mean_squared_error(y_val, y_pred_list)
R_2 = metrics.r2_score(y_val, y_pred_list)
# print(f"R_2 for the regression problem is: {R_2:.2f}")
# print(f"MSE for the regression problem is: {MSE:.2f}")
# print(f"MAE for the regression problem is: {MAE:.2f}")
return model.cpu(),train_val_loss_df , [R_2, MAE, MSE]