Skip to content

Commit 7eb81d0

Browse files
Add penalties to phi2
1 parent c483923 commit 7eb81d0

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

n3fit/src/n3fit/hyper_optimization/rewards.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class HyperLoss:
149149
The ``compute_loss`` method saves intermediate metrics such as the
150150
chi2 of the folds or the phi regardless of the loss type that has been selected.
151151
These metrics are saved in the properties
152-
``phi_vector``: list of phi per fold
152+
``phi2_vector``: list of phi per fold
153153
``chi2_matrix``: list of chi2 per fold, per replica
154154
155155
@@ -186,7 +186,7 @@ def __init__(
186186
fold_statistic, "fold_statistic", default="average"
187187
)
188188

189-
self.phi_vector = []
189+
self.phi2_vector = []
190190
self.chi2_matrix = []
191191

192192
self.penalties = {}
@@ -204,7 +204,7 @@ def compute_loss(
204204
Compute the loss, including added penalties, for a single fold.
205205
206206
Save the phi of the assemble and the chi2 of the separate replicas,
207-
and the penalties into the ``phi_vector``, ``chi2_matrix`` and ``penalties`` attributes.
207+
and the penalties into the ``phi2_vector``, ``chi2_matrix`` and ``penalties`` attributes.
208208
209209
Parameters
210210
----------
@@ -246,19 +246,19 @@ def compute_loss(
246246
>>> loss = hyper.compute_loss(penalties, experimental_loss, pdf, experimental_data)
247247
"""
248248
# calculate phi for a given k-fold using vpinterface and validphys
249-
phi_per_fold = compute_phi(pdf_object, experimental_data)
249+
phi2_per_fold = compute_phi(pdf_object, experimental_data) ** 2
250250

251251
# update hyperopt metrics
252-
# these are saved in the phi_vector and chi2_matrix attributes, excluding penalties
253-
self._save_hyperopt_metrics(phi_per_fold, kfold_loss, penalties, fold_idx)
252+
# these are saved in the `phi2_vector` and `chi2_matrix` attributes, excluding penalties
253+
self._save_hyperopt_metrics(phi2_per_fold, kfold_loss, penalties, fold_idx)
254254

255255
# Prepare the output loss, including penalties if necessary
256256
if self._penalties_in_loss:
257257
# include penalties to experimental loss
258258
kfold_loss += sum(penalties.values())
259259

260260
# add penalties to phi in the form of a sum of per-replicas averages
261-
phi_per_fold += sum(np.mean(penalty) for penalty in penalties.values())
261+
phi2_per_fold += sum(np.mean(penalty) for penalty in penalties.values())
262262

263263
# define loss for hyperopt according to the chosen loss_type
264264
if self.loss_type == "chi2":
@@ -276,13 +276,13 @@ def compute_loss(
276276
kfold_loss_average = self.reduce_over_replicas(kfold_loss, proportion=0.1)
277277
loss = validation_loss_average + (max(kfold_loss_average, 2.0) - 2.0)
278278
elif self.loss_type == "phi2":
279-
loss = phi_per_fold**2
279+
loss = phi2_per_fold
280280

281281
return loss
282282

283283
def _save_hyperopt_metrics(
284284
self,
285-
phi_per_fold: float,
285+
phi2_per_fold: float,
286286
chi2_per_fold: np.ndarray,
287287
penalties: dict[str, np.ndarray],
288288
fold_idx: int = 0,
@@ -292,8 +292,8 @@ def _save_hyperopt_metrics(
292292
293293
Parameters
294294
----------
295-
phi_per_fold: float
296-
Computed phi for a given k-fold
295+
phi2_per_fold: float
296+
Computed phi2 for a given k-fold
297297
chi2_per_fold: np.ndarray
298298
Computed chi2 for each replica for a given k-fold
299299
penalties: Dict[str, np.ndarray]
@@ -303,13 +303,13 @@ def _save_hyperopt_metrics(
303303
"""
304304
# reset chi2 and phi arrays for every trial
305305
if fold_idx == 0:
306-
self.phi_vector = []
306+
self.phi2_vector = []
307307
self.chi2_matrix = []
308308
self.penalties = {}
309309

310310
# populate chi2 matrix and phi vector calculated for a given k-fold
311311
self.chi2_matrix.append(chi2_per_fold)
312-
self.phi_vector.append(phi_per_fold)
312+
self.phi2_vector.append(phi2_per_fold)
313313

314314
# save penalties per replica for a given k-fold
315315
for name, values in penalties.items():

n3fit/src/n3fit/model_trainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ def hyperparametrizable(self, params):
11291129
"trvl_losses_phi": np.array(trvl_phi_per_fold),
11301130
"experimental_losses": l_exper,
11311131
"hyper_losses": np.array(self._hyper_loss.chi2_matrix),
1132-
"hyper_losses_phi": np.array(self._hyper_loss.phi_vector),
1132+
"hyper_losses_phi": np.array(self._hyper_loss.phi2_vector),
11331133
"penalties": {
11341134
name: np.array(values)
11351135
for name, values in self._hyper_loss.penalties.items()

0 commit comments

Comments
 (0)