mirror of
https://github.com/IBM/ai-privacy-toolkit.git
synced 2026-05-08 11:32:37 +02:00
Remove redundant code.
Use data wrappers in model wrapper APIs. More typing.
This commit is contained in:
parent
9f4d649934
commit
3d82db80c4
5 changed files with 57 additions and 166 deletions
|
|
@ -1,34 +1,34 @@
|
|||
from abc import ABC, abstractmethod
|
||||
from typing import Union, List, Any, Optional
|
||||
import numpy as np
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
class Model(ABC):
|
||||
from apt.utils.datasets import BaseDataset, DATA_ARRAY_TYPE
|
||||
|
||||
|
||||
class Model(metaclass=ABCMeta):
|
||||
"""
|
||||
Base class for ML model wrappers.
|
||||
Abstract base class for ML model wrappers.
|
||||
"""
|
||||
|
||||
def __init__(self, model: Any, **kwargs):
|
||||
"""
|
||||
Initialize a `Model` wrapper object.
|
||||
Initialize a `Model` wrapper object.
|
||||
|
||||
:param model: The original model object (of the underlying ML framework)
|
||||
:param model: The original model object (of the underlying ML framework)
|
||||
"""
|
||||
self._model = model
|
||||
|
||||
@abstractmethod
|
||||
def fit(self, x: np.ndarray, y: np.ndarray, **kwargs) -> None:
|
||||
def fit(self, train_data: BaseDataset, **kwargs) -> None:
|
||||
"""
|
||||
Fit the model using the training data `(x, y)`.
|
||||
Fit the model using the training data.
|
||||
|
||||
:param x: Training data.
|
||||
:type x: `np.ndarray` or `pandas.DataFrame`
|
||||
:param y: True labels.
|
||||
:type y: `np.ndarray` or `pandas.DataFrame`
|
||||
:param train_data: Training data.
|
||||
:type train_data: `BaseDataset`
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def predict(self, x: np.ndarray, **kwargs) -> np.ndarray:
|
||||
def predict(self, x: DATA_ARRAY_TYPE, **kwargs) -> DATA_ARRAY_TYPE:
|
||||
"""
|
||||
Perform predictions using the model for input `x`.
|
||||
|
||||
|
|
@ -46,67 +46,3 @@ class Model(ABC):
|
|||
:return: The model.
|
||||
"""
|
||||
return self._model
|
||||
|
||||
|
||||
class SingleOutputModel(Model):
|
||||
"""
|
||||
Wrapper class for ML models whose output is a single value (e.g., classification with label only output, regression).
|
||||
"""
|
||||
|
||||
|
||||
class MultipleOutputModel(Model):
|
||||
"""
|
||||
Wrapper class for ML models whose output is a vector (e.g., class probabilities or logits).
|
||||
"""
|
||||
|
||||
|
||||
class ModelWithLoss(Model):
|
||||
"""
|
||||
Wrapper class for ML models that support computing loss values for predictions.
|
||||
"""
|
||||
|
||||
def __init__(self, model: Any, loss: Optional[Any] = None, **kwargs):
|
||||
"""
|
||||
Initialize a `ModelWithLoss` wrapper object.
|
||||
|
||||
:param model: The original model object (of the underlying ML framework)
|
||||
:param loss: The loss function/object of the model (of the underlying ML framework)
|
||||
"""
|
||||
super().__init__(model, **kwargs)
|
||||
self._loss = loss
|
||||
|
||||
|
||||
# Probably not needed for now, as we will not be using these wrappers directly in ART.
|
||||
# @abstractmethod
|
||||
# def loss(self, x: np.ndarray, y: np.ndarray, **kwargs) -> np.ndarray:
|
||||
# """
|
||||
# Compute the loss of the model for samples `x`.
|
||||
#
|
||||
# :param x: Input samples.
|
||||
# :type x: `np.ndarray` or `pandas.DataFrame`
|
||||
# :param y: True labels.
|
||||
# :type y: `np.ndarray` or `pandas.DataFrame`
|
||||
# :return: Loss values.
|
||||
# """
|
||||
# raise NotImplementedError
|
||||
|
||||
|
||||
# Probably not needed for now, as we will not be using these wrappers directly in ART.
|
||||
# class ModelWithGradients(Model):
|
||||
# """
|
||||
# Wrapper class for ML models that support computing gradients.
|
||||
# """
|
||||
# @abstractmethod
|
||||
# def class_gradient(self, x: np.ndarray, label: Union[int, List[int], None] = None, **kwargs) -> np.ndarray:
|
||||
# """
|
||||
# Compute per-class derivatives w.r.t. input `x`.
|
||||
#
|
||||
# :param x: Input samples.
|
||||
# :type x: `np.ndarray` or `pandas.DataFrame`
|
||||
# :param label: Index of a specific class. If provided, the gradient of the specified class
|
||||
# is computed for all samples. Otherwise, gradients for all classes are computed for all samples.
|
||||
# :param label: int
|
||||
# :return: Gradients of input features w.r.t. each class in the form `(batch_size, nb_classes, input_shape)` when
|
||||
# computing for all classes, or `(batch_size, 1, input_shape)` when `label` is specified.
|
||||
# """
|
||||
# raise NotImplementedError
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue