Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Common Private Loss Module with tempita #20567

Merged
merged 133 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
f8362d7
ENH add common link function submodule
lorentzenchr Jan 2, 2021
afdb67e
ENH add common loss function submodule
lorentzenchr Jan 2, 2021
830b814
CLN replace deprecated np.int by int
lorentzenchr Apr 2, 2021
9504c89
DOC document default=1 for n_threads
lorentzenchr Apr 2, 2021
fb3bce2
CLN comments and line wrapping
lorentzenchr Apr 14, 2021
2c86bf4
CLN comments and doc
lorentzenchr Apr 14, 2021
d68c07e
BUG remove useless line of code
lorentzenchr Apr 14, 2021
3d9c800
CLN remove line that was commented out
lorentzenchr Apr 14, 2021
aba1b67
CLN nitpicks in comments and docstrings
lorentzenchr Apr 14, 2021
022e418
ENH set NPY_NO_DEPRECATED_API
lorentzenchr Apr 14, 2021
49bb402
MNT change NPY_1_13_API_VERSION to NPY_1_7_API_VERSION
lorentzenchr Apr 15, 2021
6d77090
MNT comment out NPY_NO_DEPRECATED_API
lorentzenchr Apr 15, 2021
ceda673
TST restructure domain test cases
lorentzenchr May 8, 2021
c73e3fa
DOC add losses to API reference
lorentzenchr May 9, 2021
e650522
MNT add classes to __init__
lorentzenchr May 9, 2021
a31d8fb
CLN fix import
lorentzenchr May 9, 2021
e5b6266
DOC minor docstring changes
lorentzenchr May 9, 2021
3492383
TST prefer docstring over comment
lorentzenchr May 10, 2021
9d86d82
ENH define loss.is_multiclass
lorentzenchr May 10, 2021
cc90e4d
DOC fix typos
lorentzenchr May 10, 2021
d0b48ac
CLN address review comments
lorentzenchr May 18, 2021
7794617
DOC small docstring improvements
lorentzenchr Jun 15, 2021
35b7423
TST test more losses in test_specific_fit_intercept_only
lorentzenchr Jun 15, 2021
b390002
FIX test_loss_boundary
lorentzenchr Jul 18, 2021
12b4634
ENH Tempita for losses
lorentzenchr Jul 18, 2021
061a41b
MNT apply black
lorentzenchr Jul 19, 2021
98f8877
TST replace np.quantile by np.percentile
lorentzenchr Jul 20, 2021
3f8ffe9
ENH make Interval a dataclass
lorentzenchr Aug 15, 2021
b5e61d2
DOC improve docstrings in link.py
lorentzenchr Aug 15, 2021
cfdd67c
MNT use numpy dtype instead of Python type
lorentzenchr Aug 15, 2021
73311e9
TST add negative intervals
lorentzenchr Aug 15, 2021
3692280
ENH add __post_init__ to class Interval
lorentzenchr Aug 15, 2021
4cb4d3d
MNT rename cython losses
lorentzenchr Aug 15, 2021
c13e3b1
TST loss.predict_proba
lorentzenchr Aug 15, 2021
390ff19
TST predict_proba and gradient_proba
lorentzenchr Aug 15, 2021
ebd9f40
MNT use is_multiclass in tests instead of n_classes <= 2
lorentzenchr Aug 15, 2021
60e0fc4
DOC docstring predict_proba and more
lorentzenchr Aug 15, 2021
386aae9
Merge branch 'main' into loss_module_tempita
lorentzenchr Aug 15, 2021
70199a4
MNT remove top_path from gen_from_templates
lorentzenchr Aug 15, 2021
5721077
CI add --allow-releaseinfo-change in circleci
lorentzenchr Aug 15, 2021
f53e3f8
TST test graceful squeezing
lorentzenchr Aug 17, 2021
df072e4
Merge branch 'main' into loss_module_tempita
lorentzenchr Aug 17, 2021
a5c1d3c
CLN no extra methods for HalfSquaredError
lorentzenchr Aug 17, 2021
7bee26b
TST remove testing if approx_hessian=True
lorentzenchr Aug 19, 2021
15b7c99
DOC remove loss module for classes.rst
lorentzenchr Aug 23, 2021
67a3069
TST that losses can be pickled
lorentzenchr Aug 27, 2021
696d18f
TST add test_loss_on_specific_values
lorentzenchr Aug 28, 2021
cef6e24
FIX make cython inheritance happy and losses pickable
lorentzenchr Aug 28, 2021
130306c
ENH support const memoryviews by ReadonlyWrapper
lorentzenchr Aug 29, 2021
27f3dea
address review comments
lorentzenchr Aug 30, 2021
86d659c
FEA add ReadonlyWrapper
lorentzenchr Aug 31, 2021
7d30abb
ENH add common link function submodule
lorentzenchr Jan 2, 2021
53a4774
ENH add common loss function submodule
lorentzenchr Jan 2, 2021
25012bc
CLN replace deprecated np.int by int
lorentzenchr Apr 2, 2021
d6c9307
DOC document default=1 for n_threads
lorentzenchr Apr 2, 2021
5682a72
CLN comments and line wrapping
lorentzenchr Apr 14, 2021
6ace462
CLN comments and doc
lorentzenchr Apr 14, 2021
1a5ae1c
BUG remove useless line of code
lorentzenchr Apr 14, 2021
af93e7b
CLN remove line that was commented out
lorentzenchr Apr 14, 2021
9ed2096
CLN nitpicks in comments and docstrings
lorentzenchr Apr 14, 2021
b5e8224
ENH set NPY_NO_DEPRECATED_API
lorentzenchr Apr 14, 2021
c5c0d55
MNT change NPY_1_13_API_VERSION to NPY_1_7_API_VERSION
lorentzenchr Apr 15, 2021
d7b105e
MNT comment out NPY_NO_DEPRECATED_API
lorentzenchr Apr 15, 2021
4b48294
TST restructure domain test cases
lorentzenchr May 8, 2021
a4572a4
DOC add losses to API reference
lorentzenchr May 9, 2021
64964d6
MNT add classes to __init__
lorentzenchr May 9, 2021
43b6269
CLN fix import
lorentzenchr May 9, 2021
f90049b
DOC minor docstring changes
lorentzenchr May 9, 2021
47691ff
TST prefer docstring over comment
lorentzenchr May 10, 2021
b2e0856
ENH define loss.is_multiclass
lorentzenchr May 10, 2021
35ce8d2
DOC fix typos
lorentzenchr May 10, 2021
5904c8b
CLN address review comments
lorentzenchr May 18, 2021
3a7122b
DOC small docstring improvements
lorentzenchr Jun 15, 2021
c3b7658
TST test more losses in test_specific_fit_intercept_only
lorentzenchr Jun 15, 2021
eae2def
FIX test_loss_boundary
lorentzenchr Jul 18, 2021
ec5fd02
ENH Tempita for losses
lorentzenchr Jul 18, 2021
a9d93b4
MNT apply black
lorentzenchr Jul 19, 2021
f8a024a
TST replace np.quantile by np.percentile
lorentzenchr Jul 20, 2021
dfcd078
ENH make Interval a dataclass
lorentzenchr Aug 15, 2021
b5c5bf5
DOC improve docstrings in link.py
lorentzenchr Aug 15, 2021
3d6a477
MNT use numpy dtype instead of Python type
lorentzenchr Aug 15, 2021
c2f0f8e
TST add negative intervals
lorentzenchr Aug 15, 2021
33cabc4
ENH add __post_init__ to class Interval
lorentzenchr Aug 15, 2021
4cd2826
MNT rename cython losses
lorentzenchr Aug 15, 2021
5f61a90
TST loss.predict_proba
lorentzenchr Aug 15, 2021
7e1af6c
TST predict_proba and gradient_proba
lorentzenchr Aug 15, 2021
fb1ab5a
MNT use is_multiclass in tests instead of n_classes <= 2
lorentzenchr Aug 15, 2021
bdb6d18
DOC docstring predict_proba and more
lorentzenchr Aug 15, 2021
0750300
MNT remove top_path from gen_from_templates
lorentzenchr Aug 15, 2021
04df8a9
TST test graceful squeezing
lorentzenchr Aug 17, 2021
86b9293
CLN no extra methods for HalfSquaredError
lorentzenchr Aug 17, 2021
330b98e
TST remove testing if approx_hessian=True
lorentzenchr Aug 19, 2021
d9b6bc8
DOC remove loss module for classes.rst
lorentzenchr Aug 23, 2021
96ab3ba
TST that losses can be pickled
lorentzenchr Aug 27, 2021
6c8136e
TST add test_loss_on_specific_values
lorentzenchr Aug 28, 2021
fa15691
FIX make cython inheritance happy and losses pickable
lorentzenchr Aug 28, 2021
202953b
ENH support const memoryviews by ReadonlyWrapper
lorentzenchr Aug 29, 2021
9a89ff6
address review comments
lorentzenchr Aug 30, 2021
5a54fbe
CLN nitpick
lorentzenchr Aug 31, 2021
2820be5
CLN import ReadonlyWrapper from utils
lorentzenchr Aug 31, 2021
73054cd
Merge
lorentzenchr Aug 31, 2021
310eca7
Merge branch 'main' into loss_module_tempita
lorentzenchr Sep 6, 2021
4525085
MNT replace ReadonlyWrapper by ReadonlyArrayWrapper
lorentzenchr Sep 6, 2021
439ee83
trigger CI
lorentzenchr Sep 6, 2021
a201fd0
MNT rename out parameters
lorentzenchr Oct 23, 2021
0c7c68b
CLN address review comments
lorentzenchr Oct 23, 2021
5f3b0a5
TST increase maxiter
lorentzenchr Oct 24, 2021
d6d1516
Merge branch 'main' into loss_module_tempita
lorentzenchr Oct 24, 2021
12f529e
Revert "TST increase maxiter"
lorentzenchr Oct 24, 2021
643ad05
MNT composition instead of inheritance
lorentzenchr Nov 9, 2021
cb3cacc
MNT interval_raw_prediction never used
lorentzenchr Nov 9, 2021
2147c61
CLN closs and link as args in __init__
lorentzenchr Nov 10, 2021
48fc50b
trigger CI
lorentzenchr Nov 10, 2021
1a031ff
trigger CI
lorentzenchr Nov 11, 2021
18e0c4e
Merge branch 'main' into loss_module_tempita
lorentzenchr Nov 11, 2021
0068c68
DEBUG print infos
lorentzenchr Nov 11, 2021
903be0b
DEBUG remove readonly_memmap test
lorentzenchr Nov 12, 2021
4492c58
Revert "DEBUG remove readonly_memmap test"
lorentzenchr Nov 12, 2021
ef3b9e7
TST skip test if data not aligned
lorentzenchr Nov 13, 2021
942c142
Revert "DEBUG print infos"
lorentzenchr Nov 13, 2021
0b1d618
TST zeros instead of empty initial guess
lorentzenchr Nov 13, 2021
c30535f
Revert "TST skip test if data not aligned"
lorentzenchr Nov 13, 2021
e9c551c
DEBUG set boundscheck=True
lorentzenchr Nov 13, 2021
88e543c
Revert "DEBUG set boundscheck=True"
lorentzenchr Nov 14, 2021
a0b3b86
CLN setup.py
lorentzenchr Nov 14, 2021
00328b2
CLN rename binary and categorical cross entropy to binomial and multi…
lorentzenchr Nov 15, 2021
54f78fc
Merge branch 'main' into loss_module_tempita
lorentzenchr Nov 15, 2021
d7d25b6
Merge branch 'main' into loss_module_tempita
lorentzenchr Nov 19, 2021
98d4790
trigger CI
lorentzenchr Nov 20, 2021
9d8b0e9
Merge branch 'main' into loss_module_tempita
lorentzenchr Nov 20, 2021
3b9403f
TST aligned create_memmap_backed_data
lorentzenchr Nov 20, 2021
f5949d3
FIX replace CyBinaryCrossEntropy by CyHalfBinomialLoss
lorentzenchr Nov 21, 2021
d967740
MNT remove Cython compiler directives due to #21512
lorentzenchr Nov 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 additions & 0 deletions doc/modules/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1646,3 +1646,27 @@ Recently deprecated

To be removed in 1.0 (renaming of 0.25)
---------------------------------------

.. _loss_function_ref:

:mod:`sklearn._loss`: Private Loss Function Classes
===========================================================

lorentzenchr marked this conversation as resolved.
Show resolved Hide resolved
.. automodule:: sklearn._loss
:no-members:
:no-inherited-members:

.. currentmodule:: sklearn

.. autosummary::
:toctree: generated/
:template: class.rst

_loss.HalfSquaredError
_loss.AbsoluteError
_loss.PinballLoss
_loss.HalfPoissonLoss
_loss.HalfGammaLoss
_loss.HalfTweedieLoss
_loss.BinaryCrossEntropy
_loss.CategoricalCrossEntropy
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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,
BinaryCrossEntropy,
CategoricalCrossEntropy,
)


__all__ = [
"HalfSquaredError",
"AbsoluteError",
"PinballLoss",
"HalfPoissonLoss",
"HalfGammaLoss",
"HalfTweedieLoss",
"BinaryCrossEntropy",
"CategoricalCrossEntropy",
]
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 inherited by children
lorentzenchr marked this conversation as resolved.
Show resolved Hide resolved
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 inherited by children
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 CyBinaryCrossEntropy(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