Even belatedly, let me throw in my 2 drachmas. I am of the opinion that you can train
a model using KFold cross-validation as long as you do it inside a loop. I am not posting everything, just the juice. Mind you that the functions below can be used with any model, be it an sklearn predictor/pipeline, a Keras or a Pytorch model after affecting the necessary array/tensor conversions is some cases. Here goes:
#Get the necessary metrics (in this case for a severely imbalanced dataset)
def get_scores(model,X,y):
pred=np.round(model.predict(X))
probs=model.predict_proba(X)[:,1]
precision,recall,_=precision_recall_curve(y,probs)
accu=accuracy_score(y,pred)
pr_auc=auc(recall,precision)
f2=fbeta_score(y,pred,beta=2)
return pred,accu,pr_auc,f2
#Train model with KFold cross-validation
def train_model(model,X,y):
accu_list,pr_auc_list,f2_list=[],[],[]
kf=StratifiedKFold(5,False,seed)
for train,val in kf.split(X,y):
X_train,y_train=X[train],y[train]
X_val,y_val=X[val],y[val]
model.fit(X_train,y_train)
_,accu,pr_auc,f2=get_scores(model,X_val,y_val)
accu_list.append(accu)
pr_auc_list.append(pr_auc)
f2_list.append(f2)
print(f'Training Accuracy: {np.mean(accu_list):.3f}')
print(f'Training PR_AUC: {np.mean(pr_auc_list):.3f}')
print(f'Training F2: {np.mean(f2_list):.3f}')
return model
So, after training and when you want to predict unknown data you would just do:
fitted_model=train_model(model,X,y)
I hope that works out for you. In my case it works everytime. Now, computational cost is different - and very sad - matter.