plqcom.PLQLoss¶
PLQLoss: Piecewise Linear Quadratic Loss function, with Decomposition to ReLU-ReHU Composition Loss functions
Classes¶
PLQLoss is a class represents a continuous convex piecewise quadratic loss function, which adopts three types of |
Functions¶
|
|
|
|
|
Module Contents¶
- class plqcom.PLQLoss.PLQLoss(quad_coef=None, form='plq', cutpoints=np.empty(shape=(0,)), points=np.empty(shape=(0, 2)))¶
Bases:
objectPLQLoss is a class represents a continuous convex piecewise quadratic loss function, which adopts three types of input forms: ‘plq’, ‘max’ and ‘points’.
- Parameters:
- quad_coef{dict-like} of {‘a’: [], ‘b’: [], ‘c’: []}
The quadratic coefficients in pieces of the PLQLoss. The i-th piece Q is: a[i]* x**2 + b[i] * x + c[i]
- formstr, optional, default: ‘plq’
Input form of the PLQ function:
'plq','max', or'points'. For'plq', cutpoints must be given explicitly. For'max', cutpoints are computed from the pointwise maximum of quadratics. For'points', the function is piecewise linear through the given points; the first and last pieces extend the adjacent segments.- cutpoints{array-like} of float, optional, default: None
cutpoints of the PLQLoss, except -np.inf and np.inf
if the form is ‘max’ or ‘points’, the cutpoints is not necessary
if the form is ‘plq’, the cutpoints is necessary
- points{array-like} of (x,y) pairs [(x1, y1), (x2, y2), … (xn, yn)]
or {dict-like} of {‘x’: [x1, x2, …, xn], ‘y’: [y1, y2, … yn]} or {2d-array-like} of [[x1, x2, …, xn], [y1, y2, … yn]] optional, default: None
Points coordinates of the piecewise linear form of the PLQLoss. The PLQLoss will be constructed by straight lines between each two adjcent points according to their x coordinates. Two points with the same x coordinates will be rejected.
if the form is ‘points’, the points is necessary
if the form is ‘max’ or ‘plq’, the points is not necessary
Examples
>>> import numpy as np >>> from plqcom import PLQLoss >>> cutpoints = [0., 1.] >>> quad_coef = {'a': np.array([0., .5, 0.]), 'b': np.array([-1, 0., 1]), 'c': np.array([0., 0., -.5])} >>> random_loss = PLQLoss(quad_coef, cutpoints=cutpoints) >>> x = np.arange(-2,2,.05) >>> random_loss(x)
- cutpoints¶
- min_val¶
- min_knot¶
- __call__(x)¶
Evaluation of PLQLoss function.
- Parameters:
- x{array-like} of shape {n_samples}
- Training vector, where `n_samples` is the number of samples.
- Returns:
- y{array-like} of shape {n_samples}
The values of the PLQLoss function on each x y[j] = quad_coef[‘a’][i]*x[j]**2 + quad_coef[‘b’][i]*x[j] + quad_coef[‘c’][i], if cutpoints[i] < x[j] < cutpoints[i+1]
- plqcom.PLQLoss.max_to_plq(quad_coef)¶
- plqcom.PLQLoss.points_to_plq(points)¶
- plqcom.PLQLoss.merge_successive_intervals(quad_coef, cutpoints)¶