From 62791cf976ba3f91dd001c0facdc1983b69d2587 Mon Sep 17 00:00:00 2001 From: juacrumar Date: Thu, 26 Jan 2023 12:23:31 +0100 Subject: [PATCH 1/4] fix penalties and add tests --- .../src/n3fit/hyper_optimization/penalties.py | 18 ++++----- n3fit/src/n3fit/tests/test_penalties.py | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 n3fit/src/n3fit/tests/test_penalties.py diff --git a/n3fit/src/n3fit/hyper_optimization/penalties.py b/n3fit/src/n3fit/hyper_optimization/penalties.py index a17067edaf..d7b35b877f 100644 --- a/n3fit/src/n3fit/hyper_optimization/penalties.py +++ b/n3fit/src/n3fit/hyper_optimization/penalties.py @@ -20,7 +20,7 @@ """ import numpy as np from validphys import fitveto -from n3fit.vpinterface import N3PDF +from n3fit.vpinterface import N3PDF, integrability_numbers def saturation(pdf_models=None, n=100, min_x=1e-6, max_x=1e-4, flavors=None, **_kwargs): @@ -43,8 +43,8 @@ def saturation(pdf_models=None, n=100, min_x=1e-6, max_x=1e-4, flavors=None, **_ ------- >>> from n3fit.hyper_optimization.penalties import saturation >>> from n3fit.model_gen import pdfNN_layer_generator - >>> fake_fl = [{'fl' : i, 'largex' : [0,1], 'smallx': [1,2]} for i in ['u', 'ubar', 'd', 'dbar', 'c', 'cbar', 's', 'sbar']] - >>> pdf_model = pdfNN_layer_generator(nodes=[8], activations=['linear'], seed=0, flav_info=fake_fl) + >>> fake_fl = [{'fl' : i, 'largex' : [0,1], 'smallx': [1,2]} for i in ['u', 'ubar', 'd', 'dbar', 'c', 'g', 's', 'sbar']] + >>> pdf_model = pdfNN_layer_generator(nodes=[8], activations=['linear'], seed=0, flav_info=fake_fl, fitbasis="FLAVOUR") >>> isinstance(saturation(pdf_model, 5), float) True @@ -82,7 +82,7 @@ def patience(stopping_object=None, alpha=1e-4, **_kwargs): >>> from n3fit.hyper_optimization.penalties import patience >>> from types import SimpleNamespace >>> fake_stopping = SimpleNamespace(e_best_chi2=1000, stopping_patience=500, total_epochs=5000, vl_loss=2.42) - >>> patience(None, fake_stopping, alpha=1e-4) + >>> patience(fake_stopping, alpha=1e-4) 3.434143467595683 """ @@ -90,7 +90,7 @@ def patience(stopping_object=None, alpha=1e-4, **_kwargs): patience = stopping_object.stopping_patience max_epochs = stopping_object.total_epochs diff = abs(max_epochs - patience - epoch_best) - vl_loss = np.take(stopping_object.vl_chi2, 0) + vl_loss = np.take(stopping_object.vl_loss, 0) return vl_loss * np.exp(alpha * diff) @@ -105,14 +105,14 @@ def integrability(pdf_models=None, **_kwargs): ------- >>> from n3fit.hyper_optimization.penalties import integrability >>> from n3fit.model_gen import pdfNN_layer_generator - >>> fake_fl = [{'fl' : i, 'largex' : [0,1], 'smallx': [1,2]} for i in ['u', 'ubar', 'd', 'dbar', 'c', 'cbar', 's', 'sbar']] - >>> pdf_model = pdfNN_layer_generator(nodes=[8], activations=['linear'], seed=0, flav_info=fake_fl) - >>> isinstance(integrability([pdf_model], None), float) + >>> fake_fl = [{'fl' : i, 'largex' : [0,1], 'smallx': [1,2]} for i in ['u', 'ubar', 'd', 'dbar', 'c', 'g', 's', 'sbar']] + >>> pdf_model = pdfNN_layer_generator(nodes=[8], activations=['linear'], seed=0, flav_info=fake_fl, fitbasis="FLAVOUR") + >>> isinstance(integrability(pdf_model), float) True """ pdf_instance = N3PDF(pdf_models) - integ_values = pdf_instance.integrability_numbers() + integ_values = integrability_numbers(pdf_instance) integ_overflow = np.sum(integ_values[integ_values > fitveto.INTEG_THRESHOLD]) if integ_overflow > 50.0: # before reaching an overflow, just give a stupidly big number diff --git a/n3fit/src/n3fit/tests/test_penalties.py b/n3fit/src/n3fit/tests/test_penalties.py new file mode 100644 index 0000000000..f822f88b90 --- /dev/null +++ b/n3fit/src/n3fit/tests/test_penalties.py @@ -0,0 +1,40 @@ +""" + Test the penalties for n3fit hyperopt +""" +from types import SimpleNamespace +from n3fit.hyper_optimization.penalties import integrability, patience, saturation +from n3fit.model_gen import pdfNN_layer_generator + + +def test_saturation(): + """Check the saturation penalty runs and returns a float""" + fake_fl = [ + {"fl": i, "largex": [0, 1], "smallx": [1, 2]} + for i in ["u", "ubar", "d", "dbar", "c", "g", "s", "sbar"] + ] + pdf_model = pdfNN_layer_generator( + nodes=[8], activations=["linear"], seed=0, flav_info=fake_fl, fitbasis="FLAVOUR" + ) + assert isinstance(saturation(pdf_model, 5), float) + + +def test_patience(): + """Check that the patience penalty runs and returns a float""" + fake_stopping = SimpleNamespace( + e_best_chi2=1000, stopping_patience=500, total_epochs=5000, vl_loss=2.42 + ) + res = patience(stopping_object=fake_stopping, alpha=1e-4) + assert isinstance(res, float) + + +def test_integrability_numbers(): + """Check that the integrability penality can be run + and that it returns indeed a float""" + fake_fl = [ + {"fl": i, "largex": [0, 1], "smallx": [1, 2]} + for i in ["u", "ubar", "d", "dbar", "c", "g", "s", "sbar"] + ] + pdf_model = pdfNN_layer_generator( + nodes=[8], activations=["linear"], seed=0, flav_info=fake_fl, fitbasis="FLAVOUR" + ) + assert isinstance(integrability(pdf_model), float) From 8cefdf494df5d4f16f9f8f6dc97fed6981ce1b41 Mon Sep 17 00:00:00 2001 From: "Juan M. Cruz-Martinez" Date: Thu, 26 Jan 2023 13:21:41 +0100 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Roy Stegeman --- n3fit/src/n3fit/tests/test_penalties.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/n3fit/src/n3fit/tests/test_penalties.py b/n3fit/src/n3fit/tests/test_penalties.py index f822f88b90..ab6515513b 100644 --- a/n3fit/src/n3fit/tests/test_penalties.py +++ b/n3fit/src/n3fit/tests/test_penalties.py @@ -7,7 +7,7 @@ def test_saturation(): - """Check the saturation penalty runs and returns a float""" + """Check that the saturation penalty runs and returns a float""" fake_fl = [ {"fl": i, "largex": [0, 1], "smallx": [1, 2]} for i in ["u", "ubar", "d", "dbar", "c", "g", "s", "sbar"] @@ -28,8 +28,7 @@ def test_patience(): def test_integrability_numbers(): - """Check that the integrability penality can be run - and that it returns indeed a float""" + """Check that the integrability penalty runs and returns a float""" fake_fl = [ {"fl": i, "largex": [0, 1], "smallx": [1, 2]} for i in ["u", "ubar", "d", "dbar", "c", "g", "s", "sbar"] From 582727983bcfbcf9e9a10e5bfab0dd8d78036821 Mon Sep 17 00:00:00 2001 From: "Juan M. Cruz-Martinez" Date: Mon, 30 Jan 2023 12:41:43 +0100 Subject: [PATCH 3/4] Update n3fit/src/n3fit/hyper_optimization/penalties.py --- n3fit/src/n3fit/hyper_optimization/penalties.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/n3fit/src/n3fit/hyper_optimization/penalties.py b/n3fit/src/n3fit/hyper_optimization/penalties.py index d7b35b877f..6e2e2ab12f 100644 --- a/n3fit/src/n3fit/hyper_optimization/penalties.py +++ b/n3fit/src/n3fit/hyper_optimization/penalties.py @@ -90,7 +90,7 @@ def patience(stopping_object=None, alpha=1e-4, **_kwargs): patience = stopping_object.stopping_patience max_epochs = stopping_object.total_epochs diff = abs(max_epochs - patience - epoch_best) - vl_loss = np.take(stopping_object.vl_loss, 0) + vl_loss = np.take(stopping_object.vl_chi2, 0) return vl_loss * np.exp(alpha * diff) From ed21dc0c557ad4ac59ef44625b97a69b37040257 Mon Sep 17 00:00:00 2001 From: "Juan M. Cruz-Martinez" Date: Mon, 30 Jan 2023 12:41:59 +0100 Subject: [PATCH 4/4] Update n3fit/src/n3fit/tests/test_penalties.py --- n3fit/src/n3fit/tests/test_penalties.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/n3fit/src/n3fit/tests/test_penalties.py b/n3fit/src/n3fit/tests/test_penalties.py index ab6515513b..3899f1a5a8 100644 --- a/n3fit/src/n3fit/tests/test_penalties.py +++ b/n3fit/src/n3fit/tests/test_penalties.py @@ -21,7 +21,7 @@ def test_saturation(): def test_patience(): """Check that the patience penalty runs and returns a float""" fake_stopping = SimpleNamespace( - e_best_chi2=1000, stopping_patience=500, total_epochs=5000, vl_loss=2.42 + e_best_chi2=1000, stopping_patience=500, total_epochs=5000, vl_chi2=2.42 ) res = patience(stopping_object=fake_stopping, alpha=1e-4) assert isinstance(res, float)