diff --git a/mlair/model_modules/model_class.py b/mlair/model_modules/model_class.py index dbf7771e376d9bd68f594ecad319554c984dc974..b1779ecd7c087519e8cb8e78b2c9998214d12758 100644 --- a/mlair/model_modules/model_class.py +++ b/mlair/model_modules/model_class.py @@ -2,7 +2,7 @@ Module for neural models to use during experiment. To work properly, each customised model needs to inherit from AbstractModelClass and needs an implementation of the -set_model and set_loss method. +set_model method. In this module, you can find some exemplary model classes that have been build and were running in a experiment. @@ -33,10 +33,11 @@ How to create a customised model? # apply to model self.set_model() - self.set_loss() - self.set_custom_objects(loss=self.loss) + self.set_compile_options() + self.set_custom_objects(loss=self.compile_options['loss']) -* Make sure to add the `super().__init__()` and at least `set_model()` and `set_loss()` to your custom init method. +* Make sure to add the `super().__init__()` and at least `set_model()` and `set_compile_options()` to your custom init + method. * If you have custom objects in your model, that are not part of keras, you need to add them to custom objects. To do this, call `set_custom_objects` with arbitrarily kwargs. In the shown example, the loss has been added, because it wasn't a standard loss. Apart from this, we always encourage you to add the loss as custom object, to prevent @@ -60,14 +61,20 @@ How to create a customised model? self.model = keras.Model(inputs=x_input, outputs=[out_main]) * Your are free, how to design your model. Just make sure to save it in the class attribute model. -* Finally, set your custom loss. +* Additionally, set your custom compile options including the loss. .. code-block:: python class MyCustomisedModel(AbstractModelClass): - def set_loss(self): + def set_compile_options(self): + self.initial_lr = 1e-2 + self.optimizer = keras.optimizers.SGD(lr=self.initial_lr, momentum=0.9) + self.lr_decay = mlair.model_modules.keras_extensions.LearningRateDecay(base_lr=self.initial_lr, + drop=.94, + epochs_drop=10) self.loss = keras.losses.mean_squared_error + self.compile_options = {"metrics": ["mse", "mae"]} * If you have a branched model with multiple outputs, you need either set only a single loss for all branch outputs or to provide the same number of loss functions considering the right order. E.g. @@ -80,7 +87,7 @@ How to create a customised model? ... self.model = keras.Model(inputs=x_input, outputs=[out_minor_1, out_minor_2, out_main]) - def set_loss(self): + def set_compile_options(self): self.loss = [keras.losses.mean_absolute_error] + # for out_minor_1 [keras.losses.mean_squared_error] + # for out_minor_2 [keras.losses.mean_squared_error] # for out_main @@ -111,7 +118,6 @@ True import mlair.model_modules.keras_extensions __author__ = "Lukas Leufen, Felix Kleinert" -# __date__ = '2019-12-12' __date__ = '2020-05-12' from abc import ABC