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.
Before this, i've tried bagging with scikit's Neural Network to test scikit's BaggingClassifier and it worked. Also i've tested scikit's GridSearchCV with keras-wrapper to search for 2D CNN's hyperparameters, it also worked.
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.
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 :
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 :
(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?
If more details are needed, i'm ready to provide. Any help is appreciated, thanks.
This is not supported currently in sklearn using Keras. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.