-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelasticnet_af.hpp
More file actions
355 lines (321 loc) · 15 KB
/
elasticnet_af.hpp
File metadata and controls
355 lines (321 loc) · 15 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/// \file
/// elasticnet_af library
///
/// Copyright Filip Matzner 2024.
///
/// Use, modification and distribution is subject to the
/// MIT License. See the accompanying file LICENSE.
///
/// Project home: https://github.com/FloopCZ/elasticnet_af
#pragma once
#include <algorithm>
#include <arrayfire.h>
#include <cassert>
#include <fmt/format.h>
#include <numeric>
#include <optional>
#include <random>
#include <stdexcept>
namespace elasticnet_af {
/// Generate a geometric sequence of numbers (based on NumPy geomspace).
///
/// Expects two matrices of dimensions (1, n) with desired start and end values.
/// Returns a matrix of dimensions (num, n) with the corresponding geometric sequence in each
/// column.
inline af::array geomspace(const af::array& start, const af::array& end, long num)
{
assert(start.dims(0) == 1 && end.dims(0) == 1 && start.elements() == end.elements());
long n_seqs = start.elements();
af::array flip_mask = af::flat(end < start);
af::array log_start = af::log10(af::min(start, end));
af::array log_end = af::log10(af::max(start, end));
af::array xs = af::tile(af::seq(num), 1, n_seqs).as(start.type());
xs = af::pow(10., log_start + xs * (log_end - log_start) / (num - 1));
xs(af::span, flip_mask) = af::flip(xs(af::span, flip_mask), 0);
return xs;
}
/// Return 1 for positive values, 0 for zero and -1 for negative values.
inline af::array signum(const af::array& arr)
{
return -af::sign(arr) + af::sign(-arr);
}
/// ElasticNet optimizer.
///
/// Combination of ridge and lasso regression with the L1 and L2 regularization.
///
/// The ElasticNet model solves the optimization problem:
/// min_{B} 1/2 * ||Y - X @ B||^2 + lambda * (alpha * ||B||_1 + (1 - alpha) * ||B||_2^2)
/// where X is the input matrix with dimensions (n_samples, n_features), Y is the output matrix
/// with dimensions (n_samples, n_targets), B is the coefficients matrix with dimensions
/// (n_features, n_targets), lambda is the regularization strength, and alpha is the mixing
/// parameter between L1 and L2 regularization.
///
/// See the object constructor for more details on the parameters.
class ElasticNet {
public:
struct options {
double lambda = 0.01;
double alpha = 0.5;
double tol = 1e-8;
long path_len = 100;
long max_grad_steps = 1000;
bool standardize_var = true;
bool warm_start = false;
bool random_index = true;
};
protected:
options opts_;
af::array mean_;
af::array std_;
af::array nonzero_std_;
af::array B_star_;
af::array intercept_;
long n_predictors_;
long n_targets_;
af::dtype type_;
/// Store the mean and std of the input matrix.
void store_standardization_stats(const af::array& X, const std::optional<af::array>& W)
{
if (W.has_value()) {
mean_ = af::mean(X, *W, 0);
} else {
mean_ = af::mean(X, 0);
}
std_ = af::stdev(X, AF_VARIANCE_POPULATION, 0);
nonzero_std_ = af::where(af::flat(std_));
}
/// Standardize the input matrix to have zero mean and optionally unit variance.
///
/// Uses the standardization coefficients stored in the object.
/// Constant columns are left unchanged.
af::array standardize(af::array X) const
{
if (X.dims(1) != mean_.dims(1))
throw std::invalid_argument(fmt::format(
"The input matrix has a different number of columns ({}) than the stored mean ({}).",
X.dims(1), mean_.dims(1)));
X(af::span, nonzero_std_) -= mean_(0, nonzero_std_);
if (opts_.standardize_var) X(af::span, nonzero_std_) /= std_(0, nonzero_std_);
return X;
}
/// Denormalize the coefficients and intercept to be used on data that have not been normalized.
///
/// Auxiliary function called as a finalizer after fitting.
void destandardize_coefficients()
{
// Adapt the coefficients and intercept to non-standardized predictors.
if (opts_.standardize_var) B_star_ /= af::tile(std_(0, nonzero_std_).T(), 1, n_targets_);
intercept_ -= af::matmul(mean_(0, nonzero_std_), B_star_);
// Extend the coefficients to the full predictor matrix including the constant columns.
af::array B_star_full = af::constant(0, n_predictors_, n_targets_, type_);
B_star_full(nonzero_std_, af::span) = B_star_;
B_star_ = B_star_full;
}
public:
/// Create an ElasticNet object with the given parameters.
///
/// \param lambda The strength of the regularization.
/// \param alpha The mixing parameter between L1 and L2 regularization. The
/// larger the alpha, the more L1 regularization is used.
/// \param tol The tolerance in the coefficients update for the convergence of the algorithm.
/// \param path_len The number of lambda values to use in the pathwise descent.
/// \param max_grad_steps The maximum number of gradient steps to take.
/// \param standardize_var Standardize the input matrix to unit variance.
/// \param warm_start Warm start the algorithm from ridge regression solution. In this case,
/// the path_len parameter must be set to 1, because the first path step
/// lambda is set such that all the coefficients are zero.
/// \param random_index Randomly shuffle indices before every coordinate descent loop.
/// \throws std::invalid_argument If the parameters are invalid.
ElasticNet(options opts) : opts_(std::move(opts))
{
if (opts_.lambda < 0) throw std::invalid_argument("The lambda must be non-negative.");
if (opts_.alpha < 0 || opts_.alpha > 1)
throw std::invalid_argument("The alpha must be between 0 and 1.");
if (opts_.tol <= 0) throw std::invalid_argument("The tolerance must be positive.");
if (opts_.path_len < 0)
throw std::invalid_argument("The path length must be non-negative.");
if (opts_.warm_start && opts_.path_len != 1)
throw std::invalid_argument(
"The path length must be set to 1 when using warm start. Lambda path does not "
"provide any benefit when we already have all the coefficients nonzero because of "
"the warm start.");
}
/// Fit the ElasticNet model to the given input-output data.
///
/// \param X The input matrix with dimensions (n_samples, n_features).
/// \param Y The output matrix with dimensions (n_samples, n_targets).
/// \param W The weights for each observation with dimensions (n_samples, 1). The
/// weights are internally normalized to unit mean and are shared for all targets.
/// \throws std::invalid_argument If the input matrices have incompatible dimensions or types.
/// \return True if the algorithm converged, false otherwise.
bool fit(af::array X, af::array Y, std::optional<af::array> W = {})
{
n_predictors_ = X.dims(1);
n_targets_ = Y.dims(1);
type_ = X.type();
const long n_samples = X.dims(0);
// Check the parameters.
if (Y.dims(0) != n_samples)
throw std::invalid_argument(fmt::format(
"The input matrix X has {} rows, but the output matrix Y has {} rows.", n_samples,
Y.dims(0)));
if (Y.type() != type_) throw std::invalid_argument("X and Y must have the same data type.");
if (W.has_value()) {
if (W->dims() != af::dim4{n_samples})
throw std::invalid_argument("W must have dimensions (n_samples, 1).");
if (W->type() != type_)
throw std::invalid_argument("X and W must have the same data type.");
// Normalize weights.
*W /= af::mean(*W);
}
// Standardize the predictors.
store_standardization_stats(X, W);
if (nonzero_std_.elements() == 0)
throw std::invalid_argument("All columns of the input matrix are constant.");
X = standardize(std::move(X));
if (af::count<long>(nonzero_std_) != n_predictors_)
X = X(af::span, nonzero_std_); // Remove constant columns (std == 0).
const long n_nonconst_predictors = X.dims(1);
// Subtract the intercept from the targets.
if (W.has_value()) {
intercept_ = af::mean(Y, *W, 0);
} else {
intercept_ = af::mean(Y, 0);
}
Y -= intercept_;
// Apply the weights.
if (W.has_value()) {
af::array sqrt_W = af::sqrt(*W);
X *= sqrt_W;
Y *= sqrt_W;
}
// Initial guess are zero coefficients.
B_star_ = af::constant(0, n_nonconst_predictors, n_targets_, type_);
// With regularization disabled, simply return the least squares solution.
if (opts_.lambda == 0.) {
af::deviceGC(); // Call garbage collector before solve() to avoid OOM.
B_star_ = af::solve(X, Y);
destandardize_coefficients();
return true;
}
// Warm start from the ridge regression solution if requested.
if (opts_.warm_start) {
af::array reg = std::sqrt(opts_.lambda * (1. - opts_.alpha))
* af::identity(X.dims(1), X.dims(1), X.type());
af::array X_reg = af::join(0, X, std::move(reg));
af::array Y_reg = af::join(0, Y, af::constant(0, {X.dims(1), Y.dims(1)}, Y.type()));
af::deviceGC(); // Call garbage collector before solve() to avoid OOM.
B_star_ = af::solve(X_reg, Y_reg);
}
// Early return if there is nothing else to do.
if (opts_.path_len <= 0 || (opts_.warm_start && opts_.alpha == 0.)) {
destandardize_coefficients();
return true;
}
// Generate "the path" of lambda values for each target.
const af::array lambda_path = [&]() {
if (opts_.path_len == 1 || opts_.alpha < opts_.tol)
return af::constant(opts_.lambda, opts_.path_len, n_targets_, type_);
const af::array lambda_max =
af::max(af::abs(af::matmulTN(X, Y)), 0) / n_samples / opts_.alpha;
return geomspace(
lambda_max, af::constant(opts_.lambda, 1, n_targets_, type_), opts_.path_len);
}();
// Precompute covariance matrices.
const af::array X_X_covs = af::matmulTN(X, X);
const af::array Y_X_covs = af::matmulTN(Y, X);
// Coordinate array and random generator for selecting random indices.
std::vector<long> idxs(n_nonconst_predictors);
std::iota(idxs.begin(), idxs.end(), 0L);
std::seed_seq seed_seq{n_predictors_, n_targets_, n_samples, opts_.path_len};
std::minstd_rand idx_prng{seed_seq};
// Run the coordinate graient descent.
bool converged = true;
for (long path_step = 0; path_step < opts_.path_len; ++path_step) {
af::array lambda = lambda_path(path_step, af::span);
long grad_step = 0;
for (; grad_step < opts_.max_grad_steps; ++grad_step) {
af::array B = B_star_;
if (opts_.random_index) std::shuffle(idxs.begin(), idxs.end(), idx_prng);
// Use the covariance update rule with the precomputed Gram matrices.
for (long j : idxs) {
af::array cov_update = Y_X_covs(af::span, j).T()
- af::matmulTN(X_X_covs(af::span, j), B_star_)
+ X_X_covs(j, j) * B_star_(j, af::span);
af::array soft_update =
signum(cov_update) * af::max(af::abs(cov_update) - lambda * opts_.alpha, 0.);
B_star_(j, af::span) =
soft_update / (X_X_covs(j, j) + lambda * (1. - opts_.alpha));
}
// Terminating condition.
af::array B_star_max = af::max(af::abs(B_star_), 0);
af::array delta_ratio = af::max(af::abs(B_star_ - B), 0) / B_star_max;
if (af::count<long>(B_star_max) == 0 || af::allTrue<bool>(delta_ratio < opts_.tol))
break;
}
if (grad_step == opts_.max_grad_steps) {
converged = false;
break;
}
}
destandardize_coefficients();
return converged;
}
/// Predict the output values for the given input matrix.
///
/// \param X The input matrix with dimensions (n_samples, n_features).
/// \return The predicted output matrix with dimensions (n_samples, n_targets).
/// \throws std::logic_error If the model has not been fitted yet.
/// \throws std::invalid_argument If the input matrix has incompatible dimensions.
af::array predict(const af::array& X) const
{
if (B_star_.isempty()) throw std::logic_error("The model has not been fitted yet.");
if (X.dims(1) != mean_.dims(1))
throw std::invalid_argument(fmt::format(
"The input matrix has a different number of columns ({}) than the fitted matrix "
"({}).",
X.dims(1), mean_.dims(1)));
return intercept_ + af::matmul(X, B_star_);
}
/// Compute the ElasticNet cost.
///
/// \param X The input matrix with dimensions (n_samples, n_features).
/// \param Y The output matrix with dimensions (n_samples, n_targets).
/// \return The cost for each target dimensions (1, n_targets).
/// \throws std::logic_error Forwarded from \ref predict.
/// \throws std::invalid_argument If the output matrix has incompatible dimensions or forwarded
/// from \ref predict..
af::array cost(const af::array& X, const af::array& Y) const
{
if (Y.dims(1) != intercept_.dims(1))
throw std::invalid_argument(fmt::format(
"The output matrix has a different number of columns ({}) than the fitted matrix "
"({}).",
Y.dims(1), intercept_.dims(1)));
af::array sse = af::sum(af::pow(Y - predict(X), 2.), 0) / 2.;
af::array l2 = opts_.lambda * (1. - opts_.alpha) * af::sum(B_star_ * B_star_, 0) / 2.;
af::array l1 = opts_.lambda * opts_.alpha * af::sum(af::abs(B_star_), 0);
return sse + l2 + l1;
}
/// Return the coefficients of the model.
///
/// \param intercept Whether to include the intercept as the zeroth coefficient.
/// \return The coefficients matrix with dimensions (n_features, n_targets).
/// \throws std::logic_error If the model has not been fitted yet.
af::array coefficients(bool intercept = false) const
{
if (B_star_.isempty()) throw std::logic_error("The model has not been fitted yet.");
if (intercept) return af::join(0, intercept_, B_star_);
return B_star_;
}
/// Return the intercept of the model.
///
/// \return The intercept vector with dimensions (1, n_targets).
/// \throws std::logic_error If the model has not been fitted yet.
af::array intercept() const
{
if (B_star_.isempty()) throw std::logic_error("The model has not been fitted yet.");
return intercept_;
}
};
} // namespace elasticnet_af