0

I'm trying to follow this reference to use GridSearchCV from Sklearn in order to find the best hyper parameters for a Keras Neural Network. Here's the code I have written:

import numpy as np
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.metrics import mean_squared_error
from math import sqrt
from sklearn.model_selection import ShuffleSplit

def create_model():
    model=Sequential()
    model.add(Dense(18, input_dim=18, activation='relu'))
    model.add(Dense(20, activation='relu'))
    model.add(Dense(1))
    model.compile(loss=mean_squared_error, optimizer='adam')
    return model

x_train=np.load('x_train.npy')
y_train=np.load('y_train.npy')
x_dev=np.load('x_dev.npy')
y_dev=np.load('y_dev.npy')
model=KerasRegressor(build_fn=create_model, verbose=0)
batch_size1=np.arange(32, 1024, 100)
epochs1=np.arange(10, 100, 40)
param_grid=dict(batch_size=batch_size1, epochs=epochs1)
cv = ShuffleSplit(n_splits=3, test_size=0.01, random_state=0)
grid=GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1, cv=cv)
grid_result=grid.fit(x_train, y_train)
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))

And I am getting the following error: ValueError: setting an array element with a sequence. error message, photo 1 error message, photo 2 As far as I understood from this link the problems is caused because of one of two reasons: 1- Data and labels are not in the same length. 2- For a specific feature vector, the number of elements are not equal. But I'm quite sure my data are labeled correctly and I can apply simple sklearn and keras models on the same data set without any problems. So, what might be the cause of this error?

Saleh
  • 169
  • 1
  • 6

1 Answers1

0

You are attempting to set the keras loss to an sklearn function. Try setting it to a string instead.

model.compile(loss='mean_squared_error', optimizer='adam')
Bert Kellerman
  • 1,590
  • 10
  • 17