simpleml.models.base_keras_model

Base module for keras models. Keras has a native persistence mechanism so need to overwrite other methods at the root

Module Contents

Classes

KerasModel

Base Keras model class. Keras objects are incrementally structured until

Attributes

LOGGER

__author__

simpleml.models.base_keras_model.LOGGER[source]
simpleml.models.base_keras_model.__author__ = Elisha Yadgaran[source]
class simpleml.models.base_keras_model.KerasModel(use_training_generator=False, training_generator_params=None, use_validation_generator=False, validation_generator_params=None, use_sequence_object=False, **kwargs)[source]

Bases: simpleml.models.base_model.LibraryModel

Base Keras model class. Keras objects are incrementally structured until fit. Also dont have separable params. Class hijacks params to store fit params instead (enables full specification on init for reproducibility)

Pass default save method as Keras’s persistence pattern

Parameters
  • use_training_generator (Bool) – Whether to propagate use of a generator object when training – does not allow for using a generator in production – only fit_generator

  • use_validation_generator (Bool) – Whether to ALSO use a generator for validation data while training. Does nothing if use_training_generator is false

  • training_generator_params – parameters to pass to the generator method for train split - normal fit(_generator) params should be passed as params={}

  • validation_generator_params – parameters to pass to the generator method for validation split - normal fit(_generator) params should be passed as params={}

abstract _create_external_model(self, **kwargs)[source]

Abstract method for each subclass to implement should return the desired model object

Must return external_file

Keras pattern is: external_model = SomeWrappedKerasClass(**kwargs) return self.build_network(external_model)

_fit(self)[source]

Keras fit parameters (epochs, callbacks…) are stored as self.params so retrieve them automatically

_fit_generator(self)[source]

Keras fit parameters (epochs, callbacks…) are stored as self.params so retrieve them automatically

build_network(self, external_model, **kwargs)[source]

Design choice to require build network method instead of exposing raw Keras objects that can be modified later. Simplifies saving and loading pattern because initialized object should also be the final state (as long as manual override doesnt happen)

get_params(self, **kwargs)[source]

Get fit params

set_params(self, **kwargs)[source]

Keras networks don’t have params beyond layers, which should be configured in self.build_network, so use this for fit params - self.fit will auto pull params and pass them to the fit method.

TODO: Figure out if changing params should be allowed after fit. If they are, would need to reinitialize model, otherwise it would train more epochs and not forget the original training. If not, once fit, we can treat the model as static, and no longer able to be changed

For now going with option 2 - cannot refit models

static transfer_weights(new_model, old_model)[source]