在TensorFlow中使用KFold交叉验证

我正在尝试在神经网络中使用sklearn和TensorFlow包实现KFold验证。

我的代码如下所示。

def training(self):    n_split = 3    instances = self.instance    labels = self.labels    for train_index, test_index in KFold(n_split).split(instances):        x_train, x_test = instances[train_index], instances[test_index]        y_train, y_test = labels[train_index], labels[test_index]        model = self.mlp_model()        model.fit(x_train, y_train, epochs=20)        print('Model Evaluation', model.evaluate(x_test, y_test))

遗憾的是,我遇到了一个错误

raise KeyError(f"None of [{key}] are in the [{axis_name}]")KeyError: "None of [Int64Index([160, 161, 162, 163, 164, 165, 166, 167, 168, 169,\n            ...\n            468, 469, 470, 471, 472, 473, 474, 475, 476, 477],\n           dtype='int64', length=318)] are in the [columns]"

我的instances的形状是(478,15),labels的形状是(478,)。

我的instances数据集看起来像这样

|     | SibSp | Parch | Female | Male | C | Q | S | 1 | 2 | 3 | Master ||-----|-------|-------|--------|------|---|---|---|---|---|---|--------|| 374 | 0     | 0     | 0      | 1    | 0 | 0 | 1 | 0 | 0 | 1 | 1      || 579 | 0     | 0     | 0      | 1    | 0 | 0 | 1 | 0 | 0 | 1 | 0      || 784 | 0     | 0     | 0      | 1    | 1 | 0 | 1 | 0 | 0 | 1 | 0      |

labels看起来像这样

|     | Age ||-----|-----|| 127 | 24  || 262 | 52  || 615 | 24  |

问题的可能原因是什么?可能的解决方案是什么?期待您的回复。提前感谢您。


回答:

这是一个使用KFold交叉验证在TensorFlowCIFAR10数据集上的代码示例。

获取数据

import tensorflow as tfimport numpy as np (input, target), (_, _) = tf.keras.datasets.cifar10.load_data()# 将数字解析为浮点数input = input.astype('float32') / 255target = tf.keras.utils.to_categorical(target , num_classes=10)print(input.shape, target.shape)# (50000, 32, 32, 3) (50000, 10)

模型

def my_model():    return tf.keras.Sequential(        [            tf.keras.Input(shape=(32, 32, 3)),            tf.keras.layers.Conv2D(16, 3, activation="relu"),            tf.keras.layers.Conv2D(32, 3, activation="relu"),            tf.keras.layers.Conv2D(64, 3, activation="relu"),            tf.keras.layers.Conv2D(128, 3, activation="relu"),            tf.keras.layers.Conv2D(256, 3, activation="relu"),            tf.keras.layers.GlobalAveragePooling2D(),             tf.keras.layers.Dense(10, activation='softmax')                ]    )

我们将在KFold循环中调用这个模型。

K-Fold训练

from sklearn.model_selection import KFoldimport numpy as npfor kfold, (train, test) in enumerate(KFold(n_splits=3,                                 shuffle=True).split(input, target)):    # 清除会话     tf.keras.backend.clear_session()    # 调用并编译模型     seq_model = my_model()    seq_model.compile(        loss  = tf.keras.losses.CategoricalCrossentropy(),        metrics  = tf.keras.metrics.CategoricalAccuracy(),        optimizer = tf.keras.optimizers.Adam())    print('训练集')    print(input[train].shape)    print(target[train].shape)    print('测试集')    print(input[test].shape)    print(target[test].shape)    # 运行模型     seq_model.fit(input[train], target[train],              batch_size=128, epochs=2, validation_data=(input[test], target[test]))    seq_model.save_weights(f'wg_{kfold}.h5')

日志

训练集(33333, 32, 32, 3)(33333, 10)测试集(16667, 32, 32, 3)(16667, 10)Epoch 1/211s 41ms/step - loss: 1.9961 - categorical_accuracy: 0.2363 - val_loss: 1.6851 - val_categorical_accuracy: 0.3435Epoch 2/210s 37ms/step - loss: 1.6322 - categorical_accuracy: 0.3836 - val_loss: 1.5780 - val_categorical_accuracy: 0.4193训练集(33333, 32, 32, 3)(33333, 10)测试集(16667, 32, 32, 3)(16667, 10)Epoch 1/211s 39ms/step - loss: 2.0254 - categorical_accuracy: 0.2197 - val_loss: 1.6799 - val_categorical_accuracy: 0.3601Epoch 2/210s 37ms/step - loss: 1.6687 - categorical_accuracy: 0.3739 - val_loss: 1.5222 - val_categorical_accuracy: 0.4362训练集(33334, 32, 32, 3)(33334, 10)测试集(16666, 32, 32, 3)(16666, 10)Epoch 1/211s 41ms/step - loss: 2.0170 - categorical_accuracy: 0.2212 - val_loss: 1.7452 - val_categorical_accuracy: 0.3134Epoch 2/210s 37ms/step - loss: 1.7110 - categorical_accuracy: 0.3363 - val_loss: 1.5928 - val_categorical_accuracy: 0.4164

更新

另一个使用pandas数据框的方法。

# 加载df = pd.read_csv('train.csv')# [可选]: 打乱数据df = df.sample(frac=1).reset_index(drop=True)# 折叠分割 kfold = KFold(n_splits=3, shuffle=True)for each_fold, (trn_idx, val_idx) in enumerate(.split(np.arange(df.shape[0]),                                                        df.target.values)):       # 获取折叠数据        train_labels = df.iloc[trn_idx].reset_index(drop=True)        val_labels   = df.iloc[val_idx].reset_index(drop=True)        # train_labels: 训练对(数据 + 标签)       # 做一些事情

让我们再给出一个多标签数据的示例。这次我们将首先创建一个折叠列。请看下面的示例。

# !pip install iterative-stratificationfrom iterstrat.ml_stratifiers import MultilabelStratifiedKFold# 加载数据 df = pd.read_csv('train.csv')# 添加一个新列,目前所有值设置为-1# 我们稍后会更新它 df.loc[:, 'kfold'] = -1 # [可选]: 打乱 df = df.sample(frac=1).reset_index(drop=True)# 由于我们现在处理的是多标签情况, # 目标值会有不止一列 # 所以我们只获取目标值 # 我们将删除 id / image_id / image_name / blabla.. 列target = df.drop('id', axis-1).values mskf = MultilabelStratifiedKFold(n_splits=5)for each_kfold, (trn_idx, val_idx) in enumerate(mskf.split(df, target):       df.loc[val_idx, 'kfold] = each_fold # 更新 `kfold` 列 

之后,n_split = 5 将出现在 ‘kfold’ 列中,也可以使用 df.head() 进行检查。接下来,我们可以做如下操作

def program(fold):    # 获取折叠数据    train_labels = df[df.kfold != fold].reset_index(drop=True)    val_labels   = df[df.kfold == fold].reset_index(drop=True)    # train_labels: 训练对(数据 + 标签)    # 做一些事情for i in range(n_split):      program(fold=i) 

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注