Updating DQN training to not thread the DQN bc that causes a core dump

This commit is contained in:
2025-01-31 23:08:07 +00:00
parent 6cf5a47005
commit 1326dfe406
7 changed files with 1098 additions and 361 deletions

View File

@@ -40,3 +40,141 @@
2025-01-30 00:56:56,285 - INFO - Scaled validation target shape: (3028,) 2025-01-30 00:56:56,285 - INFO - Scaled validation target shape: (3028,)
2025-01-30 00:56:56,285 - INFO - Scaled testing target shape: (3030,) 2025-01-30 00:56:56,285 - INFO - Scaled testing target shape: (3030,)
2025-01-30 00:56:56,285 - INFO - Starting LSTM hyperparameter optimization with Optuna using 10 parallel trials... 2025-01-30 00:56:56,285 - INFO - Starting LSTM hyperparameter optimization with Optuna using 10 parallel trials...
2025-01-30 05:59:42,913 - INFO - ===== Resource Statistics =====
2025-01-30 05:59:42,913 - INFO - Physical CPU Cores: 28
2025-01-30 05:59:42,913 - INFO - Logical CPU Cores: 56
2025-01-30 05:59:42,913 - INFO - CPU Usage per Core: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
2025-01-30 05:59:42,913 - INFO - No GPUs detected.
2025-01-30 05:59:42,913 - INFO - =================================
2025-01-30 05:59:42,917 - INFO - Configured TensorFlow to use CPU with optimized thread settings.
2025-01-30 05:59:42,917 - INFO - Loading data from: BAT.csv
2025-01-30 05:59:44,354 - INFO - Data columns after renaming: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
2025-01-30 05:59:44,373 - INFO - Data loaded and sorted successfully.
2025-01-30 05:59:44,373 - INFO - Calculating technical indicators...
2025-01-30 05:59:44,437 - INFO - Technical indicators calculated successfully.
2025-01-30 05:59:44,447 - INFO - Starting parallel feature engineering with 54 workers...
2025-01-30 05:59:53,981 - INFO - Parallel feature engineering completed.
2025-01-30 05:59:54,103 - INFO - Scaled training features shape: (14134, 15, 17)
2025-01-30 05:59:54,103 - INFO - Scaled validation features shape: (3028, 15, 17)
2025-01-30 05:59:54,103 - INFO - Scaled testing features shape: (3030, 15, 17)
2025-01-30 05:59:54,103 - INFO - Scaled training target shape: (14134,)
2025-01-30 05:59:54,103 - INFO - Scaled validation target shape: (3028,)
2025-01-30 05:59:54,103 - INFO - Scaled testing target shape: (3030,)
2025-01-30 05:59:54,103 - INFO - Starting LSTM hyperparameter optimization with Optuna using 54 parallel trials...
2025-01-30 07:24:16,261 - INFO - Best LSTM Hyperparameters: {'num_lstm_layers': 1, 'lstm_units': 128, 'dropout_rate': 0.2601904776023419, 'learning_rate': 0.0029898637649075103, 'optimizer': 'Nadam', 'decay': 6.765206579726146e-06}
2025-01-30 07:24:16,587 - INFO - Training best LSTM model with optimized hyperparameters...
2025-01-30 07:54:53,021 - INFO - Evaluating final LSTM model...
2025-01-30 07:54:54,677 - INFO - Test MSE: 0.0892
2025-01-30 07:54:54,678 - INFO - Test RMSE: 0.2986
2025-01-30 07:54:54,678 - INFO - Test MAE: 0.1908
2025-01-30 07:54:54,678 - INFO - Test R2 Score: 0.9926
2025-01-30 07:54:54,678 - INFO - Directional Accuracy: 0.4675
2025-01-30 07:55:15,118 - WARNING - You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`.
2025-01-30 07:55:15,226 - INFO - Saved best LSTM model and scaler objects (best_lstm_model.h5, scaler_features.pkl, scaler_target.pkl).
2025-01-30 07:55:15,226 - INFO - Starting DQN hyperparameter tuning with Optuna using 54 parallel trials...
2025-01-31 00:33:51,246 - INFO - ===== Resource Statistics =====
2025-01-31 00:33:51,246 - INFO - Physical CPU Cores: 28
2025-01-31 00:33:51,246 - INFO - Logical CPU Cores: 56
2025-01-31 00:33:51,246 - INFO - CPU Usage per Core: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
2025-01-31 00:33:51,246 - INFO - No GPUs detected.
2025-01-31 00:33:51,247 - INFO - =================================
2025-01-31 00:33:51,247 - INFO - Configured TensorFlow to use CPU with optimized thread settings.
2025-01-31 00:33:51,247 - INFO - Loading data from: BAT.csv
2025-01-31 00:33:52,623 - INFO - Data columns after renaming: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
2025-01-31 00:33:52,640 - INFO - Data loaded and sorted successfully.
2025-01-31 00:33:52,640 - INFO - Calculating technical indicators...
2025-01-31 00:33:52,680 - INFO - Technical indicators calculated successfully.
2025-01-31 00:33:52,690 - INFO - Starting parallel feature engineering with 54 workers...
2025-01-31 00:34:02,440 - INFO - Parallel feature engineering completed.
2025-01-31 00:34:02,527 - INFO - Scaled training features shape: (14134, 15, 17)
2025-01-31 00:34:02,527 - INFO - Scaled validation features shape: (3028, 15, 17)
2025-01-31 00:34:02,527 - INFO - Scaled testing features shape: (3030, 15, 17)
2025-01-31 00:34:02,527 - INFO - Scaled training target shape: (14134,)
2025-01-31 00:34:02,527 - INFO - Scaled validation target shape: (3028,)
2025-01-31 00:34:02,527 - INFO - Scaled testing target shape: (3030,)
2025-01-31 00:34:02,527 - INFO - Starting LSTM hyperparameter optimization with Optuna using 54 parallel trials...
2025-01-31 02:07:22,583 - INFO - Best LSTM Hyperparameters: {'num_lstm_layers': 1, 'lstm_units': 128, 'dropout_rate': 0.18042015532719258, 'learning_rate': 0.008263668593877975, 'optimizer': 'Nadam', 'decay': 7.065697336348234e-05}
2025-01-31 02:07:22,887 - INFO - Training best LSTM model with optimized hyperparameters...
2025-01-31 02:29:45,755 - INFO - Evaluating final LSTM model...
2025-01-31 02:29:47,478 - INFO - Test MSE: 0.0765
2025-01-31 02:29:47,479 - INFO - Test RMSE: 0.2765
2025-01-31 02:29:47,479 - INFO - Test MAE: 0.1770
2025-01-31 02:29:47,479 - INFO - Test R2 Score: 0.9937
2025-01-31 02:29:47,479 - INFO - Directional Accuracy: 0.4823
2025-01-31 02:30:07,570 - WARNING - You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`.
2025-01-31 02:30:07,639 - INFO - Saved best LSTM model and scaler objects (best_lstm_model.h5, scaler_features.pkl, scaler_target.pkl).
2025-01-31 02:30:07,640 - INFO - Starting DQN hyperparameter tuning with Optuna using 54 parallel trials...
2025-01-31 02:49:01,996 - INFO - ===== Resource Statistics =====
2025-01-31 02:49:01,996 - INFO - Physical CPU Cores: 28
2025-01-31 02:49:01,996 - INFO - Logical CPU Cores: 56
2025-01-31 02:49:01,996 - INFO - CPU Usage per Core: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
2025-01-31 02:49:01,996 - INFO - No GPUs detected.
2025-01-31 02:49:01,996 - INFO - =================================
2025-01-31 02:49:01,997 - INFO - Configured TensorFlow to use CPU with optimized thread settings.
2025-01-31 02:49:01,997 - INFO - Loading data from: BAT.csv
2025-01-31 02:49:03,423 - INFO - Data columns after renaming: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
2025-01-31 02:49:03,440 - INFO - Data loaded and sorted successfully.
2025-01-31 02:49:03,440 - INFO - Calculating technical indicators...
2025-01-31 02:49:03,479 - INFO - Technical indicators calculated successfully.
2025-01-31 02:49:03,489 - INFO - Starting parallel feature engineering with 54 workers...
2025-01-31 02:49:12,566 - INFO - Parallel feature engineering completed.
2025-01-31 02:49:12,661 - INFO - Scaled training features shape: (14134, 15, 17)
2025-01-31 02:49:12,661 - INFO - Scaled validation features shape: (3028, 15, 17)
2025-01-31 02:49:12,661 - INFO - Scaled testing features shape: (3030, 15, 17)
2025-01-31 02:49:12,661 - INFO - Scaled training target shape: (14134,)
2025-01-31 02:49:12,661 - INFO - Scaled validation target shape: (3028,)
2025-01-31 02:49:12,661 - INFO - Scaled testing target shape: (3030,)
2025-01-31 02:49:12,662 - INFO - Starting LSTM hyperparameter optimization with Optuna using 54 parallel trials...
2025-01-31 04:26:41,571 - INFO - Best LSTM Hyperparameters: {'num_lstm_layers': 1, 'lstm_units': 128, 'dropout_rate': 0.22440715941937378, 'learning_rate': 0.003707877783335322, 'optimizer': 'Adam', 'decay': 8.301665110041122e-05}
2025-01-31 04:26:41,960 - INFO - Training best LSTM model with optimized hyperparameters...
2025-01-31 04:51:19,731 - INFO - Evaluating final LSTM model...
2025-01-31 04:51:21,389 - INFO - Test MSE: 0.0877
2025-01-31 04:51:21,389 - INFO - Test RMSE: 0.2962
2025-01-31 04:51:21,390 - INFO - Test MAE: 0.1893
2025-01-31 04:51:21,390 - INFO - Test R2 Score: 0.9927
2025-01-31 04:51:21,390 - INFO - Directional Accuracy: 0.4764
2025-01-31 04:51:21,734 - WARNING - You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`.
2025-01-31 04:51:21,768 - INFO - Saved best LSTM model and scaler objects (best_lstm_model.h5, scaler_features.pkl, scaler_target.pkl).
2025-01-31 04:51:21,768 - INFO - Starting DQN hyperparameter tuning with Optuna using 54 parallel trials...
2025-01-31 22:41:43,005 - INFO - ===== Resource Statistics =====
2025-01-31 22:41:43,005 - INFO - Physical CPU Cores: 28
2025-01-31 22:41:43,005 - INFO - Logical CPU Cores: 56
2025-01-31 22:41:43,005 - INFO - CPU Usage per Core: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
2025-01-31 22:41:43,005 - INFO - No GPUs detected.
2025-01-31 22:41:43,005 - INFO - =================================
2025-01-31 22:41:43,006 - INFO - Configured TensorFlow to use CPU with optimized thread settings.
2025-01-31 22:41:43,006 - INFO - Loading data from: BAT.csv
2025-01-31 22:41:44,326 - INFO - Data columns after renaming: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
2025-01-31 22:41:44,339 - INFO - Data loaded and sorted successfully.
2025-01-31 22:41:44,339 - INFO - Calculating technical indicators...
2025-01-31 22:41:44,370 - INFO - Technical indicators calculated successfully.
2025-01-31 22:41:44,379 - INFO - Starting parallel feature engineering with 54 workers...
2025-01-31 22:41:53,902 - INFO - Parallel feature engineering completed.
2025-01-31 22:41:54,028 - INFO - Scaled training features shape: (14134, 15, 17)
2025-01-31 22:41:54,028 - INFO - Scaled validation features shape: (3028, 15, 17)
2025-01-31 22:41:54,028 - INFO - Scaled testing features shape: (3030, 15, 17)
2025-01-31 22:41:54,028 - INFO - Scaled training target shape: (14134,)
2025-01-31 22:41:54,028 - INFO - Scaled validation target shape: (3028,)
2025-01-31 22:41:54,029 - INFO - Scaled testing target shape: (3030,)
2025-01-31 22:41:54,029 - INFO - Starting LSTM hyperparameter optimization with Optuna using 54 parallel trials...
2025-01-31 22:50:02,369 - INFO - ===== Resource Statistics =====
2025-01-31 22:50:02,369 - INFO - Physical CPU Cores: 28
2025-01-31 22:50:02,369 - INFO - Logical CPU Cores: 56
2025-01-31 22:50:02,369 - INFO - CPU Usage per Core: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
2025-01-31 22:50:02,369 - INFO - No GPUs detected.
2025-01-31 22:50:02,369 - INFO - =================================
2025-01-31 22:50:02,370 - INFO - Configured TensorFlow to use CPU with optimized thread settings.
2025-01-31 22:50:02,370 - INFO - Loading data from: BAT.csv
2025-01-31 22:50:03,713 - INFO - Data columns after renaming: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
2025-01-31 22:50:03,730 - INFO - Data loaded and sorted successfully.
2025-01-31 22:50:03,730 - INFO - Calculating technical indicators...
2025-01-31 22:50:03,769 - INFO - Technical indicators calculated successfully.
2025-01-31 22:50:03,779 - INFO - Starting parallel feature engineering with 54 workers...
2025-01-31 22:50:13,311 - INFO - Parallel feature engineering completed.
2025-01-31 22:50:13,420 - INFO - Scaled training features shape: (14134, 15, 17)
2025-01-31 22:50:13,421 - INFO - Scaled validation features shape: (3028, 15, 17)
2025-01-31 22:50:13,421 - INFO - Scaled testing features shape: (3030, 15, 17)
2025-01-31 22:50:13,421 - INFO - Scaled training target shape: (14134,)
2025-01-31 22:50:13,421 - INFO - Scaled validation target shape: (3028,)
2025-01-31 22:50:13,421 - INFO - Scaled testing target shape: (3030,)
2025-01-31 22:50:13,421 - INFO - Starting LSTM hyperparameter optimization with Optuna using 54 parallel trials...

View File

@@ -5,6 +5,8 @@ import numpy as np
import pandas as pd import pandas as pd
import logging import logging
from tabulate import tabulate from tabulate import tabulate
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import seaborn as sns import seaborn as sns
import psutil import psutil
@@ -37,16 +39,15 @@ import time
# Suppress TensorFlow logs beyond errors # Suppress TensorFlow logs beyond errors
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# =============================================================================
# GLOBAL LOCK FOR DQN TRAINING (to force one-at-a-time usage of the shared LSTM)
# =============================================================================
dqn_lock = threading.Lock()
# ============================ # ============================
# Resource Detection Functions # Resource Detection Functions
# ============================ # ============================
def get_cpu_info(): def get_cpu_info():
"""
Retrieves CPU information including physical and logical cores and current usage per core.
Returns:
dict: Dictionary containing physical cores, logical cores, and CPU usage per core.
"""
cpu_count = psutil.cpu_count(logical=False) # Physical cores cpu_count = psutil.cpu_count(logical=False) # Physical cores
cpu_count_logical = psutil.cpu_count(logical=True) # Logical cores cpu_count_logical = psutil.cpu_count(logical=True) # Logical cores
cpu_percent = psutil.cpu_percent(interval=1, percpu=True) cpu_percent = psutil.cpu_percent(interval=1, percpu=True)
@@ -57,12 +58,6 @@ def get_cpu_info():
} }
def get_gpu_info(): def get_gpu_info():
"""
Retrieves GPU information including load, memory usage, and temperature.
Returns:
list: List of dictionaries containing GPU stats.
"""
gpus = GPUtil.getGPUs() gpus = GPUtil.getGPUs()
gpu_info = [] gpu_info = []
for gpu in gpus: for gpu in gpus:
@@ -78,13 +73,6 @@ def get_gpu_info():
return gpu_info return gpu_info
def configure_tensorflow(cpu_stats, gpu_stats): def configure_tensorflow(cpu_stats, gpu_stats):
"""
Configures TensorFlow to utilize available CPU and GPU resources efficiently.
Args:
cpu_stats (dict): Dictionary containing CPU statistics.
gpu_stats (list): List of dictionaries containing GPU statistics.
"""
logical_cores = cpu_stats['logical_cores'] logical_cores = cpu_stats['logical_cores']
os.environ["OMP_NUM_THREADS"] = str(logical_cores) os.environ["OMP_NUM_THREADS"] = str(logical_cores)
os.environ["TF_NUM_INTRAOP_THREADS"] = str(logical_cores) os.environ["TF_NUM_INTRAOP_THREADS"] = str(logical_cores)
@@ -108,12 +96,6 @@ def configure_tensorflow(cpu_stats, gpu_stats):
# Resource Monitoring Function (Optional) # Resource Monitoring Function (Optional)
# ============================ # ============================
def monitor_resources(interval=60): def monitor_resources(interval=60):
"""
Continuously monitors and logs CPU and GPU usage at specified intervals.
Args:
interval (int): Time in seconds between each monitoring snapshot.
"""
while True: while True:
cpu = psutil.cpu_percent(interval=1, percpu=True) cpu = psutil.cpu_percent(interval=1, percpu=True)
gpu = get_gpu_info() gpu = get_gpu_info()
@@ -153,7 +135,6 @@ def load_data(file_path):
'close': 'Close' 'close': 'Close'
} }
df.rename(columns=rename_mapping, inplace=True) df.rename(columns=rename_mapping, inplace=True)
logging.info(f"Data columns after renaming: {df.columns.tolist()}") logging.info(f"Data columns after renaming: {df.columns.tolist()}")
df.sort_values('Date', inplace=True) df.sort_values('Date', inplace=True)
df.reset_index(drop=True, inplace=True) df.reset_index(drop=True, inplace=True)
@@ -245,10 +226,11 @@ def parse_arguments():
help='Number of episodes to evaluate DQN in the tuning step. Default=1 (entire dataset once).') help='Number of episodes to evaluate DQN in the tuning step. Default=1 (entire dataset once).')
parser.add_argument('--n_trials_lstm', type=int, default=30, parser.add_argument('--n_trials_lstm', type=int, default=30,
help='Number of Optuna trials for LSTM. Default=30.') help='Number of Optuna trials for LSTM. Default=30.')
# The following arguments are no longer used in sequential DQN training:
parser.add_argument('--n_trials_dqn', type=int, default=20, parser.add_argument('--n_trials_dqn', type=int, default=20,
help='Number of Optuna trials for DQN. Default=20.') help='(Unused in sequential DQN training)')
parser.add_argument('--max_parallel_trials', type=int, default=None, parser.add_argument('--max_parallel_trials', type=int, default=None,
help='Maximum number of parallel Optuna trials. Defaults to (logical cores - 2).') help='(Unused in sequential DQN training)')
parser.add_argument('--preprocess_workers', type=int, default=None, parser.add_argument('--preprocess_workers', type=int, default=None,
help='Number of worker processes for data preprocessing. Defaults to (logical cores - 2).') help='Number of worker processes for data preprocessing. Defaults to (logical cores - 2).')
parser.add_argument('--monitor_resources', action='store_true', parser.add_argument('--monitor_resources', action='store_true',
@@ -274,7 +256,6 @@ class ActionLoggingCallback(BaseCallback):
self.reward_buffer = [] self.reward_buffer = []
def _on_step(self): def _on_step(self):
# For Stable Baselines3, access actions and rewards via self.locals
action = self.locals.get('action', None) action = self.locals.get('action', None)
reward = self.locals.get('reward', None) reward = self.locals.get('reward', None)
if action is not None: if action is not None:
@@ -305,10 +286,8 @@ class ActionLoggingCallback(BaseCallback):
def parallel_feature_engineering(row): def parallel_feature_engineering(row):
""" """
Placeholder function for feature engineering. Modify as needed. Placeholder function for feature engineering. Modify as needed.
Args: Args:
row (pd.Series): A row from the DataFrame. row (pd.Series): A row from the DataFrame.
Returns: Returns:
pd.Series: Processed row. pd.Series: Processed row.
""" """
@@ -318,11 +297,9 @@ def parallel_feature_engineering(row):
def feature_engineering_parallel(df, num_workers): def feature_engineering_parallel(df, num_workers):
""" """
Applies feature engineering in parallel using multiprocessing. Applies feature engineering in parallel using multiprocessing.
Args: Args:
df (pd.DataFrame): DataFrame to process. df (pd.DataFrame): DataFrame to process.
num_workers (int): Number of worker processes. num_workers (int): Number of worker processes.
Returns: Returns:
pd.DataFrame: Processed DataFrame. pd.DataFrame: Processed DataFrame.
""" """
@@ -334,124 +311,8 @@ def feature_engineering_parallel(df, num_workers):
return df_processed return df_processed
# ============================ # ============================
# Main Function with Enhanced Optimizations # LSTM Model Construction & Training (Including Optuna Tuning)
# ============================ # ============================
def main():
args = parse_arguments()
csv_path = args.csv_path
lstm_window_size = args.lstm_window_size
dqn_total_timesteps = args.dqn_total_timesteps
dqn_eval_episodes = args.dqn_eval_episodes
n_trials_lstm = args.n_trials_lstm
n_trials_dqn = args.n_trials_dqn
max_parallel_trials = args.max_parallel_trials
preprocess_workers = args.preprocess_workers
enable_resource_monitor = args.monitor_resources
# =============================
# Setup Logging
# =============================
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("LSTMDQN.log"),
logging.StreamHandler(sys.stdout)
])
# =============================
# Resource Detection & Logging
# =============================
cpu_stats = get_cpu_info()
gpu_stats = get_gpu_info()
logging.info("===== Resource Statistics =====")
logging.info(f"Physical CPU Cores: {cpu_stats['physical_cores']}")
logging.info(f"Logical CPU Cores: {cpu_stats['logical_cores']}")
logging.info(f"CPU Usage per Core: {cpu_stats['cpu_percent']}%")
if gpu_stats:
logging.info("GPU Statistics:")
for gpu in gpu_stats:
logging.info(f"GPU {gpu['id']} - {gpu['name']}: Load: {gpu['load']}%, "
f"Memory Used: {gpu['memory_used']}MB / {gpu['memory_total']}MB, "
f"Temperature: {gpu['temperature']}°C")
else:
logging.info("No GPUs detected.")
logging.info("=================================")
# =============================
# Configure TensorFlow
# =============================
configure_tensorflow(cpu_stats, gpu_stats)
# =============================
# Start Resource Monitoring (Optional)
# =============================
if enable_resource_monitor:
logging.info("Starting real-time resource monitoring...")
resource_monitor_thread = threading.Thread(target=monitor_resources, args=(60,), daemon=True)
resource_monitor_thread.start()
##########################################
# A) LSTM PART: LOAD, PREPROCESS, TUNE
##########################################
# 1) LOAD & preprocess
df = load_data(csv_path)
df = calculate_technical_indicators(df)
feature_columns = [
'SMA_5','SMA_10','EMA_5','EMA_10','STDDEV_5',
'RSI','MACD','ADX','OBV','Volume','Open','High','Low',
'BB_Upper','BB_Lower','BB_Width','MFI'
]
target_column = 'Close'
df = df[['Date'] + feature_columns + [target_column]].dropna()
# 2) Controlled Parallel Data Preprocessing
if preprocess_workers is None:
# Default to logical cores minus 2 to prevent overloading
preprocess_workers = max(1, cpu_stats['logical_cores'] - 2)
else:
preprocess_workers = min(preprocess_workers, cpu_stats['logical_cores'])
df = feature_engineering_parallel(df, num_workers=preprocess_workers)
scaler_features = MinMaxScaler()
scaler_target = MinMaxScaler()
X_all = df[feature_columns].values
y_all = df[[target_column]].values
X_scaled = scaler_features.fit_transform(X_all)
y_scaled = scaler_target.fit_transform(y_all).flatten()
# 3) Create sequences
def create_sequences(features, target, window_size):
X_seq, y_seq = [], []
for i in range(len(features) - window_size):
X_seq.append(features[i:i+window_size])
y_seq.append(target[i+window_size])
return np.array(X_seq), np.array(y_seq)
X, y = create_sequences(X_scaled, y_scaled, lstm_window_size)
# 4) Split into train/val/test
train_size = int(len(X) * 0.7)
val_size = int(len(X) * 0.15)
test_size = len(X) - train_size - val_size
X_train, y_train = X[:train_size], y[:train_size]
X_val, y_val = X[train_size: train_size + val_size], y[train_size: train_size + val_size]
X_test, y_test = X[train_size + val_size:], y[train_size + val_size:]
logging.info(f"Scaled training features shape: {X_train.shape}")
logging.info(f"Scaled validation features shape: {X_val.shape}")
logging.info(f"Scaled testing features shape: {X_test.shape}")
logging.info(f"Scaled training target shape: {y_train.shape}")
logging.info(f"Scaled validation target shape: {y_val.shape}")
logging.info(f"Scaled testing target shape: {y_test.shape}")
# 5) Build and compile LSTM model
def build_lstm(input_shape, hyperparams): def build_lstm(input_shape, hyperparams):
model = Sequential() model = Sequential()
num_layers = hyperparams['num_lstm_layers'] num_layers = hyperparams['num_lstm_layers']
@@ -479,122 +340,12 @@ def main():
model.compile(loss=Huber(), optimizer=opt, metrics=['mae']) model.compile(loss=Huber(), optimizer=opt, metrics=['mae'])
return model return model
# 6) Optuna objective for LSTM # NOTE: The following lstm_objective is now defined as an inner function in main,
def lstm_objective(trial): # so that it can access X_train, y_train, X_val, y_val.
num_lstm_layers = trial.suggest_int('num_lstm_layers', 1, 3)
lstm_units = trial.suggest_categorical('lstm_units', [32, 64, 96, 128])
dropout_rate = trial.suggest_float('dropout_rate', 0.1, 0.5)
learning_rate = trial.suggest_loguniform('learning_rate', 1e-5, 1e-2)
optimizer_name = trial.suggest_categorical('optimizer', ['Adam', 'Nadam'])
decay = trial.suggest_float('decay', 0.0, 1e-4)
hyperparams = { # ============================
'num_lstm_layers': num_lstm_layers, # Custom Gym Environment with LSTM Predictions
'lstm_units': lstm_units, # ============================
'dropout_rate': dropout_rate,
'learning_rate': learning_rate,
'optimizer': optimizer_name,
'decay': decay
}
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
early_stop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
lr_reduce = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=1e-6)
cb_prune = KerasPruningCallback(trial, 'val_loss')
history = model_.fit(
X_train, y_train,
epochs=100,
batch_size=16,
validation_data=(X_val, y_val),
callbacks=[early_stop, lr_reduce, cb_prune],
verbose=0
)
val_mae = min(history.history['val_mae'])
return val_mae
# 7) Hyperparameter Optimization with Optuna (Parallelized)
if max_parallel_trials is None:
# Default to logical cores minus 2 to prevent overloading
max_parallel_trials = max(1, cpu_stats['logical_cores'] - 2)
else:
max_parallel_trials = min(max_parallel_trials, cpu_stats['logical_cores'])
logging.info(f"Starting LSTM hyperparameter optimization with Optuna using {max_parallel_trials} parallel trials...")
study_lstm = optuna.create_study(direction='minimize')
study_lstm.optimize(lstm_objective, n_trials=n_trials_lstm, n_jobs=max_parallel_trials)
best_lstm_params = study_lstm.best_params
logging.info(f"Best LSTM Hyperparameters: {best_lstm_params}")
# 8) Train final LSTM
final_lstm = build_lstm((X_train.shape[1], X_train.shape[2]), best_lstm_params)
early_stop_final = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True)
lr_reduce_final = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=1e-6)
logging.info("Training best LSTM model with optimized hyperparameters...")
hist = final_lstm.fit(
X_train, y_train,
epochs=300,
batch_size=16,
validation_data=(X_val, y_val),
callbacks=[early_stop_final, lr_reduce_final],
verbose=1
)
# 9) Evaluate LSTM
def evaluate_lstm(model, X_test, y_test):
logging.info("Evaluating final LSTM model...")
y_pred_scaled = model.predict(X_test).flatten()
y_pred_scaled = np.clip(y_pred_scaled, 0, 1)
y_pred = scaler_target.inverse_transform(y_pred_scaled.reshape(-1, 1)).flatten()
y_test_actual = scaler_target.inverse_transform(y_test.reshape(-1, 1)).flatten()
mse_ = mean_squared_error(y_test_actual, y_pred)
rmse_ = np.sqrt(mse_)
mae_ = mean_absolute_error(y_test_actual, y_pred)
r2_ = r2_score(y_test_actual, y_pred)
direction_actual = np.sign(np.diff(y_test_actual))
direction_pred = np.sign(np.diff(y_pred))
directional_accuracy = np.mean(direction_actual == direction_pred)
logging.info(f"Test MSE: {mse_:.4f}")
logging.info(f"Test RMSE: {rmse_:.4f}")
logging.info(f"Test MAE: {mae_:.4f}")
logging.info(f"Test R2 Score: {r2_:.4f}")
logging.info(f"Directional Accuracy: {directional_accuracy:.4f}")
# Plot Actual vs Predicted
plt.figure(figsize=(14, 7))
plt.plot(y_test_actual, label='Actual Price')
plt.plot(y_pred, label='Predicted Price')
plt.title('LSTM: Actual vs Predicted Closing Prices')
plt.legend()
plt.grid(True)
plt.savefig('lstm_actual_vs_pred.png')
plt.close()
# Tabulate first 40 results
table = []
limit = min(40, len(y_test_actual))
for i in range(limit):
table.append([i, round(y_test_actual[i], 2), round(y_pred[i], 2)])
headers = ["Index", "Actual Price", "Predicted Price"]
print("\nFirst 40 Actual vs. Predicted Prices:")
print(tabulate(table, headers=headers, tablefmt="pretty"))
return r2_, directional_accuracy
_r2, _diracc = evaluate_lstm(final_lstm, X_test, y_test)
# 10) Save LSTM and Scalers
final_lstm.save('best_lstm_model.h5')
joblib.dump(scaler_features, 'scaler_features.pkl')
joblib.dump(scaler_target, 'scaler_target.pkl')
logging.info("Saved best LSTM model and scaler objects (best_lstm_model.h5, scaler_features.pkl, scaler_target.pkl).")
############################################################
# B) DQN PART: BUILD ENV THAT USES THE LSTM + FORECAST
############################################################
class StockTradingEnvWithLSTM(gym.Env): class StockTradingEnvWithLSTM(gym.Env):
""" """
A custom OpenAI Gym environment for stock trading that integrates LSTM model predictions. A custom OpenAI Gym environment for stock trading that integrates LSTM model predictions.
@@ -634,6 +385,8 @@ def main():
shape=(len(feature_columns) + 3 + 1,), shape=(len(feature_columns) + 3 + 1,),
dtype=np.float32 dtype=np.float32
) )
# Forced lock for LSTM predictions
self.lstm_lock = threading.Lock()
def reset(self): def reset(self):
self.balance = self.initial_balance self.balance = self.initial_balance
@@ -657,17 +410,16 @@ def main():
# LSTM prediction # LSTM prediction
if self.current_step < self.window_size: if self.current_step < self.window_size:
# Not enough history => no forecast
predicted_close = 0.0 predicted_close = 0.0
else: else:
seq = self.raw_features[self.current_step - self.window_size: self.current_step] seq = self.raw_features[self.current_step - self.window_size: self.current_step]
seq_scaled = self.scaler_features.transform(seq) seq_scaled = self.scaler_features.transform(seq)
seq_scaled = np.expand_dims(seq_scaled, axis=0) # shape (1, window_size, #features) seq_scaled = np.expand_dims(seq_scaled, axis=0) # shape (1, window_size, #features)
with self.lstm_lock:
pred_scaled = self.lstm_model.predict(seq_scaled, verbose=0).flatten()[0] pred_scaled = self.lstm_model.predict(seq_scaled, verbose=0).flatten()[0]
pred_scaled = np.clip(pred_scaled, 0, 1) pred_scaled = np.clip(pred_scaled, 0, 1)
unscaled = self.scaler_target.inverse_transform([[pred_scaled]])[0, 0] unscaled = self.scaler_target.inverse_transform([[pred_scaled]])[0, 0]
# Normalize predicted close price (assuming a typical price range) predicted_close = unscaled / 1000.0 # Adjust normalization as needed
predicted_close = unscaled / 1000.0
obs = np.concatenate([row_norm, additional, [predicted_close]]).astype(np.float32) obs = np.concatenate([row_norm, additional, [predicted_close]]).astype(np.float32)
return obs return obs
@@ -684,9 +436,7 @@ def main():
self.balance -= (cost + fee) self.balance -= (cost + fee)
old_shares = self.shares_held old_shares = self.shares_held
self.shares_held += shares_bought self.shares_held += shares_bought
self.cost_basis = ( self.cost_basis = ((self.cost_basis * old_shares) + (shares_bought * current_price)) / self.shares_held
(self.cost_basis * old_shares) + (shares_bought * current_price)
) / self.shares_held
elif action == 0: # SELL elif action == 0: # SELL
if self.shares_held > 0: if self.shares_held > 0:
@@ -699,33 +449,24 @@ def main():
self.net_worth = self.balance + self.shares_held * current_price self.net_worth = self.balance + self.shares_held * current_price
self.current_step += 1 self.current_step += 1
done = (self.current_step >= self.max_steps - 1) done = (self.current_step >= self.max_steps - 1)
reward = self.net_worth - self.initial_balance reward = self.net_worth - self.initial_balance
obs = self._get_obs() obs = self._get_obs()
return obs, reward, done, {} return obs, reward, done, {}
def render(self, mode='human'): def render(self, mode='human'):
profit = self.net_worth - self.initial_balance profit = self.net_worth - self.initial_balance
print(f"Step: {self.current_step}, " print(f"Step: {self.current_step}, Balance={self.balance:.2f}, Shares={self.shares_held}, NetWorth={self.net_worth:.2f}, Profit={profit:.2f}")
f"Balance={self.balance:.2f}, "
f"Shares={self.shares_held}, "
f"NetWorth={self.net_worth:.2f}, "
f"Profit={profit:.2f}")
###################################
# C) DQN HYPERPARAMETER TUNING WITH LSTM
###################################
from stable_baselines3.common.evaluation import evaluate_policy
# ============================
# DQN Training & Evaluation Functions (Sequential Loop)
# ============================
def evaluate_dqn_networth(model, env, n_episodes=1): def evaluate_dqn_networth(model, env, n_episodes=1):
""" """
Evaluates the trained DQN model by simulating trading over a specified number of episodes. Evaluates the trained DQN model by simulating trading over a specified number of episodes.
Args: Args:
model (stable_baselines3.DQN): Trained DQN model. model (DQN): Trained DQN model.
env (gym.Env): Trading environment instance. env (gym.Env): Trading environment instance.
n_episodes (int): Number of episodes to run for evaluation. n_episodes (int): Number of episodes to run for evaluation.
Returns: Returns:
float: Average final net worth across episodes. float: Average final net worth across episodes.
""" """
@@ -739,116 +480,310 @@ def main():
final_net_worths.append(env.net_worth) final_net_worths.append(env.net_worth)
return np.mean(final_net_worths) return np.mean(final_net_worths)
def dqn_objective(trial): def train_and_evaluate_dqn(hyperparams, env_params, total_timesteps, eval_episodes):
""" """
Objective function for Optuna to optimize DQN hyperparameters. Trains a single DQN agent on an environment (using the frozen LSTM) with given hyperparameters,
Minimizes the negative of the final net worth achieved by the DQN agent. then evaluates its final net worth.
Args: Args:
trial (optuna.trial.Trial): Optuna trial object. hyperparams (dict): Hyperparameters for the DQN model.
env_params (dict): Parameters to create the StockTradingEnvWithLSTM.
total_timesteps (int): Total timesteps for training.
eval_episodes (int): Number of episodes for evaluation.
Returns: Returns:
float: Negative of the final net worth. agent, final_net_worth
""" """
lr = trial.suggest_loguniform("lr", 1e-5, 1e-2) env = StockTradingEnvWithLSTM(**env_params)
gamma = trial.suggest_float("gamma", 0.8, 0.9999)
exploration_fraction = trial.suggest_float("exploration_fraction", 0.01, 0.3)
buffer_size = trial.suggest_categorical("buffer_size", [5000, 10000, 20000])
batch_size = trial.suggest_categorical("batch_size", [32, 64, 128])
# Initialize environment
env = StockTradingEnvWithLSTM(
df=df,
feature_columns=feature_columns,
lstm_model=final_lstm, # Use the trained LSTM model
scaler_features=scaler_features,
scaler_target=scaler_target,
window_size=lstm_window_size
)
vec_env = DummyVecEnv([lambda: env]) vec_env = DummyVecEnv([lambda: env])
with dqn_lock:
# Initialize DQN model agent = DQN(
dqn_action_logger = ActionLoggingCallback(verbose=0)
model = DQN(
'MlpPolicy', 'MlpPolicy',
vec_env, vec_env,
verbose=0, verbose=0,
learning_rate=lr, learning_rate=hyperparams['lr'],
gamma=gamma, gamma=hyperparams['gamma'],
exploration_fraction=exploration_fraction, exploration_fraction=hyperparams['exploration_fraction'],
buffer_size=buffer_size, buffer_size=hyperparams['buffer_size'],
batch_size=batch_size, batch_size=hyperparams['batch_size'],
train_freq=4, train_freq=4,
target_update_interval=1000 target_update_interval=1000
) )
agent.learn(total_timesteps=total_timesteps, callback=ActionLoggingCallback(verbose=0))
final_net_worth = evaluate_dqn_networth(agent, env, n_episodes=eval_episodes)
return agent, final_net_worth
# Train DQN model # ============================
model.learn(total_timesteps=dqn_total_timesteps, callback=dqn_action_logger) # MAIN FUNCTION WITH ENHANCED OPTIMIZATIONS
# ============================
def main():
args = parse_arguments()
csv_path = args.csv_path
lstm_window_size = args.lstm_window_size
dqn_total_timesteps = args.dqn_total_timesteps
dqn_eval_episodes = args.dqn_eval_episodes
n_trials_lstm = args.n_trials_lstm
preprocess_workers = args.preprocess_workers
enable_resource_monitor = args.monitor_resources
# Evaluate final net worth # -----------------------------
final_net_worth = evaluate_dqn_networth(model, env, n_episodes=dqn_eval_episodes) # Setup Logging
# Objective is to maximize net worth, so return negative # -----------------------------
return -final_net_worth logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.FileHandler("LSTMDQN.log"), logging.StreamHandler(sys.stdout)])
# 11) Hyperparameter Optimization with Optuna (Parallelized) # -----------------------------
if max_parallel_trials is None: # Resource Detection & Logging
# Default to logical cores minus 2 to prevent overloading # -----------------------------
max_parallel_trials = max(1, cpu_stats['logical_cores'] - 2) cpu_stats = get_cpu_info()
gpu_stats = get_gpu_info()
logging.info("===== Resource Statistics =====")
logging.info(f"Physical CPU Cores: {cpu_stats['physical_cores']}")
logging.info(f"Logical CPU Cores: {cpu_stats['logical_cores']}")
logging.info(f"CPU Usage per Core: {cpu_stats['cpu_percent']}%")
if gpu_stats:
logging.info("GPU Statistics:")
for gpu in gpu_stats:
logging.info(f"GPU {gpu['id']} - {gpu['name']}: Load: {gpu['load']}%, Memory Used: {gpu['memory_used']}MB / {gpu['memory_total']}MB, Temperature: {gpu['temperature']}°C")
else: else:
max_parallel_trials = min(max_parallel_trials, cpu_stats['logical_cores']) logging.info("No GPUs detected.")
logging.info("=================================")
logging.info(f"Starting DQN hyperparameter tuning with Optuna using {max_parallel_trials} parallel trials...") # -----------------------------
study_dqn = optuna.create_study(direction='minimize') # Configure TensorFlow
study_dqn.optimize(dqn_objective, n_trials=n_trials_dqn, n_jobs=max_parallel_trials) # -----------------------------
best_dqn_params = study_dqn.best_params configure_tensorflow(cpu_stats, gpu_stats)
logging.info(f"Best DQN Hyperparameters: {best_dqn_params}")
################################### # -----------------------------
# D) TRAIN FINAL DQN WITH BEST PARAMETERS # Start Resource Monitoring (Optional)
################################### # -----------------------------
logging.info("Training final DQN model with best hyperparameters...") if enable_resource_monitor:
env_final = StockTradingEnvWithLSTM( logging.info("Starting real-time resource monitoring...")
df=df, resource_monitor_thread = threading.Thread(target=monitor_resources, args=(60,), daemon=True)
feature_columns=feature_columns, resource_monitor_thread.start()
lstm_model=final_lstm,
scaler_features=scaler_features, ##########################################
scaler_target=scaler_target, # A) LSTM PART: LOAD, PREPROCESS, TUNE
window_size=lstm_window_size ##########################################
# 1) LOAD & preprocess
df = load_data(csv_path)
df = calculate_technical_indicators(df)
feature_columns = [
'SMA_5','SMA_10','EMA_5','EMA_10','STDDEV_5',
'RSI','MACD','ADX','OBV','Volume','Open','High','Low',
'BB_Upper','BB_Lower','BB_Width','MFI'
]
target_column = 'Close'
df = df[['Date'] + feature_columns + [target_column]].dropna()
# 2) Controlled Parallel Data Preprocessing
if preprocess_workers is None:
preprocess_workers = max(1, cpu_stats['logical_cores'] - 2)
else:
preprocess_workers = min(preprocess_workers, cpu_stats['logical_cores'])
df = feature_engineering_parallel(df, num_workers=preprocess_workers)
scaler_features = MinMaxScaler()
scaler_target = MinMaxScaler()
X_all = df[feature_columns].values
y_all = df[[target_column]].values
X_scaled = scaler_features.fit_transform(X_all)
y_scaled = scaler_target.fit_transform(y_all).flatten()
# 3) Create sequences for LSTM
def create_sequences(features, target, window_size):
X_seq, y_seq = [], []
for i in range(len(features) - window_size):
X_seq.append(features[i:i+window_size])
y_seq.append(target[i+window_size])
return np.array(X_seq), np.array(y_seq)
X, y = create_sequences(X_scaled, y_scaled, lstm_window_size)
# 4) Split into train/val/test
train_size = int(len(X) * 0.7)
val_size = int(len(X) * 0.15)
test_size = len(X) - train_size - val_size
X_train, y_train = X[:train_size], y[:train_size]
X_val, y_val = X[train_size: train_size + val_size], y[train_size: train_size + val_size]
X_test, y_test = X[train_size + val_size:], y[train_size + val_size:]
logging.info(f"Scaled training features shape: {X_train.shape}")
logging.info(f"Scaled validation features shape: {X_val.shape}")
logging.info(f"Scaled testing features shape: {X_test.shape}")
logging.info(f"Scaled training target shape: {y_train.shape}")
logging.info(f"Scaled validation target shape: {y_val.shape}")
logging.info(f"Scaled testing target shape: {y_test.shape}")
# 5) Define the LSTM objective function here (so it has access to X_train, y_train, X_val, y_val)
def lstm_objective(trial):
num_lstm_layers = trial.suggest_int('num_lstm_layers', 1, 3)
lstm_units = trial.suggest_categorical('lstm_units', [32, 64, 96, 128])
dropout_rate = trial.suggest_float('dropout_rate', 0.1, 0.5)
learning_rate = trial.suggest_float('learning_rate', 1e-5, 1e-2, log=True)
optimizer_name = trial.suggest_categorical('optimizer', ['Adam', 'Nadam'])
decay = trial.suggest_float('decay', 0.0, 1e-4)
hyperparams = {
'num_lstm_layers': num_lstm_layers,
'lstm_units': lstm_units,
'dropout_rate': dropout_rate,
'learning_rate': learning_rate,
'optimizer': optimizer_name,
'decay': decay
}
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
early_stop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
lr_reduce = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=1e-6)
cb_prune = KerasPruningCallback(trial, 'val_loss')
history = model_.fit(
X_train, y_train,
epochs=100,
batch_size=16,
validation_data=(X_val, y_val),
callbacks=[early_stop, lr_reduce, cb_prune],
verbose=0
) )
vec_env_final = DummyVecEnv([lambda: env_final]) val_mae = min(history.history['val_mae'])
return val_mae
final_dqn_logger = ActionLoggingCallback(verbose=1) # Enable detailed logging # 6) Hyperparameter Optimization with Optuna for the LSTM
logging.info(f"Starting LSTM hyperparameter optimization with Optuna using {cpu_stats['logical_cores']-2} parallel trials...")
study_lstm = optuna.create_study(direction='minimize')
study_lstm.optimize(lstm_objective, n_trials=n_trials_lstm, n_jobs=cpu_stats['logical_cores']-2)
best_lstm_params = study_lstm.best_params
logging.info(f"Best LSTM Hyperparameters: {best_lstm_params}")
final_model = DQN( # 7) Train final LSTM with best hyperparameters
'MlpPolicy', final_lstm = build_lstm((X_train.shape[1], X_train.shape[2]), best_lstm_params)
vec_env_final, early_stop_final = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True)
verbose=1, lr_reduce_final = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=1e-6)
learning_rate=best_dqn_params['lr'], logging.info("Training best LSTM model with optimized hyperparameters...")
gamma=best_dqn_params['gamma'], hist = final_lstm.fit(
exploration_fraction=best_dqn_params['exploration_fraction'], X_train, y_train,
buffer_size=best_dqn_params['buffer_size'], epochs=300,
batch_size=best_dqn_params['batch_size'], batch_size=16,
train_freq=4, validation_data=(X_val, y_val),
target_update_interval=1000 callbacks=[early_stop_final, lr_reduce_final],
verbose=1
) )
final_model.learn(total_timesteps=dqn_total_timesteps, callback=final_dqn_logger)
final_model.save("best_dqn_model_lstm.zip") # 8) Evaluate LSTM
logging.info("Final DQN model trained and saved as 'best_dqn_model_lstm.zip'.") def evaluate_final_lstm(model, X_test, y_test):
logging.info("Evaluating final LSTM model...")
y_pred_scaled = model.predict(X_test).flatten()
y_pred_scaled = np.clip(y_pred_scaled, 0, 1)
y_pred = scaler_target.inverse_transform(y_pred_scaled.reshape(-1, 1)).flatten()
y_test_actual = scaler_target.inverse_transform(y_test.reshape(-1, 1)).flatten()
mse_ = mean_squared_error(y_test_actual, y_pred)
rmse_ = np.sqrt(mse_)
mae_ = mean_absolute_error(y_test_actual, y_pred)
r2_ = r2_score(y_test_actual, y_pred)
direction_actual = np.sign(np.diff(y_test_actual))
direction_pred = np.sign(np.diff(y_pred))
directional_accuracy = np.mean(direction_actual == direction_pred)
logging.info(f"Test MSE: {mse_:.4f}")
logging.info(f"Test RMSE: {rmse_:.4f}")
logging.info(f"Test MAE: {mae_:.4f}")
logging.info(f"Test R2 Score: {r2_:.4f}")
logging.info(f"Directional Accuracy: {directional_accuracy:.4f}")
plt.figure(figsize=(14, 7))
plt.plot(y_test_actual, label='Actual Price')
plt.plot(y_pred, label='Predicted Price')
plt.title('LSTM: Actual vs Predicted Closing Prices')
plt.legend()
plt.grid(True)
plt.savefig('lstm_actual_vs_pred.png')
plt.close()
table = []
limit = min(40, len(y_test_actual))
for i in range(limit):
table.append([i, round(y_test_actual[i], 2), round(y_pred[i], 2)])
headers = ["Index", "Actual Price", "Predicted Price"]
print("\nFirst 40 Actual vs. Predicted Prices:")
print(tabulate(table, headers=headers, tablefmt="pretty"))
return r2_, directional_accuracy
_r2, _diracc = evaluate_final_lstm(final_lstm, X_test, y_test)
# 9) Save final LSTM model and scalers
final_lstm.save('best_lstm_model.h5')
joblib.dump(scaler_features, 'scaler_features.pkl')
joblib.dump(scaler_target, 'scaler_target.pkl')
logging.info("Saved best LSTM model and scaler objects (best_lstm_model.h5, scaler_features.pkl, scaler_target.pkl).")
############################################################
# B) DQN PART: BUILD ENV THAT USES THE FROZEN LSTM + FORECAST
############################################################
# (StockTradingEnvWithLSTM is defined above)
################################### ###################################
# E) FINAL INFERENCE & LOG RESULTS # C) SEQUENTIAL DQN TRAINING WITH LSTM INTEGRATION
###################################
env_params = {
'df': df,
'feature_columns': feature_columns,
'lstm_model': final_lstm, # Use the frozen, best LSTM model
'scaler_features': scaler_features,
'scaler_target': scaler_target,
'window_size': lstm_window_size,
'initial_balance': 10000,
'transaction_cost': 0.001
}
# Base DQN hyperparameters (adjust as needed)
base_hyperparams = {
'lr': 1e-3,
'gamma': 0.95,
'exploration_fraction': 0.1,
'buffer_size': 10000,
'batch_size': 64
}
# Define performance threshold (final net worth must be above this)
PERFORMANCE_THRESHOLD = 10500.0
current_hyperparams = base_hyperparams.copy()
max_attempts = 10
best_agent = None
for attempt in range(max_attempts):
logging.info(f"Training DQN agent: Attempt {attempt+1} with hyperparameters: {current_hyperparams}")
agent, net_worth = train_and_evaluate_dqn(current_hyperparams, env_params,
total_timesteps=dqn_total_timesteps,
eval_episodes=dqn_eval_episodes)
logging.info(f"Agent achieved final net worth: ${net_worth:.2f}")
if net_worth >= PERFORMANCE_THRESHOLD:
logging.info("Agent meets performance criteria!")
best_agent = agent
best_agent.save("best_dqn_model_lstm.zip")
break
else:
logging.info("Performance below threshold. Adjusting hyperparameters and retrying...")
current_hyperparams['lr'] *= 0.9 # decrease learning rate by 10%
current_hyperparams['exploration_fraction'] = min(current_hyperparams['exploration_fraction'] + 0.02, 0.3)
if best_agent is None:
logging.warning("Failed to train a satisfactory DQN agent after multiple attempts.")
else:
logging.info("Final DQN agent trained and saved.")
###################################
# D) FINAL INFERENCE & LOG RESULTS
################################### ###################################
logging.info("Running final inference with the trained DQN model...") logging.info("Running final inference with the trained DQN model...")
env_test = StockTradingEnvWithLSTM( env_test = StockTradingEnvWithLSTM(**env_params)
df=df,
feature_columns=feature_columns,
lstm_model=final_lstm,
scaler_features=scaler_features,
scaler_target=scaler_target,
window_size=lstm_window_size
)
obs = env_test.reset() obs = env_test.reset()
done = False done = False
total_reward = 0.0 total_reward = 0.0
@@ -857,7 +792,7 @@ def main():
while not done: while not done:
step_count += 1 step_count += 1
action, _ = final_model.predict(obs, deterministic=True) action, _ = best_agent.predict(obs, deterministic=True)
obs, reward, done, info = env_test.step(action) obs, reward, done, info = env_test.step(action)
total_reward += reward total_reward += reward
step_data.append({ step_data.append({
@@ -902,31 +837,24 @@ def main():
logging.info("Final inference completed. Results logged and displayed.") logging.info("Final inference completed. Results logged and displayed.")
################################### ###################################
# F) OPTIONAL: RETRY LOOP IF NET WORTH < THRESHOLD # E) OPTIONAL: RETRY LOOP IF NET WORTH < THRESHOLD
################################### ###################################
NET_WORTH_THRESHOLD = 10500.0 # example threshold if final_net_worth < PERFORMANCE_THRESHOLD:
logging.warning(f"Final net worth (${final_net_worth:.2f}) is below ${PERFORMANCE_THRESHOLD:.2f}. Retraining the same DQN model to learn from mistakes...")
if final_net_worth < NET_WORTH_THRESHOLD:
logging.warning(f"Final net worth (${final_net_worth:.2f}) is below ${NET_WORTH_THRESHOLD:.2f}. Retraining the same DQN model to learn from mistakes...")
# We continue training the SAME final_model without resetting its replay buffer.
# By setting `reset_num_timesteps=False`, we keep the replay buffer and learned weights.
additional_timesteps = 50000 additional_timesteps = 50000
logging.info(f"Retraining the existing DQN model for an additional {additional_timesteps} timesteps (keeping old experiences).") logging.info(f"Retraining the existing DQN model for an additional {additional_timesteps} timesteps (keeping old experiences).")
best_agent.learn(
# If you want to see action distributions again, you can keep the same callback or define a new one:
final_model.learn(
total_timesteps=additional_timesteps, total_timesteps=additional_timesteps,
reset_num_timesteps=False, # Keep replay buffer + internal step counter reset_num_timesteps=False, # Keep replay buffer + internal step counter
callback=final_dqn_logger # Optional: to log actions again callback=ActionLoggingCallback(verbose=1)
) )
# Evaluate again
obs = env_test.reset() obs = env_test.reset()
done = False done = False
second_total_reward = 0.0 second_total_reward = 0.0
while not done: while not done:
action, _ = final_model.predict(obs, deterministic=True) action, _ = best_agent.predict(obs, deterministic=True)
obs, reward, done, info = env_test.step(action) obs, reward, done, info = env_test.step(action)
second_total_reward += reward second_total_reward += reward
@@ -934,9 +862,9 @@ def main():
second_profit = second_net_worth - env_test.initial_balance second_profit = second_net_worth - env_test.initial_balance
logging.info(f"After additional training, new final net worth=${second_net_worth:.2f}, profit=${second_profit:.2f}") logging.info(f"After additional training, new final net worth=${second_net_worth:.2f}, profit=${second_profit:.2f}")
if second_net_worth < NET_WORTH_THRESHOLD: if second_net_worth < PERFORMANCE_THRESHOLD:
logging.warning("Even after continued training, net worth is still below threshold. Consider a deeper hyperparameter search or analyzing the environment settings.") logging.warning("Even after continued training, net worth is still below threshold. Consider a deeper hyperparameter search or analyzing the environment settings.")
if __name__ == "__main__":
main() main()

View File

@@ -0,0 +1,324 @@
(venv) kleinpanic@kleinpanic:~/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src$ py LSTMDQN.py BAT.csv
2025-01-31 00:33:45.402617: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1738283625.423731 635164 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1738283625.430264 635164 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2025-01-31 00:33:45.451539: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2025-01-31 00:33:51,246 - INFO - ===== Resource Statistics =====
2025-01-31 00:33:51,246 - INFO - Physical CPU Cores: 28
2025-01-31 00:33:51,246 - INFO - Logical CPU Cores: 56
2025-01-31 00:33:51,246 - INFO - CPU Usage per Core: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
2025-01-31 00:33:51,246 - INFO - No GPUs detected.
2025-01-31 00:33:51,247 - INFO - =================================
2025-01-31 00:33:51,247 - INFO - Configured TensorFlow to use CPU with optimized thread settings.
2025-01-31 00:33:51,247 - INFO - Loading data from: BAT.csv
2025-01-31 00:33:52,623 - INFO - Data columns after renaming: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
2025-01-31 00:33:52,640 - INFO - Data loaded and sorted successfully.
2025-01-31 00:33:52,640 - INFO - Calculating technical indicators...
2025-01-31 00:33:52,680 - INFO - Technical indicators calculated successfully.
2025-01-31 00:33:52,690 - INFO - Starting parallel feature engineering with 54 workers...
2025-01-31 00:34:02,440 - INFO - Parallel feature engineering completed.
2025-01-31 00:34:02,527 - INFO - Scaled training features shape: (14134, 15, 17)
2025-01-31 00:34:02,527 - INFO - Scaled validation features shape: (3028, 15, 17)
2025-01-31 00:34:02,527 - INFO - Scaled testing features shape: (3030, 15, 17)
2025-01-31 00:34:02,527 - INFO - Scaled training target shape: (14134,)
2025-01-31 00:34:02,527 - INFO - Scaled validation target shape: (3028,)
2025-01-31 00:34:02,527 - INFO - Scaled testing target shape: (3030,)
2025-01-31 00:34:02,527 - INFO - Starting LSTM hyperparameter optimization with Optuna using 54 parallel trials...
[I 2025-01-31 00:34:02,528] A new study created in memory with name: no-name-30abc2af-0d5d-4afc-9e51-0e6ab5344277
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py:487: FutureWarning: suggest_loguniform has been deprecated in v3.0.0. This feature will be removed in v6.0.0. See https://github.com/optuna/optuna/releases/tag/v3.0.0. Use suggest_float(..., log=True) instead.
learning_rate = trial.suggest_loguniform('learning_rate', 1e-5, 1e-2)
2025-01-31 00:34:02.545693: E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:152] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/keras/src/layers/rnn/bidirectional.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(**kwargs)
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/keras/src/optimizers/base_optimizer.py:86: UserWarning: Argument `decay` is no longer supported and will be ignored.
warnings.warn(
[I 2025-01-31 01:47:50,865] Trial 25 finished with value: 0.0044469027779996395 and parameters: {'num_lstm_layers': 1, 'lstm_units': 96, 'dropout_rate': 0.41552068050266755, 'learning_rate': 0.0020464230384217887, 'optimizer': 'Nadam', 'decay': 4.152362979808315e-05}. Best is trial 25 with value: 0.0044469027779996395.
[I 2025-01-31 01:51:53,458] Trial 1 finished with value: 0.004896007943898439 and parameters: {'num_lstm_layers': 2, 'lstm_units': 32, 'dropout_rate': 0.26347160232211786, 'learning_rate': 0.005618445438864423, 'optimizer': 'Adam', 'decay': 9.002232128681866e-05}. Best is trial 25 with value: 0.0044469027779996395.
[I 2025-01-31 01:52:07,955] Trial 13 finished with value: 0.004379551392048597 and parameters: {'num_lstm_layers': 1, 'lstm_units': 128, 'dropout_rate': 0.1879612031755749, 'learning_rate': 0.00045486151574373985, 'optimizer': 'Adam', 'decay': 7.841076864183645e-05}. Best is trial 13 with value: 0.004379551392048597.
[I 2025-01-31 01:56:35,039] Trial 2 finished with value: 0.0035048779100179672 and parameters: {'num_lstm_layers': 1, 'lstm_units': 128, 'dropout_rate': 0.18042015532719258, 'learning_rate': 0.008263668593877975, 'optimizer': 'Nadam', 'decay': 7.065697336348234e-05}. Best is trial 2 with value: 0.0035048779100179672.
[I 2025-01-31 01:59:49,276] Trial 8 finished with value: 0.004185597877949476 and parameters: {'num_lstm_layers': 2, 'lstm_units': 96, 'dropout_rate': 0.1225129824590411, 'learning_rate': 0.0032993925521966573, 'optimizer': 'Adam', 'decay': 7.453500347854662e-05}. Best is trial 2 with value: 0.0035048779100179672.
[I 2025-01-31 01:59:49,666] Trial 12 pruned. Trial was pruned at epoch 61.
[I 2025-01-31 01:59:57,670] Trial 7 pruned. Trial was pruned at epoch 60.
[I 2025-01-31 02:00:00,145] Trial 6 pruned. Trial was pruned at epoch 48.
[I 2025-01-31 02:00:02,845] Trial 0 pruned. Trial was pruned at epoch 53.
[I 2025-01-31 02:00:03,464] Trial 14 pruned. Trial was pruned at epoch 52.
[I 2025-01-31 02:00:08,618] Trial 20 pruned. Trial was pruned at epoch 41.
[I 2025-01-31 02:00:09,918] Trial 18 pruned. Trial was pruned at epoch 57.
[I 2025-01-31 02:00:18,111] Trial 11 pruned. Trial was pruned at epoch 48.
[I 2025-01-31 02:00:18,175] Trial 24 pruned. Trial was pruned at epoch 70.
[I 2025-01-31 02:00:24,035] Trial 19 pruned. Trial was pruned at epoch 71.
[I 2025-01-31 02:00:25,349] Trial 15 pruned. Trial was pruned at epoch 61.
[I 2025-01-31 02:00:28,094] Trial 21 pruned. Trial was pruned at epoch 53.
[I 2025-01-31 02:00:30,582] Trial 27 pruned. Trial was pruned at epoch 70.
[I 2025-01-31 02:00:34,584] Trial 16 pruned. Trial was pruned at epoch 54.
[I 2025-01-31 02:00:36,311] Trial 4 pruned. Trial was pruned at epoch 41.
[I 2025-01-31 02:00:36,943] Trial 10 pruned. Trial was pruned at epoch 58.
[I 2025-01-31 02:00:41,876] Trial 26 pruned. Trial was pruned at epoch 54.
[I 2025-01-31 02:00:42,253] Trial 5 pruned. Trial was pruned at epoch 54.
[I 2025-01-31 02:00:42,354] Trial 22 pruned. Trial was pruned at epoch 54.
[I 2025-01-31 02:01:21,394] Trial 17 pruned. Trial was pruned at epoch 63.
[I 2025-01-31 02:02:27,396] Trial 28 finished with value: 0.005718659609556198 and parameters: {'num_lstm_layers': 1, 'lstm_units': 32, 'dropout_rate': 0.256096112829434, 'learning_rate': 1.7863513392726302e-05, 'optimizer': 'Nadam', 'decay': 4.8981982638899195e-05}. Best is trial 2 with value: 0.0035048779100179672.
[I 2025-01-31 02:04:43,158] Trial 9 finished with value: 0.004240941721946001 and parameters: {'num_lstm_layers': 1, 'lstm_units': 96, 'dropout_rate': 0.13786769624978978, 'learning_rate': 0.00038368722697235065, 'optimizer': 'Nadam', 'decay': 5.219728457137628e-05}. Best is trial 2 with value: 0.0035048779100179672.
[I 2025-01-31 02:04:47,356] Trial 29 pruned. Trial was pruned at epoch 89.
[I 2025-01-31 02:04:58,802] Trial 23 finished with value: 0.004438518546521664 and parameters: {'num_lstm_layers': 1, 'lstm_units': 96, 'dropout_rate': 0.10170042323024542, 'learning_rate': 2.1295423006302236e-05, 'optimizer': 'Nadam', 'decay': 1.9256711241510017e-05}. Best is trial 2 with value: 0.0035048779100179672.
[I 2025-01-31 02:07:22,581] Trial 3 finished with value: 0.004468627739697695 and parameters: {'num_lstm_layers': 1, 'lstm_units': 128, 'dropout_rate': 0.2941741845859971, 'learning_rate': 0.00015534552759452507, 'optimizer': 'Adam', 'decay': 3.964121547616277e-05}. Best is trial 2 with value: 0.0035048779100179672.
2025-01-31 02:07:22,583 - INFO - Best LSTM Hyperparameters: {'num_lstm_layers': 1, 'lstm_units': 128, 'dropout_rate': 0.18042015532719258, 'learning_rate': 0.008263668593877975, 'optimizer': 'Nadam', 'decay': 7.065697336348234e-05}
2025-01-31 02:07:22,887 - INFO - Training best LSTM model with optimized hyperparameters...
Epoch 1/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 22s 21ms/step - loss: 0.0176 - mae: 0.0468 - val_loss: 3.7775e-04 - val_mae: 0.0096 - learning_rate: 0.0083
Epoch 2/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 5.1259e-04 - mae: 0.0169 - val_loss: 5.0930e-04 - val_mae: 0.0269 - learning_rate: 0.0083
Epoch 3/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 3.5887e-04 - mae: 0.0161 - val_loss: 1.2987e-04 - val_mae: 0.0054 - learning_rate: 0.0083
Epoch 4/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 3.4157e-04 - mae: 0.0157 - val_loss: 1.4855e-04 - val_mae: 0.0068 - learning_rate: 0.0083
Epoch 5/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 3.3388e-04 - mae: 0.0151 - val_loss: 1.2859e-04 - val_mae: 0.0064 - learning_rate: 0.0083
Epoch 6/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 3.3468e-04 - mae: 0.0153 - val_loss: 1.3908e-04 - val_mae: 0.0086 - learning_rate: 0.0083
Epoch 7/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 3.0007e-04 - mae: 0.0139 - val_loss: 1.4985e-04 - val_mae: 0.0053 - learning_rate: 0.0083
Epoch 8/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 2.8960e-04 - mae: 0.0145 - val_loss: 1.0344e-04 - val_mae: 0.0059 - learning_rate: 0.0083
Epoch 9/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.8612e-04 - mae: 0.0107 - val_loss: 1.2100e-04 - val_mae: 0.0089 - learning_rate: 0.0041
Epoch 10/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.8470e-04 - mae: 0.0113 - val_loss: 1.4217e-04 - val_mae: 0.0115 - learning_rate: 0.0041
Epoch 11/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.8239e-04 - mae: 0.0113 - val_loss: 7.3773e-05 - val_mae: 0.0052 - learning_rate: 0.0041
Epoch 12/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.8133e-04 - mae: 0.0113 - val_loss: 8.1284e-05 - val_mae: 0.0063 - learning_rate: 0.0041
Epoch 13/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.6633e-04 - mae: 0.0107 - val_loss: 1.0878e-04 - val_mae: 0.0099 - learning_rate: 0.0041
Epoch 14/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.2703e-04 - mae: 0.0095 - val_loss: 8.1036e-05 - val_mae: 0.0085 - learning_rate: 0.0021
Epoch 15/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.2520e-04 - mae: 0.0093 - val_loss: 6.9320e-05 - val_mae: 0.0073 - learning_rate: 0.0021
Epoch 16/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.2067e-04 - mae: 0.0092 - val_loss: 5.2056e-05 - val_mae: 0.0046 - learning_rate: 0.0021
Epoch 17/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.1662e-04 - mae: 0.0092 - val_loss: 8.4469e-05 - val_mae: 0.0092 - learning_rate: 0.0021
Epoch 18/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 1.1402e-04 - mae: 0.0092 - val_loss: 5.3823e-05 - val_mae: 0.0040 - learning_rate: 0.0021
Epoch 19/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 9.8560e-05 - mae: 0.0083 - val_loss: 4.5592e-05 - val_mae: 0.0051 - learning_rate: 0.0010
Epoch 20/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 9.8293e-05 - mae: 0.0082 - val_loss: 4.5364e-05 - val_mae: 0.0049 - learning_rate: 0.0010
Epoch 21/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 9.5821e-05 - mae: 0.0083 - val_loss: 4.0955e-05 - val_mae: 0.0042 - learning_rate: 0.0010
Epoch 22/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 8.5071e-05 - mae: 0.0079 - val_loss: 3.6926e-05 - val_mae: 0.0038 - learning_rate: 0.0010
Epoch 23/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 9.3654e-05 - mae: 0.0081 - val_loss: 4.7498e-05 - val_mae: 0.0061 - learning_rate: 0.0010
Epoch 24/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.7295e-05 - mae: 0.0076 - val_loss: 3.5652e-05 - val_mae: 0.0039 - learning_rate: 5.1648e-04
Epoch 25/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 8.1205e-05 - mae: 0.0077 - val_loss: 3.5340e-05 - val_mae: 0.0040 - learning_rate: 5.1648e-04
Epoch 26/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.9519e-05 - mae: 0.0076 - val_loss: 3.3783e-05 - val_mae: 0.0038 - learning_rate: 5.1648e-04
Epoch 27/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 8.3218e-05 - mae: 0.0078 - val_loss: 3.3893e-05 - val_mae: 0.0039 - learning_rate: 5.1648e-04
Epoch 28/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 8.2856e-05 - mae: 0.0078 - val_loss: 3.7778e-05 - val_mae: 0.0045 - learning_rate: 5.1648e-04
Epoch 29/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 8.1744e-05 - mae: 0.0076 - val_loss: 3.1605e-05 - val_mae: 0.0038 - learning_rate: 2.5824e-04
Epoch 30/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.3165e-05 - mae: 0.0072 - val_loss: 3.1850e-05 - val_mae: 0.0038 - learning_rate: 2.5824e-04
Epoch 31/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.4117e-05 - mae: 0.0073 - val_loss: 3.1598e-05 - val_mae: 0.0038 - learning_rate: 2.5824e-04
Epoch 32/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 8.8020e-05 - mae: 0.0076 - val_loss: 3.8364e-05 - val_mae: 0.0048 - learning_rate: 2.5824e-04
Epoch 33/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.2452e-05 - mae: 0.0073 - val_loss: 4.1319e-05 - val_mae: 0.0053 - learning_rate: 2.5824e-04
Epoch 34/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.9039e-05 - mae: 0.0071 - val_loss: 3.2345e-05 - val_mae: 0.0041 - learning_rate: 1.2912e-04
Epoch 35/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.0146e-05 - mae: 0.0072 - val_loss: 3.3009e-05 - val_mae: 0.0042 - learning_rate: 1.2912e-04
Epoch 36/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.0245e-05 - mae: 0.0071 - val_loss: 3.1106e-05 - val_mae: 0.0041 - learning_rate: 1.2912e-04
Epoch 37/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.0257e-05 - mae: 0.0072 - val_loss: 3.1513e-05 - val_mae: 0.0040 - learning_rate: 1.2912e-04
Epoch 38/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.8350e-05 - mae: 0.0070 - val_loss: 3.0209e-05 - val_mae: 0.0039 - learning_rate: 1.2912e-04
Epoch 39/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 8.3547e-05 - mae: 0.0072 - val_loss: 3.0854e-05 - val_mae: 0.0040 - learning_rate: 6.4560e-05
Epoch 40/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.2400e-05 - mae: 0.0071 - val_loss: 2.9529e-05 - val_mae: 0.0037 - learning_rate: 6.4560e-05
Epoch 41/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.4073e-05 - mae: 0.0069 - val_loss: 2.9258e-05 - val_mae: 0.0037 - learning_rate: 6.4560e-05
Epoch 42/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.5838e-05 - mae: 0.0070 - val_loss: 2.9054e-05 - val_mae: 0.0037 - learning_rate: 6.4560e-05
Epoch 43/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.0313e-05 - mae: 0.0070 - val_loss: 2.9163e-05 - val_mae: 0.0037 - learning_rate: 6.4560e-05
Epoch 44/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.0101e-05 - mae: 0.0071 - val_loss: 2.8841e-05 - val_mae: 0.0037 - learning_rate: 6.4560e-05
Epoch 45/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.8816e-05 - mae: 0.0071 - val_loss: 2.8675e-05 - val_mae: 0.0037 - learning_rate: 6.4560e-05
Epoch 46/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.4251e-05 - mae: 0.0069 - val_loss: 2.8767e-05 - val_mae: 0.0037 - learning_rate: 3.2280e-05
Epoch 47/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.3158e-05 - mae: 0.0069 - val_loss: 2.9648e-05 - val_mae: 0.0038 - learning_rate: 3.2280e-05
Epoch 48/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.4270e-05 - mae: 0.0069 - val_loss: 2.8902e-05 - val_mae: 0.0037 - learning_rate: 3.2280e-05
Epoch 49/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.2356e-05 - mae: 0.0068 - val_loss: 2.9181e-05 - val_mae: 0.0038 - learning_rate: 3.2280e-05
Epoch 50/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.6547e-05 - mae: 0.0069 - val_loss: 2.8695e-05 - val_mae: 0.0037 - learning_rate: 3.2280e-05
Epoch 51/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.0234e-05 - mae: 0.0067 - val_loss: 2.9130e-05 - val_mae: 0.0038 - learning_rate: 1.6140e-05
Epoch 52/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.3895e-05 - mae: 0.0069 - val_loss: 2.8748e-05 - val_mae: 0.0037 - learning_rate: 1.6140e-05
Epoch 53/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.2657e-05 - mae: 0.0068 - val_loss: 2.9734e-05 - val_mae: 0.0039 - learning_rate: 1.6140e-05
Epoch 54/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.9419e-05 - mae: 0.0068 - val_loss: 2.8744e-05 - val_mae: 0.0037 - learning_rate: 1.6140e-05
Epoch 55/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.0539e-05 - mae: 0.0068 - val_loss: 2.8263e-05 - val_mae: 0.0037 - learning_rate: 1.6140e-05
Epoch 56/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.0298e-05 - mae: 0.0068 - val_loss: 2.9675e-05 - val_mae: 0.0039 - learning_rate: 8.0700e-06
Epoch 57/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.4799e-05 - mae: 0.0067 - val_loss: 2.9589e-05 - val_mae: 0.0039 - learning_rate: 8.0700e-06
Epoch 58/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.7056e-05 - mae: 0.0069 - val_loss: 2.8803e-05 - val_mae: 0.0037 - learning_rate: 8.0700e-06
Epoch 59/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.3120e-05 - mae: 0.0068 - val_loss: 2.9058e-05 - val_mae: 0.0038 - learning_rate: 8.0700e-06
Epoch 60/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.5512e-05 - mae: 0.0069 - val_loss: 2.9056e-05 - val_mae: 0.0038 - learning_rate: 8.0700e-06
Epoch 61/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.6107e-05 - mae: 0.0068 - val_loss: 2.9655e-05 - val_mae: 0.0039 - learning_rate: 4.0350e-06
Epoch 62/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.1988e-05 - mae: 0.0068 - val_loss: 2.9478e-05 - val_mae: 0.0039 - learning_rate: 4.0350e-06
Epoch 63/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.4365e-05 - mae: 0.0068 - val_loss: 2.9044e-05 - val_mae: 0.0038 - learning_rate: 4.0350e-06
Epoch 64/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.8052e-05 - mae: 0.0068 - val_loss: 2.9246e-05 - val_mae: 0.0038 - learning_rate: 4.0350e-06
Epoch 65/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.2301e-05 - mae: 0.0068 - val_loss: 2.8845e-05 - val_mae: 0.0038 - learning_rate: 4.0350e-06
Epoch 66/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.3675e-05 - mae: 0.0069 - val_loss: 2.9359e-05 - val_mae: 0.0038 - learning_rate: 2.0175e-06
Epoch 67/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.2341e-05 - mae: 0.0068 - val_loss: 2.8623e-05 - val_mae: 0.0037 - learning_rate: 2.0175e-06
Epoch 68/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.3251e-05 - mae: 0.0069 - val_loss: 2.9224e-05 - val_mae: 0.0038 - learning_rate: 2.0175e-06
Epoch 69/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.5100e-05 - mae: 0.0069 - val_loss: 2.8827e-05 - val_mae: 0.0038 - learning_rate: 2.0175e-06
Epoch 70/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.6786e-05 - mae: 0.0068 - val_loss: 2.8537e-05 - val_mae: 0.0037 - learning_rate: 2.0175e-06
Epoch 71/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.9285e-05 - mae: 0.0069 - val_loss: 2.8691e-05 - val_mae: 0.0037 - learning_rate: 1.0087e-06
Epoch 72/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.2495e-05 - mae: 0.0068 - val_loss: 2.8928e-05 - val_mae: 0.0038 - learning_rate: 1.0087e-06
Epoch 73/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.3063e-05 - mae: 0.0068 - val_loss: 2.8745e-05 - val_mae: 0.0038 - learning_rate: 1.0087e-06
Epoch 74/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 7.0096e-05 - mae: 0.0069 - val_loss: 2.8655e-05 - val_mae: 0.0037 - learning_rate: 1.0087e-06
Epoch 75/300
884/884 ━━━━━━━━━━━━━━━━━━━━ 18s 20ms/step - loss: 6.6190e-05 - mae: 0.0069 - val_loss: 2.9064e-05 - val_mae: 0.0038 - learning_rate: 1.0087e-06
2025-01-31 02:29:45,755 - INFO - Evaluating final LSTM model...
95/95 ━━━━━━━━━━━━━━━━━━━━ 2s 13ms/step
2025-01-31 02:29:47,478 - INFO - Test MSE: 0.0765
2025-01-31 02:29:47,479 - INFO - Test RMSE: 0.2765
2025-01-31 02:29:47,479 - INFO - Test MAE: 0.1770
2025-01-31 02:29:47,479 - INFO - Test R2 Score: 0.9937
2025-01-31 02:29:47,479 - INFO - Directional Accuracy: 0.4823
First 40 Actual vs. Predicted Prices:
+-------+--------------+-------------------+
| Index | Actual Price | Predicted Price |
+-------+--------------+-------------------+
| 0 | 65.26 | 64.37000274658203 |
| 1 | 65.12 | 64.76000213623047 |
| 2 | 65.32 | 64.98999786376953 |
| 3 | 65.29 | 65.0999984741211 |
| 4 | 65.26 | 65.04000091552734 |
| 5 | 65.29 | 65.16000366210938 |
| 6 | 65.26 | 65.19999694824219 |
| 7 | 65.48 | 65.06999969482422 |
| 8 | 65.29 | 65.08999633789062 |
| 9 | 65.25 | 65.04000091552734 |
| 10 | 65.35 | 65.0999984741211 |
| 11 | 65.14 | 65.05000305175781 |
| 12 | 65.2 | 65.0199966430664 |
| 13 | 65.21 | 65.01000213623047 |
| 14 | 65.1 | 64.94000244140625 |
| 15 | 65.45 | 64.87000274658203 |
| 16 | 65.26 | 65.13999938964844 |
| 17 | 65.24 | 65.08999633789062 |
| 18 | 65.43 | 65.12000274658203 |
| 19 | 65.22 | 65.18000030517578 |
| 20 | 65.34 | 65.16999816894531 |
| 21 | 65.13 | 65.20999908447266 |
| 22 | 65.05 | 65.01000213623047 |
| 23 | 64.94 | 65.05000305175781 |
| 24 | 64.94 | 64.91000366210938 |
| 25 | 64.85 | 64.83000183105469 |
| 26 | 64.98 | 64.83000183105469 |
| 27 | 64.93 | 64.80999755859375 |
| 28 | 64.86 | 64.80999755859375 |
| 29 | 64.71 | 64.81999969482422 |
| 30 | 64.89 | 64.56999969482422 |
| 31 | 64.89 | 64.7699966430664 |
| 32 | 64.97 | 64.83000183105469 |
| 33 | 65.03 | 64.79000091552734 |
| 34 | 64.99 | 64.95999908447266 |
| 35 | 64.95 | 64.8499984741211 |
| 36 | 64.89 | 64.88999938964844 |
| 37 | 64.87 | 64.8499984741211 |
| 38 | 64.72 | 64.87000274658203 |
| 39 | 64.63 | 64.70999908447266 |
+-------+--------------+-------------------+
2025-01-31 02:30:07,570 - WARNING - You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`.
2025-01-31 02:30:07,639 - INFO - Saved best LSTM model and scaler objects (best_lstm_model.h5, scaler_features.pkl, scaler_target.pkl).
2025-01-31 02:30:07,640 - INFO - Starting DQN hyperparameter tuning with Optuna using 54 parallel trials...
[I 2025-01-31 02:30:07,640] A new study created in memory with name: no-name-7f6e13ed-f0e1-4c91-bfa6-ff8fbfdd7d46
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py:753: FutureWarning: suggest_loguniform has been deprecated in v3.0.0. This feature will be removed in v6.0.0. See https://github.com/optuna/optuna/releases/tag/v3.0.0. Use suggest_float(..., log=True) instead.
lr = trial.suggest_loguniform("lr", 1e-5, 1e-2)
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py:753: FutureWarning: suggest_loguniform has been deprecated in v3.0.0. This feature will be removed in v6.0.0. See https://github.com/optuna/optuna/releases/tag/v3.0.0. Use suggest_float(..., log=True) instead.
lr = trial.suggest_loguniform("lr", 1e-5, 1e-2)
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py:753: FutureWarning: suggest_loguniform has been deprecated in v3.0.0. This feature will be removed in v6.0.0. See https://github.com/optuna/optuna/releases/tag/v3.0.0. Use suggest_float(..., log=True) instead.
lr = trial.suggest_loguniform("lr", 1e-5, 1e-2)
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py:753: FutureWarning: suggest_loguniform has been deprecated in v3.0.0. This feature will be removed in v6.0.0. See https://github.com/optuna/optuna/releases/tag/v3.0.0. Use suggest_float(..., log=True) instead.
lr = trial.suggest_loguniform("lr", 1e-5, 1e-2)
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/stable_baselines3/common/vec_env/patch_gym.py:49: UserWarning: You provided an OpenAI Gym environment. We strongly recommend transitioning to Gymnasium environments. Stable-Baselines3 is automatically wrapping your environments in a compatibility layer, which could potentially cause issues.
warnings.warn(
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/stable_baselines3/common/vec_env/patch_gym.py:49: UserWarning: You provided an OpenAI Gym environment. We strongly recommend transitioning to Gymnasium environments. Stable-Baselines3 is automatically wrapping your environments in a compatibility layer, which could potentially cause issues.
warnings.warn(
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/stable_baselines3/common/vec_env/patch_gym.py:49: UserWarning: You provided an OpenAI Gym environment. We strongly recommend transitioning to Gymnasium environments. Stable-Baselines3 is automatically wrapping your environments in a compatibility layer, which could potentially cause issues.
warnings.warn(
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/stable_baselines3/common/vec_env/patch_gym.py:49: UserWarning: You provided an OpenAI Gym environment. We strongly recommend transitioning to Gymnasium environments. Stable-Baselines3 is automatically wrapping your environments in a compatibility layer, which could potentially cause issues.
warnings.warn(
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/stable_baselines3/common/vec_env/patch_gym.py:49: UserWarning: You provided an OpenAI Gym environment. We strongly recommend transitioning to Gymnasium environments. Stable-Baselines3 is automatically wrapping your environments in a compatibility layer, which could potentially cause issues.
warnings.warn(
Exception ignored in: <function Variable.__del__ at 0x79927f66a8e0>
Traceback (most recent call last):
File "/home/kleinpanic/.pyenv/versions/3.11.4/lib/python3.11/tkinter/__init__.py", line 410, in __del__
if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: main thread is not in main loop
Exception ignored in: <function Variable.__del__ at 0x79927f66a8e0>
Traceback (most recent call last):
File "/home/kleinpanic/.pyenv/versions/3.11.4/lib/python3.11/tkinter/__init__.py", line 410, in __del__
if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: main thread is not in main loop
Exception ignored in: <function Variable.__del__ at 0x79927f66a8e0>
Traceback (most recent call last):
File "/home/kleinpanic/.pyenv/versions/3.11.4/lib/python3.11/tkinter/__init__.py", line 410, in __del__
if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: main thread is not in main loop
Exception ignored in: <function Variable.__del__ at 0x79927f66a8e0>
Traceback (most recent call last):
File "/home/kleinpanic/.pyenv/versions/3.11.4/lib/python3.11/tkinter/__init__.py", line 410, in __del__
if self._tk.getboolean(self._tk.call("info", "exists", self._name)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: main thread is not in main loop
Exception ignored in: <function Image.__del__ at 0x79920e1fc9a0>
Traceback (most recent call last):
File "/home/kleinpanic/.pyenv/versions/3.11.4/lib/python3.11/tkinter/__init__.py", line 4082, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread
zsh: IOT instruction (core dumped) python3 LSTMDQN.py BAT.csv

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 91 KiB

View File

@@ -0,0 +1,347 @@
(venv) kleinpanic@kleinpanic:~/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src$ py LSTMDQN.py BAT.csv
2025-01-31 22:41:37.524313: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1738363297.545380 3148462 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1738363297.551750 3148462 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2025-01-31 22:41:37.573675: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2025-01-31 22:41:43,005 - INFO - ===== Resource Statistics =====
2025-01-31 22:41:43,005 - INFO - Physical CPU Cores: 28
2025-01-31 22:41:43,005 - INFO - Logical CPU Cores: 56
2025-01-31 22:41:43,005 - INFO - CPU Usage per Core: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
2025-01-31 22:41:43,005 - INFO - No GPUs detected.
2025-01-31 22:41:43,005 - INFO - =================================
2025-01-31 22:41:43,006 - INFO - Configured TensorFlow to use CPU with optimized thread settings.
2025-01-31 22:41:43,006 - INFO - Loading data from: BAT.csv
2025-01-31 22:41:44,326 - INFO - Data columns after renaming: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
2025-01-31 22:41:44,339 - INFO - Data loaded and sorted successfully.
2025-01-31 22:41:44,339 - INFO - Calculating technical indicators...
2025-01-31 22:41:44,370 - INFO - Technical indicators calculated successfully.
2025-01-31 22:41:44,379 - INFO - Starting parallel feature engineering with 54 workers...
2025-01-31 22:41:53,902 - INFO - Parallel feature engineering completed.
2025-01-31 22:41:54,028 - INFO - Scaled training features shape: (14134, 15, 17)
2025-01-31 22:41:54,028 - INFO - Scaled validation features shape: (3028, 15, 17)
2025-01-31 22:41:54,028 - INFO - Scaled testing features shape: (3030, 15, 17)
2025-01-31 22:41:54,028 - INFO - Scaled training target shape: (14134,)
2025-01-31 22:41:54,028 - INFO - Scaled validation target shape: (3028,)
2025-01-31 22:41:54,029 - INFO - Scaled testing target shape: (3030,)
2025-01-31 22:41:54,029 - INFO - Starting LSTM hyperparameter optimization with Optuna using 54 parallel trials...
[I 2025-01-31 22:41:54,029] A new study created in memory with name: no-name-58aeb7f7-b8be-4643-9d01-0d7bcf35db2e
/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py:370: FutureWarning: suggest_loguniform has been deprecated in v3.0.0. This feature will be removed in v6.0.0. See https://github.com/optuna/optuna/releases/tag/v3.0.0. Use suggest_float(..., log=True) instead.
learning_rate = trial.suggest_loguniform('learning_rate', 1e-5, 1e-2)
[W 2025-01-31 22:41:54,037] Trial 0 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 128, 'dropout_rate': 0.3458004047482393, 'learning_rate': 0.00032571516657639116, 'optimizer': 'Adam', 'decay': 5.1271378208025266e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,040] Trial 1 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 64, 'dropout_rate': 0.41366725075244426, 'learning_rate': 1.4215518116455374e-05, 'optimizer': 'Adam', 'decay': 2.4425472693131955e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,041] Trial 0 failed with value None.
[W 2025-01-31 22:41:54,044] Trial 2 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 64, 'dropout_rate': 0.4338960746358078, 'learning_rate': 0.0008904040106011442, 'optimizer': 'Nadam', 'decay': 5.346913345250019e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,045] Trial 1 failed with value None.
[W 2025-01-31 22:41:54,048] Trial 3 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 64, 'dropout_rate': 0.12636442800548273, 'learning_rate': 0.00021216094172774624, 'optimizer': 'Adam', 'decay': 6.289573710217091e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,051] Trial 4 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 96, 'dropout_rate': 0.4118163224442708, 'learning_rate': 0.0001753425558060621, 'optimizer': 'Nadam', 'decay': 1.0106893106530013e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,053] Trial 5 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 96, 'dropout_rate': 0.22600776619683294, 'learning_rate': 4.6020052773101484e-05, 'optimizer': 'Nadam', 'decay': 1.401502701741485e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,054] Trial 2 failed with value None.
[W 2025-01-31 22:41:54,059] Trial 6 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 96, 'dropout_rate': 0.49745444543788064, 'learning_rate': 0.004560559624417403, 'optimizer': 'Adam', 'decay': 9.80562105055051e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,060] Trial 3 failed with value None.
[W 2025-01-31 22:41:54,064] Trial 7 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 32, 'dropout_rate': 0.11175568582439271, 'learning_rate': 0.000970072556392495, 'optimizer': 'Adam', 'decay': 5.792236253956584e-06} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,065] Trial 4 failed with value None.
[W 2025-01-31 22:41:54,069] Trial 8 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 128, 'dropout_rate': 0.4128314285072633, 'learning_rate': 0.000545928656752339, 'optimizer': 'Adam', 'decay': 8.349182110406793e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,095] Trial 8 failed with value None.
[W 2025-01-31 22:41:54,073] Trial 5 failed with value None.
[W 2025-01-31 22:41:54,076] Trial 10 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 96, 'dropout_rate': 0.312090359026424, 'learning_rate': 0.004334434878981849, 'optimizer': 'Nadam', 'decay': 8.946685227991797e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,078] Trial 11 failed with parameters: {'num_lstm_layers': 3, 'lstm_units': 32, 'dropout_rate': 0.3176109191721788, 'learning_rate': 0.0010138486071155559, 'optimizer': 'Nadam', 'decay': 2.864596673239629e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,084] Trial 6 failed with value None.
[W 2025-01-31 22:41:54,084] Trial 12 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 96, 'dropout_rate': 0.23624224169024638, 'learning_rate': 0.0007065434808473306, 'optimizer': 'Adam', 'decay': 1.6045047417478787e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,088] Trial 7 failed with value None.
[W 2025-01-31 22:41:54,072] Trial 9 failed with parameters: {'num_lstm_layers': 3, 'lstm_units': 64, 'dropout_rate': 0.32982534569008337, 'learning_rate': 0.00044815992336546054, 'optimizer': 'Nadam', 'decay': 1.2045464023339681e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,097] Trial 10 failed with value None.
[W 2025-01-31 22:41:54,101] Trial 11 failed with value None.
[W 2025-01-31 22:41:54,104] Trial 12 failed with value None.
[W 2025-01-31 22:41:54,108] Trial 9 failed with value None.
[W 2025-01-31 22:41:54,126] Trial 13 failed with parameters: {'num_lstm_layers': 3, 'lstm_units': 32, 'dropout_rate': 0.4314674109696518, 'learning_rate': 0.00020500811974021594, 'optimizer': 'Nadam', 'decay': 9.329438318207097e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,126] Trial 13 failed with value None.
[W 2025-01-31 22:41:54,137] Trial 14 failed with parameters: {'num_lstm_layers': 3, 'lstm_units': 64, 'dropout_rate': 0.45933740233556053, 'learning_rate': 0.0016981825407295947, 'optimizer': 'Nadam', 'decay': 3.7526439477629106e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,138] Trial 14 failed with value None.
[W 2025-01-31 22:41:54,139] Trial 15 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 128, 'dropout_rate': 0.13179726561423677, 'learning_rate': 0.009702870830616994, 'optimizer': 'Nadam', 'decay': 1.5717160470745384e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,140] Trial 15 failed with value None.
[W 2025-01-31 22:41:54,142] Trial 16 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 64, 'dropout_rate': 0.1184952725205303, 'learning_rate': 0.0002901212127436873, 'optimizer': 'Adam', 'decay': 1.2671796687995818e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,143] Trial 16 failed with value None.
[W 2025-01-31 22:41:54,145] Trial 17 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 128, 'dropout_rate': 0.3911357548507932, 'learning_rate': 2.1174519659994443e-05, 'optimizer': 'Adam', 'decay': 7.113124525281298e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,146] Trial 18 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 128, 'dropout_rate': 0.194308829860494, 'learning_rate': 2.3684641389781485e-05, 'optimizer': 'Nadam', 'decay': 2.1823222065039084e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,146] Trial 17 failed with value None.
[W 2025-01-31 22:41:54,147] Trial 19 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 64, 'dropout_rate': 0.34952903992289974, 'learning_rate': 0.0001649975428188158, 'optimizer': 'Nadam', 'decay': 8.961070238582916e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,148] Trial 18 failed with value None.
[W 2025-01-31 22:41:54,150] Trial 19 failed with value None.
[W 2025-01-31 22:41:54,151] Trial 20 failed with parameters: {'num_lstm_layers': 3, 'lstm_units': 32, 'dropout_rate': 0.24862299600787863, 'learning_rate': 3.160302043940613e-05, 'optimizer': 'Nadam', 'decay': 4.432627646713297e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,152] Trial 22 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 128, 'dropout_rate': 0.24247452680935244, 'learning_rate': 0.009143026717679506, 'optimizer': 'Nadam', 'decay': 3.8695560131185495e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,154] Trial 23 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 96, 'dropout_rate': 0.27974565379013505, 'learning_rate': 0.0005552121580002416, 'optimizer': 'Adam', 'decay': 6.460942114176827e-06} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,155] Trial 20 failed with value None.
[W 2025-01-31 22:41:54,155] Trial 21 failed with parameters: {'num_lstm_layers': 3, 'lstm_units': 64, 'dropout_rate': 0.31566223075768207, 'learning_rate': 0.00013277190404539305, 'optimizer': 'Nadam', 'decay': 5.448184988496794e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,156] Trial 22 failed with value None.
[W 2025-01-31 22:41:54,157] Trial 24 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 64, 'dropout_rate': 0.20684570701871122, 'learning_rate': 2.02919005955524e-05, 'optimizer': 'Nadam', 'decay': 6.367297091468678e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,158] Trial 23 failed with value None.
[W 2025-01-31 22:41:54,158] Trial 25 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 64, 'dropout_rate': 0.14749229469818195, 'learning_rate': 1.6074589705354466e-05, 'optimizer': 'Nadam', 'decay': 2.9293835054420393e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,161] Trial 26 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 128, 'dropout_rate': 0.38879633341946584, 'learning_rate': 2.5036537142341482e-05, 'optimizer': 'Nadam', 'decay': 4.8346386929100394e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,161] Trial 21 failed with value None.
[W 2025-01-31 22:41:54,161] Trial 27 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 32, 'dropout_rate': 0.4311830196294676, 'learning_rate': 6.15743775325322e-05, 'optimizer': 'Adam', 'decay': 2.5290071255921133e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,162] Trial 28 failed with parameters: {'num_lstm_layers': 1, 'lstm_units': 32, 'dropout_rate': 0.14813081091496075, 'learning_rate': 0.0017948222377220397, 'optimizer': 'Adam', 'decay': 9.679895886200194e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,163] Trial 24 failed with value None.
[W 2025-01-31 22:41:54,164] Trial 29 failed with parameters: {'num_lstm_layers': 2, 'lstm_units': 64, 'dropout_rate': 0.4827525644514289, 'learning_rate': 0.000583829520138558, 'optimizer': 'Adam', 'decay': 3.9540551700479366e-05} because of the following error: NameError("name 'X_train' is not defined").
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 383, in lstm_objective
model_ = build_lstm((X_train.shape[1], X_train.shape[2]), hyperparams)
^^^^^^^
NameError: name 'X_train' is not defined
[W 2025-01-31 22:41:54,165] Trial 25 failed with value None.
[W 2025-01-31 22:41:54,166] Trial 26 failed with value None.
[W 2025-01-31 22:41:54,167] Trial 27 failed with value None.
[W 2025-01-31 22:41:54,168] Trial 28 failed with value None.
[W 2025-01-31 22:41:54,169] Trial 29 failed with value None.
Traceback (most recent call last):
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 897, in <module>
main()
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/LSTMDQN.py", line 685, in main
best_lstm_params = study_lstm.best_params
^^^^^^^^^^^^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/study.py", line 119, in best_params
return self.best_trial.params
^^^^^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/study/study.py", line 162, in best_trial
best_trial = self._storage.get_best_trial(self._study_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/kleinpanic/git-clones/MidasTechnologies/src/Machine-Learning/LSTM-python/src/venv/lib/python3.11/site-packages/optuna/storages/_in_memory.py", line 249, in get_best_trial
raise ValueError("No trials are completed yet.")
ValueError: No trials are completed yet.