'Looping Different Data Sets and Predicting with LSTM

I've been working on keras, LSTM for a while now but still fairly new.

I am facing an issue where I would need to restart Jupyter Notebook every time I train and predict with keras LSTM.

This would post a problem when I need to loop multiple predictions with different data sets, using the same LSTM model setup.

An alternative is to train and predict data set by data set, restarting and running all after every prediction is completed, which would be a very tedious process.

for example:

for i in ...: # Loop for different data set

    ...

    # Create LSTM Model
    model = Sequential()
    model.add(LSTM(units=32,
                   activation='relu',
                   batch_input_shape=(1, timestep_n, feature_n),
                   return_sequences=True))
    model.add(Dense(units=1, 
                    activation='relu'))
    model.compile(loss='mean_squared_error', optimizer=adam, metrics=['accuracy'])
    ...

    # Train Model
    model.fit(x, y, epochs=100)

    ...

    # Predict
    result = model.predict(x_p)

    # End Loop for next data set

I have tried clearing the session before I create every model for training but the values predicted are different from when I was running a single training and prediction.

    session = tf.get_default_session()
    if session is not None:
        session.close()
    K.clear_session()
    gc.collect()

I have also used random seed in hopes of getting the same value every run. For consistency.

    SEED = 1
    np.random.seed(SEED)
    tf.set_random_seed(SEED)
    random.seed(SEED)

I am thinking of declaring/creating a "NEW" LSTM model in every loop to predict values of different data sets, without having to manually restarting the notebook and running for every data set.

Even if I perform del model at the end of the loop, somehow the memory is not properly cleared.

Could someone help with this?



Solution 1:[1]

What are you getting for this:

for i in ...: # Loop for different data set

...

model = None
# Create LSTM Model
model = Sequential()
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LemonPy