[英]How to do bagging using scikit BaggingClassifier with keras Convolutional Neural Network as base estimator via keras-scikit wrapper?
I'm trying to make an ensemble learning, which is bagging using scikit-learn BaggingClassifier with 2D Convolutional Neural Networks (CNN) as the base estimators.我正在尝试进行集成学习,即使用 scikit-learn BaggingClassifier 和 2D 卷积神经网络 (CNN) 作为基本估计器进行装袋。
Before this, i've tried bagging with scikit's Neural Network to test scikit's BaggingClassifier and it worked.在此之前,我曾尝试使用 scikit 的神经网络进行装袋以测试 scikit 的 BaggingClassifier 并且它有效。 Also i've tested scikit's GridSearchCV with keras-wrapper to search for 2D CNN's hyperparameters, it also worked.
我还用 keras-wrapper 测试了 scikit 的 GridSearchCV 来搜索 2D CNN 的超参数,它也有效。
Just now, when i tried using scikit's BaggingClassifier with keras-wrapper to wrap then create ensemble learning with the 2D CNN model as base estimator, i got an error.刚才,当我尝试使用 scikit 的 BaggingClassifier 和 keras-wrapper 进行包装,然后使用 2D CNN 模型作为基础估计器创建集成学习时,出现错误。
Here is the code snippet :这是代码片段:
def baggingCNN(self):
from sklearn.ensemble import BaggingClassifier
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils.np_utils import to_categorical
patternTraining = np.reshape(self.patternTraining,
(self.patternTraining.shape[0], 1, 1, self.patternTraining.shape[1]))
patternTesting = np.reshape(self.patternTesting,
(self.patternTesting.shape[0], 1, 1, self.patternTesting.shape[1]))
X = patternTraining
Y_binary = to_categorical(self.targetTraining)
cnnA=KerasClassifier(self.create_cnn_model_A(patternTraining.shape[1],patternTraining.shape[2],patternTraining.shape[3]),nb_epoch=500, batch_size=64, verbose=1)
bagging=BaggingClassifier(base_estimator=cnnA, n_estimators=3, verbose=1, n_jobs=3, max_samples=1)
bagging.fit(X, Y_binary)
Here is the create_cnn_model_A function looks like :这是 create_cnn_model_A 函数的样子:
def create_cnn_model_A(self, sizeDepth, sizeRow, sizeCol):
from keras.models import Sequential
import keras.layers.core as core
import keras.layers.convolutional as conv
from keras.regularizers import l2, activity_l2, l1, activity_l1, l1l2, activity_l1l2
numFilter = 32
nStride = 1
model = Sequential()
model.add(conv.Convolution2D(nb_filter=numFilter, nb_row=1, nb_col=2, activation='relu',
input_shape=(sizeDepth, sizeRow, sizeCol), border_mode='same'))
model.add(conv.Convolution2D(nb_filter=numFilter, nb_row=1, nb_col=3, activation='relu',
input_shape=(sizeDepth, sizeRow, sizeCol), border_mode='same'))
model.add(conv.Convolution2D(nb_filter=numFilter, nb_row=1, nb_col=4, activation='relu',
input_shape=(sizeDepth, sizeRow, sizeCol), border_mode='same'))
model.add(conv.MaxPooling2D(pool_size=(1, 2), strides=(nStride, nStride), dim_ordering="th"))
model.add(conv.Convolution2D(nb_filter=numFilter, nb_row=1, nb_col=2, activation='relu',
input_shape=(sizeDepth, sizeRow, sizeCol), border_mode='same'))
model.add(conv.Convolution2D(nb_filter=numFilter, nb_row=1, nb_col=2, activation='relu',
input_shape=(sizeDepth, sizeRow, sizeCol), border_mode='same'))
model.add(conv.MaxPooling2D(pool_size=(1, 2), strides=(nStride, nStride), dim_ordering="th"))
model.add(core.Flatten())
model.add(core.Dense(output_dim=50, activation='relu', W_regularizer=l2(0.01)))
model.add(core.Dense(output_dim=18, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy', 'precision', 'recall'])
return model
Here is the shape of the self.patternTraining & self.targetTraining before reshaping :这是重塑前 self.patternTraining 和 self.targetTraining 的形状:
(1361, 45) (1361,)
And this is the error i got :这是我得到的错误:
Traceback (most recent call last):
File "/home/berylramadhian/PycharmProjects/Relation Extraction/TestModule2.py", line 153, in <module>
clsf.baggingCNN()
File "/home/berylramadhian/PycharmProjects/Relation Extraction/MachineLearning.py", line 511, in baggingCNN
bagging.fit(X, Y_binary)
File "/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/bagging.py", line 248, in fit
return self._fit(X, y, self.max_samples, sample_weight=sample_weight)
File "/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/bagging.py", line 284, in _fit
X, y = check_X_y(X, y, ['csr', 'csc'])
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 521, in check_X_y
ensure_min_features, warn_on_dtype, estimator)
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 405, in check_array
% (array.ndim, estimator_name))
ValueError: Found array with dim 4. Estimator expected <= 2.
I thought this is some kind of array-shape-error but i don't know how to resolve this.我认为这是某种数组形状错误,但我不知道如何解决这个问题。 Or maybe it is not yet possible to use scikit's BaggingClassifier with keras' 2D CNN via keras-wrapper?
或者也许还不可能通过 keras-wrapper 将 scikit 的 BaggingClassifier 与 keras 的 2D CNN 一起使用?
If more details are needed, i'm ready to provide.如果需要更多详细信息,我已准备好提供。 Any help is appreciated, thanks.
任何帮助表示赞赏,谢谢。
This is not supported currently in sklearn using Keras.使用 Keras 的 sklearn 当前不支持此功能。 You have to implement yourself or I raised a same issue in the community months ago.
您必须自己实施,否则我几个月前在社区中提出了同样的问题。 I got an apt response and currently, they are trying to implement it.
我得到了一个恰当的回应,目前,他们正在尝试实施它。 Wait for the next version or check the issue to know more.
等待下一个版本或检查问题以了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.