74
74
from collections import defaultdict
75
75
import os .path
76
76
77
+ from sklearn .utils ._arpack import _init_arpack_v0
77
78
from sklearn .utils import gen_batches
78
79
from sklearn .utils .validation import check_random_state
79
80
from sklearn .utils .extmath import randomized_svd
@@ -256,7 +257,7 @@ def svd_timing(X, n_comps, n_iter, n_oversamples,
256
257
return U , mu , V , call_time
257
258
258
259
259
- def norm_diff (A , norm = 2 , msg = True ):
260
+ def norm_diff (A , norm = 2 , msg = True , random_state = None ):
260
261
"""
261
262
Compute the norm diff with the original matrix, when randomized
262
263
SVD is called with *params.
@@ -268,7 +269,11 @@ def norm_diff(A, norm=2, msg=True):
268
269
print ("... computing %s norm ..." % norm )
269
270
if norm == 2 :
270
271
# s = sp.linalg.norm(A, ord=2) # slow
271
- value = sp .sparse .linalg .svds (A , k = 1 , return_singular_vectors = False )
272
+ v0 = _init_arpack_v0 (min (A .shape ), random_state )
273
+ value = sp .sparse .linalg .svds (A ,
274
+ k = 1 ,
275
+ return_singular_vectors = False ,
276
+ v0 = v0 )
272
277
else :
273
278
if sp .sparse .issparse (A ):
274
279
value = sp .sparse .linalg .norm (A , ord = norm )
@@ -298,7 +303,7 @@ def bench_a(X, dataset_name, power_iter, n_oversamples, n_comps):
298
303
all_time = defaultdict (list )
299
304
if enable_spectral_norm :
300
305
all_spectral = defaultdict (list )
301
- X_spectral_norm = norm_diff (X , norm = 2 , msg = False )
306
+ X_spectral_norm = norm_diff (X , norm = 2 , msg = False , random_state = 0 )
302
307
all_frobenius = defaultdict (list )
303
308
X_fro_norm = norm_diff (X , norm = 'fro' , msg = False )
304
309
@@ -312,8 +317,9 @@ def bench_a(X, dataset_name, power_iter, n_oversamples, n_comps):
312
317
all_time [label ].append (time )
313
318
if enable_spectral_norm :
314
319
A = U .dot (np .diag (s ).dot (V ))
315
- all_spectral [label ].append (norm_diff (X - A , norm = 2 ) /
316
- X_spectral_norm )
320
+ all_spectral [label ].append (
321
+ norm_diff (X - A , norm = 2 , random_state = 0 ) / X_spectral_norm
322
+ )
317
323
f = scalable_frobenius_norm_discrepancy (X , U , s , V )
318
324
all_frobenius [label ].append (f / X_fro_norm )
319
325
@@ -327,8 +333,9 @@ def bench_a(X, dataset_name, power_iter, n_oversamples, n_comps):
327
333
all_time [label ].append (time )
328
334
if enable_spectral_norm :
329
335
A = U .dot (np .diag (s ).dot (V ))
330
- all_spectral [label ].append (norm_diff (X - A , norm = 2 ) /
331
- X_spectral_norm )
336
+ all_spectral [label ].append (
337
+ norm_diff (X - A , norm = 2 , random_state = 0 ) / X_spectral_norm
338
+ )
332
339
f = scalable_frobenius_norm_discrepancy (X , U , s , V )
333
340
all_frobenius [label ].append (f / X_fro_norm )
334
341
@@ -353,7 +360,7 @@ def bench_b(power_list):
353
360
for rank in ranks :
354
361
X = make_low_rank_matrix (effective_rank = rank , ** data_params )
355
362
if enable_spectral_norm :
356
- X_spectral_norm = norm_diff (X , norm = 2 , msg = False )
363
+ X_spectral_norm = norm_diff (X , norm = 2 , msg = False , random_state = 0 )
357
364
X_fro_norm = norm_diff (X , norm = 'fro' , msg = False )
358
365
359
366
for n_comp in [int (rank / 2 ), rank , rank * 2 ]:
@@ -364,8 +371,10 @@ def bench_b(power_list):
364
371
power_iteration_normalizer = 'LU' )
365
372
if enable_spectral_norm :
366
373
A = U .dot (np .diag (s ).dot (V ))
367
- all_spectral [label ].append (norm_diff (X - A , norm = 2 ) /
368
- X_spectral_norm )
374
+ all_spectral [label ].append (
375
+ norm_diff (X - A , norm = 2 , random_state = 0 ) /
376
+ X_spectral_norm
377
+ )
369
378
f = scalable_frobenius_norm_discrepancy (X , U , s , V )
370
379
all_frobenius [label ].append (f / X_fro_norm )
371
380
@@ -388,7 +397,7 @@ def bench_c(datasets, n_comps):
388
397
continue
389
398
390
399
if enable_spectral_norm :
391
- X_spectral_norm = norm_diff (X , norm = 2 , msg = False )
400
+ X_spectral_norm = norm_diff (X , norm = 2 , msg = False , random_state = 0 )
392
401
X_fro_norm = norm_diff (X , norm = 'fro' , msg = False )
393
402
n_comps = np .minimum (n_comps , np .min (X .shape ))
394
403
@@ -401,8 +410,9 @@ def bench_c(datasets, n_comps):
401
410
all_time [label ].append (time )
402
411
if enable_spectral_norm :
403
412
A = U .dot (np .diag (s ).dot (V ))
404
- all_spectral [label ].append (norm_diff (X - A , norm = 2 ) /
405
- X_spectral_norm )
413
+ all_spectral [label ].append (
414
+ norm_diff (X - A , norm = 2 , random_state = 0 ) / X_spectral_norm
415
+ )
406
416
f = scalable_frobenius_norm_discrepancy (X , U , s , V )
407
417
all_frobenius [label ].append (f / X_fro_norm )
408
418
@@ -415,8 +425,9 @@ def bench_c(datasets, n_comps):
415
425
all_time [label ].append (time )
416
426
if enable_spectral_norm :
417
427
A = U .dot (np .diag (s ).dot (V ))
418
- all_spectral [label ].append (norm_diff (X - A , norm = 2 ) /
419
- X_spectral_norm )
428
+ all_spectral [label ].append (
429
+ norm_diff (X - A , norm = 2 , random_state = 0 ) / X_spectral_norm
430
+ )
420
431
f = scalable_frobenius_norm_discrepancy (X , U , s , V )
421
432
all_frobenius [label ].append (f / X_fro_norm )
422
433
0 commit comments