:py:mod:`mlair.model_modules` ============================= .. py:module:: mlair.model_modules .. autoapi-nested-parse:: Collection of all modules that are related to a model. Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 abstract_model_class/index.rst advanced_paddings/index.rst branched_input_networks/index.rst convolutional_networks/index.rst flatten/index.rst fully_connected_networks/index.rst inception_model/index.rst keras_extensions/index.rst linear_model/index.rst loss/index.rst model_class/index.rst probability_models/index.rst recurrent_networks/index.rst residual_networks/index.rst u_networks/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: mlair.model_modules.AbstractModelClass .. py:class:: AbstractModelClass(input_shape, output_shape) Bases: :py:obj:`abc.ABC` The AbstractModelClass provides a unified skeleton for any model provided to the machine learning workflow. The model can always be accessed by calling ModelClass.model or directly by an model method without parsing the model attribute name (e.g. ModelClass.model.compile -> ModelClass.compile). Beside the model, this class provides the corresponding loss function. .. py:attribute:: _requirements :annotation: = [] .. py:method:: load_model(self, name: str, compile: bool = False) -> None .. py:method:: __getattr__(self, name: str) -> Any Is called if __getattribute__ is not able to find requested attribute. Normally, the model class is saved into a variable like `model = ModelClass()`. To bypass a call like `model.model` to access the _model attribute, this method tries to search for the named attribute in the self.model namespace and returns this attribute if available. Therefore, following expression is true: `ModelClass().compile == ModelClass().model.compile` as long the called attribute/method is not part if the ModelClass itself. :param name: name of the attribute or method to call :return: attribute or method from self.model namespace .. py:method:: model(self) -> tensorflow.keras.Model :property: The model property containing a keras.Model instance. :return: the keras model .. py:method:: custom_objects(self) -> Dict :property: The custom objects property collects all non-keras utilities that are used in the model class. To load such a customised and already compiled model (e.g. from local disk), this information is required. :return: custom objects in a dictionary .. py:method:: compile_options(self) -> Dict :property: The compile options property allows the user to use all keras.compile() arguments. They can ether be passed as dictionary (1), as attribute, without setting compile_options (2) or as mixture (partly defined as instance attributes and partly parsing a dictionary) of both of them (3). The method will raise an Error when the same parameter is set differently. Example (1) Recommended (includes check for valid keywords which are used as args in keras.compile) .. code-block:: python def set_compile_options(self): self.compile_options = {"optimizer": keras.optimizers.SGD(), "loss": keras.losses.mean_squared_error, "metrics": ["mse", "mae"]} Example (2) .. code-block:: python def set_compile_options(self): self.optimizer = keras.optimizers.SGD() self.loss = keras.losses.mean_squared_error self.metrics = ["mse", "mae"] Example (3) Correct: .. code-block:: python def set_compile_options(self): self.optimizer = keras.optimizers.SGD() self.loss = keras.losses.mean_squared_error self.compile_options = {"metrics": ["mse", "mae"]} Incorrect: (Will raise an error) .. code-block:: python def set_compile_options(self): self.optimizer = keras.optimizers.SGD() self.loss = keras.losses.mean_squared_error self.compile_options = {"optimizer": keras.optimizers.Adam(), "metrics": ["mse", "mae"]} Note: * As long as the attribute and the dict value have exactly the same values, the setter method will not raise an error * For example (2) there is no check implemented, if the attributes are valid compile options :return: .. py:method:: __extract_from_tuple(tup) :staticmethod: Return element of tuple if it contains only a single element. .. py:method:: __compare_keras_optimizers(first, second) :staticmethod: Compares if optimiser and all settings of the optimisers are exactly equal. :return True if optimisers are interchangeable, or False if optimisers are distinguishable. .. py:method:: get_settings(self) -> Dict Get all class attributes that are not protected in the AbstractModelClass as dictionary. :return: all class attributes .. py:method:: set_model(self) :abstractmethod: Abstract method to set model. .. py:method:: set_compile_options(self) :abstractmethod: This method only has to be defined in child class, when additional compile options should be used () (other options than optimizer and loss) Has to be set as dictionary: {'optimizer': None, 'loss': None, 'metrics': None, 'loss_weights': None, 'sample_weight_mode': None, 'weighted_metrics': None, 'target_tensors': None } :return: .. py:method:: set_custom_objects(self, **kwargs) -> None Set custom objects that are not part of keras framework. These custom objects are needed if an already compiled model is loaded from disk. There is a special treatment for the Padding2D class, which is a base class for different padding types. For a correct behaviour, all supported subclasses are added as custom objects in addition to the given ones. :param kwargs: all custom objects, that should be saved .. py:method:: requirements(cls) :classmethod: Return requirements and own arguments without duplicates. .. py:method:: own_args(cls, *args) :classmethod: Return all arguments (including kwonlyargs). .. py:method:: super_args(cls) :classmethod: