adriangb / scikeras

Scikit-Learn API wrapper for Keras.
https://www.adriangb.com/scikeras/
MIT License
239 stars 47 forks source link

Invalid parameter 'model' for estimator KerasRegressor #322

Closed nagsubhadeep closed 4 months ago

nagsubhadeep commented 4 months ago

Can you please tell me where am I going wrong with this implementation? Why am I getting the error: "Invalid parameter 'model' for estimator KerasRegressor" ? I would sincerely appreciate your help. This code works perfectly in the tensorflow keras wrapper for Keras Regressor, but not scikeras.

`

    from scikeras.wrappers import KerasRegressor
    import numpy as np
    from sklearn import datasets
    from sklearn.model_selection import train_test_split, GridSearchCV, KFold
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.optimizers import Adam

    diabetes = datasets.load_diabetes()
    X = diabetes.data
    y = diabetes.target

    # Split data into training, validation, and test sets
    X_train_val, X_test, y_train_val, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
    X_train, X_val, y_train, y_val = train_test_split(X_train_val, y_train_val, test_size=0.125, random_state=1)  # 0.125 x 0.8 = 0.1

    # Define a model builder function
    def auto_CreateNeural(optimizer='adam', activation='relu'):
        regressor = Sequential()
        regressor.add(Dense(10, input_dim=X.shape[1], activation=activation))
        regressor.add(Dense(1))
        regressor.compile(loss='mean_squared_error', optimizer=optimizer)

        return regressor

    # Wrap the model with KerasRegressor
    regressor = KerasRegressor(build_fn=auto_CreateNeural, verbose=1)

    # Define parameters for GridSearchCV
    param_grid = {
        'model__optimizer': ['adam', 'sgd'],
        'model__activation': ['relu', 'tanh'],
        'model__batch_size': [4, 8],
        'model__epochs': [10, 20]
    }

    # Setup cross-validation
    kf = KFold(n_splits=3, shuffle=True, random_state=1)
    grid = GridSearchCV(estimator=regressor, param_grid=param_grid, cv=kf, scoring='neg_mean_squared_error', return_train_score=True)

    # Perform Grid Search
    grid_result = grid.fit(X_train_val, y_train_val)

    # Evaluate the best model on the test set
    best_model = grid.best_estimator_
    test_loss = best_model.score(X_test, y_test)

    # Output results
    print("Best GridSearchCV score: {:.2f}".format(grid_result.best_score_))
    print("Best parameters: {}".format(grid_result.best_params_))
    print("Test set loss: {:.2f}".format(test_loss))

    # Optionally, check how it performs on the validation set if needed
    validation_loss = best_model.score(X_val, y_val)
    print("Validation set loss: {:.2f}".format(validation_loss))

`

Error:

ValueError Traceback (most recent call last) Cell In[48], line 43 40 grid = GridSearchCV(estimator=regressor, param_grid=param_grid, cv=kf, scoring='neg_mean_squared_error', return_train_score=True) 42 # Perform Grid Search ---> 43 grid_result = grid.fit(X_train_val, y_train_val) 45 # Evaluate the best model on the test set 46 best_model = grid.bestestimator

 ValueError: Invalid parameter 'model' for estimator KerasRegressor(
build_fn=<function auto_CreateNeural at 0x71a81c62f600>
verbose=1

). Valid parameters are: ['build_fn', 'verbose'].

adriangb commented 4 months ago

Can you include the output of pip freeze please?

nagsubhadeep commented 4 months ago

The issue got resolved with version 0.11.0. Thanks