From e0f0035324635725b45a7a4ba08b6a18c06084b5 Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 11:17:07 +0200 Subject: [PATCH 01/20] Add preprocessing regression test --- n3fit/src/n3fit/tests/test_preprocessing.py | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 n3fit/src/n3fit/tests/test_preprocessing.py diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py new file mode 100644 index 0000000000..6ef2079a7f --- /dev/null +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -0,0 +1,29 @@ +from n3fit.layers import Preprocessing +import tensorflow as tf +import numpy as np + + +def test_preprocessing(): + """Regression test""" + # taken from basic runcard + flav_info = [ + {'fl': 'sng', 'smallx': [1.05, 1.19], 'largex': [1.47, 2.7]}, # 'trainable': False}, + {'fl': 'g', 'smallx': [0.94, 1.25], 'largex': [0.11, 5.87]}, # 'trainable': False}, + {'fl': 'v', 'smallx': [0.54, 0.75], 'largex': [1.15, 2.76]}, # 'trainable': False}, + {'fl': 'v3', 'smallx': [0.21, 0.57], 'largex': [1.35, 3.08]}, + {'fl': 'v8', 'smallx': [0.52, 0.76], 'largex': [0.77, 3.56]}, + {'fl': 't3', 'smallx': [-0.37, 1.52], 'largex': [1.74, 3.39]}, + {'fl': 't8', 'smallx': [0.56, 1.29], 'largex': [1.45, 3.03]}, + {'fl': 'cp', 'smallx': [0.12, 1.19], 'largex': [1.83, 6.7]}, + ] + prepro = Preprocessing(flav_info=flav_info, seed=0) + test_x = tf.random.uniform(shape=(1, 4, 1), seed=42) + test_prefactors = tf.constant([[ + [4.28409985e-04, 2.33196355e-02, 2.19706148e-02, 3.12583987e-04, 1.52197943e-04, 6.52114686e-05, 1.36401606e-04, 1.18874144e-04], + [5.75779974e-02, 2.65057504e-01, 2.13224307e-01, 3.90783511e-02, 3.33565399e-02, 1.79548096e-02, 3.96815017e-02, 2.73224674e-02], + [1.78151987e-02, 1.46395922e-01, 1.27500281e-01, 1.30333444e-02, 9.50834434e-03, 5.17322961e-03, 1.01000341e-02, 7.87989516e-03], + [2.80656964e-02, 1.83944702e-01, 1.56266689e-01, 2.01078728e-02, 1.55397197e-02, 8.49795807e-03, 1.71364844e-02, 1.28620844e-02], + ]]) + prefactors = prepro(test_x) + assert np.allclose(test_prefactors, prefactors) + From 9cc19072b2c89ab793f478dfc4b32fce318cab49 Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 11:25:11 +0200 Subject: [PATCH 02/20] Change kernel to alphas and betas in preprocessing --- n3fit/src/n3fit/layers/preprocessing.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index e5d45c7038..9edf9cde96 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -45,7 +45,8 @@ def __init__( self.output_dim = len(flav_info) self.initializer = initializer self.large_x = large_x - self.kernel = [] + self.alphas = [] + self.betas = [] super().__init__(**kwargs) def generate_weight(self, weight_name, kind, dictionary, set_to_zero=False): @@ -90,22 +91,22 @@ def generate_weight(self, weight_name, kind, dictionary, set_to_zero=False): trainable=trainable, constraint=weight_constraint, ) - self.kernel.append(newpar) + return newpar def build(self, input_shape): # Run through the whole basis for flav_dict in self.flav_info: flav_name = flav_dict["fl"] alpha_name = f"alpha_{flav_name}" - self.generate_weight(alpha_name, "smallx", flav_dict) + self.alphas.append(self.generate_weight(alpha_name, "smallx", flav_dict)) beta_name = f"beta_{flav_name}" - self.generate_weight(beta_name, "largex", flav_dict, set_to_zero=not self.large_x) + self.betas.append(self.generate_weight(beta_name, "largex", flav_dict, set_to_zero=not self.large_x)) super(Preprocessing, self).build(input_shape) def call(self, inputs, **kwargs): x = inputs pdf_list = [] - for i in range(0, self.output_dim * 2, 2): - pdf_list.append(x ** (1 - self.kernel[i][0]) * (1 - x) ** self.kernel[i + 1][0]) + for i in range(0, self.output_dim): + pdf_list.append(x ** (1 - self.alphas[i][0]) * (1 - x) ** self.betas[i][0]) return op.concatenate(pdf_list, axis=-1) From 05710236e273cbdc0f9e212e60ced56a2ed8d7ba Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 11:28:49 +0200 Subject: [PATCH 03/20] Simplify preprocessing call by stacking alphas and betas and using broadcasting --- n3fit/src/n3fit/layers/preprocessing.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index 9edf9cde96..fb201d2849 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -104,9 +104,8 @@ def build(self, input_shape): super(Preprocessing, self).build(input_shape) - def call(self, inputs, **kwargs): - x = inputs - pdf_list = [] - for i in range(0, self.output_dim): - pdf_list.append(x ** (1 - self.alphas[i][0]) * (1 - x) ** self.betas[i][0]) - return op.concatenate(pdf_list, axis=-1) + def call(self, x): + alphas = op.stack(self.alphas, axis=1) + betas = op.stack(self.betas, axis=1) + + return x ** (1 - alphas) * (1 - x) ** betas From 5f34bc93de8d7903d826a4029d634c26790335ae Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 11:38:56 +0200 Subject: [PATCH 04/20] Improve documentation --- n3fit/src/n3fit/layers/preprocessing.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index fb201d2849..0df8799db1 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -51,8 +51,7 @@ def __init__( def generate_weight(self, weight_name, kind, dictionary, set_to_zero=False): """ - Generates weights according to the flavour dictionary and adds them - to the kernel list of the class + Generates weights according to the flavour dictionary Parameters ---------- @@ -105,6 +104,17 @@ def build(self, input_shape): super(Preprocessing, self).build(input_shape) def call(self, x): + """ + Compute preprocessing prefactor. + + Parameters + ---------- + x: tensor(shape=[1,N,1]) + + Returns + ------- + prefactor: tensor(shape=[1,N,F]) + """ alphas = op.stack(self.alphas, axis=1) betas = op.stack(self.betas, axis=1) From 9dc338f57896ad6873d0e58b4206d316bc35734d Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 11:41:56 +0200 Subject: [PATCH 05/20] Simplify notation --- n3fit/src/n3fit/layers/preprocessing.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index 0df8799db1..faf5e088c5 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -69,18 +69,16 @@ def generate_weight(self, weight_name, kind, dictionary, set_to_zero=False): initializer = MetaLayer.init_constant(0.0) trainable = False else: - limits = dictionary[kind] - ldo = limits[0] - lup = limits[1] + minval, maxval = dictionary[kind] trainable = dictionary.get("trainable", True) # Set the initializer and move the seed one up initializer = MetaLayer.select_initializer( - self.initializer, minval=ldo, maxval=lup, seed=self.seed + self.initializer, minval=minval, maxval=maxval, seed=self.seed ) self.seed += 1 # If we are training, constrain the weights to be within the limits if trainable: - weight_constraint = constraints.MinMaxWeight(ldo, lup) + weight_constraint = constraints.MinMaxWeight(minval, maxval) # Generate the new trainable (or not) parameter newpar = self.builder_helper( From 5f0801a38a6779f4980be1d7cf5548f5e06735ee Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 12:09:32 +0200 Subject: [PATCH 06/20] Remove initializer as argument, as it is never set differently from default :q --- n3fit/src/n3fit/layers/preprocessing.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index faf5e088c5..508455d4cb 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -34,7 +34,11 @@ class Preprocessing(MetaLayer): """ def __init__( - self, flav_info=None, seed=0, initializer="random_uniform", large_x=True, **kwargs, + self, + flav_info=None, + seed=0, + large_x=True, + **kwargs, ): if flav_info is None: raise ValueError( @@ -43,7 +47,7 @@ def __init__( self.flav_info = flav_info self.seed = seed self.output_dim = len(flav_info) - self.initializer = initializer + self.initializer = "random_uniform" self.large_x = large_x self.alphas = [] self.betas = [] From daf178899e2ef678ac8ed76ff4277c9b042899e4 Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 12:12:05 +0200 Subject: [PATCH 07/20] Add type hints --- n3fit/src/n3fit/layers/preprocessing.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index 508455d4cb..d1d3486689 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -35,9 +35,9 @@ class Preprocessing(MetaLayer): def __init__( self, - flav_info=None, - seed=0, - large_x=True, + flav_info: list = None, + seed: int = 0, + large_x: bool = True, **kwargs, ): if flav_info is None: @@ -53,7 +53,12 @@ def __init__( self.betas = [] super().__init__(**kwargs) - def generate_weight(self, weight_name, kind, dictionary, set_to_zero=False): + def generate_weight(self, + weight_name: str, + kind: str, + dictionary: dict, + set_to_zero: bool = False + ): """ Generates weights according to the flavour dictionary From f8569118e82438e5273b6ea817b4890ce83672c2 Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 12:19:43 +0200 Subject: [PATCH 08/20] Remove unused output_dim attribute --- n3fit/src/n3fit/layers/preprocessing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index d1d3486689..c9c9d40089 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -46,7 +46,6 @@ def __init__( ) self.flav_info = flav_info self.seed = seed - self.output_dim = len(flav_info) self.initializer = "random_uniform" self.large_x = large_x self.alphas = [] From 49981851380583ce8662aebd58e7cfae09d0138e Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Fri, 7 Jul 2023 12:21:34 +0200 Subject: [PATCH 09/20] Uniformize namings with builder_helper --- n3fit/src/n3fit/layers/preprocessing.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index c9c9d40089..f463a04ace 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -53,7 +53,7 @@ def __init__( super().__init__(**kwargs) def generate_weight(self, - weight_name: str, + name: str, kind: str, dictionary: dict, set_to_zero: bool = False @@ -63,7 +63,7 @@ def generate_weight(self, Parameters ---------- - weight_name: str + name: str name to be given to the generated weight kind: str where to find the limits of the weight in the dictionary @@ -72,7 +72,7 @@ def generate_weight(self, set_to_zero: bool set the weight to constant 0 """ - weight_constraint = None + constraint = None if set_to_zero: initializer = MetaLayer.init_constant(0.0) trainable = False @@ -86,15 +86,15 @@ def generate_weight(self, self.seed += 1 # If we are training, constrain the weights to be within the limits if trainable: - weight_constraint = constraints.MinMaxWeight(minval, maxval) + constraint = constraints.MinMaxWeight(minval, maxval) # Generate the new trainable (or not) parameter newpar = self.builder_helper( - name=weight_name, + name=name, kernel_shape=(1,), initializer=initializer, trainable=trainable, - constraint=weight_constraint, + constraint=constraint, ) return newpar From efa28fa0eb449910e0b16b9a49a1f736e529f490 Mon Sep 17 00:00:00 2001 From: Aron Date: Thu, 27 Jul 2023 13:11:27 +0200 Subject: [PATCH 10/20] Black, isort --- n3fit/src/n3fit/layers/preprocessing.py | 11 ++-- n3fit/src/n3fit/tests/test_preprocessing.py | 72 ++++++++++++++++----- 2 files changed, 60 insertions(+), 23 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index f463a04ace..6e7d834047 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -52,12 +52,7 @@ def __init__( self.betas = [] super().__init__(**kwargs) - def generate_weight(self, - name: str, - kind: str, - dictionary: dict, - set_to_zero: bool = False - ): + def generate_weight(self, name: str, kind: str, dictionary: dict, set_to_zero: bool = False): """ Generates weights according to the flavour dictionary @@ -105,7 +100,9 @@ def build(self, input_shape): alpha_name = f"alpha_{flav_name}" self.alphas.append(self.generate_weight(alpha_name, "smallx", flav_dict)) beta_name = f"beta_{flav_name}" - self.betas.append(self.generate_weight(beta_name, "largex", flav_dict, set_to_zero=not self.large_x)) + self.betas.append( + self.generate_weight(beta_name, "largex", flav_dict, set_to_zero=not self.large_x) + ) super(Preprocessing, self).build(input_shape) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index 6ef2079a7f..fba653e8a4 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -1,6 +1,7 @@ -from n3fit.layers import Preprocessing -import tensorflow as tf import numpy as np +import tensorflow as tf + +from n3fit.layers import Preprocessing def test_preprocessing(): @@ -8,22 +9,61 @@ def test_preprocessing(): # taken from basic runcard flav_info = [ {'fl': 'sng', 'smallx': [1.05, 1.19], 'largex': [1.47, 2.7]}, # 'trainable': False}, - {'fl': 'g', 'smallx': [0.94, 1.25], 'largex': [0.11, 5.87]}, # 'trainable': False}, - {'fl': 'v', 'smallx': [0.54, 0.75], 'largex': [1.15, 2.76]}, # 'trainable': False}, - {'fl': 'v3', 'smallx': [0.21, 0.57], 'largex': [1.35, 3.08]}, - {'fl': 'v8', 'smallx': [0.52, 0.76], 'largex': [0.77, 3.56]}, - {'fl': 't3', 'smallx': [-0.37, 1.52], 'largex': [1.74, 3.39]}, - {'fl': 't8', 'smallx': [0.56, 1.29], 'largex': [1.45, 3.03]}, - {'fl': 'cp', 'smallx': [0.12, 1.19], 'largex': [1.83, 6.7]}, + {'fl': 'g', 'smallx': [0.94, 1.25], 'largex': [0.11, 5.87]}, # 'trainable': False}, + {'fl': 'v', 'smallx': [0.54, 0.75], 'largex': [1.15, 2.76]}, # 'trainable': False}, + {'fl': 'v3', 'smallx': [0.21, 0.57], 'largex': [1.35, 3.08]}, + {'fl': 'v8', 'smallx': [0.52, 0.76], 'largex': [0.77, 3.56]}, + {'fl': 't3', 'smallx': [-0.37, 1.52], 'largex': [1.74, 3.39]}, + {'fl': 't8', 'smallx': [0.56, 1.29], 'largex': [1.45, 3.03]}, + {'fl': 'cp', 'smallx': [0.12, 1.19], 'largex': [1.83, 6.7]}, ] prepro = Preprocessing(flav_info=flav_info, seed=0) test_x = tf.random.uniform(shape=(1, 4, 1), seed=42) - test_prefactors = tf.constant([[ - [4.28409985e-04, 2.33196355e-02, 2.19706148e-02, 3.12583987e-04, 1.52197943e-04, 6.52114686e-05, 1.36401606e-04, 1.18874144e-04], - [5.75779974e-02, 2.65057504e-01, 2.13224307e-01, 3.90783511e-02, 3.33565399e-02, 1.79548096e-02, 3.96815017e-02, 2.73224674e-02], - [1.78151987e-02, 1.46395922e-01, 1.27500281e-01, 1.30333444e-02, 9.50834434e-03, 5.17322961e-03, 1.01000341e-02, 7.87989516e-03], - [2.80656964e-02, 1.83944702e-01, 1.56266689e-01, 2.01078728e-02, 1.55397197e-02, 8.49795807e-03, 1.71364844e-02, 1.28620844e-02], - ]]) + test_prefactors = tf.constant( + [ + [ + [ + 4.28409985e-04, + 2.33196355e-02, + 2.19706148e-02, + 3.12583987e-04, + 1.52197943e-04, + 6.52114686e-05, + 1.36401606e-04, + 1.18874144e-04, + ], + [ + 5.75779974e-02, + 2.65057504e-01, + 2.13224307e-01, + 3.90783511e-02, + 3.33565399e-02, + 1.79548096e-02, + 3.96815017e-02, + 2.73224674e-02, + ], + [ + 1.78151987e-02, + 1.46395922e-01, + 1.27500281e-01, + 1.30333444e-02, + 9.50834434e-03, + 5.17322961e-03, + 1.01000341e-02, + 7.87989516e-03, + ], + [ + 2.80656964e-02, + 1.83944702e-01, + 1.56266689e-01, + 2.01078728e-02, + 1.55397197e-02, + 8.49795807e-03, + 1.71364844e-02, + 1.28620844e-02, + ], + ] + ] + ) prefactors = prepro(test_x) assert np.allclose(test_prefactors, prefactors) - From 1f1a829ea907079a30f7f7bcdbdfa506e758ca28 Mon Sep 17 00:00:00 2001 From: Aron Date: Thu, 27 Jul 2023 13:14:36 +0200 Subject: [PATCH 11/20] Remove self.initializer --- n3fit/src/n3fit/layers/preprocessing.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index 6e7d834047..09befc8cb4 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -46,7 +46,6 @@ def __init__( ) self.flav_info = flav_info self.seed = seed - self.initializer = "random_uniform" self.large_x = large_x self.alphas = [] self.betas = [] @@ -76,7 +75,7 @@ def generate_weight(self, name: str, kind: str, dictionary: dict, set_to_zero: b trainable = dictionary.get("trainable", True) # Set the initializer and move the seed one up initializer = MetaLayer.select_initializer( - self.initializer, minval=minval, maxval=maxval, seed=self.seed + "random_uniform", minval=minval, maxval=maxval, seed=self.seed ) self.seed += 1 # If we are training, constrain the weights to be within the limits From 387d3040aa9fb1e45df6e896c4b44e2bb7e9a8a0 Mon Sep 17 00:00:00 2001 From: Aron Date: Thu, 27 Jul 2023 13:19:04 +0200 Subject: [PATCH 12/20] Remove tensorflow from test --- n3fit/src/n3fit/tests/test_preprocessing.py | 78 ++++++++++++--------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index fba653e8a4..3a8abbc0e2 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -1,5 +1,4 @@ import numpy as np -import tensorflow as tf from n3fit.layers import Preprocessing @@ -18,52 +17,61 @@ def test_preprocessing(): {'fl': 'cp', 'smallx': [0.12, 1.19], 'largex': [1.83, 6.7]}, ] prepro = Preprocessing(flav_info=flav_info, seed=0) - test_x = tf.random.uniform(shape=(1, 4, 1), seed=42) - test_prefactors = tf.constant( + np.random.seed(42) + test_x = np.random.uniform(size=(1, 4, 1)) + test_prefactors = np.array( [ [ [ - 4.28409985e-04, - 2.33196355e-02, - 2.19706148e-02, - 3.12583987e-04, - 1.52197943e-04, - 6.52114686e-05, - 1.36401606e-04, - 1.18874144e-04, + 4.65414822e-01, + 3.86829615e-01, + 3.37056696e-01, + 2.07052663e-01, + 2.88195640e-01, + 1.44142181e-01, + 2.54527658e-01, + 5.00672087e-02, ], [ - 5.75779974e-02, - 2.65057504e-01, - 2.13224307e-01, - 3.90783511e-02, - 3.33565399e-02, - 1.79548096e-02, - 3.96815017e-02, - 2.73224674e-02, + 4.95845545e-03, + 8.82980123e-04, + 4.71688760e-03, + 1.34412316e-03, + 2.12713517e-03, + 3.22611211e-03, + 1.32401168e-04, + 4.70521542e-08, ], [ - 1.78151987e-02, - 1.46395922e-01, - 1.27500281e-01, - 1.30333444e-02, - 9.50834434e-03, - 5.17322961e-03, - 1.01000341e-02, - 7.87989516e-03, + 9.99829322e-02, + 4.81918976e-02, + 8.90727192e-02, + 4.71593030e-02, + 6.22668676e-02, + 5.96290566e-02, + 2.02597082e-02, + 5.59301290e-04, ], [ - 2.80656964e-02, - 1.83944702e-01, - 1.56266689e-01, - 2.01078728e-02, - 1.55397197e-02, - 8.49795807e-03, - 1.71364844e-02, - 1.28620844e-02, + 2.06480861e-01, + 1.27716750e-01, + 1.73152477e-01, + 1.01946764e-01, + 1.33761078e-01, + 1.03049524e-01, + 6.74647838e-02, + 4.97586234e-03, ], ] ] ) prefactors = prepro(test_x) assert np.allclose(test_prefactors, prefactors) + + +def main(): + test_preprocessing() + + +if __name__ == '__main__': + main() From 920af7f3a419ed731053eb85c238b0eefddbf0b9 Mon Sep 17 00:00:00 2001 From: Aron Date: Thu, 27 Jul 2023 13:20:30 +0200 Subject: [PATCH 13/20] Uncomment trainable --- n3fit/src/n3fit/tests/test_preprocessing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index 3a8abbc0e2..ed8a34422c 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -7,9 +7,9 @@ def test_preprocessing(): """Regression test""" # taken from basic runcard flav_info = [ - {'fl': 'sng', 'smallx': [1.05, 1.19], 'largex': [1.47, 2.7]}, # 'trainable': False}, - {'fl': 'g', 'smallx': [0.94, 1.25], 'largex': [0.11, 5.87]}, # 'trainable': False}, - {'fl': 'v', 'smallx': [0.54, 0.75], 'largex': [1.15, 2.76]}, # 'trainable': False}, + {'fl': 'sng', 'smallx': [1.05, 1.19], 'largex': [1.47, 2.7], 'trainable': False}, + {'fl': 'g', 'smallx': [0.94, 1.25], 'largex': [0.11, 5.87], 'trainable': False}, + {'fl': 'v', 'smallx': [0.54, 0.75], 'largex': [1.15, 2.76], 'trainable': False}, {'fl': 'v3', 'smallx': [0.21, 0.57], 'largex': [1.35, 3.08]}, {'fl': 'v8', 'smallx': [0.52, 0.76], 'largex': [0.77, 3.56]}, {'fl': 't3', 'smallx': [-0.37, 1.52], 'largex': [1.74, 3.39]}, From 6c58dccb8a7a58ac7b3d7c9a242fdebd9a4f7fc2 Mon Sep 17 00:00:00 2001 From: Aron Date: Thu, 27 Jul 2023 14:42:37 +0200 Subject: [PATCH 14/20] Remove main function from test --- n3fit/src/n3fit/tests/test_preprocessing.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index ed8a34422c..4fa2eb60d9 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -67,11 +67,3 @@ def test_preprocessing(): ) prefactors = prepro(test_x) assert np.allclose(test_prefactors, prefactors) - - -def main(): - test_preprocessing() - - -if __name__ == '__main__': - main() From 746df3f081c933c4598c1b9a25fadbffacf7ecdf Mon Sep 17 00:00:00 2001 From: Aron Date: Thu, 27 Jul 2023 16:03:19 +0200 Subject: [PATCH 15/20] Convert test input to tensor --- n3fit/src/n3fit/tests/test_preprocessing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index 4fa2eb60d9..ca82863b62 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -1,5 +1,6 @@ import numpy as np +from n3fit.backends import operations as op from n3fit.layers import Preprocessing @@ -18,7 +19,7 @@ def test_preprocessing(): ] prepro = Preprocessing(flav_info=flav_info, seed=0) np.random.seed(42) - test_x = np.random.uniform(size=(1, 4, 1)) + test_x = op.numpy_to_tensor(np.random.uniform(size=(1, 4, 1))) test_prefactors = np.array( [ [ From 351d5c4da2b0b0c029b0cff0f5f8a5340f8a49b7 Mon Sep 17 00:00:00 2001 From: RoyStegeman Date: Thu, 27 Jul 2023 17:17:13 +0100 Subject: [PATCH 16/20] fix preproc test --- n3fit/src/n3fit/tests/test_preprocessing.py | 89 ++++++++++----------- 1 file changed, 42 insertions(+), 47 deletions(-) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index ca82863b62..6b14815b24 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -1,6 +1,5 @@ import numpy as np -from n3fit.backends import operations as op from n3fit.layers import Preprocessing @@ -19,52 +18,48 @@ def test_preprocessing(): ] prepro = Preprocessing(flav_info=flav_info, seed=0) np.random.seed(42) - test_x = op.numpy_to_tensor(np.random.uniform(size=(1, 4, 1))) - test_prefactors = np.array( + test_x = np.random.uniform(size=(1, 4, 1)) + test_prefactors = [ [ - [ - [ - 4.65414822e-01, - 3.86829615e-01, - 3.37056696e-01, - 2.07052663e-01, - 2.88195640e-01, - 1.44142181e-01, - 2.54527658e-01, - 5.00672087e-02, - ], - [ - 4.95845545e-03, - 8.82980123e-04, - 4.71688760e-03, - 1.34412316e-03, - 2.12713517e-03, - 3.22611211e-03, - 1.32401168e-04, - 4.70521542e-08, - ], - [ - 9.99829322e-02, - 4.81918976e-02, - 8.90727192e-02, - 4.71593030e-02, - 6.22668676e-02, - 5.96290566e-02, - 2.02597082e-02, - 5.59301290e-04, - ], - [ - 2.06480861e-01, - 1.27716750e-01, - 1.73152477e-01, - 1.01946764e-01, - 1.33761078e-01, - 1.03049524e-01, - 6.74647838e-02, - 4.97586234e-03, - ], - ] - ] - ) + 3.2668063e-01, + 6.7284244e-01, + 3.9915803e-01, + 1.5305418e-01, + 1.8249598e-01, + 7.2065055e-02, + 3.1497714e-01, + 1.3243365e-01, + ], + [ + 4.6502685e-04, + 2.4272988e-02, + 2.2857290e-02, + 3.3989837e-04, + 1.6686485e-04, + 7.2010938e-05, + 1.4990549e-04, + 1.3058851e-04, + ], + [ + 3.5664584e-02, + 2.0763232e-01, + 1.7362502e-01, + 2.5178187e-02, + 2.0087767e-02, + 1.0968268e-02, + 2.2659913e-02, + 1.6590673e-02, + ], + [ + 1.0150098e-01, + 3.5557139e-01, + 2.6867521e-01, + 6.4237528e-02, + 5.9941418e-02, + 3.0898115e-02, + 7.7342905e-02, + 4.8169162e-02, + ], + ] prefactors = prepro(test_x) assert np.allclose(test_prefactors, prefactors) From 02161d57f332bf6da298f22df63495b255328573 Mon Sep 17 00:00:00 2001 From: Aron Date: Mon, 7 Aug 2023 14:06:11 +0200 Subject: [PATCH 17/20] Add Optional typehint --- n3fit/src/n3fit/layers/preprocessing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/n3fit/src/n3fit/layers/preprocessing.py b/n3fit/src/n3fit/layers/preprocessing.py index 09befc8cb4..77ea760607 100644 --- a/n3fit/src/n3fit/layers/preprocessing.py +++ b/n3fit/src/n3fit/layers/preprocessing.py @@ -1,3 +1,5 @@ +from typing import Optional + from n3fit.backends import MetaLayer, constraints from n3fit.backends import operations as op @@ -35,7 +37,7 @@ class Preprocessing(MetaLayer): def __init__( self, - flav_info: list = None, + flav_info: Optional[list] = None, seed: int = 0, large_x: bool = True, **kwargs, From b0e8f26775c945698f3b15475cd00c9737a1714d Mon Sep 17 00:00:00 2001 From: Aron Date: Mon, 7 Aug 2023 14:11:39 +0200 Subject: [PATCH 18/20] Add np.testing --- n3fit/src/n3fit/tests/test_preprocessing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index 6b14815b24..218d0a7d90 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -62,4 +62,4 @@ def test_preprocessing(): ], ] prefactors = prepro(test_x) - assert np.allclose(test_prefactors, prefactors) + np.testing.assert_allclose(test_prefactors, prefactors) From 9d9b0c0360cf6c0a45a2bcf76d0518a80901d8f6 Mon Sep 17 00:00:00 2001 From: Aron Jansen Date: Mon, 7 Aug 2023 14:12:46 +0200 Subject: [PATCH 19/20] Set seed to 1 instead of 0 Co-authored-by: Juan M. Cruz-Martinez --- n3fit/src/n3fit/tests/test_preprocessing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index 218d0a7d90..784dd31f7c 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -16,7 +16,7 @@ def test_preprocessing(): {'fl': 't8', 'smallx': [0.56, 1.29], 'largex': [1.45, 3.03]}, {'fl': 'cp', 'smallx': [0.12, 1.19], 'largex': [1.83, 6.7]}, ] - prepro = Preprocessing(flav_info=flav_info, seed=0) + prepro = Preprocessing(flav_info=flav_info, seed=1) np.random.seed(42) test_x = np.random.uniform(size=(1, 4, 1)) test_prefactors = [ From 5b49c83cb77c4e70a854cc483f5f5e7af854101d Mon Sep 17 00:00:00 2001 From: RoyStegeman Date: Mon, 7 Aug 2023 13:30:44 +0100 Subject: [PATCH 20/20] update values test_preprocessing --- n3fit/src/n3fit/tests/test_preprocessing.py | 80 +++++++++++---------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/n3fit/src/n3fit/tests/test_preprocessing.py b/n3fit/src/n3fit/tests/test_preprocessing.py index 784dd31f7c..3bf6a8966c 100644 --- a/n3fit/src/n3fit/tests/test_preprocessing.py +++ b/n3fit/src/n3fit/tests/test_preprocessing.py @@ -21,45 +21,47 @@ def test_preprocessing(): test_x = np.random.uniform(size=(1, 4, 1)) test_prefactors = [ [ - 3.2668063e-01, - 6.7284244e-01, - 3.9915803e-01, - 1.5305418e-01, - 1.8249598e-01, - 7.2065055e-02, - 3.1497714e-01, - 1.3243365e-01, - ], - [ - 4.6502685e-04, - 2.4272988e-02, - 2.2857290e-02, - 3.3989837e-04, - 1.6686485e-04, - 7.2010938e-05, - 1.4990549e-04, - 1.3058851e-04, - ], - [ - 3.5664584e-02, - 2.0763232e-01, - 1.7362502e-01, - 2.5178187e-02, - 2.0087767e-02, - 1.0968268e-02, - 2.2659913e-02, - 1.6590673e-02, - ], - [ - 1.0150098e-01, - 3.5557139e-01, - 2.6867521e-01, - 6.4237528e-02, - 5.9941418e-02, - 3.0898115e-02, - 7.7342905e-02, - 4.8169162e-02, - ], + [ + 3.7446213e-01, + 1.9785003e-01, + 2.7931085e-01, + 2.0784079e-01, + 4.5369801e-01, + 2.7796263e-01, + 5.4610312e-01, + 2.4907256e-02, + ], + [ + 6.2252983e-04, + 3.0504008e-05, + 4.5713778e-03, + 1.0905267e-03, + 4.0506415e-02, + 5.9004971e-05, + 4.5114113e-03, + 2.6757403e-09, + ], + [ + 4.1631009e-02, + 1.0586979e-02, + 8.3202787e-02, + 4.3506064e-02, + 2.2559988e-01, + 1.5161950e-02, + 1.0105091e-01, + 1.4808348e-04, + ], + [ + 1.1616933e-01, + 4.2717375e-02, + 1.5620175e-01, + 9.7478621e-02, + 3.2600221e-01, + 5.8901049e-02, + 2.1937098e-01, + 1.8343410e-03, + ], + ] ] prefactors = prepro(test_x) np.testing.assert_allclose(test_prefactors, prefactors)