Skip to content

Commit

Permalink
MRG Common Private Loss Module with tempita (scikit-learn#20567)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorentzenchr authored and samronsin committed Nov 30, 2021
1 parent 4f4cef6 commit 001341e
Show file tree
Hide file tree
Showing 11 changed files with 3,644 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ _configtest.o.d
.mypy_cache/

# files generated from a template
sklearn/_loss/_loss.pyx
sklearn/utils/_seq_dataset.pyx
sklearn/utils/_seq_dataset.pxd
sklearn/utils/_weight_vector.pyx
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ allow_redefinition = True
[check-manifest]
# ignore files missing in VCS
ignore =
sklearn/_loss/_loss.pyx
sklearn/linear_model/_sag_fast.pyx
sklearn/utils/_seq_dataset.pyx
sklearn/utils/_seq_dataset.pxd
Expand Down
27 changes: 27 additions & 0 deletions sklearn/_loss/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
The :mod:`sklearn._loss` module includes loss function classes suitable for
fitting classification and regression tasks.
"""

from .loss import (
HalfSquaredError,
AbsoluteError,
PinballLoss,
HalfPoissonLoss,
HalfGammaLoss,
HalfTweedieLoss,
HalfBinomialLoss,
HalfMultinomialLoss,
)


__all__ = [
"HalfSquaredError",
"AbsoluteError",
"PinballLoss",
"HalfPoissonLoss",
"HalfGammaLoss",
"HalfTweedieLoss",
"HalfBinomialLoss",
"HalfMultinomialLoss",
]
75 changes: 75 additions & 0 deletions sklearn/_loss/_loss.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# cython: language_level=3

import numpy as np
cimport numpy as np

np.import_array()


# Fused types for y_true, y_pred, raw_prediction
ctypedef fused Y_DTYPE_C:
np.npy_float64
np.npy_float32


# Fused types for gradient and hessian
ctypedef fused G_DTYPE_C:
np.npy_float64
np.npy_float32


# Struct to return 2 doubles
ctypedef struct double_pair:
double val1
double val2


# C base class for loss functions
cdef class CyLossFunction:
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil


cdef class CyHalfSquaredError(CyLossFunction):
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil


cdef class CyAbsoluteError(CyLossFunction):
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil


cdef class CyPinballLoss(CyLossFunction):
cdef readonly double quantile # readonly makes it accessible from Python
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil


cdef class CyHalfPoissonLoss(CyLossFunction):
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil


cdef class CyHalfGammaLoss(CyLossFunction):
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil


cdef class CyHalfTweedieLoss(CyLossFunction):
cdef readonly double power # readonly makes it accessible from Python
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil


cdef class CyHalfBinomialLoss(CyLossFunction):
cdef double cy_loss(self, double y_true, double raw_prediction) nogil
cdef double cy_gradient(self, double y_true, double raw_prediction) nogil
cdef double_pair cy_grad_hess(self, double y_true, double raw_prediction) nogil
Loading

0 comments on commit 001341e

Please sign in to comment.