0
$\begingroup$

My model-

#define model
model = Sequential()
model.add(Dense(128, activation='relu', input_dim=n_input_1))
model.add(Dense(64, activation='relu'))
#model.add(Dense(32, activation='relu'))
#model.add(Dense(16, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse',metrics=['mse'])

Now I am trying to tune the hyper parameters by this code-

from sklearn.model_selection import GridSearchCV
# fix random seed for reproducibility
seed = 7
np.random.seed(seed)

# define the grid search parameters
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, 
cv=3,scoring='neg_mean_absolute_error')
grid_result = grid.fit(scaled_train,y_train_c)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

I am getting this error-

TypeError                                 Traceback (most recent call last)
<ipython-input-10-c6e1e39d878e> in <module>
      9 param_grid = dict(batch_size=batch_size, epochs=epochs)
     10 grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3,scoring='neg_mean_absolute_error')
---> 11 grid_result = grid.fit(scaled_train,y_train_c)
     12 # summarize results
     13 print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    631         n_splits = cv.get_n_splits(X, y, groups)
    632 
--> 633         base_estimator = clone(self.estimator)
    634 
    635         parallel = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\base.py in clone(estimator, safe)
     58                             "it does not seem to be a scikit-learn estimator "
     59                             "as it does not implement a 'get_params' methods."
---> 60                             % (repr(estimator), type(estimator)))
     61     klass = estimator.__class__
     62     new_object_params = estimator.get_params(deep=False)

TypeError: Cannot clone object '<keras.engine.sequential.Sequential object at 0x0000023DD4D5F488>' (type <class 'keras.engine.sequential.Sequential'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

I have no idea whats causing this issue. I am implementing a MLP for time series forecasting.

$\endgroup$
2
$\begingroup$

The xyzSearchCV classes in sklearn require the estimator to be compatible with the sklearn API; in particular, the estimator('s clones)'s hyperparameters get set using set_params, which needs to be defined as a class method for the estimator. So a Keras Sequential model will not work. Keras does provide an sklearn wrapper: https://keras.io/scikit-learn-api/

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.