估计器 KerasRegressor 的参数“模型”无效

问题描述 投票:0回答:1

你们能告诉我这个实现哪里出了问题吗?为什么我收到错误:“估计器 KerasRegressor 的参数‘模型’无效”?我衷心感谢您的帮助。

    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))

错误:

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.best_estimator\_

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

此代码在 Keras 回归器的张量流 keras 包装器中完美运行,但不适用于 scikeras。

keras scikit-learn scikeras
1个回答
0
投票

尝试更新

scikeras
,因为我认为现在需要
model=
而不是
build_fn=

当我进行这些修改时,它运行时没有错误:

from keras.layers import Dense, Input
...

def auto_CreateNeural(optimizer='adam', activation='relu', batch_size=1, epochs=1):
    regressor = Sequential()
    regressor.add(Input(shape=(X.shape[1],), batch_size=batch_size)) #batch_size is optional
    regressor.add(Dense(10, activation=activation))
    regressor.add(Dense(1))
    regressor.compile(loss='mean_squared_error', optimizer=optimizer)

    return regressor

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

输出:

...
8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step 
Best parameters: {'model__activation': 'tanh', 'model__batch_size': 8, 'model__epochs': 20, 'model__optimizer': 'sgd'}
Test set loss: -0.07 
Validation set loss: -0.16
© www.soinside.com 2019 - 2024. All rights reserved.