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