How to perform k-fold cross validation with tensorflow?

后端 未结 2 1553
灰色年华
灰色年华 2021-01-31 04:39

I am following the IRIS example of tensorflow.

My case now is I have all data in a single CSV file, not separated, and I want to apply k-fold cross validation on that da

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 05:18

    NN's are usually used with large datasets where CV is not used - and very expensive. In the case of IRIS (50 samples for each species), you probably need it.. why not use scikit-learn with different random seeds to split your training and testing?

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
    

    for k in kfold:

    1. split data differently passing a different value to "random_state"
    2. learn the net using _train
    3. test using _test

    If you dont like the random seed and want a more structured k-fold split, you can use this taken from here.

    from sklearn.model_selection import KFold, cross_val_score
    X = ["a", "a", "b", "c", "c", "c"]
    k_fold = KFold(n_splits=3)
    for train_indices, test_indices in k_fold.split(X):
        print('Train: %s | test: %s' % (train_indices, test_indices))
    Train: [2 3 4 5] | test: [0 1]
    Train: [0 1 4 5] | test: [2 3]
    Train: [0 1 2 3] | test: [4 5]
    

提交回复
热议问题