Belle II Software  release-06-01-15
using_tfdeploy.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import numpy as np
12 try:
13  import tensorflow as tf
14 except BaseException:
15  pass
16 import basf2_mva
17 import basf2_mva_util
18 import time
19 import tempfile
20 import os
21 
22 import tfdeploy
23 
24 """
25 This example shows how to save a tensorflow net with tfdeploy, so you won't need tensorflow for mva expert anymore.
26 """
27 
28 
29 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
30 
31  tf.reset_default_graph()
32  # The name is needed for finding the tensor later
33  x = tf.placeholder(tf.float32, [None, number_of_features], name='input')
34  y = tf.placeholder(tf.float32, [None, 1])
35 
36  def layer(x, shape, name, unit=tf.sigmoid):
37  with tf.name_scope(name):
38  weights = tf.Variable(tf.truncated_normal(shape, stddev=1.0 / np.sqrt(float(shape[0]))), name='weights')
39  biases = tf.Variable(tf.constant(0.0, shape=[shape[1]]), name='biases')
40  layer = unit(tf.matmul(x, weights) + biases)
41  return layer
42 
43  inference_hidden1 = layer(x, [number_of_features, number_of_features + 1], 'inference_hidden1')
44  inference_activation = layer(inference_hidden1, [number_of_features + 1, 1], 'inference_sigmoid', unit=tf.sigmoid)
45 
46  epsilon = 1e-5
47  inference_loss = -tf.reduce_sum(y * tf.log(inference_activation + epsilon) +
48  (1.0 - y) * tf.log(1 - inference_activation + epsilon))
49 
50  inference_optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
51  inference_minimize = inference_optimizer.minimize(inference_loss)
52 
53  init = tf.global_variables_initializer()
54 
55  config = tf.ConfigProto()
56  config.gpu_options.allow_growth = True
57  session = tf.Session(config=config)
58 
59  session.run(init)
60  # for making new tensorflow_op which does the same as inference_activation but using an other name.
61  state = State(x, y, tf.identity(inference_activation, name='output'), inference_loss, inference_minimize, session)
62 
63  return state
64 
65 
66 def partial_fit(state, X, S, y, w, epoch):
67  """
68  Pass received data to tensorflow session
69  """
70  feed_dict = {state.x: X, state.y: y}
71  state.session.run(state.optimizer, feed_dict=feed_dict)
72 
73  if epoch % 100 == 0:
74  avg_cost = state.session.run(state.cost, feed_dict=feed_dict)
75  print("Epoch:", '%04d' % (epoch), "cost=", "{:.9f}".format(avg_cost))
76  return True
77 
78 
79 def end_fit(state):
80  """
81  Save tfdeploy.Model
82  """
83  model = tfdeploy.Model()
84  model.add(state.activation, state.session)
85  with tempfile.TemporaryDirectory() as path:
86  filename = os.path.join(path, 'model.pkl')
87  model.save(filename)
88  with open(filename, 'rb') as file:
89  data = file.read()
90 
91  del state
92  return data
93 
94 
95 def load(obj):
96  """
97  Load tfdeploy.Model
98  """
99  with tempfile.TemporaryDirectory() as path:
100  filename = os.path.join(path, 'model.pkl')
101  with open(filename, 'w+b') as file:
102  file.write(bytes(obj))
103  model = tfdeploy.Model(filename)
104  state = State()
105  state.inp, state.outp = model.get('input', 'output')
106  return state
107 
108 
109 def apply(state, X):
110  """
111  Apply estimator to passed data.
112  """
113  r = state.outp.eval({state.inp: X}).flatten()
114  return np.require(r, dtype=np.float32, requirements=['A', 'W', 'C', 'O'])
115 
116 
117 class State(object):
118  """
119  Tensorflow state
120  """
121 
122  def __init__(self, x=None, y=None, activation=None, cost=None, optimizer=None, session=None):
123  """ Constructor of the state object """
124 
125  self.xx = x
126 
127  self.yy = y
128 
129  self.activationactivation = activation
130 
131  self.costcost = cost
132 
133  self.optimizeroptimizer = optimizer
134 
135  self.sessionsession = session
136 
137 
138 if __name__ == "__main__":
139  from basf2 import conditions
140  # NOTE: do not use testing payloads in production! Any results obtained like this WILL NOT BE PUBLISHED
141  conditions.testing_payloads = [
142  'localdb/database.txt'
143  ]
144 
145  general_options = basf2_mva.GeneralOptions()
146  general_options.m_datafiles = basf2_mva.vector("train.root")
147  general_options.m_identifier = "Simple"
148  general_options.m_treename = "tree"
149  variables = ['M', 'p', 'pt', 'pz',
150  'daughter(0, p)', 'daughter(0, pz)', 'daughter(0, pt)',
151  'daughter(1, p)', 'daughter(1, pz)', 'daughter(1, pt)',
152  'daughter(2, p)', 'daughter(2, pz)', 'daughter(2, pt)',
153  'chiProb', 'dr', 'dz',
154  'daughter(0, dr)', 'daughter(1, dr)',
155  'daughter(0, dz)', 'daughter(1, dz)',
156  'daughter(0, chiProb)', 'daughter(1, chiProb)', 'daughter(2, chiProb)',
157  'daughter(0, kaonID)', 'daughter(0, pionID)',
158  'daughterInvariantMass(0, 1)', 'daughterInvariantMass(0, 2)', 'daughterInvariantMass(1, 2)']
159  general_options.m_variables = basf2_mva.vector(*variables)
160  general_options.m_target_variable = "isSignal"
161 
162  specific_options = basf2_mva.PythonOptions()
163  specific_options.m_framework = "tensorflow"
164  specific_options.m_steering_file = 'mva/examples/tensorflow/using_tfdeploy.py'
165  specific_options.m_nIterations = 10
166  specific_options.m_mini_batch_size = 100
167  specific_options.m_normalize = True
168  training_start = time.time()
169  basf2_mva.teacher(general_options, specific_options)
170  training_stop = time.time()
171  training_time = training_stop - training_start
172  method = basf2_mva_util.Method(general_options.m_identifier)
173  inference_start = time.time()
174  test_data = ["test.root"]
175  p, t = method.apply_expert(basf2_mva.vector(*test_data), general_options.m_treename)
176  inference_stop = time.time()
177  inference_time = inference_stop - inference_start
179  print("Tensorflow", training_time, inference_time, auc)
def calculate_roc_auc(p, t)
y
target placeholder
def __init__(self, x=None, y=None, activation=None, cost=None, optimizer=None, session=None)
optimizer
optimizer used to minimize cost function
x
feature matrix placeholder
session
tensorflow session
activation
activation function