8000 MNT add __reduce__ to loss objects by adrinjalali · Pull Request #30356 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

MNT add __reduce__ to loss objects #30356

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

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions sklearn/_loss/_loss.pyx.tp
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,9 @@ cdef inline double_pair cgrad_hess_exponential(
cdef class CyLossFunction:
"""Base class for convex loss functions."""

def __reduce__(self):
Copy link
Member < 8000 /div>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it needed actually? It seems to me like CyLossFunction is only used as a base class and you are not not really meant to create an instance of CyLossFunction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed since not all losses have a parameter, which means they won't have our custom __reduce__ generated for them. Note the {{if param is not None}} around the __reduce__ implementation bellow.

return (self.__class__, ())

cdef double cy_loss(self, double y_true, double raw_prediction) noexcept nogil:
"""Compute the loss for a single sample.

Expand Down Expand Up @@ -1013,6 +1016,11 @@ cdef class {{name}}(CyLossFunction):
self.{{param}} = {{param}}
{{endif}}

{{if param is not None}}
def __reduce__(self):
return (self.__class__, (self.{{param}},))
{{endif}}

cdef inline double cy_loss(self, double y_true, double raw_prediction) noexcept nogil:
return {{closs}}(y_true, raw_prediction{{with_param}})

Expand Down
0