From c51e25a5be5678804bf6c3b50df9b995e3e2da35 Mon Sep 17 00:00:00 2001 From: henry-israel Date: Tue, 17 Sep 2024 09:43:45 +0100 Subject: [PATCH 1/6] Big update, now can use tensor flow as well --- MachineLearningMCMC/__init__.py | 5 ++ MachineLearningMCMC/__main__.py | 40 +--------- MachineLearningMCMC/config_reader.py | 73 +++++++++++++++++++ .../machine_learning/fml_interface.py | 30 +++++++- .../machine_learning/ml_factory.py | 35 ++++++--- .../machine_learning/scikit_interface.py | 19 ++--- .../machine_learning/tf_interface.py | 51 +++++++++++++ configs/scikit_config.yml | 22 ++++++ ...ummy_config.yaml => tensorflow_config.yml} | 0 9 files changed, 212 insertions(+), 63 deletions(-) create mode 100644 MachineLearningMCMC/config_reader.py create mode 100644 MachineLearningMCMC/machine_learning/tf_interface.py create mode 100644 configs/scikit_config.yml rename configs/{dummy_config.yaml => tensorflow_config.yml} (100%) diff --git a/MachineLearningMCMC/__init__.py b/MachineLearningMCMC/__init__.py index e69de29..7be3a6e 100644 --- a/MachineLearningMCMC/__init__.py +++ b/MachineLearningMCMC/__init__.py @@ -0,0 +1,5 @@ +from config_reader import ConfigReader +from machine_learning.ml_factory import MLFactory +from machine_learning.fml_interface import FmlInterface +from machine_learning.scikit_interface import SciKitInterface +from file_handling.chain_handler import ChainHandler \ No newline at end of file diff --git a/MachineLearningMCMC/__main__.py b/MachineLearningMCMC/__main__.py index c06bb40..69d8c72 100644 --- a/MachineLearningMCMC/__main__.py +++ b/MachineLearningMCMC/__main__.py @@ -1,8 +1,5 @@ -import yaml import argparse - -from file_handling.chain_handler import ChainHandler -from machine_learning.ml_factory import MLFactory +from config_reader import ConfigReader if __name__=="__main__": @@ -11,36 +8,7 @@ args = parser.parse_args() - with open(args.config, 'r') as c: - yaml_config = yaml.safe_load(c) - - # Process MCMC chain - file_handler = ChainHandler(yaml_config["FileSettings"]["FileName"], - yaml_config["FileSettings"]["ChainName"], - yaml_config["FileSettings"]["Verbose"]) - - file_handler.ignore_plots(yaml_config["FileSettings"]["IgnoredParameters"]) - file_handler.add_additional_plots(yaml_config["FileSettings"]["ParameterNames"]) - file_handler.add_additional_plots(yaml_config["FileSettings"]["LabelName"], True) - - file_handler.add_new_cuts(yaml_config["FileSettings"]["ParameterCuts"]) - - file_handler.convert_ttree_to_array() - - factory = MLFactory(file_handler, yaml_config["FileSettings"]["LabelName"]) - if yaml_config["FitterSettings"]["FitterPackage"].lower() == "scikit": - interface = factory.setup_scikit_model(yaml_config["FitterSettings"]["FitterName"], - **yaml_config["FitterSettings"]["FitterKwargs"]) - - else: - raise ValueError("Input not recognised!") - - if yaml_config["FitterSettings"].get("AddFromExternalModel"): - external_model = yaml_config["FitterSettings"]["ExternalModel"] - interface.load_model(external_model) - - interface.set_training_test_set(yaml_config["FitterSettings"]["TestSize"]) + config_reader = ConfigReader(args.config) + config_reader() - interface.train_model() - interface.test_model() - interface.save_model(yaml_config["FileSettings"]["ModelOutputName"]) \ No newline at end of file + \ No newline at end of file diff --git a/MachineLearningMCMC/config_reader.py b/MachineLearningMCMC/config_reader.py new file mode 100644 index 0000000..3876dd9 --- /dev/null +++ b/MachineLearningMCMC/config_reader.py @@ -0,0 +1,73 @@ +import yaml + +from file_handling.chain_handler import ChainHandler +from machine_learning.ml_factory import MLFactory +from machine_learning.fml_interface import FmlInterface + + +class ConfigReader: + + # Strictly unecessary but nice conceptually + _file_handler = None + _interface = None + + def __init__(self, config: str): + with open(config, 'r') as c: + self._yaml_config = yaml.safe_load(c) + + + def setup_file_handler(self)->None: + # Process MCMC chain + self._file_handler = ChainHandler(self._yaml_config["FileSettings"]["FileName"], + self._yaml_config["FileSettings"]["ChainName"], + self._yaml_config["FileSettings"]["Verbose"]) + + self._file_handler.ignore_plots(self._yaml_config["FileSettings"]["IgnoredParameters"]) + self._file_handler.add_additional_plots(self._yaml_config["FileSettings"]["ParameterNames"]) + self._file_handler.add_additional_plots(self._yaml_config["FileSettings"]["LabelName"], True) + + self._file_handler.add_new_cuts(self._yaml_config["FileSettings"]["ParameterCuts"]) + + self._file_handler.convert_ttree_to_array() + + + def setup_ml_interface(self)->None: + if self._file_handler is None: + raise Exception("Cannot initialise ML interface without first setting up file handler!") + + + factory = MLFactory(self._file_handler, self._yaml_config["FileSettings"]["LabelName"]) + if self._yaml_config["FitterSettings"]["FitterPackage"].lower() == "scikit": + self._interface = factory.setup_scikit_model(self._yaml_config["FitterSettings"]["FitterName"], + **self._yaml_config["FitterSettings"]["FitterKwargs"]) + + elif self._yaml_config["FitterSettings"]["FitterPackage"].lower() == "tensorflow": + self._interface = factory.setup_tensorflow_model(self._yaml_config["FitterSettings"]["FitterName"], + **self._yaml_config["FitterSettings"]["FitterKwargs"]) + + else: + raise ValueError("Input not recognised!") + + if self._yaml_config["FitterSettings"].get("AddFromExternalModel"): + external_model = self._yaml_config["FitterSettings"]["ExternalModel"] + self._interface.load_model(external_model) + + else: + self._interface.set_training_test_set(self._yaml_config["FitterSettings"]["TestSize"]) + + self._interface.train_model() + self._interface.test_model() + self._interface.save_model(self._yaml_config["FileSettings"]["ModelOutputName"]) + + def __call__(self) -> None: + self.setup_file_handler() + self.setup_ml_interface() + + + @property + def chain_handler(self)->ChainHandler | None: + return self._file_handler + + @property + def ml_interface(self)->FmlInterface | None: + return self._interface \ No newline at end of file diff --git a/MachineLearningMCMC/machine_learning/fml_interface.py b/MachineLearningMCMC/machine_learning/fml_interface.py index 2ad1956..deffd5d 100644 --- a/MachineLearningMCMC/machine_learning/fml_interface.py +++ b/MachineLearningMCMC/machine_learning/fml_interface.py @@ -10,8 +10,6 @@ import matplotlib.pyplot as plt import scipy.stats as stats import numpy as np -from astropy.visualization import LogStretch -from astropy.visualization.mpl_normalize import ImageNormalize import pickle @@ -60,6 +58,10 @@ def model(self)->Any: # Returns model being used return self._model + @property + def training_data(self)->pd.DataFrame: + return self._training_data + def add_model(self, ml_model: Any)->None: # Add ML model into your interface self._model = ml_model @@ -89,6 +91,20 @@ def load_model(self, input_file: str): print(f"Attempting to load file from {input_file}") with open(input_file, 'r') as f: self._model = pickle.load(f) + + def test_model(self): + + if self._model is None: + raise ValueError("No Model has been set!") + + if self._test_data is None or self._test_labels is None: + raise ValueError("No test data set") + + prediction = self.model_predict(self._test_data) + test_as_numpy = self._test_labels.to_numpy().T[0] + + self.evaluate_model(prediction, test_as_numpy) + def evaluate_model(self, predicted_values, true_values, outfile: str=""): print(f"Mean Absolute Error : {metrics.mean_absolute_error(predicted_values,true_values)}") @@ -124,4 +140,12 @@ def evaluate_model(self, predicted_values, true_values, outfile: str=""): print(f"Saving QQ to {outfile}") - fig.savefig(outfile) \ No newline at end of file + fig.savefig(outfile) + plt.close() + + # Gonna draw a hist + difs = true_values-predicted_values + print(f"mean: {np.mean(difs)}, std dev: {np.std(difs)}") + plt.hist(difs, bins=100, density=True, range=(np.std(difs)*-5, np.std(difs)*5)) + plt.xlabel("True - Pred") + plt.savefig(f"diffs_5sigma_range_{outfile}") \ No newline at end of file diff --git a/MachineLearningMCMC/machine_learning/ml_factory.py b/MachineLearningMCMC/machine_learning/ml_factory.py index 262e27c..532a1a8 100644 --- a/MachineLearningMCMC/machine_learning/ml_factory.py +++ b/MachineLearningMCMC/machine_learning/ml_factory.py @@ -5,12 +5,11 @@ from typing import Any, Dict from machine_learning.scikit_interface import SciKitInterface +from machine_learning.tf_interface import TfInterface import sklearn.ensemble as ske +import tensorflow.keras as tfk from file_handling.chain_handler import ChainHandler -from functools import partial - - class MLFactory: # Implement algorithms here @@ -22,7 +21,8 @@ class MLFactory: "histboost" : ske.HistGradientBoostingRegressor }, "tensorflow": - { + { + "sequential" : tfk.Sequential } } @@ -37,18 +37,20 @@ def __setup_package_factory(self, package: str, algorithm: str, **kwargs): Rough method for setting up a package """ + package = package.lower() - if package not in self.__IMPLEMENTED_ALGORITHMS: + if package not in self.__IMPLEMENTED_ALGORITHMS.keys(): raise ValueError(f"{package} not included, currently accepted packages are :\n \ {list(self.__IMPLEMENTED_ALGORITHMS.keys())}") algorithm = algorithm.lower() + print(package) - if algorithm not in self.__IMPLEMENTED_ALGORITHMS[package]: + if algorithm not in self.__IMPLEMENTED_ALGORITHMS[package].keys(): raise ValueError(f"{algorithm} not implemented for {package}, currently accepted algorithms for {package} are:\n \ {list(self.__IMPLEMENTED_ALGORITHMS[package].keys())}") - return self.__IMPLEMENTED_ALGORITHMS[package][algorithm](**kwargs) + return self.__IMPLEMENTED_ALGORITHMS[package][algorithm](*kwargs) def setup_scikit_model(self, algorithm: str, **kwargs)->SciKitInterface: # Simple wrapper for scikit packages @@ -56,7 +58,18 @@ def setup_scikit_model(self, algorithm: str, **kwargs)->SciKitInterface: interface.add_model(self.__setup_package_factory(package="scikit", algorithm=algorithm, **kwargs)) return interface - def setup_tensorflow_model(self, algorithm: str, network_structure: Dict[str, Any], **kwargs): - model = self.__setup_package_factory(package="tesnorflow", algorithm=algorithm, kwargs=kwargs) - return model - \ No newline at end of file + def setup_tensorflow_model(self, algorithm: str, **kwargs): + interface = TfInterface(self._chain, self._prediction_variable) + + interface.add_model(self.__setup_package_factory(package="tensorflow", algorithm=algorithm)) + + + for layer in kwargs["Layers"]: + layer_id = list(layer.keys())[0] + + interface.add_layer(layer_id, layer[layer_id]) + + interface.build_model(kwargs["BuildSettings"]) + + interface.set_training_settings(kwargs["FitSettings"]) + return interface \ No newline at end of file diff --git a/MachineLearningMCMC/machine_learning/scikit_interface.py b/MachineLearningMCMC/machine_learning/scikit_interface.py index 30e3580..719f65e 100644 --- a/MachineLearningMCMC/machine_learning/scikit_interface.py +++ b/MachineLearningMCMC/machine_learning/scikit_interface.py @@ -4,6 +4,11 @@ from file_handling.chain_handler import ChainHandler from machine_learning.fml_interface import FmlInterface +""" +TODO: + - Add staged predict +""" + class SciKitInterface(FmlInterface): def __init__(self, chain: ChainHandler, prediction_variable: str) -> None: super().__init__(chain, prediction_variable) @@ -25,16 +30,4 @@ def model_predict(self, test_data: DataFrame): raise ValueError("No Model has been set!") return self._model.predict(test_data) - - def test_model(self): - - if self._model is None: - raise ValueError("No Model has been set!") - - if self._test_data is None or self._test_labels is None: - raise ValueError("No training data set") - - prediction = self.model_predict(self._test_data) - test_as_numpy = self._test_labels.to_numpy().T[0] - - self.evaluate_model(prediction, test_as_numpy) + \ No newline at end of file diff --git a/MachineLearningMCMC/machine_learning/tf_interface.py b/MachineLearningMCMC/machine_learning/tf_interface.py new file mode 100644 index 0000000..a5313bf --- /dev/null +++ b/MachineLearningMCMC/machine_learning/tf_interface.py @@ -0,0 +1,51 @@ +# Let's make a tensor flow interface! +from typing import Any +from machine_learning.fml_interface import FmlInterface +import tensorflow as tf +from file_handling.chain_handler import ChainHandler + +class TfInterface(FmlInterface): + __TF_LAYER_IMPLEMENTATIONS = { + "dense": tf.keras.layers.Dense, + "dropout": tf.keras.layers.Dropout + } + + def __init__(self, chain: ChainHandler, prediction_variable: str) -> None: + super().__init__(chain, prediction_variable) + + self._model = None + self._layers = [] + self._training_settings = {} + + + def add_layer(self, layer_id, layer_args): + if layer_id not in self.__TF_LAYER_IMPLEMENTATIONS.keys(): + raise ValueError(f"{layer_id} not implemented yet!") + + self._layers.append(self.__TF_LAYER_IMPLEMENTATIONS[layer_id.lower()](**layer_args)) + + def build_model(self, kwargs_dict): + if self._model is None or not self._layers: + raise ValueError("No model can be built! Please setup model and layers") + + for layer in self._layers: + self._model.add(layer) + + self._model.build() + + self._model.compile(**kwargs_dict) + + def set_training_settings(self, kwargs): + self._training_settings = kwargs + + def train_model(self): + self._model.fit(self._training_data, self._training_labels, **self._training_settings) + + def save_model(self, output_file: str): + self._model.export(output_file) + + def load_model(self, input_file: str): + self._model = tf.saved_model.load(input_file) + + def model_predict(self, testing_data): + return self._model.predict_on_batch(testing_data) diff --git a/configs/scikit_config.yml b/configs/scikit_config.yml new file mode 100644 index 0000000..2568d23 --- /dev/null +++ b/configs/scikit_config.yml @@ -0,0 +1,22 @@ +FileSettings: + FileName : "/users/php20hti/php20hti/public/chains/all_par_chain_without_adaption.root" + # FileName : "/users/php20hti/t2ksft/regeneration_times_project/inputs/markov_chains/oscpar_only_datafit.root" + ChainName : "posteriors" + # ChainName : "osc_posteriors" + ParameterNames : ["sin2th","delm2", "delta", "xsec", "sk", "nd"] + # ParameterNames : ["d", "th"] + LabelName : "LogL" + IgnoredParameters : ["LogL_systematic_xsec_cov", "Log", "LogL_systematic_nddet_cov", ] + ParameterCuts : ["step>10000"] #["LogL<12345678", "step>10000"] + Verbose : False + ModelOutputName: "tf_model_full.pkl" + +FitterSettings: + + FitterPackage : "SciKit" + FitterName : "histboost" + TestSize : 0.9 + + FitterKwargs: + max_iter: 10000 + diff --git a/configs/dummy_config.yaml b/configs/tensorflow_config.yml similarity index 100% rename from configs/dummy_config.yaml rename to configs/tensorflow_config.yml From 1a265ada3167c3062a72c7890b31cce68eefcc7f Mon Sep 17 00:00:00 2001 From: henry-israel Date: Tue, 17 Sep 2024 09:49:51 +0100 Subject: [PATCH 2/6] Updates readme --- README.md | 39 ++++++++++++++++++++++++++++++++++- configs/tensorflow_config.yml | 39 +++++++++++++++++++++++++++-------- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5f2e2c2..0dbf176 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ Very simple tool for predicting likelihoods from Markov Chains Using ML tools. C # Configs Configs are in YAML format +For all pacakges the initial setup is very similar: + ```yaml FileSettings: FileName : "/path/to/file" # Name of File @@ -27,8 +29,10 @@ FileSettings: # Where is the model being pickled? ModelOutputName : "histboost_model_full.pkl" +``` - +For scikit learn based pacakges the settings are then set in the following way, where FitterKwargs directly sets the keyword arguments for the scikit fitting tool being used +```yaml FitterSettings: # Package model is included in FitterPackage : "SciKit" @@ -44,8 +48,41 @@ FitterSettings: verbose: True max_iter: 10000 # n_estimators = 200 +``` + +For TensorFlow based packages the settings are more complex +```yaml +FitterSettings: + + FitterPackage : "TensorFlow" + FitterName : "Sequential" + TestSize : 0.9 + + FitterKwargs: + BuildSettings: + optimizer: 'adam' + loss: 'mse' + + FitSettings: + epochs: 20 + batch_size: 20 + + Layers: + - dense: + units: 128 + - dense: + units: 64 + - dropout: + rate: 0.5 + - dense: + units: 16 + - dense: + units: 1 ``` + +Here FitterKwargs is now split into sub-settings with `BuildSettings` being passed to the model `compile` method, `FitSettings` setting up training information, and `Layers` defining the types + kwargs of each layer in the model. New layers can be implemented in the `__TF_LAYER_IMPLEMENTATIONS` object which lives in `machine_learning/tf_interface` + # Executables Simply run `python MachineLearningMCMC -c /path/to/toml/config` and it'll automatically run the chain. diff --git a/configs/tensorflow_config.yml b/configs/tensorflow_config.yml index 3b6c641..f0551ec 100644 --- a/configs/tensorflow_config.yml +++ b/configs/tensorflow_config.yml @@ -7,16 +7,37 @@ FileSettings: # ParameterNames : ["d", "th"] LabelName : "LogL" IgnoredParameters : ["LogL_systematic_xsec_cov", "Log", "LogL_systematic_nddet_cov", ] - ParameterCuts : ["LogL<12345678", "step>10000"] + ParameterCuts : ["step>10000"] #["LogL<12345678", "step>10000"] Verbose : False - ModelOutputName: "histboost_model_full.pkl" - + ModelOutputName: "tf_model_full.pkl" FitterSettings: - FitterPackage : "SciKit" - FitterName : "HistBoost" - TestSize : 0.8 + + FitterPackage : "TensorFlow" + FitterName : "Sequential" + TestSize : 0.9 + FitterKwargs: - # n_jobs : 32 - verbose: True - max_iter: 10000 + BuildSettings: + optimizer: 'adam' + loss: 'mse' + + TrainSettings: + epochs: 20 + batch_size: 20 + + Layers: + - dense: + units: 128 + - dense: + units: 64 + - dropout: + rate: 0.5 + - dense: + units: 16 + - dense: + units: 1 + + FitSettings: + batch_size: 20 + epochs: 100 \ No newline at end of file From 04b3fb33ab379fcec8aa949f02c950fe03464992 Mon Sep 17 00:00:00 2001 From: henry-israel Date: Tue, 17 Sep 2024 09:50:18 +0100 Subject: [PATCH 3/6] Update requirements --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index c537162..7bf1e2e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ statsmodels mpl_scatter_density scipy astropy -pyyaml \ No newline at end of file +pyyaml +tensorflow # the biggun' \ No newline at end of file From 58f3945cdf7255a898a0059feea36a024b5c5696 Mon Sep 17 00:00:00 2001 From: henry-israel Date: Tue, 17 Sep 2024 10:02:21 +0100 Subject: [PATCH 4/6] remove debug print --- MachineLearningMCMC/machine_learning/fml_interface.py | 2 ++ MachineLearningMCMC/machine_learning/ml_factory.py | 1 - MachineLearningMCMC/machine_learning/tf_interface.py | 4 ++-- configs/tensorflow_config.yml | 4 ---- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/MachineLearningMCMC/machine_learning/fml_interface.py b/MachineLearningMCMC/machine_learning/fml_interface.py index deffd5d..f9804d4 100644 --- a/MachineLearningMCMC/machine_learning/fml_interface.py +++ b/MachineLearningMCMC/machine_learning/fml_interface.py @@ -109,6 +109,8 @@ def test_model(self): def evaluate_model(self, predicted_values, true_values, outfile: str=""): print(f"Mean Absolute Error : {metrics.mean_absolute_error(predicted_values,true_values)}") + print(predicted_values) + lobf = np.poly1d(np.polyfit(predicted_values, true_values, 1)) print(f"Line of best fit : y={lobf.c[0]}x + {lobf.c[1]}") diff --git a/MachineLearningMCMC/machine_learning/ml_factory.py b/MachineLearningMCMC/machine_learning/ml_factory.py index 532a1a8..2f1b236 100644 --- a/MachineLearningMCMC/machine_learning/ml_factory.py +++ b/MachineLearningMCMC/machine_learning/ml_factory.py @@ -44,7 +44,6 @@ def __setup_package_factory(self, package: str, algorithm: str, **kwargs): {list(self.__IMPLEMENTED_ALGORITHMS.keys())}") algorithm = algorithm.lower() - print(package) if algorithm not in self.__IMPLEMENTED_ALGORITHMS[package].keys(): raise ValueError(f"{algorithm} not implemented for {package}, currently accepted algorithms for {package} are:\n \ diff --git a/MachineLearningMCMC/machine_learning/tf_interface.py b/MachineLearningMCMC/machine_learning/tf_interface.py index a5313bf..c922889 100644 --- a/MachineLearningMCMC/machine_learning/tf_interface.py +++ b/MachineLearningMCMC/machine_learning/tf_interface.py @@ -36,7 +36,7 @@ def build_model(self, kwargs_dict): self._model.compile(**kwargs_dict) def set_training_settings(self, kwargs): - self._training_settings = kwargs + self._training_settings = kwargs def train_model(self): self._model.fit(self._training_data, self._training_labels, **self._training_settings) @@ -48,4 +48,4 @@ def load_model(self, input_file: str): self._model = tf.saved_model.load(input_file) def model_predict(self, testing_data): - return self._model.predict_on_batch(testing_data) + return self._model.predict_on_batch(testing_data).reshape(1,-1) diff --git a/configs/tensorflow_config.yml b/configs/tensorflow_config.yml index f0551ec..d43069b 100644 --- a/configs/tensorflow_config.yml +++ b/configs/tensorflow_config.yml @@ -22,10 +22,6 @@ FitterSettings: optimizer: 'adam' loss: 'mse' - TrainSettings: - epochs: 20 - batch_size: 20 - Layers: - dense: units: 128 From 4a4316c9bc32c46012e7f34e59d850e7a220eb9f Mon Sep 17 00:00:00 2001 From: henry-israel Date: Tue, 17 Sep 2024 10:15:06 +0100 Subject: [PATCH 5/6] Small tweaks to evaluate model output format to be consistent with scikit --- MachineLearningMCMC/machine_learning/fml_interface.py | 4 +++- MachineLearningMCMC/machine_learning/tf_interface.py | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/MachineLearningMCMC/machine_learning/fml_interface.py b/MachineLearningMCMC/machine_learning/fml_interface.py index f9804d4..86d99bc 100644 --- a/MachineLearningMCMC/machine_learning/fml_interface.py +++ b/MachineLearningMCMC/machine_learning/fml_interface.py @@ -107,9 +107,11 @@ def test_model(self): def evaluate_model(self, predicted_values, true_values, outfile: str=""): + print(predicted_values) + print(true_values) + print(f"Mean Absolute Error : {metrics.mean_absolute_error(predicted_values,true_values)}") - print(predicted_values) lobf = np.poly1d(np.polyfit(predicted_values, true_values, 1)) diff --git a/MachineLearningMCMC/machine_learning/tf_interface.py b/MachineLearningMCMC/machine_learning/tf_interface.py index c922889..c61bd67 100644 --- a/MachineLearningMCMC/machine_learning/tf_interface.py +++ b/MachineLearningMCMC/machine_learning/tf_interface.py @@ -25,6 +25,7 @@ def add_layer(self, layer_id, layer_args): self._layers.append(self.__TF_LAYER_IMPLEMENTATIONS[layer_id.lower()](**layer_args)) def build_model(self, kwargs_dict): + if self._model is None or not self._layers: raise ValueError("No model can be built! Please setup model and layers") @@ -42,10 +43,11 @@ def train_model(self): self._model.fit(self._training_data, self._training_labels, **self._training_settings) def save_model(self, output_file: str): - self._model.export(output_file) + self._model.save(output_file) def load_model(self, input_file: str): self._model = tf.saved_model.load(input_file) def model_predict(self, testing_data): - return self._model.predict_on_batch(testing_data).reshape(1,-1) + # Hacky but means it's consistent with sci-kit interface + return self._model.predict_on_batch(testing_data).T[0] From fe3e50c7c6c0dc307f8d96e030b2e107a2061003 Mon Sep 17 00:00:00 2001 From: henry-israel Date: Tue, 17 Sep 2024 10:15:19 +0100 Subject: [PATCH 6/6] Changes config file extension --- configs/tensorflow_config.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/configs/tensorflow_config.yml b/configs/tensorflow_config.yml index d43069b..7fbc6c3 100644 --- a/configs/tensorflow_config.yml +++ b/configs/tensorflow_config.yml @@ -9,7 +9,7 @@ FileSettings: IgnoredParameters : ["LogL_systematic_xsec_cov", "Log", "LogL_systematic_nddet_cov", ] ParameterCuts : ["step>10000"] #["LogL<12345678", "step>10000"] Verbose : False - ModelOutputName: "tf_model_full.pkl" + ModelOutputName: "tf_model_full.keras" FitterSettings: @@ -35,5 +35,5 @@ FitterSettings: units: 1 FitSettings: - batch_size: 20 - epochs: 100 \ No newline at end of file + batch_size: 100 + epochs: 10 \ No newline at end of file