具有交叉验证的卷积神经网络

Posted

技术标签:

【中文标题】具有交叉验证的卷积神经网络【英文标题】:convolutional neural network with cross validation 【发布时间】:2020-03-27 15:37:36 【问题描述】:

我想在这里使用 CNN,但我失败了。我在这里闲逛的原因是什么?而且这里的建模是对的吗?老师先介绍了cnn,然后是cross-validation,然后让我们再继续cnn

scores = []
best_svr = SVR(kernel='rbf')
cv = KFold(n_splits=10, random_state=42, shuffle=True)
for train_index, test_index in cv.split(X):
print("Train Index: ", train_index, "\n")
print("Test Index: ", test_index)

X_train, X_test, Y_train, Y_test = X[train_index], X[test_index], Y[train_index], Y[test_index]
best_svr.fit(X_train, Y_train)
scores.append(best_svr.score(X_test, Y_test))

best_svr.fit(X_train, Y_train)
scores.append(best_svr.score(X_test, Y_test))

print(np.mean(scores))

from sklearn import preprocessing


min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)

X_scale

from sklearn.model_selection import train_test_split

我需要有 30% 到 70% 的交叉验证率吗?

X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, Y, test_size=0.3)

X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.5)


print(X_train.shape, X_val.shape, X_test.shape, Y_train.shape, Y_val.shape, Y_test.shape)

from keras.models import Sequential
from keras.layers import Dense


from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense


classifier = Sequential()

model = Sequential([
Dense(32, activation='relu', input_shape=(13,)),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid'),
])


model.compile(optimizer='sgd',
          loss='binary_crossentropy',
          metrics=['accuracy'])

【问题讨论】:

这篇文章需要更多的细节和清晰度。包括您的所有代码,并且只包含相关的代码。另外,提供输入/输出形状。 如果我提供我的邮件地址,你能帮我吗?我是学生,我很着急 在这里或其他地方,您只需要更清楚自己需要什么。你一半的代码是不相关的,我不知道你的数据是什么样的。如果你至少清楚地解释你的目标,只留下相关代码,并提供你的数据.shape,我将能够提供帮助。 我想创建一个带有交叉验证的 cnn 模型。我的目标是通过在评估中包含 14 个值来找到最佳结果。其实还是说说我的数据集的主题吧。创建一个模型,通过采用各种身体值来预测人们是否会患心脏病。 这里写不了所有的代码,但是我做不到,所以我想把整个代码扔出邮件。 【参考方案1】:

由于您的代码不清楚,并且您想使用交叉验证创建 CNN 模型。

这里我给出了CNN using K-fold Cross Validationcifar10 数据集的端到端实现

from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.losses import sparse_categorical_crossentropy
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import KFold
import numpy as np

# Model configuration
batch_size = 50
img_width, img_height, img_num_channels = 32, 32, 3
loss_function = sparse_categorical_crossentropy
no_classes = 100
no_epochs = 5
optimizer = Adam()
validation_split = 0.2
verbosity = 1
num_folds = 10

# Load CIFAR-10 data
(input_train, target_train), (input_test, target_test) = cifar10.load_data()

# Determine shape of the data
input_shape = (img_width, img_height, img_num_channels)

# Parse numbers as floats
input_train = input_train.astype('float32')
input_test = input_test.astype('float32')

# Normalize data
input_train = input_train / 255
input_test = input_test / 255

# Define per-fold score containers
acc_per_fold = []
loss_per_fold = []

# Merge inputs and targets
inputs = np.concatenate((input_train, input_test), axis=0)
targets = np.concatenate((target_train, target_test), axis=0)

# Define the K-fold Cross Validator
kfold = KFold(n_splits=num_folds, shuffle=True)

# K-fold Cross Validation model evaluation
fold_no = 1
for train, test in kfold.split(inputs, targets):

  # Define the model architecture
  model = Sequential()
  model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Flatten())
  model.add(Dense(256, activation='relu'))
  model.add(Dense(128, activation='relu'))
  model.add(Dense(no_classes, activation='softmax'))

  # Compile the model
  model.compile(loss=loss_function,
                optimizer=optimizer,
                metrics=['accuracy'])


  # Generate a print
  print('------------------------------------------------------------------------')
  print(f'Training for fold fold_no ...')

  # Fit data to model
  history = model.fit(inputs[train], targets[train],
              batch_size=batch_size,
              epochs=no_epochs,
              verbose=verbosity,
              validation_split=validation_split)

  # Generate generalization metrics
  scores = model.evaluate(inputs[test], targets[test], verbose=0)
  print(f'Score for fold fold_no: model.metrics_names[0] of scores[0]; model.metrics_names[1] of scores[1]*100%')
  acc_per_fold.append(scores[1] * 100)
  loss_per_fold.append(scores[0])

  # Increase fold number
  fold_no = fold_no + 1

# == Provide average scores ==
print('------------------------------------------------------------------------')
print('Score per fold')
for i in range(0, len(acc_per_fold)):
  print('------------------------------------------------------------------------')
  print(f'> Fold i+1 - Loss: loss_per_fold[i] - Accuracy: acc_per_fold[i]%')
print('------------------------------------------------------------------------')
print('Average scores for all folds:')
print(f'> Accuracy: np.mean(acc_per_fold) (+- np.std(acc_per_fold))')
print(f'> Loss: np.mean(loss_per_fold)')
print('------------------------------------------------------------------------')

输出:

------------------------------------------------------------------------
Training for fold 1 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.6028 - accuracy: 0.4241 - val_loss: 1.2599 - val_accuracy: 0.5454
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.1737 - accuracy: 0.5828 - val_loss: 1.0888 - val_accuracy: 0.6157
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0001 - accuracy: 0.6462 - val_loss: 0.9834 - val_accuracy: 0.6640
Epoch 4/5
864/864 [==============================] - 3s 4ms/step - loss: 0.8859 - accuracy: 0.6864 - val_loss: 0.9913 - val_accuracy: 0.6666
Epoch 5/5
864/864 [==============================] - 3s 4ms/step - loss: 0.7843 - accuracy: 0.7260 - val_loss: 0.9798 - val_accuracy: 0.6680
Score for fold 1: loss of 0.9948607087135315; accuracy of 66.29999876022339%
------------------------------------------------------------------------
Training for fold 2 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.5524 - accuracy: 0.4370 - val_loss: 1.2327 - val_accuracy: 0.5632
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.1632 - accuracy: 0.5873 - val_loss: 1.0859 - val_accuracy: 0.6224
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0155 - accuracy: 0.6442 - val_loss: 1.0312 - val_accuracy: 0.6423
Epoch 4/5
864/864 [==============================] - 4s 4ms/step - loss: 0.9158 - accuracy: 0.6812 - val_loss: 0.9618 - val_accuracy: 0.6631
Epoch 5/5
864/864 [==============================] - 4s 4ms/step - loss: 0.8364 - accuracy: 0.7086 - val_loss: 0.9984 - val_accuracy: 0.6591
Score for fold 2: loss of 0.9941340684890747; accuracy of 65.9833312034607%
------------------------------------------------------------------------
Training for fold 3 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.7273 - accuracy: 0.3665 - val_loss: 1.3823 - val_accuracy: 0.5056
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.2402 - accuracy: 0.5565 - val_loss: 1.1531 - val_accuracy: 0.5948
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0643 - accuracy: 0.6227 - val_loss: 1.0656 - val_accuracy: 0.6228
Epoch 4/5
864/864 [==============================] - 3s 4ms/step - loss: 0.9462 - accuracy: 0.6652 - val_loss: 1.0105 - val_accuracy: 0.6481
Epoch 5/5
864/864 [==============================] - 3s 4ms/step - loss: 0.8643 - accuracy: 0.6962 - val_loss: 0.9708 - val_accuracy: 0.6574
Score for fold 3: loss of 0.9846121668815613; accuracy of 65.75000286102295%
------------------------------------------------------------------------
Training for fold 4 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.5849 - accuracy: 0.4206 - val_loss: 1.2727 - val_accuracy: 0.5404
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.1909 - accuracy: 0.5740 - val_loss: 1.1391 - val_accuracy: 0.5958
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0392 - accuracy: 0.6309 - val_loss: 1.0151 - val_accuracy: 0.6448
Epoch 4/5
864/864 [==============================] - 3s 4ms/step - loss: 0.9441 - accuracy: 0.6671 - val_loss: 0.9850 - val_accuracy: 0.6587
Epoch 5/5
864/864 [==============================] - 3s 4ms/step - loss: 0.8711 - accuracy: 0.6930 - val_loss: 0.9605 - val_accuracy: 0.6644
Score for fold 4: loss of 0.9718314409255981; accuracy of 65.88333249092102%
------------------------------------------------------------------------
Training for fold 5 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.5709 - accuracy: 0.4274 - val_loss: 1.2966 - val_accuracy: 0.5347
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.1923 - accuracy: 0.5733 - val_loss: 1.1713 - val_accuracy: 0.5831
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0433 - accuracy: 0.6288 - val_loss: 1.0558 - val_accuracy: 0.6331
Epoch 4/5
864/864 [==============================] - 3s 4ms/step - loss: 0.9493 - accuracy: 0.6647 - val_loss: 0.9925 - val_accuracy: 0.6548
Epoch 5/5
864/864 [==============================] - 3s 4ms/step - loss: 0.8768 - accuracy: 0.6917 - val_loss: 0.9913 - val_accuracy: 0.6576
Score for fold 5: loss of 1.0219489336013794; accuracy of 64.55000042915344%
------------------------------------------------------------------------
Training for fold 6 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.5822 - accuracy: 0.4234 - val_loss: 1.2895 - val_accuracy: 0.5400
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.1874 - accuracy: 0.5754 - val_loss: 1.1711 - val_accuracy: 0.5851
Epoch 3/5
864/864 [==============================] - 4s 4ms/step - loss: 1.0302 - accuracy: 0.6347 - val_loss: 1.1429 - val_accuracy: 0.6061
Epoch 4/5
864/864 [==============================] - 4s 4ms/step - loss: 0.9258 - accuracy: 0.6736 - val_loss: 0.9924 - val_accuracy: 0.6523
Epoch 5/5
864/864 [==============================] - 4s 4ms/step - loss: 0.8485 - accuracy: 0.7004 - val_loss: 0.9666 - val_accuracy: 0.6632
Score for fold 6: loss of 0.9477494359016418; accuracy of 66.90000295639038%
------------------------------------------------------------------------
Training for fold 7 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.6998 - accuracy: 0.3718 - val_loss: 1.3872 - val_accuracy: 0.5042
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.2831 - accuracy: 0.5393 - val_loss: 1.1861 - val_accuracy: 0.5735
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.1358 - accuracy: 0.5964 - val_loss: 1.1421 - val_accuracy: 0.5977
Epoch 4/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0401 - accuracy: 0.6329 - val_loss: 1.0570 - val_accuracy: 0.6299
Epoch 5/5
864/864 [==============================] - 3s 4ms/step - loss: 0.9719 - accuracy: 0.6573 - val_loss: 1.0385 - val_accuracy: 0.6361
Score for fold 7: loss of 1.0463552474975586; accuracy of 63.70000243186951%
------------------------------------------------------------------------
Training for fold 8 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.6252 - accuracy: 0.4026 - val_loss: 1.3876 - val_accuracy: 0.4915
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.3046 - accuracy: 0.5272 - val_loss: 1.2219 - val_accuracy: 0.5628
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.1584 - accuracy: 0.5878 - val_loss: 1.1462 - val_accuracy: 0.5921
Epoch 4/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0595 - accuracy: 0.6233 - val_loss: 1.1313 - val_accuracy: 0.6027
Epoch 5/5
864/864 [==============================] - 3s 4ms/step - loss: 0.9765 - accuracy: 0.6548 - val_loss: 1.0254 - val_accuracy: 0.6435
Score for fold 8: loss of 1.0177277326583862; accuracy of 64.60000276565552%
------------------------------------------------------------------------
Training for fold 9 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.5862 - accuracy: 0.4179 - val_loss: 1.3305 - val_accuracy: 0.5156
Epoch 2/5
864/864 [==============================] - 3s 4ms/step - loss: 1.1818 - accuracy: 0.5764 - val_loss: 1.1473 - val_accuracy: 0.5919
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0095 - accuracy: 0.6437 - val_loss: 1.0510 - val_accuracy: 0.6325
Epoch 4/5
864/864 [==============================] - 3s 4ms/step - loss: 0.9063 - accuracy: 0.6813 - val_loss: 1.0125 - val_accuracy: 0.6522
Epoch 5/5
864/864 [==============================] - 3s 4ms/step - loss: 0.8237 - accuracy: 0.7111 - val_loss: 0.9782 - val_accuracy: 0.6614
Score for fold 9: loss of 0.987509548664093; accuracy of 65.56666493415833%
------------------------------------------------------------------------
Training for fold 10 ...
Epoch 1/5
864/864 [==============================] - 4s 4ms/step - loss: 1.5503 - accuracy: 0.4342 - val_loss: 1.3075 - val_accuracy: 0.5289
Epoch 2/5
864/864 [==============================] - 4s 4ms/step - loss: 1.1923 - accuracy: 0.5733 - val_loss: 1.1332 - val_accuracy: 0.5969
Epoch 3/5
864/864 [==============================] - 3s 4ms/step - loss: 1.0505 - accuracy: 0.6287 - val_loss: 1.0580 - val_accuracy: 0.6283
Epoch 4/5
864/864 [==============================] - 3s 4ms/step - loss: 0.9530 - accuracy: 0.6652 - val_loss: 1.0023 - val_accuracy: 0.6519
Epoch 5/5
864/864 [==============================] - 3s 4ms/step - loss: 0.8798 - accuracy: 0.6899 - val_loss: 0.9842 - val_accuracy: 0.6621
Score for fold 10: loss of 0.9913936257362366; accuracy of 65.86666703224182%
------------------------------------------------------------------------
Score per fold
------------------------------------------------------------------------
> Fold 1 - Loss: 0.9948607087135315 - Accuracy: 66.29999876022339%
------------------------------------------------------------------------
> Fold 2 - Loss: 0.9941340684890747 - Accuracy: 65.9833312034607%
------------------------------------------------------------------------
> Fold 3 - Loss: 0.9846121668815613 - Accuracy: 65.75000286102295%
------------------------------------------------------------------------
> Fold 4 - Loss: 0.9718314409255981 - Accuracy: 65.88333249092102%
------------------------------------------------------------------------
> Fold 5 - Loss: 1.0219489336013794 - Accuracy: 64.55000042915344%
------------------------------------------------------------------------
> Fold 6 - Loss: 0.9477494359016418 - Accuracy: 66.90000295639038%
------------------------------------------------------------------------
> Fold 7 - Loss: 1.0463552474975586 - Accuracy: 63.70000243186951%
------------------------------------------------------------------------
> Fold 8 - Loss: 1.0177277326583862 - Accuracy: 64.60000276565552%
------------------------------------------------------------------------
> Fold 9 - Loss: 0.987509548664093 - Accuracy: 65.56666493415833%
------------------------------------------------------------------------
> Fold 10 - Loss: 0.9913936257362366 - Accuracy: 65.86666703224182%
------------------------------------------------------------------------
Average scores for all folds:
> Accuracy: 65.5100005865097 (+- 0.9018555780168425)
> Loss: 0.9958122909069062
------------------------------------------------------------------------

【讨论】:

以上是关于具有交叉验证的卷积神经网络的主要内容,如果未能解决你的问题,请参考以下文章

基于卷积神经网络的多字符类型验证码识别

深度学习基于卷积神经网络(tensorflow)的人脸识别项目

图像识别基于cnn卷积神经网络之验证码识别matlab源码

低卷积神经网络精度 CIFAR-10

用卷积神经网络提取图像特征

如何使用 KFold 交叉验证输出作为 CNN 输入进行图像处理?