-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNet.py
More file actions
291 lines (223 loc) · 8.06 KB
/
Copy pathRNet.py
File metadata and controls
291 lines (223 loc) · 8.06 KB
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
import numpy as np
from numpy import vstack
from pandas import read_csv
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score, roc_auc_score, precision_score, average_precision_score
from sklearn.metrics import confusion_matrix, recall_score, f1_score
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torch.utils.data import random_split
import torch
from torch import Tensor
from torch.nn import Linear
from torch.nn import ReLU
from torch.nn import Module
from torch.optim import Adam
from torch.nn import MSELoss
import time
import copy
import sys
import random
import numpy as np
import torch
seed = 42
# Python
random.seed(seed)
# NumPy
np.random.seed(seed)
# PyTorch (CPU)
torch.manual_seed(seed)
# PyTorch (GPU)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# Ensure deterministic behavior (may reduce performance)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
import torch.nn as nn
def reinitialize_weights(model):
for m in model.modules():
if hasattr(m, "reset_parameters"):
m.reset_parameters()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class WineCSVDataset(Dataset):
#Constructor for initially loading
def __init__(self,path):
df = read_csv(path, header=None)
# Remove inconsistent rows
cols_to_check = df.columns[:-1]
# Drop rows where those columns have identical values
dfclean = df.drop_duplicates(subset=cols_to_check, keep='first')
# Store the inputs and outputs
self.X = dfclean.values[:, :-1]
self.y = dfclean.values[:, -1] # Assuming your outcome variable is in the last column
self.X = self.X.astype('float32')
# Get the number of rows in the dataset
def __len__(self):
return len(self.X)
# Get a row at an index
def __getitem__(self,idx):
return [self.X[idx], self.y[idx]]
import WineMLP
# Create training loop based off our custom class
def train_model(train_dl, model, epochs=100, lr=0.01, momentum=0.9):
global sc
# Define your optimisation function for reducing loss when weights are calculated
# and propogated through the network
start = time.time()
criterion = MSELoss()
optimizer = Adam(model.parameters(), lr=lr)
loss = 0.0
for epoch in range(epochs):
model.train()
# Iterate through training data loader
sc = 0
for i, (inputs, targets) in enumerate(train_dl):
optimizer.zero_grad()
inputs = inputs.to(torch.float32)
outputs = model(inputs)
loss = criterion(outputs.flatten().float(), targets.flatten().float())
loss.backward()
optimizer.step()
sc += inputs.shape[0]
if (epoch % 100 == 0):
trainMSE = evaluate_model(train_dl, model)
print("Epoch " + str(epoch) + ", trainMSE = " + str(trainMSE) + ", loss = " + str(loss.data.item()))
sys.stdout.flush()
time_delta = time.time() - start
sys.stdout.flush()
return model
import math
def evaluate_model(test_dl, model):
preds = []
actuals = []
for (i, (inputs, targets)) in enumerate(test_dl):
#Evaluate the model on the test set
inputs = inputs.to(torch.float32)
yhat = model(inputs)
#Retrieve a numpy weights array
yhat = yhat.detach().numpy()
# Extract the weights using detach to get the numerical values in an ndarray, instead of tensor
actual = targets.numpy()
actual = actual.reshape((len(actual), 1))
# Round to get the class value i.e. sick vs not sick
# yhat = yhat.round()
# Store the predictions in the empty lists initialised at the start of the class
# print(yhat)
# print(actual)
preds.append(yhat)
actuals.append(actual)
# Stack the predictions and actual arrays vertically
preds, actuals = vstack(preds), vstack(actuals)
return np.mean(np.square(preds - actuals))
nFeat = 0
def prepare_wine_dataset(path, fold):
global nFeat
dataset1 = WineCSVDataset(path)
dataset2 = WineCSVDataset(path)
train = dataset1
test = dataset2
ltot = len(train)
nFeat = len(train.X[0])
trsize = int( 4 * ltot / 5 );
tesize = int( ltot / 5 );
print("trsize = " + str(trsize))
print("tesize = " + str(tesize))
cTr = 0
cTe = 0
X_train = np.zeros((trsize, nFeat))
y_train = np.zeros(trsize)
X_test = np.zeros((tesize, nFeat))
y_test = np.zeros(tesize)
for i in range(0, ltot):
if (not ((fold * tesize <= i) and (i < (fold+1) * tesize) ) ):
for j in range(nFeat):
X_train[cTr][j] = train.X[i][j]
y_train[cTr] = train.y[i]
if (cTr < trsize-1):
cTr += 1
else:
for j in range(0, nFeat):
X_test[cTe][j] = train.X[i][j]
y_test[cTe] = train.y[i]
cTe += 1
print("cTr = ", str(cTr));
print("cTe = ", str(cTe));
trsize = cTr;
tesize = cTe;
X_train = X_train[0:cTr, : ]
y_train = y_train[0:cTr]
X_test = X_test[0:cTe, : ]
y_test = y_test[0:cTe]
train.X = X_train
train.y = y_train
test.X = X_test
test.y = y_test
# # Prepare data loaders
train_dl = DataLoader(train, batch_size=len(train), shuffle=False)
test_dl = DataLoader(test, batch_size=1024, shuffle=False)
return train_dl, test_dl
bmse = np.zeros(5)
bgs = np.zeros(5)
tes = []
dsname = sys.argv[1]
nep = sys.argv[2]
for f in range(0, 5):
print("------------------------------------------------------------------------")
print("FOLD " + str(f))
train_dl, test_dl = prepare_wine_dataset(dsname, f)
print("nFeat = " + str(nFeat))
import subprocess
import sys
sc = 0
bestTeMSE = 1E10
bgs = 0
# gammaS = math.pow(10, 2)
# print("gammaS = " + str(gammaS))
# subprocess.run(["./S", "training.txt", str(nFeat), str(gammaS)])
# stra = torch.tensor(np.genfromtxt("S.txt", delimiter=","), requires_grad=True)
model = WineMLP.WineMLP(nFeat)
sys.stdout.flush()
meanRUNTrainMSE = 0
meanRUNTestMSE = 0
for r in range(3):
print("Run: " + str(r+1))
reinitialize_weights(model)
# with torch.no_grad():
# for param in model.parameters():
# param.fill_(0.1)
train_model(train_dl,
model,
epochs = int(nep),
lr = 0.001)
trainMSE = evaluate_model(train_dl, model)
testMSE = evaluate_model(test_dl, model)
meanRUNTrainMSE += trainMSE
meanRUNTestMSE += testMSE
print("training MSE = " + str(trainMSE))
print("test MSE = " + str(testMSE))
meanRUNTrainMSE /= 3.0
meanRUNTestMSE /= 3.0
print("mean training MSE = " + str(meanRUNTrainMSE))
print("mean test MSE = " + str(meanRUNTestMSE))
sys.stdout.flush()
if (meanRUNTestMSE < bestTeMSE):
print("----> IMPROVED bestTeMSE : " + str(meanRUNTestMSE))
#print("----> gammaS : " + str(gammaS))
bestTeMSE = meanRUNTestMSE
bmse[f] = meanRUNTestMSE
#bgs = gammaS
sys.stdout.flush()
print("END FOLD COMPUTING -------------------------------------------------")
#print("Best gammaS = " + str(bgs))
print("FOLD " + str(f) + ", Best Test MSE = " + str(bestTeMSE))
tes.append(bestTeMSE)
print("--------------------------------------------------------------------")
mmse = np.mean(bmse)
sd = 0
for i in range(0, len(tes)):
sd += (mmse - tes[i]) * (mmse - tes[i])
sd = math.sqrt(sd / float(5))
print("--------------------------------------------------------------------")
print("END: 5-FOLD MEAN TEST MSE = " + str(mmse) + ", sd = " + str(sd))
print("--------------------------------------------------------------------")