Model

The fedbiomed.common.models module includes model abstraction classes that can be used with plain framework specific models.

Please visit Declearn repository for the "TorchVector" and "NumpyVector" classes used in this module.

Classes

BaseSkLearnModel

BaseSkLearnModel(model)

Bases: Model

Wrapper of Scikit learn models.

This class implements all abstract methods from the Model API, but adds some scikit-learn-specific ones that need implementing by its children.

Attributes:

Name Type Description
model BaseEstimator

Wrapped model

param_list List[str]

List that contains layer attributes. Should be set when calling set_init_params method

Class attributes:

Name Type Description
is_classification bool

Boolean flag indicating whether the wrapped model is designed for classification or for regression supervised-learning tasks.

Parameters:

Name Type Description Default
model BaseEstimator

Model object as an instance of BaseEstimator

required

Raises:

Type Description
FedbiomedModelError

if model is not as scikit learn BaseEstimator object

Source code in fedbiomed/common/models/_sklearn.py
def __init__(
    self,
    model: BaseEstimator,
) -> None:
    """Instantiate the wrapper over a scikit-learn BaseEstimator.

    Args:
        model: Model object as an instance of [BaseEstimator][sklearn.base.BaseEstimator]

    Raises:
        FedbiomedModelError: if model is not as scikit learn [BaseEstimator][sklearn.base.BaseEstimator] object
    """
    super().__init__(model)
    self._gradients: Dict[str, np.ndarray] = {}
    self.param_list: List[str] = []
    self._optim_params: Dict[str, Any] = {}

Attributes

Functions

MLPSklearnModel

MLPSklearnModel(model)

Bases: BaseSkLearnModel

BaseSklearnModel abstract subclass for multi-layer perceptron models.

Source code in fedbiomed/common/models/_sklearn.py
def __init__(self, model: BaseEstimator) -> None:
    self._null_optim_params: Dict[str, Any] = {
        "learning_rate_init": 1.0,
        "learning_rate": "constant",
    }
    super().__init__(model)

Attributes

Functions

Model

Model(model)

Bases: Generic[_MT, DT]

Model abstraction, that wraps and handles both native models

Attributes:

Name Type Description
model Any

native model, written in a framework supported by Fed-BioMed.

Parameters:

Name Type Description Default
model _MT

native model wrapped, of child-class-specific type.

required
Source code in fedbiomed/common/models/_model.py
def __init__(self, model: _MT):
    """Constructor of Model abstract class

    Args:
        model: native model wrapped, of child-class-specific type.
    """
    self._validate_model_type(model)
    self.model: Any = model

Attributes

Functions

SGDClassifierSKLearnModel

Bases: SGDSkLearnModel

BaseSkLearnModel subclass for SGDClassifier models.

Attributes

Functions

SGDRegressorSKLearnModel

Bases: SGDSkLearnModel

BaseSkLearnModel subclass for SGDRegressor models.

Attributes

Functions

SkLearnModel

SkLearnModel(model)

Sklearn model builder.

It wraps one of Fed-BioMed BaseSkLearnModel object children, by passing a (BaseEstimator)(https://scikit-learn.org/stable/modules/generated/sklearn.base.BaseEstimator.html) object to the constructor, as shown below.

Usage

    from sklearn.linear_model import SGDClassifier
    model = SkLearnModel(SGDClassifier)
    model.set_weights(some_weights)
    type(model.model)
    # Output: <class 'sklearn.linear_model._stochastic_gradient.SGDClassifier'>

Attributes:

Name Type Description
_instance BaseSkLearnModel

instance of BaseSkLearnModel

Parameters:

Name Type Description Default
model Type[BaseEstimator]

non-initialized BaseEstimator object

required

Raises:

Type Description
FedbiomedModelError

raised if model does not belong to the implemented models.

FedbiomedModelError

raised if __name__ attribute does not belong to object. This may happen when passing an instantiated object instead of the class object (e.g. instance of SGDClassifier() instead of SGDClassifier object)

Source code in fedbiomed/common/models/_sklearn.py
def __init__(self, model: Type[BaseEstimator]):
    """Constructor of the model builder.

    Args:
        model: non-initialized [BaseEstimator][sklearn.base.BaseEstimator] object

    Raises:
        FedbiomedModelError: raised if model does not belong to the implemented models.
        FedbiomedModelError: raised if `__name__` attribute does not belong to object. This may happen
            when passing an instantiated object instead of the class object (e.g. instance of
            SGDClassifier() instead of SGDClassifier object)
    """
    if not isinstance(model, type):
        raise FedbiomedModelError(
            f"{ErrorNumbers.FB622.value}: 'SkLearnModel' received a '{type(model)}' instance as 'model' "
            "input while it was expecting a scikit-learn BaseEstimator subclass constructor."
        )
    if not issubclass(model, BaseEstimator):
        raise FedbiomedModelError(
            f"{ErrorNumbers.FB622.value}: 'SkLearnModel' received a 'model' class that is not "
            f"a scikit-learn BaseEstimator subclass: '{model}'."
        )
    if model.__name__ not in SKLEARN_MODELS:
        raise FedbiomedModelError(
            f"{ErrorNumbers.FB622.value}: 'SkLearnModel' received '{model}' as 'model' class, "
            f"support for which has not yet been implemented in Fed-BioMed."
        )
    self._instance: BaseSkLearnModel = SKLEARN_MODELS[model.__name__](model())

TorchModel

TorchModel(model)

Bases: Model

PyTorch model wrapper that ease the handling of a pytorch model

Attributes:

Name Type Description
model Module

torch.nn.Module. Pytorch model wrapped.

init_params Dict[str, Tensor]

OrderedDict. Model initial parameters. Set when calling init_training.

Source code in fedbiomed/common/models/_torch.py
def __init__(self, model: torch.nn.Module) -> None:
    """Instantiates the wrapper over a torch Module instance."""
    super().__init__(model)
    self.init_params: Dict[str, torch.Tensor] = {}

Attributes

Functions