0

我有这段代码可以构建模型并对其进行训练。

model = Sequential()
model.add(Convolution2D(32, kernel_size = (3,3),padding = 'same' ,input_shape= input_shape))
model.add(Activation('linear'))
model.add(Convolution2D(32, kernel_size = (3,3)))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same' ))
model.add(Dropout(0.5))

model.add(Convolution2D(64, kernel_size = (3,3)))
model.add(Activation('linear'))
model.add(Convolution2D(64, kernel_size = (3,3)))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same' ))
model.add(Dropout(0.5))

model.add(Convolution2D(128, kernel_size = (3,3)))
model.add(Activation('linear'))
model.add(Convolution2D(128, kernel_size = (3,3), padding = 'same' ))
model.add(Activation('linear'))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(10))
model.add(Dropout(0.5))

model.add(Activation('linear'))
model.add(Dense(numclasses))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop',metrics=["accuracy"])
hist = model.fit(X_train, y_train, batch_size=32, nb_epoch=30, verbose=1, validation_data=(X_test, y_test), callbacks = callbacks_list)

所以我训练了 9000 张图像,如下所示,但正如你所看到的,有一些泛化的迹象,我想如果不是,请纠正我

在此处输入图像描述

但无论如何,我试图实现 K 折交叉验证.. 我猜有很多不同的方法可以做到这一点,但我采用了这种方法,因为我认为这将是最简单的..

#Applying K-Fold Cross Validation

accuracy = cross_val_score(estimator=model, X = X_train, y = y_train, cv=10)
accuracy.mean()
accuracy.std()
#Predicting the test set results
y_pred = classifier.predict(X_test)

我已经实现了其他做 K-Fold 的方法,但我一直收到这个错误

TypeError                                 Traceback (most recent call last)
<ipython-input-181-765b62be4342> in <module>
      1 #Applying K-Fold Cross Validation
      2 
----> 3 accuracy = cross_val_score(estimator=model, X = X_train, y = y_train, cv=10)
      4 accuracy.mean()
      5 accuracy.std()

~/anaconda3/envs/tensorflowenv/lib/python3.7/site-packages/sklearn/model_selection/_validation.py in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, error_score)
    382     """
    383     # To ensure multimetric format is not supported
--> 384     scorer = check_scoring(estimator, scoring=scoring)
    385 
    386     cv_results = cross_validate(estimator=estimator, X=X, y=y, groups=groups,

~/anaconda3/envs/tensorflowenv/lib/python3.7/site-packages/sklearn/metrics/scorer.py in check_scoring(estimator, scoring, allow_none)
    293                 "If no scoring is specified, the estimator passed should "
    294                 "have a 'score' method. The estimator %r does not."
--> 295                 % estimator)
    296     elif isinstance(scoring, Iterable):
    297         raise ValueError("For evaluating multiple scores, use "

TypeError: If no scoring is specified, the estimator passed should have a 'score' method. The estimator <tensorflow.python.keras.engine.sequential.Sequential object at 0x7f0ec5431860> does not.

如果有人知道如何解决这个问题,请帮忙..谢谢!

4

0 回答 0