Python Forum

Full Version: LSTM-TensorFlow-Keras models and MAE
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! I am new to the forum and I need a hand! :)

I have an LSTM model uisng tensorflow and Keras. I have a set of 2 lstm-dropout layers and a dence output layer. I use Loss function=MeanAbsoluteError and optimizer=Adam. No activation function.I feed LSTM with timeseries data from 1970-2021 and the validation set is 2022 and I calculate MAE, MAPE, RMSE of (y_actual,y_predicted).

I want to create many models and chooce the best besed on min MAE. My lists of seed and lstm_units are:
seeds = [12345, 23456, 34567, 45678, 56789] and
lstm_units_list = [10, 20, 30, 40, 50]. I use 2 for-loops (seed is the outer loop).

My issue is this:
When I use a combination of one same seed and each one of lstm_units like: seeds=[12345] and lstm_units=[10], seeds=[12345] and lstm_units=[20], seeds=[12345] and lstm_units=[30] etc. I get for each pair a normal MAE (photo 2) with small variations every time I run it
When I use the 2 for-loops with full lists of seeds (5 seeds) and lstm_units (5 values) I get very abnormal results !!!!(photo 1) even for the previus combinations!!!like something has left in memory(?) and somehow add variation in y_predicted values (where MAE is calculated). I google it and I add inside the second loop (lstm_units loop) the line
tf.keras.backend.clear_session()  # Clear the session before each new model
. But nothing has changed!!

I also added the following to reduce variations in results between runs due to TensorFlow and keras non-deterministic operations.
# Set deterministic operations and disable optimizations
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"  # Disable oneDNN optimizations
os.environ["TF_DETERMINISTIC_OPS"] = "1"   # Force deterministic ops
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'  # Ensure cuDNN operations are deterministic
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"  # Disable GPU usage (optional, if needed)
os.environ["OMP_NUM_THREADS"] = "1"  # Control the number of CPU threads

tf.config.experimental.enable_op_determinism() #forces TensorFlow to use deterministic implementations
# Set the float32 precision
tf.keras.backend.set_floatx('float32')  # Ensure that all tensors use float32 precision
Nothing helped me!. I wonder what to do!! I am new to python. I use Visual Studio Code and Jupyter to write. I also have an environment set up for my files. Does pyhton need to clear up memory? does the tensorflow and keras need another handling to be used correctly?
Could anyone help me to deal with this issue?