From c8e42389fb55273ccf1e574a2c474c20cf3d1bd3 Mon Sep 17 00:00:00 2001 From: TobiasKo Date: Thu, 16 Jun 2022 23:19:35 +0200 Subject: [PATCH 1/2] Added a timebudget decorator to the event_logging module. Decorater uses a logger to pprint the output, to be consistent with logging module. Decorated fit() and predict() functions. Other functions can be decorated if needed. Signed-off-by: TobiasKo --- src/proloaf/event_logging.py | 22 +++++ src/proloaf/modelhandler.py | 3 + targets/opsd_test/config.json | 83 ++++++++++++++++++ targets/opsd_test/preprocessing.json | 37 ++++++++ targets/opsd_test/tuning.json | 122 +++++++++++++++++++++++++++ 5 files changed, 267 insertions(+) create mode 100644 targets/opsd_test/config.json create mode 100644 targets/opsd_test/preprocessing.json create mode 100644 targets/opsd_test/tuning.json diff --git a/src/proloaf/event_logging.py b/src/proloaf/event_logging.py index 0d29622..5678162 100644 --- a/src/proloaf/event_logging.py +++ b/src/proloaf/event_logging.py @@ -1,10 +1,13 @@ import os import logging import logging.config +import functools +import time from proloaf.confighandler import read_config MAIN_PATH = os.path.dirname(os.path.abspath(__file__)) + def create_event_logger( name: str, config_path: os.path = os.path.join(MAIN_PATH, 'event_logging_conf.json'), @@ -56,3 +59,22 @@ def create_event_logger( logger = logging.getLogger(name) return logger + + +def timer(logger: logging.Logger): + """ + Decorator which passes the runtime of the decorated function to a logger. + The logger then prints the name and runtime of the function, if the logger is specified to print statements + of rank "INFO". + """ + def decorator(func): + @functools.wraps(func) + def wrapper_timer(*args, **kwargs): + start_time = time.perf_counter() + value = func(*args, **kwargs) + end_time = time.perf_counter() + run_time = end_time - start_time + logger.info(f"Finished {func.__name__!r} in {run_time:.4f} secs") + return value + return wrapper_timer + return decorator diff --git a/src/proloaf/modelhandler.py b/src/proloaf/modelhandler.py index 5b4b77c..eb9e82f 100644 --- a/src/proloaf/modelhandler.py +++ b/src/proloaf/modelhandler.py @@ -45,6 +45,7 @@ from proloaf.loghandler import ( end_tensorboard, ) from proloaf.event_logging import create_event_logger +from proloaf.event_logging import timer logger = create_event_logger(__name__) @@ -861,6 +862,7 @@ class ModelHandler: end_tensorboard(tb, hparams, values) return temp_model_wrap + @timer(logger) def fit( self, train_data: proloaf.tensorloader.TimeSeriesData, @@ -900,6 +902,7 @@ class ModelHandler: ) return self + @timer(logger) def predict(self, inputs_enc: torch.Tensor, inputs_dec: torch.Tensor): """ Get the predictions for the given model and data diff --git a/targets/opsd_test/config.json b/targets/opsd_test/config.json new file mode 100644 index 0000000..e5d5e37 --- /dev/null +++ b/targets/opsd_test/config.json @@ -0,0 +1,83 @@ +{ + "data_path": "./data/opsd.csv", + "output_path": "./oracles/", + "exploration_path": "./targets/opsd/tuning.json", + "evaluation_path": "./oracles/eval_opsd_recurrent/", + "log_path": "./logs/", + "model_name": "opsd_recurrent", + "target_id": [ + "DE_load_actual_entsoe_transparency" + ], + "target_list": null, + "start_date": null, + "history_horizon": 147, + "forecast_horizon": 24, + "cap_limit": 1, + "train_split": 0.6, + "validation_split": 0.8, + "periodicity": 24, + "optimizer_name": "adam", + "exploration": false, + "cuda_id": null, + "feature_groups": [ + { + "name": "main", + "scaler": [ + "minmax", + 0.0, + 1.0 + ], + "features": [ + "DE_load_actual_entsoe_transparency", + "DE_temperature", + "DE_radiation_direct_horizontal", + "DE_radiation_diffuse_horizontal" + ] + }, + { + "name": "aux", + "scaler": null, + "features": [ + "hour_sin", + "weekday_sin", + "mnth_sin", + "hour_cos", + "weekday_cos", + "mnth_cos" + ] + } + ], + "encoder_features": [ + "DE_load_actual_entsoe_transparency" + ], + "decoder_features": [ + "hour_sin", + "weekday_sin", + "mnth_sin", + "hour_cos", + "weekday_cos", + "mnth_cos" + ], + "max_epochs": 1, + "batch_size": 32, + "learning_rate": 9.9027931032814e-05, + "early_stopping_patience": 7, + "early_stopping_margin": 0.0, + "model_class": "recurrent", + "model_parameters": { + "recurrent": { + "core_net": "torch.nn.LSTM", + "core_layers": 1, + "dropout_fc": 0.4, + "dropout_core": 0.3, + "rel_linear_hidden_size": 1.0, + "rel_core_hidden_size": 1.0, + "relu_leak": 0.1 + }, + "simple_transformer": { + "num_layers": 3, + "dropout": 0.4, + "n_heads": 6 + } + } +} diff --git a/targets/opsd_test/preprocessing.json b/targets/opsd_test/preprocessing.json new file mode 100644 index 0000000..aab2023 --- /dev/null +++ b/targets/opsd_test/preprocessing.json @@ -0,0 +1,37 @@ +{ + "data_path": "data/opsd.csv", + "raw_path": "https://data.open-power-system-data.org/", + "local": false, + "csv_files": [ + { + "file_name": "time_series/2020-10-06/time_series_60min_singleindex.csv", + "date_column": "utc_timestamp", + "time_zone": "UTC", + "dayfirst": false, + "sep": ",", + "combine": false, + "use_columns": [ + "utc_timestamp", + "AT_load_actual_entsoe_transparency", + "DE_load_actual_entsoe_transparency" + ] + }, + { + "file_name": "weather_data/2020-09-16/weather_data.csv", + "date_column": "utc_timestamp", + "time_zone": "UTC", + "dayfirst": false, + "sep": ",", + "combine": false, + "use_columns": [ + "utc_timestamp", + "AT_temperature", + "AT_radiation_direct_horizontal", + "AT_radiation_diffuse_horizontal", + "DE_temperature", + "DE_radiation_direct_horizontal", + "DE_radiation_diffuse_horizontal" + ] + } + ] +} \ No newline at end of file diff --git a/targets/opsd_test/tuning.json b/targets/opsd_test/tuning.json new file mode 100644 index 0000000..d146df2 --- /dev/null +++ b/targets/opsd_test/tuning.json @@ -0,0 +1,122 @@ +{ + "number_of_tests": 2, + "settings": { + "learning_rate": { + "function": "suggest_loguniform", + "kwargs": { + "name": "learning_rate", + "low": 1e-06, + "high": 0.0001 + } + }, + "history_horizon": { + "function": "suggest_int", + "kwargs": { + "name": "history_horizon", + "low": 1, + "high": 168 + } + }, + "batch_size": { + "function": "suggest_int", + "kwargs": { + "name": "batch_size", + "low": 12, + "high": 120 + } + }, + "model_parameters": { + "recurrent": { + "core_net": { + "function": "suggest_categorical", + "kwargs": { + "name": "core_net", + "choices": [ + "torch.nn.LSTM", + "torch.nn.GRU", + "torch.nn.RNN" + ] + } + }, + "core_layers": { + "function": "suggest_int", + "kwargs": { + "name": "core_layers", + "low": 1, + "high": 5 + } + }, + "dropout_fc": { + "function": "suggest_float", + "kwargs": { + "name": "dropout_fc", + "low": 0.0, + "high": 1.0 + } + }, + "dropout_core": { + "function": "suggest_float", + "kwargs": { + "name": "dropout_core", + "low": 0.0, + "high": 1.0 + } + }, + "rel_linear_hidden_size": { + "function": "suggest_float", + "kwargs": { + "name": "rel_linear_hidden_size", + "low": 0.0, + "high": 1.0 + } + }, + "rel_core_hidden_size": { + "function": "suggest_float", + "kwargs": { + "name": "rel_core_hidden_size", + "low": 0.0, + "high": 1.0 + } + }, + "relu_leak": { + "function": "suggest_float", + "kwargs": { + "name": "relu_leak", + "low": 0.0, + "high": 0.2 + } + } + }, + "simple_transformer": { + "num_layers": { + "function": "suggest_int", + "kwargs": { + "name": "num_layers", + "low": 1, + "high": 10 + } + }, + "dropout": { + "function": "suggest_float", + "kwargs": { + "name": "dropout", + "low": 0.0, + "high": 0.2 + } + }, + "n_heads": { + "function": "suggest_categorical", + "kwargs": { + "name": "n_heads", + "choices": [ + 1, + 2, + 3, + 6 + ] + } + } + } + } + } +} \ No newline at end of file -- GitLab From f9c868f815be8df49495dd5d2712845e55a1340c Mon Sep 17 00:00:00 2001 From: TobiasKo Date: Mon, 20 Jun 2022 16:28:46 +0200 Subject: [PATCH 2/2] test target folder removed Signed-off-by: TobiasKo --- targets/opsd_test/config.json | 83 ------------------ targets/opsd_test/preprocessing.json | 37 -------- targets/opsd_test/tuning.json | 122 --------------------------- 3 files changed, 242 deletions(-) delete mode 100644 targets/opsd_test/config.json delete mode 100644 targets/opsd_test/preprocessing.json delete mode 100644 targets/opsd_test/tuning.json diff --git a/targets/opsd_test/config.json b/targets/opsd_test/config.json deleted file mode 100644 index e5d5e37..0000000 --- a/targets/opsd_test/config.json +++ /dev/null @@ -1,83 +0,0 @@ -{ - "data_path": "./data/opsd.csv", - "output_path": "./oracles/", - "exploration_path": "./targets/opsd/tuning.json", - "evaluation_path": "./oracles/eval_opsd_recurrent/", - "log_path": "./logs/", - "model_name": "opsd_recurrent", - "target_id": [ - "DE_load_actual_entsoe_transparency" - ], - "target_list": null, - "start_date": null, - "history_horizon": 147, - "forecast_horizon": 24, - "cap_limit": 1, - "train_split": 0.6, - "validation_split": 0.8, - "periodicity": 24, - "optimizer_name": "adam", - "exploration": false, - "cuda_id": null, - "feature_groups": [ - { - "name": "main", - "scaler": [ - "minmax", - 0.0, - 1.0 - ], - "features": [ - "DE_load_actual_entsoe_transparency", - "DE_temperature", - "DE_radiation_direct_horizontal", - "DE_radiation_diffuse_horizontal" - ] - }, - { - "name": "aux", - "scaler": null, - "features": [ - "hour_sin", - "weekday_sin", - "mnth_sin", - "hour_cos", - "weekday_cos", - "mnth_cos" - ] - } - ], - "encoder_features": [ - "DE_load_actual_entsoe_transparency" - ], - "decoder_features": [ - "hour_sin", - "weekday_sin", - "mnth_sin", - "hour_cos", - "weekday_cos", - "mnth_cos" - ], - "max_epochs": 1, - "batch_size": 32, - "learning_rate": 9.9027931032814e-05, - "early_stopping_patience": 7, - "early_stopping_margin": 0.0, - "model_class": "recurrent", - "model_parameters": { - "recurrent": { - "core_net": "torch.nn.LSTM", - "core_layers": 1, - "dropout_fc": 0.4, - "dropout_core": 0.3, - "rel_linear_hidden_size": 1.0, - "rel_core_hidden_size": 1.0, - "relu_leak": 0.1 - }, - "simple_transformer": { - "num_layers": 3, - "dropout": 0.4, - "n_heads": 6 - } - } -} diff --git a/targets/opsd_test/preprocessing.json b/targets/opsd_test/preprocessing.json deleted file mode 100644 index aab2023..0000000 --- a/targets/opsd_test/preprocessing.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "data_path": "data/opsd.csv", - "raw_path": "https://data.open-power-system-data.org/", - "local": false, - "csv_files": [ - { - "file_name": "time_series/2020-10-06/time_series_60min_singleindex.csv", - "date_column": "utc_timestamp", - "time_zone": "UTC", - "dayfirst": false, - "sep": ",", - "combine": false, - "use_columns": [ - "utc_timestamp", - "AT_load_actual_entsoe_transparency", - "DE_load_actual_entsoe_transparency" - ] - }, - { - "file_name": "weather_data/2020-09-16/weather_data.csv", - "date_column": "utc_timestamp", - "time_zone": "UTC", - "dayfirst": false, - "sep": ",", - "combine": false, - "use_columns": [ - "utc_timestamp", - "AT_temperature", - "AT_radiation_direct_horizontal", - "AT_radiation_diffuse_horizontal", - "DE_temperature", - "DE_radiation_direct_horizontal", - "DE_radiation_diffuse_horizontal" - ] - } - ] -} \ No newline at end of file diff --git a/targets/opsd_test/tuning.json b/targets/opsd_test/tuning.json deleted file mode 100644 index d146df2..0000000 --- a/targets/opsd_test/tuning.json +++ /dev/null @@ -1,122 +0,0 @@ -{ - "number_of_tests": 2, - "settings": { - "learning_rate": { - "function": "suggest_loguniform", - "kwargs": { - "name": "learning_rate", - "low": 1e-06, - "high": 0.0001 - } - }, - "history_horizon": { - "function": "suggest_int", - "kwargs": { - "name": "history_horizon", - "low": 1, - "high": 168 - } - }, - "batch_size": { - "function": "suggest_int", - "kwargs": { - "name": "batch_size", - "low": 12, - "high": 120 - } - }, - "model_parameters": { - "recurrent": { - "core_net": { - "function": "suggest_categorical", - "kwargs": { - "name": "core_net", - "choices": [ - "torch.nn.LSTM", - "torch.nn.GRU", - "torch.nn.RNN" - ] - } - }, - "core_layers": { - "function": "suggest_int", - "kwargs": { - "name": "core_layers", - "low": 1, - "high": 5 - } - }, - "dropout_fc": { - "function": "suggest_float", - "kwargs": { - "name": "dropout_fc", - "low": 0.0, - "high": 1.0 - } - }, - "dropout_core": { - "function": "suggest_float", - "kwargs": { - "name": "dropout_core", - "low": 0.0, - "high": 1.0 - } - }, - "rel_linear_hidden_size": { - "function": "suggest_float", - "kwargs": { - "name": "rel_linear_hidden_size", - "low": 0.0, - "high": 1.0 - } - }, - "rel_core_hidden_size": { - "function": "suggest_float", - "kwargs": { - "name": "rel_core_hidden_size", - "low": 0.0, - "high": 1.0 - } - }, - "relu_leak": { - "function": "suggest_float", - "kwargs": { - "name": "relu_leak", - "low": 0.0, - "high": 0.2 - } - } - }, - "simple_transformer": { - "num_layers": { - "function": "suggest_int", - "kwargs": { - "name": "num_layers", - "low": 1, - "high": 10 - } - }, - "dropout": { - "function": "suggest_float", - "kwargs": { - "name": "dropout", - "low": 0.0, - "high": 0.2 - } - }, - "n_heads": { - "function": "suggest_categorical", - "kwargs": { - "name": "n_heads", - "choices": [ - 1, - 2, - 3, - 6 - ] - } - } - } - } - } -} \ No newline at end of file -- GitLab