/
regression.py
575 lines (481 loc) · 17.7 KB
/
regression.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from cuml.dask.common.utils import get_client
from cuml.dask.common.input_utils import DistributedDataHandler
from cuml.common import with_cupy_rmm
from cuml.dask.datasets.utils import _create_delayed
from cuml.dask.datasets.utils import _get_labels
from cuml.dask.datasets.utils import _get_X
from cuml.internals.safe_imports import gpu_only_import
import dask.array as da
from cuml.internals.safe_imports import cpu_only_import
np = cpu_only_import("numpy")
cp = gpu_only_import("cupy")
def _create_rs_generator(random_state):
if hasattr(random_state, "__module__"):
rs_type = random_state.__module__ + "." + type(random_state).__name__
else:
rs_type = type(random_state).__name__
rs = None
if rs_type == "NoneType" or rs_type == "int":
rs = da.random.RandomState(
seed=random_state, RandomState=cp.random.RandomState
)
elif rs_type == "cupy.random.generator.RandomState":
rs = da.random.RandomState(RandomState=random_state)
elif rs_type == "dask.array.random.RandomState":
rs = random_state
else:
raise ValueError(
"random_state type must be int, CuPy RandomState \
or Dask RandomState"
)
return rs
def _dask_f_order_standard_normal(nrows, ncols, dtype, seed):
local_rs = cp.random.RandomState(seed=seed)
x = local_rs.standard_normal(nrows * ncols, dtype=dtype)
x = x.reshape((nrows, ncols), order="F")
return x
def _f_order_standard_normal(client, rs, chunksizes, ncols, dtype):
workers = list(client.has_what().keys())
n_chunks = len(chunksizes)
chunks_workers = (workers * n_chunks)[:n_chunks]
chunk_seeds = rs.permutation(len(chunksizes))
chunks = [
client.submit(
_dask_f_order_standard_normal,
chunksize,
ncols,
dtype,
chunk_seeds[idx],
workers=[chunks_workers[idx]],
pure=False,
)
for idx, chunksize in enumerate(chunksizes)
]
chunks_dela = _create_delayed(chunks, dtype, chunksizes, ncols)
return da.concatenate(chunks_dela, axis=0)
def _dask_data_from_multivariate_normal(
seed, covar, n_samples, n_features, dtype
):
mean = cp.zeros(n_features)
local_rs = cp.random.RandomState()
return local_rs.multivariate_normal(mean, covar, n_samples, dtype=dtype)
def _data_from_multivariate_normal(
client, rs, covar, chunksizes, n_features, dtype
):
workers = list(client.has_what().keys())
n_chunks = len(chunksizes)
chunks_workers = (workers * n_chunks)[:n_chunks]
chunk_seeds = rs.permutation(len(chunksizes))
data_parts = [
client.submit(
_dask_data_from_multivariate_normal,
chunk_seeds[idx],
covar,
chunksizes[idx],
n_features,
dtype,
workers=[chunks_workers[idx]],
pure=False,
)
for idx, chunk in enumerate(chunksizes)
]
data_dela = _create_delayed(data_parts, dtype, chunksizes, n_features)
return da.concatenate(data_dela, axis=0)
def _dask_shuffle(part, n_samples, seed, features_indices):
X, y = part[0], part[1]
local_rs = cp.random.RandomState(seed=seed)
samples_indices = local_rs.permutation(n_samples)
X[...] = X[samples_indices, :]
X[...] = X[:, features_indices]
y[...] = y[samples_indices, :]
return X, y
def _shuffle(
client,
rs,
X,
y,
chunksizes,
n_features,
features_indices,
n_targets,
dtype,
):
data_ddh = DistributedDataHandler.create(data=(X, y), client=client)
chunk_seeds = rs.permutation(len(chunksizes))
shuffled = [
client.submit(
_dask_shuffle,
part,
chunksizes[idx],
chunk_seeds[idx],
features_indices,
workers=[w],
pure=False,
)
for idx, (w, part) in enumerate(data_ddh.gpu_futures)
]
X_shuffled = [
client.submit(_get_X, f, pure=False) for idx, f in enumerate(shuffled)
]
y_shuffled = [
client.submit(_get_labels, f, pure=False)
for idx, f in enumerate(shuffled)
]
X_dela = _create_delayed(X_shuffled, dtype, chunksizes, n_features)
y_dela = _create_delayed(y_shuffled, dtype, chunksizes, n_targets)
return da.concatenate(X_dela, axis=0), da.concatenate(y_dela, axis=0)
def _convert_to_order(client, X, chunksizes, order, n_features, dtype):
X_ddh = DistributedDataHandler.create(data=X, client=client)
X_converted = [
client.submit(cp.array, X_part, copy=False, order=order, workers=[w])
for idx, (w, X_part) in enumerate(X_ddh.gpu_futures)
]
X_dela = _create_delayed(X_converted, dtype, chunksizes, n_features)
return da.concatenate(X_dela, axis=0)
def _generate_chunks_for_qr(total_size, min_size, n_parts):
n_total_per_part = max(1, int(total_size / n_parts))
if n_total_per_part > min_size:
min_size = n_total_per_part
n_partitions = int(max(1, total_size / min_size))
rest = total_size % (n_partitions * min_size)
chunks_list = [min_size for i in range(n_partitions - 1)]
chunks_list.append(min_size + rest)
return tuple(chunks_list)
def _generate_singular_values(
n, effective_rank, tail_strength, n_samples_per_part, dtype="float32"
):
# Index of the singular values
sing_ind = cp.arange(n, dtype=dtype)
# Build the singular profile by assembling signal and noise components
tmp = sing_ind / effective_rank
low_rank = (1 - tail_strength) * cp.exp(-1.0 * tmp**2)
tail = tail_strength * cp.exp(-0.1 * tmp)
s = low_rank + tail
return s
def _dask_make_low_rank_covariance(
n_features,
effective_rank,
tail_strength,
seed,
n_parts,
n_samples_per_part,
dtype,
):
"""
This approach is a faster approach than making X as a full low
rank matrix. Here, we take advantage of the fact that with
SVD, X * X^T = V * S^2 * V^T. This means that we can
generate a covariance matrix by generating only the right
eigen-vector and the squared, low-rank singular values.
With a memory usage of only O(n_features ^ 2) in this case, we pass
this covariance matrix to workers to generate each part of X
embarrassingly parallel from a multi-variate normal with mean 0
and generated covariance.
"""
local_rs = cp.random.RandomState(seed=seed)
m2 = local_rs.standard_normal((n_features, n_features), dtype=dtype)
v, _ = cp.linalg.qr(m2)
s = _generate_singular_values(
n_features, effective_rank, tail_strength, n_samples_per_part
)
v *= s**2
return cp.dot(v, cp.transpose(v))
def _make_low_rank_covariance(
client,
n_features,
effective_rank,
tail_strength,
seed,
n_parts,
n_samples_per_part,
dtype,
):
return client.submit(
_dask_make_low_rank_covariance,
n_features,
effective_rank,
tail_strength,
seed,
n_parts,
n_samples_per_part,
dtype,
)
def make_low_rank_matrix(
n_samples=100,
n_features=100,
effective_rank=10,
tail_strength=0.5,
random_state=None,
n_parts=1,
n_samples_per_part=None,
dtype="float32",
):
""" Generate a mostly low rank matrix with bell-shaped singular values
Parameters
----------
n_samples : int, optional (default=100)
The number of samples.
n_features : int, optional (default=100)
The number of features.
effective_rank : int, optional (default=10)
The approximate number of singular vectors required to explain most of
the data by linear combinations.
tail_strength : float between 0.0 and 1.0, optional (default=0.5)
The relative importance of the fat noisy tail of the singular values
profile.
random_state : int, CuPy RandomState instance, Dask RandomState instance \
or None (default)
Determines random number generation for dataset creation. Pass an int
for reproducible output across multiple function calls.
n_parts : int, optional (default=1)
The number of parts of work.
dtype: str, optional (default='float32')
dtype of generated data
Returns
-------
X : Dask-CuPy array of shape [n_samples, n_features]
The matrix.
"""
rs = _create_rs_generator(random_state)
n = min(n_samples, n_features)
# Random (ortho normal) vectors
m1 = rs.standard_normal(
(n_samples, n),
chunks=(_generate_chunks_for_qr(n_samples, n, n_parts), -1),
dtype=dtype,
)
u, _ = da.linalg.qr(m1)
m2 = rs.standard_normal(
(n, n_features),
chunks=(-1, _generate_chunks_for_qr(n_features, n, n_parts)),
dtype=dtype,
)
v, _ = da.linalg.qr(m2)
# For final multiplication
if n_samples_per_part is None:
n_samples_per_part = max(1, int(n_samples / n_parts))
u = u.rechunk({0: n_samples_per_part, 1: -1})
v = v.rechunk({0: n_samples_per_part, 1: -1})
local_s = _generate_singular_values(
n, effective_rank, tail_strength, n_samples_per_part
)
s = da.from_array(local_s, chunks=(int(n_samples_per_part),))
u *= s
return da.dot(u, v)
@with_cupy_rmm
def make_regression(
n_samples=100,
n_features=100,
n_informative=10,
n_targets=1,
bias=0.0,
effective_rank=None,
tail_strength=0.5,
noise=0.0,
shuffle=False,
coef=False,
random_state=None,
n_parts=1,
n_samples_per_part=None,
order="F",
dtype="float32",
client=None,
use_full_low_rank=True,
):
"""
Generate a random regression problem.
The input set can either be well conditioned (by default) or have a low
rank-fat tail singular profile.
The output is generated by applying a (potentially biased) random linear
regression model with "n_informative" nonzero regressors to the previously
generated input and some gaussian centered noise with some adjustable
scale.
Parameters
----------
n_samples : int, optional (default=100)
The number of samples.
n_features : int, optional (default=100)
The number of features.
n_informative : int, optional (default=10)
The number of informative features, i.e., the number of features used
to build the linear model used to generate the output.
n_targets : int, optional (default=1)
The number of regression targets, i.e., the dimension of the y output
vector associated with a sample. By default, the output is a scalar.
bias : float, optional (default=0.0)
The bias term in the underlying linear model.
effective_rank : int or None, optional (default=None)
if not None:
The approximate number of singular vectors required to explain most
of the input data by linear combinations. Using this kind of
singular spectrum in the input allows the generator to reproduce
the correlations often observed in practice.
if None:
The input set is well conditioned, centered and gaussian with
unit variance.
tail_strength : float between 0.0 and 1.0, optional (default=0.5)
The relative importance of the fat noisy tail of the singular values
profile if "effective_rank" is not None.
noise : float, optional (default=0.0)
The standard deviation of the gaussian noise applied to the output.
shuffle : boolean, optional (default=False)
Shuffle the samples and the features.
coef : boolean, optional (default=False)
If True, the coefficients of the underlying linear model are returned.
random_state : int, CuPy RandomState instance, Dask RandomState instance \
or None (default)
Determines random number generation for dataset creation. Pass an int
for reproducible output across multiple function calls.
n_parts : int, optional (default=1)
The number of parts of work.
order : str, optional (default='F')
Row-major or Col-major
dtype: str, optional (default='float32')
dtype of generated data
use_full_low_rank : boolean (default=True)
Whether to use the entire dataset to generate the low rank matrix.
If False, it creates a low rank covariance and uses the
corresponding covariance to generate a multivariate normal
distribution on the remaining chunks
Returns
-------
X : Dask-CuPy array of shape [n_samples, n_features]
The input samples.
y : Dask-CuPy array of shape [n_samples] or [n_samples, n_targets]
The output values.
coef : Dask-CuPy array of shape [n_features] \
or [n_features, n_targets], optional
The coefficient of the underlying linear model. It is returned only if
coef is True.
Notes
-----
Known Performance Limitations:
1. When `effective_rank` is set and `use_full_low_rank` is True, \
we cannot generate order `F` by construction, and an explicit \
transpose is performed on each part. This may cause memory to spike \
(other parameters make order `F` by construction)
2. When `n_targets > 1` and `order = 'F'` as above, we have to \
explicitly transpose the `y` array. If `coef = True`, then we also \
explicitly transpose the `ground_truth` array
3. When `shuffle = True` and `order = F`, there are memory spikes to \
shuffle the `F` order arrays
.. note:: If out-of-memory errors are encountered in any of the above
configurations, try increasing the `n_parts` parameter.
"""
client = get_client(client=client)
n_informative = min(n_features, n_informative)
rs = _create_rs_generator(random_state)
if n_samples_per_part is None:
n_samples_per_part = max(1, int(n_samples / n_parts))
data_chunksizes = [n_samples_per_part] * n_parts
data_chunksizes[-1] += n_samples % n_parts
data_chunksizes = tuple(data_chunksizes)
if effective_rank is None:
# Randomly generate a well conditioned input set
if order == "F":
X = _f_order_standard_normal(
client, rs, data_chunksizes, n_features, dtype
)
elif order == "C":
X = rs.standard_normal(
(n_samples, n_features),
chunks=(data_chunksizes, -1),
dtype=dtype,
)
else:
# Randomly generate a low rank, fat tail input set
if use_full_low_rank:
X = make_low_rank_matrix(
n_samples=n_samples,
n_features=n_features,
effective_rank=effective_rank,
tail_strength=tail_strength,
random_state=rs,
n_parts=n_parts,
n_samples_per_part=n_samples_per_part,
dtype=dtype,
)
X = X.rechunk({0: data_chunksizes, 1: -1})
else:
seed = int(rs.randint(n_samples).compute())
covar = _make_low_rank_covariance(
client,
n_features,
effective_rank,
tail_strength,
seed,
n_parts,
n_samples_per_part,
dtype,
)
X = _data_from_multivariate_normal(
client, rs, covar, data_chunksizes, n_features, dtype
)
X = _convert_to_order(
client, X, data_chunksizes, order, n_features, dtype
)
# Generate a ground truth model with only n_informative features being non
# zeros (the other features are not correlated to y and should be ignored
# by a sparsifying regularizers such as L1 or elastic net)
ground_truth = 100.0 * rs.standard_normal(
(n_informative, n_targets),
chunks=(n_samples_per_part, -1),
dtype=dtype,
)
y = da.dot(X[:, :n_informative], ground_truth) + bias
if n_informative != n_features:
zeroes = 0.0 * rs.standard_normal(
(n_features - n_informative, n_targets), dtype=dtype
)
ground_truth = da.concatenate([ground_truth, zeroes], axis=0)
ground_truth = ground_truth.rechunk(-1)
# Add noise
if noise > 0.0:
y += rs.normal(scale=noise, size=y.shape, dtype=dtype)
# Randomly permute samples and features
if shuffle:
features_indices = np.random.permutation(n_features)
X, y = _shuffle(
client,
rs,
X,
y,
data_chunksizes,
n_features,
features_indices,
n_targets,
dtype,
)
ground_truth = ground_truth[features_indices, :]
y = da.squeeze(y)
if order == "F" and n_targets > 1:
y = _convert_to_order(client, y, y.chunks[0], order, n_targets, dtype)
if coef:
ground_truth = _convert_to_order(
client,
ground_truth,
ground_truth.chunks[0],
order,
n_targets,
dtype,
)
if coef:
ground_truth = da.squeeze(ground_truth)
return X, y, ground_truth
else:
return X, y