LaVOZs

The World’s Largest Online Community for Developers

'; python - How do I do a GridSearch for Timesteps for a KerasRegressor RNN? - LavOzs.Com

the GridSearch parameter tuning for batch_size, epochs, optimizer, and units work perfectly for my RNN:

X_train = []        
y_train = [] 
for i in range(60, np.ma.size(training_set_scaled)): #have used a timestep of 60 here
    X_train.append(training_set_scaled[i-60:i, 0])
    y_train.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))    

def build_regressor(units,optimizer):                                 
    regressor = Sequential()
    regressor.add(LSTM(units = units, return_sequences = True, input_shape = (X_train.shape[1], X_train.shape[2])))
    regressor.add(Dropout(rate=0.2))
    regressor.add(LSTM(units = units, return_sequences = True))
    regressor.add(Dropout(rate=0.2))
    regressor.add(LSTM(units = units, return_sequences = True))
    regressor.add(Dropout(rate=0.2))
    regressor.add(LSTM(units = units)) 
    regressor.add(Dropout(rate=0.2))
    regressor.add(Dense(units = 1))
    regressor.compile(optimizer = optimizer, loss = 'mean_squared_error')
    return regressor
regressor = KerasRegressor(build_fn = build_regressor)
parameters = {'batch_size': [25, 32],
              'epochs': [100, 200],
              'optimizer': ['adam', 'rmsprop'],
              'units': [50,75]}
grid_search = GridSearchCV(estimator = regressor,
                           param_grid = parameters,
                           scoring = 'neg_mean_squared_error',
                           cv = 10)
grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_ 

But if I want to add the Timesteps as a parameter, it gives the error: ValueError: Cannot have number of splits n_splits=10 greater than the number of samples: n_samples=0.

I know the 10 is from the cv=10. If I change it to cv=0, then I get a new error:

ValueError: k-fold cross-validation requires at least one train/test split by setting n_splits=2 or more, got n_splits=0

This is my code below:

def build_regressor(timesteps,units,optimizer):  
     X_train = []        
     y_train = [] 
    for i in range(timesteps, np.ma.size(training_set_scaled)): 
        X_train.append(training_set_scaled[i-timesteps:i, 0])
        y_train.append(training_set_scaled[i, 0])
    X_train, y_train = np.array(X_train), np.array(y_train)
    X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))                                
    regressor = Sequential()
    regressor.add(LSTM(units = units, return_sequences = True, input_shape = (X_train.shape[1], X_train.shape[2])))
    regressor.add(Dropout(rate=0.2))
    regressor.add(LSTM(units = units, return_sequences = True))
    regressor.add(Dropout(rate=0.2))
    regressor.add(LSTM(units = units, return_sequences = True))
    regressor.add(Dropout(rate=0.2))
    regressor.add(LSTM(units = units)) 
    regressor.add(Dropout(rate=0.2))
    regressor.add(Dense(units = 1))
    regressor.compile(optimizer = optimizer, loss = 'mean_squared_error')
    return regressor, X_train, y_train
regressor = KerasRegressor(build_fn = build_regressor)
parameters = {'batch_size': [25, 32],
              'epochs': [100, 200],
              'optimizer': ['adam', 'rmsprop'],
              'units': [50,75],
              'timesteps': [60,120]}
grid_search = GridSearchCV(estimator = regressor,
                           param_grid = parameters,
                           scoring = 'neg_mean_squared_error',
                           cv = 10)
grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_ 

How can I fix this error?

Thank you

Related
How do I merge two dictionaries in a single expression in Python?
How do I check if a list is empty?
How do I check whether a file exists without exceptions?
How can I safely create a nested directory?
How to get the current time in Python
How do I sort a dictionary by value?
How to make a chain of function decorators?
How to make a flat list out of list of lists?
How do I list all files of a directory?