Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

How to get KerrasRegressor to work? I keep getting an AttributeError

$
0
0

I'm trying to optimize hyperparameters for a simple sequential neural-network using the KerasRegressor as part of a learning exercise. This is my code:

from sklearn.model_selection import GridSearchCV, RandomizedSearchCVfrom scipy.stats import randint as sp_randintfrom keras.wrappers.scikit_learn import KerasRegressorfrom sklearn.metrics import mean_squared_error, make_scorer'''CREATE THE MODEL'''def design_model(features):    model = Sequential(name = "My_Sequential_Model")     model.add(InputLayer(input_shape=(features.shape[1],)))     model.add(Dense(128, activation='relu'))     model.add(Dense(1))     opt = Adam(learning_rate=0.01)    model.compile(loss='mse', metrics=['mae'], optimizer=opt)     return model'''TEST/PLOT THE MODEL: GRID SEARCH'''def do_grid_search():    batch_size = [6, 64]    epochs = [10, 30, 61]    model = KerasRegressor(build_fn=design_model, features=features_train) # KerasRegressor expects a function and not the model    param_grid = dict(batch_size=batch_size, epochs=epochs)    grid = GridSearchCV(estimator = model, param_grid=param_grid, scoring = make_scorer(mean_squared_error, greater_is_better=False),return_train_score = True)    grid_result = grid.fit(features_train, labels_train, verbose = 0)    print(grid_result)    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))    print("Traininig")    means = grid_result.cv_results_['mean_train_score']    stds = grid_result.cv_results_['std_train_score']    for mean, stdev, param in zip(means, stds, params): print("%f (%f) with: %r" % (mean, stdev, param))print("-------------- GRID SEARCH --------------------")do_grid_search()

But I keep getting the following error:

AttributeError: module 'keras.losses' has no attribute 'is_categorical_crossentropy'

What do I do? I am using Tensorflow 2.15 and Keras 2.15.


Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>