Home > Back-end >  How to set the data dimension for GridSearchCV
How to set the data dimension for GridSearchCV

Time:12-15

def rnn_model(self,activation="relu"):
    in_out_neurons = 50
    n_hidden = 512
    model = Sequential()
    model.add(LSTM(n_hidden, batch_input_shape=(None, self.seq_len, in_out_neurons), return_sequences=True))
    model.add(Dense(in_out_neurons, activation=activation))
    optimizer = Adam(learning_rate=0.001)
    model.compile(loss="mean_squared_error", optimizer=optimizer)
    model.summary()
    return model

# then try to fit the model
final_x = np.zeros((319083, 2, 50))
final_y = np.zeros((319083, 1, 50))

# this works.

model = self.rnn_model()
model.fit(         
    final_x,final_y,
    batch_size=400,
    epochs=10,
    validation_split=0.1
)

#However, when I trid to use hyperparameter sarch, this shows the error `ValueError: Invalid shape for y: (319083, 1, 50)`

activation = ["relu","sigmoid"]
model = KerasClassifier(build_fn=self.rnn_model,verbose=0)
param_grid = dict(activation=activation)
grid = GridSearchCV(estimator=model,param_grid=param_grid)
grid_result= grid.fit(final_x,final_y)

How dimension changes when using GridSearchCV

CodePudding user response:

You should be using a KerasRegressor, since your model is not a classifier in that sense:

import tensorflow as tf
import numpy as np

from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasRegressor

def rnn_model(activation="relu"):
    in_out_neurons = 50
    n_hidden = 512
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.LSTM(n_hidden, batch_input_shape=(None, 2, in_out_neurons), return_sequences=True))
    model.add(tf.keras.layers.Dense(in_out_neurons, activation=activation))
    optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
    model.compile(loss="mean_squared_error", optimizer=optimizer)
    model.summary()
    return model

final_x = np.zeros((319083, 2, 50))
final_y = np.zeros((319083, 2, 50))

model = rnn_model()
activation = ["relu","sigmoid"]
model = KerasRegressor(build_fn=rnn_model,verbose=0)
param_grid = dict(activation=activation)
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result= grid.fit(final_x,final_y)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_)) # run with a way smaller dataset
Best: 0.000000 using {'activation': 'relu'}
  • Related