|
| 1 | +:orphan: |
| 2 | + |
| 3 | +How to add a custom datafit |
| 4 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 | + |
| 6 | +.. _how: |
| 7 | + |
| 8 | +Motivated by generalized linear models but not limited to it, ``skglm`` solves problems of the form |
| 9 | + |
| 10 | +.. math:: |
| 11 | + \hat{\beta} \in |
| 12 | + \arg\min_{\beta \in \mathbb{R}^p} |
| 13 | + F(X\beta) + \Omega(\beta) |
| 14 | + := \sum_{i=1}^n f_i([X\beta]_i) + \sum_{j=1}^p \Omega_j(\beta_j) |
| 15 | + \enspace . |
| 16 | +
|
| 17 | +
|
| 18 | +Here, :math:`X \in \mathbb{R}^{n \times p}` denotes the design matrix with :math:`n` samples and :math:`p` features, |
| 19 | +and :math:`\beta \in \mathbb{R}^p` is the coefficient vector. |
| 20 | + |
| 21 | +skglm can solve any problems of this form with arbitrary smooth datafit :math:`F` and arbitrary penalty :math:`\Omega` whose proximal operator can be evaluated explicitly, by defining two classes: a ``Penalty`` and a ``Datafit``. |
| 22 | + |
| 23 | +They can then be passed to a :class:`~skglm.GeneralizedLinearEstimator`. |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + clf = GeneralizedLinearEstimator( |
| 28 | + MyDatafit(), |
| 29 | + MyPenalty(), |
| 30 | + ) |
| 31 | +
|
| 32 | +
|
| 33 | +A ``Datafit`` is a jitclass which must inherit from the ``BaseDatafit`` class: |
| 34 | + |
| 35 | +.. literalinclude:: ../skglm/datafits/base.py |
| 36 | + :pyobject: BaseDatafit |
| 37 | + |
| 38 | + |
| 39 | +To define a custom datafit, you need to implement the methods declared in the ``BaseDatafit`` class. |
| 40 | +One needs to overload at least the ``value`` and ``gradient`` methods for skglm to support the datafit. |
| 41 | +Optionally, overloading the methods with the suffix ``_sparse`` adds support for sparse datasets (CSC matrix). |
| 42 | +As an example, we show how to implement the Poisson datafit in skglm. |
| 43 | + |
| 44 | + |
| 45 | +A case in point: defining Poisson datafit |
| 46 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 47 | + |
| 48 | +First, this requires deriving some quantities used by the solvers like the gradient or the Hessian matrix of the datafit. |
| 49 | +With :math:`y \in \mathbb{R}^n` the target vector, the Poisson datafit reads |
| 50 | + |
| 51 | +.. math:: |
| 52 | + f(X\beta) = \frac{1}{n}\sum_{i=1}^n \exp([X\beta]_i) - y_i[X\beta]_i |
| 53 | + \enspace . |
| 54 | +
|
| 55 | +
|
| 56 | +Let's define some useful quantities to simplify our computations. For :math:`z \in \mathbb{R}^n` and :math:`\beta \in \mathbb{R}^p`, |
| 57 | + |
| 58 | +.. math:: |
| 59 | + f(z) = \sum_{i=1}^n f_i(z_i) \qquad F(\beta) = f(X\beta) |
| 60 | + \enspace . |
| 61 | +
|
| 62 | +
|
| 63 | +Computing the gradient of :math:`F` and its Hessian matrix yields |
| 64 | + |
| 65 | +.. math:: |
| 66 | + \nabla F(\beta) = X^{\top} \underbrace{\nabla f(X\beta)}_\textrm{raw grad} \qquad \nabla^2 F(\beta) = X^{\top} \underbrace{\nabla^2 f(X\beta)}_\textrm{raw hessian} X |
| 67 | + \enspace . |
| 68 | +
|
| 69 | +
|
| 70 | +Besides, it directly follows that |
| 71 | + |
| 72 | +.. math:: |
| 73 | + \nabla f(z) = (f_i'(z_i))_{1 \leq i \leq n} \qquad \nabla^2 f(z) = \textrm{diag}(f_i''(z_i))_{1 \leq i \leq n} |
| 74 | + \enspace . |
| 75 | +
|
| 76 | +
|
| 77 | +We can now apply these definitions to the Poisson datafit: |
| 78 | + |
| 79 | +.. math:: |
| 80 | + f_i(z_i) = \frac{1}{n} \left(\exp(z_i) - y_iz_i\right) |
| 81 | + \enspace . |
| 82 | +
|
| 83 | +
|
| 84 | +Therefore, |
| 85 | + |
| 86 | +.. math:: |
| 87 | + f_i'(z_i) = \frac{1}{n}(\exp(z_i) - y_i) \qquad f_i''(z_i) = \frac{1}{n}\exp(z_i) |
| 88 | + \enspace . |
| 89 | +
|
| 90 | +
|
| 91 | +Computing ``raw_grad`` and ``raw_hessian`` for the Poisson datafit yields |
| 92 | + |
| 93 | +.. math:: |
| 94 | + \nabla f(X\beta) = \frac{1}{n}(\exp([X\beta]_i) - y_i)_{1 \leq i \leq n} \qquad \nabla^2 f(X\beta) = \frac{1}{n}\textrm{diag}(\exp([X\beta]_i))_{1 \leq i \leq n} |
| 95 | + \enspace . |
| 96 | +
|
| 97 | +
|
| 98 | +Both ``raw_grad`` and ``raw_hessian`` are methods used by the ``ProxNewton`` solver. |
| 99 | +But other optimizers require different methods to be implemented. For instance, ``AndersonCD`` uses the ``gradient_scalar`` method: |
| 100 | +it is the derivative of the datafit with respect to the :math:`j`-th coordinate of :math:`\beta`. |
| 101 | + |
| 102 | +For the Poisson datafit, this yields |
| 103 | + |
| 104 | +.. math:: |
| 105 | + \frac{\partial F(\beta)}{\partial \beta_j} = \frac{1}{n} |
| 106 | + \sum_{i=1}^n X_{i,j} \left( |
| 107 | + \exp([X\beta]_i) - y |
| 108 | + \right) |
| 109 | + \enspace . |
| 110 | +
|
| 111 | +
|
| 112 | +When implementing these quantities in the ``Poisson`` datafit class, this gives: |
| 113 | + |
| 114 | +.. literalinclude:: ../skglm/datafits/single_task.py |
| 115 | + :pyobject: Poisson |
| 116 | + |
| 117 | + |
| 118 | +Note that we have not initialized any quantities in the ``initialize`` method. |
| 119 | +Usually it serves to compute a Lipschitz constant of the datafit, whose inverse is used by the solver as a step size. |
| 120 | +However, in this example, the Poisson datafit has no Lipschitz constant since the eigenvalues of the Hessian matrix are unbounded. |
| 121 | +This implies that a step size is not known in advance and a line search has to be performed at every epoch by the solver. |
0 commit comments