-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_selection.py
167 lines (124 loc) · 5.31 KB
/
model_selection.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
import numpy as np
from math import ceil
from sklearn.model_selection import KFold
import tensorflow as tf
import gc
def train_test_split_per_class(x, y, test_size=0.3):
test_indexes = []
for label in np.unique(y):
indexes = np.where(y == label)[0]
test_indexes.extend(np.random.choice(indexes, ceil(test_size*indexes.size), False))
x_train = x[[i for i in range(len(x)) if i not in test_indexes]]
y_train = y[[i for i in range(len(y)) if i not in test_indexes]]
x_test = x[test_indexes]
y_test = y[test_indexes]
return x_train, x_test, y_train, y_test
def cross_validation(x, y, model, k_folds, optimizer, loss, metrics, epochs, batch_size):
kf = KFold(n_splits=k_folds, shuffle=True)
iteration = 1
results = []
for train_index, test_index in kf.split(x,y):
tf.keras.backend.clear_session()
gc.collect()
try:
del model_clone
except NameError:
pass
print('Fold {}'.format(iteration))
model_clone = tf.keras.models.clone_model(model)
model_clone.compile(optimizer = optimizer, loss=loss, metrics=metrics)
x_train, X_test = x[train_index], x[test_index]
y_train, y_test = tf.one_hot(y[train_index], 5), tf.one_hot(y[test_index], 5)
model_clone.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, verbose=0)
results.append(model_clone.evaluate(X_test, y_test))
iteration+=1
return results
def cross_validation_per_class(x, y, model, k_folds, optimizer, loss, metrics, epochs, batch_size):
unique_labels = np.unique(y)
for label in unique_labels:
k_folds = min(k_folds, len(np.where(y == label)[0]))
folds = []
for label in unique_labels:
label_index = (np.where(y == label)[0])
np.random.shuffle(label_index)
label_folds = np.array_split(label_index, k_folds)
folds.append(label_folds)
results = []
for i in range(k_folds):
test_folds = []
train_folds = []
for label_folds in folds:
test_folds.append(label_folds[i])
train_folds.extend(label_folds[:i])
train_folds.extend(label_folds[i+1:])
test_index = np.concatenate(test_folds)
train_index = np.concatenate(train_folds)
tf.keras.backend.clear_session()
gc.collect()
try:
del model_clone
except NameError:
pass
print('Fold {}/{}'.format(i, k_folds))
model_clone = tf.keras.models.clone_model(model)
model_clone.compile(optimizer = optimizer, loss=loss, metrics=metrics)
x_train, X_test = x[train_index], x[test_index]
y_train, y_test = tf.one_hot(y[train_index], 5), tf.one_hot(y[test_index], 5)
model_clone.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, verbose=0)
results.append(model_clone.evaluate(X_test, y_test))
return results
def over_sampling(x, y, labels, size=0):
if size <= 0:
for label in list(set(np.unique(y)) - set(labels)):
index = np.where(y==label)[0]
size = len(index) if size==0 else min(size, len(index))
if size <= 0:
return x,y
samples = []
for label in labels:
index = np.where(y==label)[0]
if len(index) < size:
diff = size - len(index)
new_samples1 = diff // len(index)
new_samples2 = diff % len(index)
index = np.concatenate((np.repeat(index, new_samples1), np.random.choice(index, new_samples2, False)))
samples.extend(index)
x_samples = x[samples]
y_samples = y[samples]
return np.concatenate((x,x_samples)), np.concatenate((y,y_samples))
def over_sampling_cv(x, y, model, k_folds, optimizer, loss, metrics, epochs, batch_size, sampling_labels, size=0):
unique_labels = np.unique(y)
for label in unique_labels:
k_folds = min(k_folds, len(np.where(y == label)[0]))
folds = []
for label in unique_labels:
label_index = (np.where(y == label)[0])
np.random.shuffle(label_index)
label_folds = np.array_split(label_index, k_folds)
folds.append(label_folds)
results = []
for i in range(k_folds):
test_folds = []
train_folds = []
for label_folds in folds:
test_folds.append(label_folds[i])
train_folds.extend(label_folds[:i])
train_folds.extend(label_folds[i+1:])
test_index = np.concatenate(test_folds)
train_index = np.concatenate(train_folds)
tf.keras.backend.clear_session()
gc.collect()
try:
del model_clone
except NameError:
pass
print('Fold {}/{}'.format(i, k_folds))
model_clone = tf.keras.models.clone_model(model)
model_clone.compile(optimizer = optimizer, loss=loss, metrics=metrics)
x_train, x_test = x[train_index], x[test_index]
y_train, y_test = y[train_index], tf.one_hot(y[test_index], 5)
x_sampled, y_sampled = over_sampling(x_train, y_train, sampling_labels)
y_sampled = tf.one_hot(y_sampled, 5)
model_clone.fit(x_sampled, y_sampled, epochs=epochs, batch_size=batch_size, verbose=0)
results.append(model_clone.evaluate(x_test, y_test))
return results