Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analytic initialisation #41

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions n3fit/runcards/examples/simunet_examples/quick_check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#Configuration file for NNPDF++
#
############################################################
description: "Test fit for the simunet release project. Oll scaled by 10.0, other operators by 1.0 and constant initialisation about 0.0 used."

############################################################
# frac: training fraction
# ewk: apply ewk k-factors
# sys: systematics treatment (see systypes)

dataset_inputs:
# # EWPO
- {dataset: LEP_ZDATA, simu_fac: "EFT_LO", use_fixed_predictions: True}
- {dataset: LEP_BRW, simu_fac: "EFT_LO", use_fixed_predictions: True}
- {dataset: LEP_BHABHA, simu_fac: "EFT_LO", use_fixed_predictions: True}
- {dataset: ALPHAEW_WITHTHUNC, simu_fac: "EFT_LO", use_fixed_predictions: True}

# # Diboson
- {dataset: LEP_EEWW_182GEV, simu_fac: "EFT_LO", use_fixed_predictions: True}
- {dataset: LEP_EEWW_189GEV, simu_fac: "EFT_LO", use_fixed_predictions: True}
- {dataset: LEP_EEWW_198GEV, simu_fac: "EFT_LO", use_fixed_predictions: True}
- {dataset: LEP_EEWW_206GEV, simu_fac: "EFT_LO", use_fixed_predictions: True}
- {dataset: ATLAS_WW_13TEV_2016_MEMU, simu_fac: "EFT_NLO", use_fixed_predictions: True}
- {dataset: ATLAS_WZ_13TEV_2016_MTWZ, simu_fac: "EFT_NLO", use_fixed_predictions: True}
- {dataset: CMS_WZ_13TEV_2016_PTZ, simu_fac: "EFT_NLO", use_fixed_predictions: True}


fixed_pdf_fit: True
load_weights_from_fit: 221103-jmm-no_top_1000_iterated # If this is uncommented, training starts here.
analytic_initialisation_pdf: 221103-jmm-no_top_1000_iterated
use_th_covmat: False

simu_parameters:
# Dipoles
# - {name: 'OtZ', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'OtW', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'OtG', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# Quark Currents
# - {name: 'Opt', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O3pQ3', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
- {name: 'O3pq', scale: 1.0, initialisation: {type: analytic}}
- {name: 'OpQM', scale: 1.0, initialisation: {type: analytic}}
- {name: 'OpqMi', scale: 1.0, initialisation: {type: analytic}}
- {name: 'Opui', scale: 1.0, initialisation: {type: analytic}}
- {name: 'Opdi', scale: 1.0, initialisation: {type: analytic}}
# Lepton currents
- {name: 'O3pl', scale: 1.0, initialisation: {type: analytic}}
- {name: 'Opl', scale: 1.0, initialisation: {type: analytic}}
- {name: 'Ope', scale: 1.0, initialisation: {type: analytic}}
# 4 Fermions 4Q
# - {name: 'O1qd', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O1qu', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O1dt', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O1qt', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O1ut', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O11qq', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O13qq', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O8qd', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O8qu', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O8dt', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O8qt', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O8ut', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O81qq', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'O83qq', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# 4 Fermions 4HeavyQ
# - {name: 'OQQ1', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'OQQ8', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'OQt1', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'OQt8', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Ott1', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# 4 Fermions 2L2Q
# - {name: 'Oeu', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Olu', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Oed', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Olq3', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Olq1', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Oqe', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Old', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# 4 Fermions 4L
- {name: 'Oll', scale: 10.0, initialisation: {type: analytic}}
# Yukawa
# - {name: 'Omup', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Otap', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Otp', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Obp', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'Ocp', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# Bosonic
# - {name: 'OG', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
- {name: 'OWWW', scale: 1.0, initialisation: {type: analytic}}
# - {name: 'OpG', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'OpW', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
# - {name: 'OpB', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
- {name: 'OpWB', scale: 1.0, initialisation: {type: analytic}}
# - {name: 'Opd', scale: 0.01, initialisation: {type: uniform, minval: -1, maxval: 1}}
- {name: 'OpD', scale: 1.0, initialisation: {type: analytic}}


############################################################
datacuts:
t0pdfset: 221103-jmm-no_top_1000_iterated # PDF set to generate t0 covmat
q2min: 3.49 # Q2 minimum
w2min: 12.5 # W2 minimum

############################################################
theory:
theoryid: 200 # database id

############################################################
trvlseed: 475038818
nnseed: 2394641471
mcseed: 1831662593
save: "weights.h5"
genrep: true # true = generate MC replicas, false = use real data

############################################################


parameters: # This defines the parameter dictionary that is passed to the Model Trainer
nodes_per_layer: [25, 20, 8]
activation_per_layer: [tanh, tanh, linear]
initializer: glorot_normal
optimizer:
clipnorm: 6.073e-6
learning_rate: 2.621e-3
optimizer_name: Nadam
epochs: 30000
positivity:
initial: 184.8
multiplier:
integrability:
initial: 184.8
multiplier:
stopping_patience: 1.0
layer_type: dense
dropout: 0.0
threshold_chi2: 3.5

fitting:
# EVOL(QED) = sng=0,g=1,v=2,v3=3,v8=4,t3=5,t8=6,(pht=7)
# EVOLS(QED)= sng=0,g=1,v=2,v8=4,t3=4,t8=5,ds=6,(pht=7)
# FLVR(QED) = g=0, u=1, ubar=2, d=3, dbar=4, s=5, sbar=6, (pht=7)
fitbasis: EVOL # EVOL (7), EVOLQED (8), etc.
basis:
- {fl: sng, pos: false, trainable: false, mutsize: [15], mutprob: [0.05], smallx: [
1.093, 1.121], largex: [1.486, 3.287]}
- {fl: g, pos: false, trainable: false, mutsize: [15], mutprob: [0.05], smallx: [
0.8329, 1.071], largex: [3.084, 6.767]}
- {fl: v, pos: false, trainable: false, mutsize: [15], mutprob: [0.05], smallx: [
0.5202, 0.7431], largex: [1.556, 3.639]}
- {fl: v3, pos: false, trainable: false, mutsize: [15], mutprob: [0.05], smallx: [
0.1205, 0.4839], largex: [1.736, 3.622]}
- {fl: v8, pos: false, trainable: false, mutsize: [15], mutprob: [0.05], smallx: [
0.5864, 0.7987], largex: [1.559, 3.569]}
- {fl: t3, pos: false, trainable: false, mutsize: [15], mutprob: [0.05], smallx: [
-0.5019, 1.126], largex: [1.754, 3.479]}
- {fl: t8, pos: false, trainable: false, mutsize: [15], mutprob: [0.05], smallx: [
0.6305, 0.8806], largex: [1.544, 3.481]}
- {fl: t15, pos: false, trainable: false, mutsize: [15], mutprob: [0.05], smallx: [
1.087, 1.139], largex: [1.48, 3.365]}

############################################################
positivity:
posdatasets:
- {dataset: POSF2U, maxlambda: 1e6} # Positivity Lagrange Multiplier
- {dataset: POSF2DW, maxlambda: 1e6}
- {dataset: POSF2S, maxlambda: 1e6}
- {dataset: POSFLL, maxlambda: 1e6}
- {dataset: POSDYU, maxlambda: 1e10}
- {dataset: POSDYD, maxlambda: 1e10}
- {dataset: POSDYS, maxlambda: 1e10}
- {dataset: POSF2C, maxlambda: 1e6}
- {dataset: POSXUQ, maxlambda: 1e6} # Positivity of MSbar PDFs
- {dataset: POSXUB, maxlambda: 1e6}
- {dataset: POSXDQ, maxlambda: 1e6}
- {dataset: POSXDB, maxlambda: 1e6}
- {dataset: POSXSQ, maxlambda: 1e6}
- {dataset: POSXSB, maxlambda: 1e6}
- {dataset: POSXGL, maxlambda: 1e6}

############################################################
integrability:
integdatasets:
- {dataset: INTEGXT8, maxlambda: 1e2}
- {dataset: INTEGXT3, maxlambda: 1e2}

############################################################
debug: false
maxcores: 4

28 changes: 22 additions & 6 deletions n3fit/src/n3fit/layers/CombineCfac.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from validphys import initialisation_specs


class CombineCfacLayer(Layer):
"""
Creates the combination layer of SIMUnet.
Expand All @@ -16,6 +17,7 @@ def __init__(
scales,
linear_names,
initialisations,
analytic_initialisation,
initialisation_seed,
replica_number,
):
Expand All @@ -37,12 +39,15 @@ def __init__(
# At this point, create a tf object with the correct random initialisation.
initial_values = []
assert len(initialisations) == len(linear_names)
index = 0
for ini, name in zip(initialisations, linear_names):
hash_value = int(hashlib.sha1(name.encode("utf-8")).hexdigest(), 16) % (10 ** 18)
hash_value = int(hashlib.sha1(name.encode("utf-8")).hexdigest(), 16) % (
10**18
)
seed = np.int32((initialisation_seed + replica_number) ^ hash_value)

if isinstance(ini, initialisation_specs.ConstantInitialisation):
val = tf.constant(ini.value, dtype='float32', shape=(1,))
val = tf.constant(ini.value, dtype="float32", shape=(1,))
elif isinstance(ini, initialisation_specs.UniformInitialisation):
val = tf.random_uniform_initializer(
minval=ini.minval,
Expand All @@ -52,31 +57,40 @@ def __init__(
elif isinstance(ini, initialisation_specs.GaussianInitialisation):
tf.random.set_seed(seed)
val = tf.random.normal([1], ini.mean, ini.std_dev, tf.float32)
elif isinstance(ini, initialisation_specs.AnalyticInitialisation):
val = np.array([float(analytic_initialisation[index])])
else:
raise RuntimeError(
"Invalid initialisation: choose form constant, uniform or Gaussian."
)
index += 1
initial_values.append(val)

num_initial = len(initial_values)

self.scales = np.array(scales, dtype=np.float32)
if len(initial_values) > 0:
if num_initial > 0:
initial_values = tf.concat(initial_values, 0)
initial_values = tf.math.divide(initial_values, self.scales)
initial_values = tf.math.multiply(initial_values, self.scales)

if num_initial > 0:
initial_values = tf.concat(initial_values, 0)
initial_values = tf.reshape(initial_values, (num_initial,))
Comment on lines +72 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why here there is the same if statement done twice, can't it all be done in one single if?

self.w = tf.Variable(
initial_value=initial_values,
trainable=True,
)
else:
self.w = tf.Variable(
initial_value=tf.zeros(shape=(len(initial_values),), dtype="float32"),
initial_value=tf.zeros(shape=(num_initial,), dtype="float32"),
trainable=True,
)

self.scales = np.array(scales, dtype=np.float32)
self.linear_names = linear_names

def _compute_linear(self, linear_values):
scaled_values = linear_values/self.scales[:, np.newaxis]
scaled_values = linear_values / self.scales[:, np.newaxis]
return tf.reduce_sum(self.w[:, tf.newaxis] * scaled_values, axis=0)

def call(self, inputs, linear_values):
Expand Down Expand Up @@ -112,6 +126,8 @@ def call(self, inputs, linear_values):
dtype=np.float32,
)
linear = self._compute_linear(linear_values)
inputs = tf.cast(inputs, tf.float64)

return (1 + linear) * inputs

return inputs
3 changes: 3 additions & 0 deletions n3fit/src/n3fit/model_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def __init__(
simu_parameters_names=None,
simu_parameters_scales=None,
bsm_fac_initialisations=None,
analytic_initialisation=None,
bsm_initialisation_seed=0,
debug=False,
kfold_parameters=None,
Expand Down Expand Up @@ -187,6 +188,7 @@ def __init__(
self.simu_parameters_names=simu_parameters_names
self.simu_parameters_scales = simu_parameters_scales
self.bsm_fac_initialisations = bsm_fac_initialisations
self.analytic_initialisation = analytic_initialisation
self.bsm_initialisation_seed = bsm_initialisation_seed
self.fixed_pdf = fixed_pdf
self.replicas = replicas
Expand Down Expand Up @@ -511,6 +513,7 @@ def _generate_observables(
scales=np.array(self.simu_parameters_scales, dtype=np.float32),
linear_names=self.simu_parameters_names,
initialisations=self.bsm_fac_initialisations,
analytic_initialisation=self.analytic_initialisation,
initialisation_seed=self.bsm_initialisation_seed,
replica_number=self.replicas[0],
)
Expand Down
Loading