Where to create model object in keras in K-Fold Cross validation?

455 views Asked by At

Where to create Keras model object, inside K-fold loop, or outside? please explain why your answer is true.

def model_def(): 
     model = Sequential()
     model.add(.... so on....)
     model.compile(....so on ....)
     return model

Case 1:- inside the K-fold loop, so it is recreating for each loop

for train_index, test_index in kf.split(X,Y):
     model = model_def()
     model.fit(X[train_index],Y[test_index] ..... so on .....

or, Case 2:- outside the loop, so a single model object for all folding loop

model = model_def()
for train_index, test_index in kf.split(X,Y):
     model.fit(X[train_index],Y[test_index] ..... so on .....

1

There are 1 answers

0
Proko On BEST ANSWER

Inside.

For every fold, you want to have a completely new model. This means your model cannot have any weights learnt through data from another fold (that would happen if you do it inside because in every fold you operate on the same instance). The point of k-fold learning is to check how your model performs on a small part of dataset, so it should not have any information about data from other folds.