MADlib
1.3 A newer version is available
User Documentation
|
This module implements elastic net regularization for linear and logistic regression problems.
elastic_net_train( tbl_source, tbl_result, col_dep_var, col_ind_var, regress_family, alpha, lambda_value, standardize, grouping_col, optimizer, optimizer_params, excluded, max_iter, tolerance )
Arguments
TEXT. The name of the table containing the training data.
TEXT. Name of the generated table containing the output model. The output table produced by the elastic_net_train() function has the following columns:
family | The regression type: 'gaussian' or 'binomial'. |
---|---|
features | An array of the features (independent variables) passed into the analysis. |
features_selected | An array of the features selected by the analysis. |
coef_nonzero | Fitting coefficients for the selected features. |
coef_all | Coefficients for all selected and unselected features |
intercept | Fitting intercept for the model. |
log_likelihood | The negative value of the first equation above (up to a constant depending on the data set). |
standardize | BOOLEAN. Whether the data was normalized (standardize argument was TRUE). |
iteration_run | The number of iterations executed. |
TEXT. An expression for the dependent variable.
Both col_dep_var and col_ind_var can be valid Postgres expressions. For example, col_dep_var = 'log(y+1)'
, and col_ind_var = 'array[exp(x[1]), x[2], 1/(1+x[3])]'
. In the binomial case, you can use a Boolean expression, for example, col_dep_var = 'y < 0'
.
TEXT. An expression for the independent variables. Use '*'
to specify all columns of tbl_source except those listed in the excluded string. If col_dep_var is a column name, it is automatically excluded from the independent variables. However, if col_dep_var is a valid Postgres expression, any column names used within the expression are only excluded if they are explicitly included in the excluded argument. It is a good idea to add all column names involved in the dependent variable expression to the excluded string.
TEXT. The regression type, either 'gaussian' ('linear') or 'binomial' ('logistic').
FLOAT8. Elastic net control parameter, value in [0, 1].
FLOAT8. Regularization parameter, positive.
BOOLEAN, default: TRUE. Whether to normalize the data. Setting this to TRUE usually yields better results and faster convergence.
TEXT, default: NULL. Not currently implemented. Any non-NULL value is ignored. An expression list used to group the input dataset into discrete groups, running one regression per group. Similar to the SQL GROUP BY
clause. When this value is NULL, no grouping is used and a single result model is generated.
TEXT, default: 'fista'. Name of optimizer, either 'fista' or 'igd'.
TEXT, default: NULL. Optimizer parameters, delimited with commas. The parameters differ depending on the value of optimizer. See the descriptions below for details.
TEXT, default: NULL. A comma-delimited list of column names excluded from features. For example, 'col1, col2'
. If the col_ind_var is an array, excluded is a list of the integer array positions to exclude, for example '1,2'
. If this argument is NULL or an empty string ''
, no columns are excluded.
INTEGER, default: 10000. The maximum number of iterations that are allowed.
When the elastic_net_train() optimizer argument value is 'fista', the optimizer_params argument is a string containing name-value pairs with the following format. (Line breaks are inserted for readability.)
'max_stepsize = <value>, eta = <value>, warmup = <value>, warmup_lambdas = <value>, warmup_lambda_no = <value>, warmup_tolerance = <value>, use_active_set = <value>, activeset_tolerance = <value>, random_stepsize = <value>'
Parameters
Default: 2. If stepsize does not work stepsize / eta is tried. Must be greater than 1.
Default: FALSE. If warmup is TRUE, a series of lambda values, which is strictly descent and ends at the lambda value that the user wants to calculate, is used. The larger lambda gives very sparse solution, and the sparse solution again is used as the initial guess for the next lambda's solution, which speeds up the computation for the next lambda. For larger data sets, this can sometimes accelerate the whole computation and may be faster than computation on only one lambda value.
Default: NULL. The lambda value series to use when warmup is True. The default is NULL, which means that lambda values will be automatically generated.
Default: 15. How many lambdas are used in warm-up. If warmup_lambdas is not NULL, this value is overridden by the number of provided lambda values.
The value of tolerance used during warmup. The default is the same as the tolerance argument.
Default: FALSE. If use_active_set is TRUE, an active-set method is used to speed up the computation. Considerable speedup is obtained by organizing the iterations around the active set of features—those with nonzero coefficients. After a complete cycle through all the variables, we iterate on only the active set until convergence. If another complete cycle does not change the active set, we are done, otherwise the process is repeated.
Default: the value of the tolerance argument. The value of tolerance used during active set calculation.
When the elastic_net_train() optimizer argument value is 'igd', the optimizer_params argument is a string containing name-value pairs with the following format. (Line breaks are inserted for readability.)
'stepsize = <value>, step_decay = <value>, threshold = <value>, warmup = <value>, warmup_lambdas = <value>, warmup_lambda_no = <value>, warmup_tolerance = <value>, parallel = <value>'
Parameters
Default: 1e-10. When a coefficient is really small, set this coefficient to be 0.
Due to the stochastic nature of SGD, we can only obtain very small values for the fitting coefficients. Therefore, threshold is needed at the end of the computation to screen out tiny values and hard-set them to zeros. This is accomplished as follows: (1) multiply each coefficient with the standard deviation of the corresponding feature; (2) compute the average of absolute values of re-scaled coefficients; (3) divide each rescaled coefficient with the average, and if the resulting absolute value is smaller than threshold, set the original coefficient to zero.
Whether to run the computation on multiple segments. The default is True.
SGD is a sequential algorithm in nature. When running in a distributed manner, each segment of the data runs its own SGD model and then the models are averaged to get a model for each iteration. This averaging might slow down the convergence speed, although we also acquire the ability to process large datasets on multiple machines. This algorithm, therefore, provides the parallel option to allow you to choose whether to do parallel computation.
elastic_net_predict( regress_family, coefficients, intercept, ind_var )Arguments
There are several different formats of the prediction function:
SELECT madlib.elastic_net_gaussian_predict( coefficients, intercept, ind_var ) FROM tbl_result, tbl_new_source LIMIT 10;
SELECT madlib.elastic_net_binomial_predict ( coefficients, intercept, ind_var ) FROM tbl_result, tbl_new_source LIMIT 10;
SELECT madlib.elastic_net_binomial_prob( coefficients, intercept, ind_var coefficients, intercept, ind_var ) FROM tbl_result, tbl_new_source LIMIT 10;
Alternatively, you can use another prediction function that stores the prediction result in a table. This is useful if you want to use elastic net together with the general cross validation function.
SELECT madlib.elastic_net_predict( tbl_model, tbl_new_sourcedata, col_id, tbl_predict );
Arguments
You do not need to specify whether the model is "linear" or "logistic" because this information is already included in the tbl_model table.
SELECT madlib.elastic_net_train();
DROP TABLE IF EXISTS houses; CREATE TABLE houses ( id INT, tax INT, bedroom INT, bath FLOAT, price INT, size INT, lot INT ); COPY houses FROM STDIN WITH DELIMITER '|'; 1 | 590 | 2 | 1 | 50000 | 770 | 22100 2 | 1050 | 3 | 2 | 85000 | 1410 | 12000 3 | 20 | 3 | 1 | 22500 | 1060 | 3500 4 | 870 | 2 | 2 | 90000 | 1300 | 17500 5 | 1320 | 3 | 2 | 133000 | 1500 | 30000 6 | 1350 | 2 | 1 | 90500 | 820 | 25700 7 | 2790 | 3 | 2.5 | 260000 | 2130 | 25000 8 | 680 | 2 | 1 | 142500 | 1170 | 22000 9 | 1840 | 3 | 2 | 160000 | 1500 | 19000 10 | 3680 | 4 | 2 | 240000 | 2790 | 20000 11 | 1660 | 3 | 1 | 87000 | 1030 | 17500 12 | 1620 | 3 | 2 | 118600 | 1250 | 20000 13 | 3100 | 3 | 2 | 140000 | 1760 | 38000 14 | 2070 | 2 | 3 | 148000 | 1550 | 14000 15 | 650 | 3 | 1.5 | 65000 | 1450 | 12000
DROP TABLE IF EXISTS houses_en; SELECT madlib.elastic_net_train( 'houses', 'houses_en', 'price', 'array[tax, bath, size]', 'gaussian', 0.5, 0.1, TRUE, NULL, 'fista', '', NULL, 10000, 1e-6 );
-- Turn on expanded display to make it easier to read results. \x on SELECT * FROM houses_en;
SELECT *, price - predict as residual FROM ( SELECT houses.*, madlib.elastic_net_predict( 'gaussian', m.coef_nonzero, m.intercept, ARRAY[tax,bath,size] ) as predict FROM houses, houses_en m) s;
elastic_net_train()
on a subset of the data with a limited max_iter before applying it to the full data set with a large max_iter. In the pre-run, you can adjust the parameters to get the best performance and then apply the best set of parameters to the whole data set.Elastic net regularization seeks to find a weight vector that, for any given training example set, minimizes:
\[\min_{w \in R^N} L(w) + \lambda \left(\frac{(1-\alpha)}{2} \|w\|_2^2 + \alpha \|w\|_1 \right)\]
where \(L\) is the metric function that the user wants to minimize. Here \( \alpha \in [0,1] \) and \( lambda \geq 0 \). If \(alpha = 0\), we have the ridge regularization (known also as Tikhonov regularization), and if \(\alpha = 1\), we have the LASSO regularization.
For the Gaussian response family (or linear model), we have
\[L(\vec{w}) = \frac{1}{2}\left[\frac{1}{M} \sum_{m=1}^M (w^{t} x_m + w_{0} - y_m)^2 \right] \]
For the Binomial response family (or logistic model), we have
\[ L(\vec{w}) = \sum_{m=1}^M\left[y_m \log\left(1 + e^{-(w_0 + \vec{w}\cdot\vec{x}_m)}\right) + (1-y_m) \log\left(1 + e^{w_0 + \vec{w}\cdot\vec{x}_m}\right)\right]\ , \]
where \(y_m \in {0,1}\).
To get better convergence, one can rescale the value of each element of x
\[ x' \leftarrow \frac{x - \bar{x}}{\sigma_x} \]
and for Gaussian case we also let
\[y' \leftarrow y - \bar{y} \]
and then minimize with the regularization terms. At the end of the calculation, the orginal scales will be restored and an intercept term will be obtained at the same time as a by-product.
Note that fitting after scaling is not equivalent to directly fitting.
[1] Elastic net regularization. http://en.wikipedia.org/wiki/Elastic_net_regularization
[2] Beck, A. and M. Teboulle (2009), A fast iterative shrinkage-thresholding algorithm for linear inverse problems. SIAM J. on Imaging Sciences 2(1), 183-202.
[3] Shai Shalev-Shwartz and Ambuj Tewari, Stochastic Methods for l1 Regularized Loss Minimization. Proceedings of the 26th International Conference on Machine Learning, Montreal, Canada, 2009.
File elastic_net.sql_in documenting the SQL functions.
grp_validation