Skip to content

Commit

Permalink
remove most tf dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
scarlehoff committed Nov 22, 2024
1 parent 6edf250 commit fc2fe3a
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 113 deletions.
10 changes: 5 additions & 5 deletions extra_tests/regression_fits/central.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ description: n3fit regression test
# ewk: apply ewk k-factors
# sys: systematics treatment (see systypes)
dataset_inputs:
- {dataset: NMC_NC_NOTFIXED_P_EM-SIGMARED, frac: 0.5, variant: legacy}
- {dataset: SLAC_NC_NOTFIXED_P_EM-F2, frac: 0.5, variant: legacy}
- {dataset: CMS_Z0J_8TEV_PT-Y, frac: 0.5, cfac: [QCD], variant: legacy_10}
- {dataset: NMC_NC_NOTFIXED_P_EM-SIGMARED, frac: 0.55, variant: legacy}
- {dataset: SLAC_NC_NOTFIXED_P_EM-F2, frac: 0.75, variant: legacy}
- {dataset: CMS_Z0J_8TEV_PT-Y, frac: 0.75, cfac: [QCD], variant: legacy_10}
- {dataset: ATLAS_TTBAR_8TEV_TOT_X-SEC, frac: 1.0, cfac: [QCD], variant: legacy}

############################################################
Expand All @@ -32,8 +32,7 @@ genrep: False # on = generate MC replicas, False = use real data
trvlseed: 3
nnseed: 2
mcseed: 1

load: "weights.weights.h5"
sum_rules: vsr

parameters: # This defines the parameter dictionary that is passed to the Model Trainer
nodes_per_layer: [15, 10, 8]
Expand Down Expand Up @@ -78,3 +77,4 @@ integrability:
############################################################
debug: true
double_precision: false
parallel_models: true
12 changes: 3 additions & 9 deletions n3fit/src/n3fit/backends/keras_backend/MetaModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@
from pathlib import Path
import re

from keras import optimizers as Kopt
from keras.models import Model
import numpy as np
import tensorflow as tf
from tensorflow.keras import optimizers as Kopt
from tensorflow.keras.models import Model
from tensorflow.python.keras.utils import tf_utils # pylint: disable=no-name-in-module

import n3fit.backends.keras_backend.operations as op

# We need a function to transform tensors to numpy/python primitives
# which is not part of the official TF interface and can change with the version
if hasattr(tf_utils, "to_numpy_or_python_type"):
_to_numpy_or_python_type = tf_utils.to_numpy_or_python_type
elif hasattr(tf_utils, "sync_to_numpy_or_python_type"): # from TF 2.5
_to_numpy_or_python_type = tf_utils.sync_to_numpy_or_python_type
else: # in case of disaster
_to_numpy_or_python_type = lambda ret: {k: i.numpy() for k, i in ret.items()}
_to_numpy_or_python_type = lambda ret: {k: i.numpy() for k, i in ret.items()}

# Starting with TF 2.16, a memory leak in TF https://github.com/tensorflow/tensorflow/issues/64170
# makes jit compilation unusable in GPU.
Expand Down
22 changes: 10 additions & 12 deletions n3fit/src/n3fit/backends/keras_backend/base_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
The names of the layer and the activation function are the ones to be used in the n3fit runcard.
"""

from tensorflow import expand_dims, math, nn
from tensorflow.keras.layers import Dense as KerasDense
from tensorflow.keras.layers import Dropout, Lambda
from tensorflow.keras.layers import Input # pylint: disable=unused-import
from tensorflow.keras.layers import LSTM, Concatenate
from tensorflow.keras.regularizers import l1_l2
from keras.layers import Dense as KerasDense
from keras.layers import Dropout, Lambda
from keras.layers import Input # pylint: disable=unused-import
from keras.layers import LSTM, Concatenate
from keras.regularizers import l1_l2

from . import operations as ops
from .MetaLayer import MetaLayer
from .operations import concatenate_function


# Custom activation functions
def square_activation(x):
Expand All @@ -38,17 +36,17 @@ def square_singlet(x):
"""Square the singlet sector
Defined as the two first values of the NN"""
singlet_squared = x[..., :2] ** 2
return concatenate_function([singlet_squared, x[..., 2:]], axis=-1)
return ops.concatenate([singlet_squared, x[..., 2:]], axis=-1)


def modified_tanh(x):
"""A non-saturating version of the tanh function"""
return math.abs(x) * nn.tanh(x)
return ops.absolute(x) * ops.tanh(x)


def leaky_relu(x):
"""Computes the Leaky ReLU activation function"""
return nn.leaky_relu(x, alpha=0.2)
return ops.leaky_relu(x, alpha=0.2)


custom_activations = {
Expand All @@ -64,7 +62,7 @@ def LSTM_modified(**kwargs):
LSTM asks for a sample X timestep X features kind of thing so we need to reshape the input
"""
the_lstm = LSTM(**kwargs)
ExpandDim = Lambda(lambda x: expand_dims(x, axis=-1))
ExpandDim = Lambda(lambda x: ops.expand_dims(x, axis=-1))

def ReshapedLSTM(input_tensor):
if len(input_tensor.shape) == 2:
Expand Down
2 changes: 1 addition & 1 deletion n3fit/src/n3fit/backends/keras_backend/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import logging
from time import time

from keras.callbacks import Callback, TensorBoard
import numpy as np
import tensorflow as tf
from tensorflow.keras.callbacks import Callback, TensorBoard

log = logging.getLogger(__name__)

Expand Down
5 changes: 2 additions & 3 deletions n3fit/src/n3fit/backends/keras_backend/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
Implementations of weight constraints for initializers
"""

import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.constraints import MinMaxNorm
from keras import backend as K
from keras.constraints import MinMaxNorm


class MinMaxWeight(MinMaxNorm):
Expand Down
5 changes: 3 additions & 2 deletions n3fit/src/n3fit/backends/keras_backend/internal_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Library of functions that modify the internal state of Keras/Tensorflow
"""

import os

import psutil
Expand All @@ -13,10 +14,10 @@
import logging
import random as rn

import keras
from keras import backend as K
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import backend as K

log = logging.getLogger(__name__)

Expand Down
Loading

0 comments on commit fc2fe3a

Please sign in to comment.