Belle II Software light-2406-ragdoll
simple.py
1#!/usr/bin/env python3
2
3
10
11import numpy as np
12import tensorflow as tf
13import basf2_mva
14import basf2_mva_util
15import time
16
18
19
20def 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
73if __name__ == "__main__":
74 from basf2 import conditions, find_file
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 train_file = find_file("mva/train_D0toKpipi.root", "examples")
80 test_file = find_file("mva/test_D0toKpipi.root", "examples")
81
82 training_data = basf2_mva.vector(train_file)
83 testing_data = basf2_mva.vector(test_file)
84
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"
101
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
112 method = basf2_mva_util.Method(general_options.m_identifier)
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)