Source code for pygbm.loss

"""
This module contains the loss classes.

Specific losses are used for regression, binary classification or multiclass
classification.
"""
from abc import ABC, abstractmethod

from scipy.special import expit, logsumexp
import numpy as np
from numba import njit, prange

@njit
def _logsumexp(a):
"""logsumexp(x) = log(sum(exp(x)))

Custom logsumexp function with numerical stability, based on scipy's
logsumexp which is unfortunately not supported (neither is
np.logaddexp.reduce, which is equivalent). Only supports 1d arrays.
"""

a_max = np.amax(a)
if not np.isfinite(a_max):
a_max = 0

s = np.sum(np.exp(a - a_max))
return np.log(s) + a_max

@njit(fastmath=True)
def _expit(x):
# custom sigmoid because we cannot use that of scipy with numba
return 1 / (1 + np.exp(-x))

class BaseLoss(ABC):
"""Base class for a loss."""

Unless hessians are constant, arrays are initialized with undefined
values.

Parameters
----------
n_samples : int
The number of samples passed to fit()
prediction_dim : int
The dimension of a raw prediction, i.e. the number of trees
built at each iteration. Equals 1 for regression and binary
classification, or K where K is the number of classes for
multiclass classification.

Returns
-------
gradients : array-like, shape=(n_samples * prediction_dim)
hessians : array-like, shape=(n_samples * prediction_dim).
If hessians are constant (e.g. for LeastSquares loss, shape
is (1,) and the array is initialized to 1.
"""
shape = n_samples * prediction_dim
if self.hessian_is_constant:
hessians = np.ones(shape=1, dtype=np.float32)
else:
hessians = np.empty(shape=shape, dtype=np.float32)

@abstractmethod
def get_baseline_prediction(self, y_train, prediction_dim):
"""Return initial predictions (before the first iteration).

Parameters
----------
y_train : array-like, shape=(n_samples,)
The target training values.
prediction_dim : int
The dimension of one prediction: 1 for binary classification and
regression, n_classes for multiclass classification.

Returns
-------
baseline_prediction: float or array of shape (1, prediction_dim)
The baseline prediction.
"""
pass

@abstractmethod
raw_predictions):
"""Update gradients and hessians arrays, inplace.

The gradients (resp. hessians) are the first (resp. second) order
derivatives of the loss for each sample with respect to the
predictions of model, evaluated at iteration i - 1.

Parameters
----------
gradients : array-like, shape=(n_samples * prediction_dim)
The gradients (treated as OUT array).
hessians : array-like, shape=(n_samples * prediction_dim) or \
(1,)
The hessians (treated as OUT array).
y_true : array-like, shape=(n_samples,)
The true target values or each training sample.
raw_predictions : array-like, shape=(n_samples, prediction_dim)
The raw_predictions (i.e. values from the trees) of the tree
ensemble at iteration i - 1.
"""
pass

[docs]class LeastSquares(BaseLoss): """Least squares loss, for regression. For a given sample x_i, least squares loss is defined as:: loss(x_i) = (y_true_i - raw_pred_i)**2 """ hessian_is_constant = True def __call__(self, y_true, raw_predictions, average=True): # shape (n_samples, 1) --> (n_samples,). reshape(-1) is more likely to # return a view. raw_predictions = raw_predictions.reshape(-1) loss = np.power(y_true - raw_predictions, 2) return loss.mean() if average else loss
[docs] def get_baseline_prediction(self, y_train, prediction_dim): return np.mean(y_train)