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?

4
  • 1
    Can you show the full stack trace of the error? This makes it easier to trace the problem. Commented Jun 14, 2018 at 17:59
  • I edited the post and added two photos for the trace of the error Commented Jun 14, 2018 at 18:09
  • Whats the shape of x_train and y_train? Commented Jun 15, 2018 at 6:42
  • y_train (15680, 1) x_train (15680, 18) Commented Jun 15, 2018 at 10:40

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.