Skip to content

Keras Models

Peter Fackeldey edited this page Dec 18, 2017 · 2 revisions

Neural Network Models

The neural network models are stored in the KerasModels class in model.py. Until now they are initialized with the number of training features, number of classes and the learning rate. If you want to add a new model, just copy&paste:

    def example_model(self):
        """
        5 (linear connected) layer example model:
        - 1 Dense (128) layer with dimension: n_features (training variables)
        - 1 Dropout layer (reduces overtraining effects)
        - 1 Dense (64) layer with dimension: 128
        - 1 Dropout layer (reduces overtraining effects)
        - 1 Dense layer with dimension: n_classes
        """

        model = Sequential()
        model.add(Dense(128, kernel_initializer='glorot_normal',
                        activation='relu', input_dim=self.n_features))
        model.add(Dropout(0.1))
        model.add(Dense(64, activation='relu', input_dim=128))
        model.add(Dropout(0.1))
        model.add(Dense(self.n_classes, activation='softmax'))

        # Compile the model:

        model.compile(loss='categorical_crossentropy', optimizer=Adam(
            lr=self.learning_rate), metrics=['accuracy'])

        model.summary()
        model.save(self.modelname)

        if self.plot_model:
            # Visualize model as graph
            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')

        return model

in the KerasModel class. Modify the general structure of the neural network (e.g.: number of layers), the loss, the optimizer and metrics for your analysis. Finally give the model a reasonable name. The trained model is saved in the .h5 format. This allows to access the trained network later without running the full training step again. Create an instance of the class and then call the function which returns your model, e.g.:

from utils.model import KerasModels

model = KerasModels(n_features=len(config["features"]), n_classes=len(
        config["classes"]), learning_rate=0.001, plot_model=False, modelname="multiclass_model_fold{}.h5".format(args.fold))
keras_model = model.example_model()
Clone this wiki locally