/
independentLearner.py
276 lines (206 loc) · 8.82 KB
/
independentLearner.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
# %%
# Import relevant libraries
import numpy as np
from numpy.random import default_rng
from processData import processData
import datasetLoading as dataset
from sklearn.model_selection import KFold
import copy
# Set a random state for repeatable results
rng = default_rng(101)
# %%
#Sigmoid activation function
def activationSig( Z ):
return 1 / ( 1 + np.exp( -Z ) )
# Calculate the Loss
def loss (X, y, W):
N = X.shape[1]
y_predict = np.sum( X * W, axis = 1)
y_predict = activationSig(np.array(y_predict)).T
epsilon = 1e-10
return -1 / N * np.sum (y * np.log(y_predict + epsilon) + (1 - y) * np.log(1 - y_predict) )
def lNorm (W):
# The last feature will be the bias term
return np.sum(np.abs(W[1:]))
# %%
class analysis:
def __init__( self, X, y, W, limit):
self.X = X
self.y = y
self.W = W
y_predict = np.sum( self.X * self.W, axis = 1)
self.y_predict = activationSig(np.array(y_predict)).T
self.limit = np.float32(limit)
def metrics(self):
y = np.reshape( self.y, ( len( self.y ), 1 ) )
y_pred = self.y_predict
y_pred = np.array( [ 0 if i < self.limit else 1 for i in y_pred ] )
y_pred = np.reshape( y_pred, ( len( y ), 1 ) )
truePositive = np.count_nonzero( (y + y_pred) == 2)
falseNegative = np.count_nonzero( (y - y_pred) == 1)
falsePositive = np.count_nonzero( (y - y_pred) == -1)
if truePositive > 0:
precision = truePositive / ( truePositive + falsePositive )
recall = truePositive / ( truePositive + falseNegative )
f1 = 2 * ( precision * recall ) / ( precision + recall )
print('Task', i, ', F1 Score: ', np.round(f1 * 100, 2))
else:
f1 = 0
error = ( falseNegative + falsePositive ) / len( y )
print('Task', i, ', Percentage Error: ', np.round(error *100, 2))
return f1, error
# %%
# Define key parameters
m = dataset.m # number of features
t = dataset.t # number of tasks
rawData = dataset.rawData
taskList = dataset.taskList
testData = dataset.testData
test_size = 0.25 # Training and testing split for data
e = 0.1
c = 0.1
#%%
processedData = {}
data = {}
for i in taskList:
# Process the raw data for model (scale, split the data, etc)
processedData[i] = processData( rawData[i], m, test_size )
# Create data for k-folding ;)
y = np.append(processedData[i].y_train, processedData[i].y_test)
X = np.append(processedData[i].X_train, processedData[i].X_test, axis = 0 )
X_test = (processedData[i].scaleAndBias(testData[i][:, :-1]))
y_test = testData[i][:, -1]
data[i] = np.append(X, np.reshape(y, [len(y), 1]), axis = 1)
testData[i] = np.append(X_test, np.reshape(y_test, [len(y_test), 1]), axis = 1)
# Swap data around
# data2 = {}
# data2[0] = data[1]
# data2[1] = data[0]
# data = data2
# planes = ['B1', 'B2', 'A1', 'A2']
#%%
# This K-fold is for splitting the data by features such that only one feature window is considered at a time
windows = KFold(n_splits = 2)
Results = []
for i in taskList:
Window = 1
for k, window in windows.split(range(m)):
print('Window: ', Window)
# Insert a bias term into each fold and keep the target
windowDataRange = np.insert( np.append(window + 1, -1), 0, 0 )
windowData = [ data[i][ : , windowDataRange] for i in taskList ]
windowData_test = [ testData[i][ : , windowDataRange] for i in taskList ]
# Generate a new array for results for each of the folds
X_train = np.array(windowData[i][ :, :-1])
y_train = np.array(windowData[i][ :, -1])
X_test = np.array(windowData_test[i][ :, :-1])
y_test = np.array(windowData_test[i][ :, -1])
# Initialise Parameters
# Weight matrix
W = np.zeros([windowDataRange.shape[0] - 1 ])
J0 = loss(X_train, y_train, W)
# Number of features
m_window = window.shape[0]
# Determine the high impact weight
all_loss_pos = []
all_loss_neg = []
indexTracker = []
for j in range(m_window):
W_i = W.copy()
W_i[j] = W_i[j] + e
iteration_loss = loss(X_train, y_train, W_i)
all_loss_pos.append(iteration_loss)
W_i = W.copy()
W_i[j] = W_i[j] - e
iteration_loss = loss(X_train, y_train, W_i)
all_loss_neg.append(iteration_loss)
if np.min(all_loss_pos) < np.min(all_loss_neg):
W[np.argmin(all_loss_pos)] = e
indexTracker.append(np.argmin(all_loss_pos))
else:
W[np.argmin(all_loss_neg)] = -e
indexTracker.append(np.argmin(all_loss_neg))
# Empirical Loss
J = loss(X_train, y_train, W)
# Regularisation loss
lnorm = lNorm(W)
# Regularsation Parameter, lambda
reg_param = (J0 - J) / lnorm
# Total loss
T = reg_param * lnorm
# Total loss (used in iteration 1)
Y = J + T
Z = 0
while reg_param > 0: # Should be 0
# Try to take backward step
all_loss_pos = []
all_loss_neg = []
for j in indexTracker:
W_x = copy.deepcopy(W)
W_i = W_x.copy()
W_i[j] = W_i[j] - np.sign( W_i[j] ) * e
iteration_loss = loss(X_train, y_train, W_i) #+ reg_param * lNorm(W_i, t, m_window)
all_loss_pos.append(iteration_loss)
# Update the weights with the lowest overall loss from +e and -e lists
W_x[indexTracker[np.argmin(all_loss_pos)]] = W_x[indexTracker[np.argmin(all_loss_pos)]] - np.sign( W_i[j] ) * e
# Regularisation loss
T_x = reg_param * lNorm(W_x)
# Empirical Loss
J_x = loss(X_train, y_train, W_x)
# Total loss
Y_x = J_x + T_x
# This is the criteria for taking a backwards step.
if (Y_x - Y) < -c:
# If it is met then update the parameters
W = W_x
Z += 1
Y = Y_x
print('BACKWARD')
# Otherwise take a forward step
else:
# Take a forward step
all_loss_pos = []
all_loss_neg = []
W_x = copy.deepcopy(W)
for j in range(m_window + 1):
# Determine the loss for each feature block with +e
W_i = W_x.copy()
W_i[j] = W_i[j] + e
iteration_loss = loss(X_train, y_train, W_i)
all_loss_pos.append(iteration_loss)
# Determine the loss for each feature block with -e
W_i = W_x.copy()
W_i[j] = W_i[j] - e
iteration_loss = loss(X_train, y_train, W_i)
all_loss_neg.append(iteration_loss)
# Select the lowest loss from all of the +e and -e.
# The weights with the lowest loss will be updated
if np.min(all_loss_pos) < np.min(all_loss_neg):
W_x[np.argmin(all_loss_pos)] = W_x[np.argmin(all_loss_pos)] + e
J_x = np.min(all_loss_pos)
else:
W_x[np.argmin(all_loss_neg)] = W_x[np.argmin(all_loss_neg)] -e
J_x = np.min(all_loss_neg)
# Determine the lnorm for the new set of weights
lnorm_x = lNorm(W_x)
# Determine if the regularisation parameter should be updated
reg_param = np.min( (reg_param, (J - J_x) / (np.sqrt(t) * e ) ) )
# Redefine the weight matrix, losses and iteration number
W = W_x
J = J_x
lnorm = copy.deepcopy(lnorm_x)
Y = J + reg_param * lnorm
Z += 1
# Redefine the indexTracker for the backward steps
indexTracker = []
for l in np.argwhere(W != 0):
indexTracker.append(l)
if Z%10 == 0:
print(Z)
print(reg_param)
f1, error = analysis(X_test, y_test, W, 0.5).metrics()
results = [Window, e, c, Z, f1, error, W, i]
Results.append(results)
np.save('STL_Boosted_Sensitivity_Results_Final_3.npy', Results)
Window += 1
# %%