autonomio / talos

Hyperparameter Experiments with TensorFlow and Keras
https://autonom.io
MIT License
1.62k stars 270 forks source link

IndexError using Talos with LSTM layer #500

Closed AmberSJones closed 3 years ago

AmberSJones commented 3 years ago

I have a working LSTM model to which I am trying to apply Talos to tune and determine hyperparameters. The data are a time series of a single variable. My code, including model definition, parameter list, and scan call are below - I have tried diligently to follow the talos examples and documentation. When I run this, I get an error. Any help or insight is appreciated.

File "", line 8, in create_model_talos model.add(LSTM(params['cells'], input_shape=(time_steps, num_features))) # one LSTM layer IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

def create_model_talos(params, time_steps, num_features, input_loss='mae', input_optimizer='adam',
                 patience=3, monitor='val_loss', mode='min', epochs=100, validation_split=0.1):
    """Uses sequential model class from keras. Adds LSTM layer. Input samples, timesteps, features.
    Hyperparameters include number of cells, dropout rate. Output is encoded feature vector of the input data.
    Uses autoencoder by mirroring/reversing encoder to be a decoder."""
    model = Sequential()
    model.add(LSTM(params['cells'], input_shape=(time_steps, num_features)))  # one LSTM layer
    model.add(Dropout(params['dropout']))  
    model.add(RepeatVector(time_steps))  
    model.add(LSTM(params['cells'], return_sequences=True))  # mirror the encoder in the reverse fashion to create the decoder
    model.add(Dropout(params['dropout']))
    model.add(TimeDistributed(Dense(num_features)))

    print(model.optimizer)
    model.compile(loss=input_loss, optimizer=input_optimizer)

    es = tf.keras.callbacks.EarlyStopping(monitor=monitor, patience=patience, mode=mode)
    history = model.fit(
        X_train, y_train,
        epochs=epochs,  # early stopping will monitor.
        batch_size=params['batch_size'],  
        validation_split=validation_split,  
        callbacks=[es],  
        shuffle=False  
    )

    return history, model

p = {'cells': [4, 8, 16, 32, 64, 128],
     'dropout': (0, 0.4, 10),
     'batch_size': [5, 10, 25, 50]}

scan_object = talos.Scan(X_train, y_train, params=p, model=create_model_talos, experiment_name='test')
github-actions[bot] commented 3 years ago

Welcome to Talos community! Thanks so much for creating your first issue :)

mikkokotila commented 3 years ago

Have you tried if your model works when you just run it stand-alone?

AmberSJones commented 3 years ago

Yes, the model runs fine on its own.

mikkokotila commented 3 years ago

Have a look at this issue. I think it will resolve your case.

TL;DR for LSTM you have to explicitly declare validation data in Scan()

Closing here. Feel free to reopen if anything.