I am using a basic CNN model to classify my data. The dimensions of my input data is (325, 20, 244,244). The code that i have used is as follows:
model = Sequential()
model.add(Dense(2, activation='relu', input_shape=X_train.shape[1:]))
model.add(Dense(2, activation='sigmoid'))
optimizer = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
epochs = [10, 50, 100]
param_grid = dict(epochs=epochs, optimizer=optimizer)
model.compile(loss='binary_crossentropy', metrics=['accuracy'])
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy', n_jobs=-1, refit='boolean')
grid_result = grid.fit(X_train, Y_train, validation_data=(X_test, Y_test))
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
The output I got is:
grid_result = grid.fit(X_train, Y_train, validation_data=(X_test, Y_test))
Traceback (most recent call last):
File "<ipython-input-16-bb553189f3ee>", line 1, in <module>
grid_result = grid.fit(X_train, Y_train, validation_data=(X_test, Y_test))
File "C:\Users\Student\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 633, in fit
base_estimator = clone(self.estimator)
File "C:\Users\Student\Anaconda3\lib\site-packages\sklearn\base.py", line 60, in clone
% (repr(estimator), type(estimator)))
TypeError: Cannot clone object '<tensorflow.python.keras.engine.sequential.Sequential object at 0x0000025993610B08>' (type <class 'tensorflow.python.keras.engine.sequential.Sequential'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.
Can anyone please tell me what is wrong with the code and how it can be corrected.
In this link: Tensorflow Keras wrapper for sklearn and Keras wrapper
you can see that tensorflow keras have a wrapper for using keras models with sklearn.
So, you have to use KerasClassifier(build_fn=None, **sk_params) where build_fn should be a function in which you code your model and that function take parameters which you want to tune.
So you should code your model like this:
def getModel(optimizer):
model = Sequential()
model.add(Dense(2, activation='relu', input_shape=X_train.shape[1:]))
model.add(Dense(2, activation='sigmoid'))
model.compile(optimizer=optimizer , loss = tf.losses.categorical_crossentropy , metrics=['accuracy'])
return model
optimizer = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
epochs = [10, 50, 100]
param_grid = dict(epochs=epochs, optimizer=optimizer)
Kmodel = KerasClassifier(build_fn=getModel, verbose=1)
grid = GridSearchCV(estimator=Kmodel, param_grid=param_grid, scoring='accuracy', n_jobs=-1, refit='boolean')
grid_result = grid.fit(X_train, Y_train)
for coding example of KerasClassifier on mnist you can visit github
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With