Skip to content

Commit

Permalink
Update hbw/tasks/inference.py
Browse files Browse the repository at this point in the history
Co-authored-by: Mathis Frahm <[email protected]>
  • Loading branch information
Lara813 and mafrahm committed Jan 24, 2025
1 parent caccecd commit 7641039
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
27 changes: 9 additions & 18 deletions hbw/ml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,23 +335,14 @@ def open_model(self, target: law.LocalDirectoryTarget) -> dict[str, Any]:
models["parameters"] = yaml.load(f_in, Loader=yaml.Loader)

# custom loss needed due to output layer changes for negative weights
from hbw.ml.tf_util import cumulated_crossentropy, categorical_crossentropy
from hbw.ml.tf_util import cumulated_crossentropy

# Check for negative weight handling and assign loss function accordingly.
if self.negative_weights == "ignore":
models["model"] = tf.keras.models.load_model(
target["mlmodel"].path, custom_objects={categorical_crossentropy.__name__: categorical_crossentropy},
)
models["best_model"] = tf.keras.models.load_model(
target["checkpoint"].path, custom_objects={categorical_crossentropy.__name__: categorical_crossentropy},
)
else:
models["model"] = tf.keras.models.load_model(
target["mlmodel"].path, custom_objects={cumulated_crossentropy.__name__: cumulated_crossentropy},
)
models["best_model"] = tf.keras.models.load_model(
target["checkpoint"].path, custom_objects={cumulated_crossentropy.__name__: cumulated_crossentropy},
)
models["model"] = tf.keras.models.load_model(
target["mlmodel"].path, custom_objects={cumulated_crossentropy.__name__: cumulated_crossentropy},
)
models["best_model"] = tf.keras.models.load_model(
target["checkpoint"].path, custom_objects={cumulated_crossentropy.__name__: cumulated_crossentropy},
)

return models

Expand Down Expand Up @@ -553,7 +544,7 @@ def prepare_ml_model(

from keras.models import Sequential
from keras.layers import Dense, BatchNormalization
from hbw.ml.tf_util import cumulated_crossentropy, categorical_crossentropy
from hbw.ml.tf_util import cumulated_crossentropy

n_inputs = len(set(self.input_features))
n_outputs = len(self.processes)
Expand All @@ -576,7 +567,7 @@ def prepare_ml_model(
optimizer = keras.optimizers.Adam(learning_rate=0.00050)
if self.negative_weights == "ignore":
model.compile(
loss=categorical_crossentropy,
loss="categorical_crossentropy",
optimizer=optimizer,
weighted_metrics=["categorical_accuracy"],
)
Expand Down
2 changes: 1 addition & 1 deletion hbw/ml/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def prepare_ml_model(
import tensorflow.keras as keras
from keras.models import Sequential
from keras.layers import Dense, BatchNormalization
from hbw.ml.tf_util import cumulated_crossentropy # , categorical_crossentropy
from hbw.ml.tf_util import cumulated_crossentropy

n_inputs = len(set(self.input_features))
n_outputs = len(self.processes)
Expand Down
1 change: 0 additions & 1 deletion hbw/tasks/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

# Function copied from Mathis Hist hook commit
# TODO: define once at central place (hist_util.py)
# TODO: define once at central place (hist_util.py)
def apply_rebinning_edges(h: hist.Histogram, axis_name: str, edges: list):
"""
Generalized rebinning of a single axis from a hist.Histogram, using predefined edges.
Expand Down

0 comments on commit 7641039

Please sign in to comment.