-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectives.py
49 lines (42 loc) · 1.76 KB
/
objectives.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
import xgboost as xgb
from typing import Tuple
from common import parameters, predictions
from utils import sigmoid
import warnings
#suppress warnings
warnings.filterwarnings('ignore')
# Customized Objective Function for negative log-likelihood loss
def gradient(predt: np.ndarray, dtrain: xgb.DMatrix) -> np.ndarray:
'''Compute the gradient for negative log-likelihood.'''
y = dtrain.get_label()
return -(y - predt)
def hessian(predt: np.ndarray, dtrain: xgb.DMatrix) -> np.ndarray:
'''Compute the hessian for negative log-likelihood.'''
return predt * (1 - predt)
def neg_logl(predt: np.ndarray,
dtrain: xgb.DMatrix) -> Tuple[np.ndarray, np.ndarray]:
'''Negative Log-likelihood objective. Transform from log-likelihood by
adding negative sign.
'''
predt = sigmoid(predt)
grad = gradient(predt, dtrain)
hess = hessian(predt, dtrain)
return grad, hess
# Customized Objective Function for negative log-likelihood loss and adjusted KL divergence
def gradient_np(predt: np.ndarray, dtrain: xgb.DMatrix) -> np.ndarray:
'''Compute the gradient for negative log-likelihood.'''
y = dtrain.get_label()
return -(y - predt - parameters['alpha'] * (predt - predictions['pf_model']))
def hessian_np(predt: np.ndarray, dtrain: xgb.DMatrix) -> np.ndarray:
'''Compute the hessian for negative log-likelihood.'''
return (1 + parameters['alpha']) * predt * (1 - predt)
def neg_logl_np(predt: np.ndarray,
dtrain: xgb.DMatrix) -> Tuple[np.ndarray, np.ndarray]:
'''Negative Log-likelihood objective. Transform from log-likelihood by
adding negative sign.
'''
predt = sigmoid(predt)
grad = gradient_np(predt, dtrain)
hess = hessian_np(predt, dtrain)
return grad, hess