Commit bbce07cf by Pamela Osuna

one line per epoch + independent models

parent 628238b0
...@@ -7,7 +7,7 @@ from sklearn.metrics import confusion_matrix ...@@ -7,7 +7,7 @@ from sklearn.metrics import confusion_matrix
import numpy as np import numpy as np
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from imblearn.over_sampling import SMOTE from imblearn.over_sampling import SMOTE
from sklearn.model_selection import KFold from sklearn.model_selection import StratifiedKFold
from tensorflow.keras.utils import to_categorical from tensorflow.keras.utils import to_categorical
...@@ -21,12 +21,13 @@ def run_nn(input_, output_, n_experiences, params): ...@@ -21,12 +21,13 @@ def run_nn(input_, output_, n_experiences, params):
c, b, e = params c, b, e = params
#kfold validation # kfold validation
"""" """
X for the input and y for the output X for the input and y for the output
""" """
kfold = KFold(N_SPLITS, True, 1) #on definit la methode a utiliser en choisisant n_splits, shuffle on/off, random_state skf = StratifiedKFold(N_SPLITS)
#kfold = KFold(N_SPLITS, True, 1) #on definit la methode a utiliser en choisisant n_splits, shuffle on/off, random_state
X_train_kfold = [] X_train_kfold = []
X_test_kfold = [] X_test_kfold = []
...@@ -35,7 +36,8 @@ def run_nn(input_, output_, n_experiences, params): ...@@ -35,7 +36,8 @@ def run_nn(input_, output_, n_experiences, params):
#split the input data into k sets #split the input data into k sets
for train_index, test_index in kfold.split(input_): #for train_index, test_index in kfold.split(input_):
for train_index, test_index in skf.split(input_, output_):
X_train_kfold.append(input_[train_index]) X_train_kfold.append(input_[train_index])
X_test_kfold.append(input_[test_index]) X_test_kfold.append(input_[test_index])
y_train_kfold.append(output_[train_index]) y_train_kfold.append(output_[train_index])
...@@ -53,7 +55,7 @@ def run_nn(input_, output_, n_experiences, params): ...@@ -53,7 +55,7 @@ def run_nn(input_, output_, n_experiences, params):
y_validation = [[0]*(N_SPLITS-1) for i in range(N_SPLITS)] y_validation = [[0]*(N_SPLITS-1) for i in range(N_SPLITS)]
y_train = [[0]*(N_SPLITS-1) for i in range(N_SPLITS)] y_train = [[0]*(N_SPLITS-1) for i in range(N_SPLITS)]
len_validation = int(len(X_train_kfold[0])/4) len_validation = int(len(X_train_kfold[0])/(N_SPLITS))
for i in range(N_SPLITS): for i in range(N_SPLITS):
idx = 0 idx = 0
...@@ -88,10 +90,6 @@ def run_nn(input_, output_, n_experiences, params): ...@@ -88,10 +90,6 @@ def run_nn(input_, output_, n_experiences, params):
X_train[i][j] = X_train[i][j].astype('float32') X_train[i][j] = X_train[i][j].astype('float32')
X_validation[i][j] = X_validation[i][j].astype('float32') X_validation[i][j] = X_validation[i][j].astype('float32')
# defining keras model
model = m.model_architecture(c)
#compile the keras model
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
#self reminder : warning! be careful not to use i and j as indexes later in here for something else #self reminder : warning! be careful not to use i and j as indexes later in here for something else
...@@ -104,9 +102,11 @@ def run_nn(input_, output_, n_experiences, params): ...@@ -104,9 +102,11 @@ def run_nn(input_, output_, n_experiences, params):
for i in range(N_SPLITS): for i in range(N_SPLITS):
for j in range(N_SPLITS-1): for j in range(N_SPLITS-1):
#train the model #defining keras model
model.fit(X_train[i][j], train_Y_one_hot[i][j], batch_size = bs, epochs = ep, verbose = 1, validation_data = (X_validation[i][j], validation_Y_one_hot[i][j])) model = m.model_architecture(c)
#compile the keras model
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(X_train[i][j], train_Y_one_hot[i][j], batch_size = bs, epochs = ep, verbose = 2, validation_data = (X_validation[i][j], validation_Y_one_hot[i][j]))
#calculate accuracy #calculate accuracy
_,accuracy = model.evaluate(X_validation[i][j], validation_Y_one_hot[i][j], verbose = 0) _,accuracy = model.evaluate(X_validation[i][j], validation_Y_one_hot[i][j], verbose = 0)
total_acc += accuracy total_acc += accuracy
...@@ -122,7 +122,7 @@ def run_nn(input_, output_, n_experiences, params): ...@@ -122,7 +122,7 @@ def run_nn(input_, output_, n_experiences, params):
total_acc = total_acc/(N_SPLITS*(N_SPLITS-1)) total_acc = total_acc/(N_SPLITS*(N_SPLITS-1))
total_auc = total_acc/(N_SPLITS*(N_SPLITS-1)) total_auc = total_auc/(N_SPLITS*(N_SPLITS-1))
print("Average accuracy: ", total_acc) print("Average accuracy: ", total_acc)
print("Average area under the curve: ", total_auc) print("Average area under the curve: ", total_auc)
...@@ -149,12 +149,6 @@ def run_kfold(X_train, X_test, y_train, y_test, params): ...@@ -149,12 +149,6 @@ def run_kfold(X_train, X_test, y_train, y_test, params):
X_train[i] = X_train[i].astype('float32') X_train[i] = X_train[i].astype('float32')
X_test[i] = X_test[i].astype('float32') X_test[i] = X_test[i].astype('float32')
# defining keras model
model = m.model_architecture(c)
#compile the keras model
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
total_acc = 0 total_acc = 0
total_auc = 0 total_auc = 0
precs_k = [] #it will contain the average pr curve for each class precs_k = [] #it will contain the average pr curve for each class
...@@ -164,6 +158,9 @@ def run_kfold(X_train, X_test, y_train, y_test, params): ...@@ -164,6 +158,9 @@ def run_kfold(X_train, X_test, y_train, y_test, params):
for i in range(N_SPLITS): for i in range(N_SPLITS):
model = m.model_architecture(c)
#compile the keras model
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
#train the model #train the model
model.fit(X_train[i], y_train[i], batch_size = bs, epochs = ep, verbose = 1, validation_data = (X_test[i], y_test[i])) model.fit(X_train[i], y_train[i], batch_size = bs, epochs = ep, verbose = 1, validation_data = (X_test[i], y_test[i]))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment