【问题标题】:How to perform k-fold cross validation with tensorflow?如何使用 tensorflow 进行 k 折交叉验证?
【发布时间】:2017-02-06 12:02:54
【问题描述】:

我关注the IRIS example of tensorflow

我现在的情况是,我将所有数据都放在一个 CSV 文件中,没有分开,我想对该数据应用 k 折交叉验证。

我有

data_set = tf.contrib.learn.datasets.base.load_csv(filename="mydata.csv",
                                                   target_dtype=np.int)

如何使用与 IRIS 示例相同的多层神经网络对这个数据集执行 k 折交叉验证?

【问题讨论】:

    标签: python tensorflow cross-validation train-test-split


    【解决方案1】:

    我知道这个问题很老了,但如果有人想做类似的事情,请扩展 ahmedhosny's 答案:

    新的 tensorflow 数据集 API 能够使用 python 生成器创建数据集对象,因此与 scikit-learn 的 KFold 一起,一个选项可以是从 KFold.split() 生成器创建数据集:

    import numpy as np
    
    from sklearn.model_selection import LeaveOneOut,KFold
    
    import tensorflow as tf
    import tensorflow.contrib.eager as tfe
    tf.enable_eager_execution()
    
    from sklearn.datasets import load_iris
    data = load_iris()
    X=data['data']
    y=data['target']
    
    def make_dataset(X_data,y_data,n_splits):
    
        def gen():
            for train_index, test_index in KFold(n_splits).split(X_data):
                X_train, X_test = X_data[train_index], X_data[test_index]
                y_train, y_test = y_data[train_index], y_data[test_index]
                yield X_train,y_train,X_test,y_test
    
        return tf.data.Dataset.from_generator(gen, (tf.float64,tf.float64,tf.float64,tf.float64))
    
    dataset=make_dataset(X,y,10)
    

    然后可以在基于图的张量流中或使用急切执行来遍历数据集。使用急切执行:

    for X_train,y_train,X_test,y_test in tfe.Iterator(dataset):
        ....
    

    【讨论】:

    • 如果 Xy 不能像这个 sn-p 假设的那样保存在内存中怎么办?我认为使用生成器的全部意义在于按需加载样本,而不是将整个数据集加载到内存中。
    • @fabiomaia 可以使用相同的技术按需加载它们。例如,X 可以表示文件名列表,在 for 循环中您可以按需加载文件内容。
    • @gw0 它不适用于大型数据集(图像)。它仍然会在循环中消耗大量内存,无论您是预加载所有数据并拆分或拆分然后按需加载它们都是相同的内存使用量。由于内存使用过多,我已经尝试过这两种方法和程序崩溃。但是,我通过传递图像文件路径来计算它,并且在每个折叠中,我根据训练和验证(测试)的拆分索引创建数据集。现在它可以在没有过多内存使用的情况下工作。
    【解决方案2】:

    NN 通常用于不使用 CV 且非常昂贵的大型数据集。对于 IRIS(每个物种 50 个样本),您可能需要它。 为什么不使用scikit-learn with different random seeds 来拆分训练和测试?

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

    对于 kfold 中的 k:

    1. 以不同的方式拆分数据,将不同的值传递给“random_state”
    2. 使用 _train 学习网络
    3. 使用 _test 进行测试

    如果您不喜欢随机种子并想要更结构化的 k 折拆分, 你可以使用这个取自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]
    

    【讨论】:

    • 答案与问题无关!!!应该使用 Tensorflow 解决方案提供答案
    • 由于答案提供了可用于 Tensorflow 的解决方案 - 我看不到问题。
    • 我们怎样才能让它更加随机化?
    【解决方案3】:

    修改@ahmedhosny 答案

    from sklearn.model_selection import KFold, cross_val_score
    k_fold = KFold(n_splits=k)
    train_ = []
    test_ = []
    for train_indices, test_indices in k_fold.split(all_data.index):
        train_.append(train_indices)
        test_.append(test_indices)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-06
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多