This tutorial shows how to define and generate a keras model for use with TMVA.
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.regularizers import l2
from keras.optimizers import SGD
num_input_nodes = 4
num_output_nodes = 2
num_hidden_layers = 1
nodes_hidden_layer = 64
l2_val = 1e-5
model = Sequential()
model.add(Dense(nodes_hidden_layer, activation='relu', W_regularizer=l2(l2_val), input_dim=num_input_nodes))
for k in range(num_hidden_layers-1):
model.add(Dense(nodes_hidden_layer, activation='relu', W_regularizer=l2(l2_val)))
model.add(Dense(num_output_nodes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.01), metrics=['accuracy',])
model.save('model.h5')
model.summary()
try:
from keras.utils.visualize_util import plot
plot(model, to_file='model.png', show_shapes=True)
except:
print('[INFO] Failed to make model plot')
- Date
- 2017
- Author
- TMVA Team
Definition in file GenerateModel.py.