12import tensorflow
as tf
20def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
22 Return simple tensorflow model
25 gpus = tf.config.list_physical_devices('GPU')
28 tf.config.experimental.set_memory_growth(gpu,
True)
30 class my_model(tf.Module):
32 def __init__(self, **kwargs):
33 super().__init__(**kwargs)
35 self.optimizer = tf.optimizers.Adam(0.01)
36 shape = [number_of_features, number_of_features]
37 self.W_hidden1 = tf.Variable(
38 tf.random.truncated_normal(shape, stddev=1.0 / np.sqrt(float(shape[0]))),
39 name=
'hidden1_weights')
40 self.b_hidden1 = tf.Variable(tf.zeros(shape=[shape[1]]), name=
'hidden1_biases')
42 shape = [number_of_features, 1]
43 self.W_activation = tf.Variable(
44 tf.random.truncated_normal(shape, stddev=1.0 / np.sqrt(float(shape[0]))),
45 name=
'activation_weights')
46 self.b_activation = tf.Variable(tf.zeros(shape=[shape[1]]), name=
'activation_biases')
48 @tf.function(input_signature=[tf.TensorSpec(shape=[None, number_of_features], dtype=tf.float32)])
49 def __call__(self, x):
52 def dense(x, W, b, activation_function):
53 return activation_function(tf.matmul(x, W) + b)
55 hidden1 = dense(self.clean_nans(x), self.W_hidden1, self.b_hidden1, tf.nn.sigmoid)
56 activation = dense(hidden1, self.W_activation, self.b_activation, tf.nn.sigmoid)
60 def clean_nans(self, x):
61 return tf.where(tf.math.is_nan(x), tf.zeros_like(x), x)
64 def loss(self, predicted_y, target_y, w):
66 diff_from_truth = tf.where(target_y == 1., predicted_y, 1. - predicted_y)
67 return - tf.reduce_sum(w * tf.math.log(diff_from_truth + epsilon)) / tf.reduce_sum(w)
69 state = State(model=my_model())
73if __name__ ==
"__main__":
74 from basf2
import conditions, find_file
76 conditions.testing_payloads = [
77 'localdb/database.txt'
79 train_file = find_file(
"mva/train_D0toKpipi.root",
"examples")
80 test_file = find_file(
"mva/test_D0toKpipi.root",
"examples")
82 training_data = basf2_mva.vector(train_file)
83 testing_data = basf2_mva.vector(test_file)
85 general_options = basf2_mva.GeneralOptions()
86 general_options.m_datafiles = training_data
87 general_options.m_identifier =
"Simple"
88 general_options.m_treename =
"tree"
89 variables = [
'M',
'p',
'pt',
'pz',
90 'daughter(0, p)',
'daughter(0, pz)',
'daughter(0, pt)',
91 'daughter(1, p)',
'daughter(1, pz)',
'daughter(1, pt)',
92 'daughter(2, p)',
'daughter(2, pz)',
'daughter(2, pt)',
93 'chiProb',
'dr',
'dz',
94 'daughter(0, dr)',
'daughter(1, dr)',
95 'daughter(0, dz)',
'daughter(1, dz)',
96 'daughter(0, chiProb)',
'daughter(1, chiProb)',
'daughter(2, chiProb)',
97 'daughter(0, kaonID)',
'daughter(0, pionID)',
98 'daughterInvM(0, 1)',
'daughterInvM(0, 2)',
'daughterInvM(1, 2)']
99 general_options.m_variables = basf2_mva.vector(*variables)
100 general_options.m_target_variable =
"isSignal"
102 specific_options = basf2_mva.PythonOptions()
103 specific_options.m_framework =
"tensorflow"
104 specific_options.m_steering_file =
'mva/examples/tensorflow/simple.py'
105 specific_options.m_nIterations = 100
106 specific_options.m_mini_batch_size = 100
107 specific_options.m_normalize =
True
108 training_start = time.time()
109 basf2_mva.teacher(general_options, specific_options)
110 training_stop = time.time()
111 training_time = training_stop - training_start
113 inference_start = time.time()
114 p, t = method.apply_expert(testing_data, general_options.m_treename)
115 inference_stop = time.time()
116 inference_time = inference_stop - inference_start
118 print(
"Tensorflow", training_time, inference_time, auc)
def calculate_auc_efficiency_vs_background_retention(p, t, w=None)