Belle II Software  light-2212-foldex
simple.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import numpy as np
12 import tensorflow as tf
13 import basf2_mva
14 import basf2_mva_util
15 import time
16 
18 
19 
20 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
21  """
22  Return simple tensorflow model
23  """
24 
25  gpus = tf.config.list_physical_devices('GPU')
26  if gpus:
27  for gpu in gpus:
28  tf.config.experimental.set_memory_growth(gpu, True)
29 
30  class my_model(tf.Module):
31 
32  def __init__(self, **kwargs):
33  super().__init__(**kwargs)
34 
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')
41 
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')
47 
48  @tf.function(input_signature=[tf.TensorSpec(shape=[None, number_of_features], dtype=tf.float32)])
49  def __call__(self, x):
50 
51  # __call__ cannot create any new Variables
52  def dense(x, W, b, activation_function):
53  return activation_function(tf.matmul(x, W) + b)
54 
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)
57  return activation
58 
59  @tf.function
60  def clean_nans(self, x):
61  return tf.where(tf.math.is_nan(x), tf.zeros_like(x), x)
62 
63  @tf.function
64  def loss(self, predicted_y, target_y, w):
65  epsilon = 1e-5
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)
68 
69  state = State(model=my_model())
70  return state
71 
72 
73 if __name__ == "__main__":
74  from basf2 import conditions
75  # NOTE: do not use testing payloads in production! Any results obtained like this WILL NOT BE PUBLISHED
76  conditions.testing_payloads = [
77  'localdb/database.txt'
78  ]
79 
80  general_options = basf2_mva.GeneralOptions()
81  general_options.m_datafiles = basf2_mva.vector("train.root")
82  general_options.m_identifier = "Simple"
83  general_options.m_treename = "tree"
84  variables = ['M', 'p', 'pt', 'pz',
85  'daughter(0, p)', 'daughter(0, pz)', 'daughter(0, pt)',
86  'daughter(1, p)', 'daughter(1, pz)', 'daughter(1, pt)',
87  'daughter(2, p)', 'daughter(2, pz)', 'daughter(2, pt)',
88  'chiProb', 'dr', 'dz',
89  'daughter(0, dr)', 'daughter(1, dr)',
90  'daughter(0, dz)', 'daughter(1, dz)',
91  'daughter(0, chiProb)', 'daughter(1, chiProb)', 'daughter(2, chiProb)',
92  'daughter(0, kaonID)', 'daughter(0, pionID)',
93  'daughterInvM(0, 1)', 'daughterInvM(0, 2)', 'daughterInvM(1, 2)']
94  general_options.m_variables = basf2_mva.vector(*variables)
95  general_options.m_target_variable = "isSignal"
96 
97  specific_options = basf2_mva.PythonOptions()
98  specific_options.m_framework = "tensorflow"
99  specific_options.m_steering_file = 'mva/examples/tensorflow/simple.py'
100  specific_options.m_nIterations = 100
101  specific_options.m_mini_batch_size = 100
102  specific_options.m_normalize = True
103  training_start = time.time()
104  basf2_mva.teacher(general_options, specific_options)
105  training_stop = time.time()
106  training_time = training_stop - training_start
107  method = basf2_mva_util.Method(general_options.m_identifier)
108  inference_start = time.time()
109  test_data = ["test.root"] * 10
110  p, t = method.apply_expert(basf2_mva.vector(*test_data), general_options.m_treename)
111  inference_stop = time.time()
112  inference_time = inference_stop - inference_start
114  print("Tensorflow", training_time, inference_time, auc)
def calculate_auc_efficiency_vs_background_retention(p, t, w=None)