tensorflow with scikeras.wrappers KerasRegressor returning ValueError

85 Views Asked by At

I'm trying to model a DNN for the California housing dataset within sklearn using tensorflow. To define hyperparameters I would like to use gridsearchCV but encountering

ValueError: Could not interpret metric identifier: loss

The actual compiling of the model seems to work as all epochs are being finished.

import tensorflow as tf
from keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from scikeras.wrappers import KerasRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# load and prepare data
housing = fetch_california_housing()
X_train_full, X_test, y_train_full, y_test = train_test_split(housing.data, housing.target, random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)


def build_model(layers, units, learning_rate):
    model = Sequential()
    model.add(Dense(units, input_dim=X_train.shape[1], activation='relu'))
    
    for _ in range(1, layers):
        model.add(Dense(units, activation='relu'))
    model.add(Dense(units=1, activation='linear'))

    optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
    model.compile(optimizer=optimizer, loss='mean_squared_error', metrics=['mae'])
    return model

# KerasRegressor-Wrapper
model = KerasRegressor(build_fn=build_model, epochs=20, verbose=1, learning_rate=0.001, layers=3, units=64)

# Para-Grid 
param_grid = {
    'layers': [3],
    'units': [64],
    'learning_rate': [0.0001],
    'batch_size': [32],
}

# GridSearchCV
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, scoring='neg_mean_squared_error',error_score='raise')
grid_result = grid.fit(X_train, y_train)

# Results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))

0

There are 0 best solutions below