14 from keras.layers
import Dense, Input
15 from keras.models
import Model, load_model
16 from keras.losses
import binary_crossentropy
24 def __init__(self, model=None, custom_objects=None, **kwargs):
25 """ Constructor of the state object """
34 for key, value
in kwargs.items():
36 setattr(self, key, value)
39 def feature_importance(state):
41 Return a list containing the feature importances
46 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
48 Return default tensorflow model
50 input = Input(shape=(number_of_features,))
51 net = Dense(units=1)(input)
53 state =
State(Model(input, net))
55 state.model.compile(optimizer=
"adam", loss=binary_crossentropy, metrics=[
'accuracy'])
64 Load Tensorflow estimator into state
66 with tempfile.TemporaryDirectory()
as path:
67 with open(os.path.join(path,
'weights.h5'),
'w+b')
as file:
68 file.write(bytes(obj[0]))
69 state =
State(load_model(os.path.join(path,
'weights.h5'), custom_objects=obj[1]))
71 for index, key
in enumerate(obj[2]):
72 setattr(state, key, obj[index + 3])
79 Apply estimator to passed data.
81 r = state.model.predict(X).flatten()
82 return np.require(r, dtype=np.float32, requirements=[
'A',
'W',
'C',
'O'])
85 def begin_fit(state, Xtest, Stest, ytest, wtest):
87 Returns just the state object
92 def partial_fit(state, X, S, y, w, epoch):
94 Pass received data to tensorflow session
96 state.model.fit(X, y, batch_size=100, epochs=10)
102 Store tensorflow session in a graph
105 with tempfile.TemporaryDirectory()
as path:
106 state.model.save(os.path.join(path,
'weights.h5'))
107 with open(os.path.join(path,
'weights.h5'),
'rb')
as file:
110 obj_to_save = [data, state.custom_objects, state.collection_keys]
111 for key
in state.collection_keys:
112 obj_to_save.append(getattr(state, key))
custom_objects
used by keras to load custom objects like custom layers
collection_keys
list of keys to save
def __init__(self, model=None, custom_objects=None, **kwargs)