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 quandratic 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 quandratic loss function, which adopts three types of input forms: ‘plq’, ‘max’ and ‘points’.
- Parameters:
- quad_coef{dict-like} of {‘a’: [], ‘b’: [], ‘c’: []}
The quandratic 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’
The form of the input PLQ function.
- ‘plq’ for the PLQ form
In this form, cutpoints must be given explicitly.
- ‘max’ for the max form
The max form is a special form of the PLQ function, which is the pointwise maximum of several linear or quadratic functions. The cutpoints are not necessary in this form, since they will be automatically calculated.
- ‘points’ for the piecewise linear form based on given points
The piecewise linear form is a special form of the PLQ function, which is the piecewise linear function. The function will connect the given points to form a piecewise linear loss. For the first piece and the last piece related to infinity, they will be the same as their adjacent piece.
- 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)¶