TensorFox 1.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- TensorFox/Alternating_Least_Squares.py +264 -0
- TensorFox/Auxiliar.py +686 -0
- TensorFox/Compression.py +624 -0
- TensorFox/Conversion.py +494 -0
- TensorFox/Critical.py +3320 -0
- TensorFox/Display.py +466 -0
- TensorFox/GaussNewton.py +811 -0
- TensorFox/Initialization.py +386 -0
- TensorFox/MultilinearAlgebra.py +649 -0
- TensorFox/TensorFox.py +1194 -0
- TensorFox/TensorLearning.py +1103 -0
- TensorFox/__init__.py +7 -0
- tensorfox-1.2.2.dist-info/METADATA +257 -0
- tensorfox-1.2.2.dist-info/RECORD +17 -0
- tensorfox-1.2.2.dist-info/WHEEL +5 -0
- tensorfox-1.2.2.dist-info/licenses/LICENSE +674 -0
- tensorfox-1.2.2.dist-info/top_level.txt +1 -0
TensorFox/Compression.py
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Compression Module
|
|
3
|
+
==================
|
|
4
|
+
This module is responsible for all routines related to the compression of tensors, which amounts to computing its
|
|
5
|
+
MLSVD and truncating it.
|
|
6
|
+
|
|
7
|
+
References
|
|
8
|
+
==========
|
|
9
|
+
|
|
10
|
+
- L. De Lathauwer, B. De Moor, and J. Vandewalle, A Multilinear Singular Value Decomposition, SIAM J. Matrix Anal.
|
|
11
|
+
Appl., 21 (2000), pp. 1253-1278.
|
|
12
|
+
|
|
13
|
+
- N. Vannieuwenhoven, R. Vandebril, and K. Meerbergen, A new truncation strategy for the higher-order singular value
|
|
14
|
+
decomposition, SIAM J. Sci. Comput. 34 (2012), no. 2, A1027-A1052.
|
|
15
|
+
|
|
16
|
+
- https://en.wikipedia.org/wiki/Higher-order_singular_value_decomposition#Computation
|
|
17
|
+
|
|
18
|
+
- N. Halko, P. Martinsson, and J. Tropp. Finding structure with randomness: probabilistic algorithms for constructing
|
|
19
|
+
approximate matrix decompositions (2009).
|
|
20
|
+
|
|
21
|
+
- S. Voronin and P.Martinsson. RSVDPACK: Subroutines for computing partial singular value decompositions via
|
|
22
|
+
randomized sampling on single core, multi core, and GPU architectures (2015).
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
# Python modules
|
|
26
|
+
import sys
|
|
27
|
+
import time
|
|
28
|
+
import numpy as np
|
|
29
|
+
from numpy import identity, ones, empty, array, uint64, float32, float64, copy, sqrt, dot, ndarray, argmax, newaxis, sign, log10
|
|
30
|
+
from numpy.linalg import norm
|
|
31
|
+
from scipy import sparse
|
|
32
|
+
from scipy.linalg import qr, svd
|
|
33
|
+
import warnings
|
|
34
|
+
warnings.simplefilter(action='ignore', category=FutureWarning)
|
|
35
|
+
|
|
36
|
+
# Tensor Fox modules
|
|
37
|
+
import TensorFox.Auxiliar as aux
|
|
38
|
+
import TensorFox.Conversion as cnv
|
|
39
|
+
import TensorFox.MultilinearAlgebra as mlinalg
|
|
40
|
+
|
|
41
|
+
# try to import dot_product_mkl. It will be used in intermediate computations.
|
|
42
|
+
try:
|
|
43
|
+
from sparse_dot_mkl import dot_product_mkl
|
|
44
|
+
except:
|
|
45
|
+
print('-> Module sparse_dot_mkl could not be imported. Tensor Fox routine will be used for sparse matrix multiplications.\nFor more information see https://github.com/felipebottega/Tensor-Fox/blob/master/README.md#sparse-dot-mkl-requirements.', file=sys.stderr)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def mlsvd(T, Tsize, R, options):
|
|
49
|
+
"""
|
|
50
|
+
This function computes a truncated MLSVD of tensors of any order. The output is such that T = (U_1,...,U_L)*S, and
|
|
51
|
+
UT is the list of the transposes of U.
|
|
52
|
+
The parameter n_iter of the randomized SVD is set to 2. It is only good to increase this value when the tensor has
|
|
53
|
+
much noise. Still this issue is addressed by the low rank CPD approximation, so n_iter=2 is enough.
|
|
54
|
+
We remark that if T is given as a sparse tensor, the component 'data' must be a float32.
|
|
55
|
+
|
|
56
|
+
Inputs
|
|
57
|
+
------
|
|
58
|
+
T: float array
|
|
59
|
+
Objective tensor in coordinates.
|
|
60
|
+
Tsize: float
|
|
61
|
+
Frobenius norm of T.
|
|
62
|
+
R: int
|
|
63
|
+
An upper bound for the multilinear rank of T. Normally one will use the rank of T.
|
|
64
|
+
options: class with the parameters previously defined.
|
|
65
|
+
|
|
66
|
+
Outputs
|
|
67
|
+
-------
|
|
68
|
+
S: float array
|
|
69
|
+
Core tensor of the MLSVD.
|
|
70
|
+
U: list of float 2-D arrays
|
|
71
|
+
List with truncated matrices of the original U.
|
|
72
|
+
T1: float 2-D arrays
|
|
73
|
+
First unfolding of T.
|
|
74
|
+
sigmas: list of float 1-D arrays
|
|
75
|
+
List with truncated arrays of the original sigmas.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
# INITIALIZE RELEVANT VARIABLES.
|
|
79
|
+
|
|
80
|
+
sigmas = []
|
|
81
|
+
U = []
|
|
82
|
+
|
|
83
|
+
# Verify if T is sparse, in which case it will be given as a list with the data.
|
|
84
|
+
if type(T) == list:
|
|
85
|
+
data, idxs, dims = T
|
|
86
|
+
data = array(data, dtype=float64)
|
|
87
|
+
idxs = array(idxs, dtype=uint64)
|
|
88
|
+
dims = array(dims)
|
|
89
|
+
else:
|
|
90
|
+
dims = T.shape
|
|
91
|
+
L = len(dims)
|
|
92
|
+
|
|
93
|
+
# Set options.
|
|
94
|
+
options = aux.make_options(options)
|
|
95
|
+
trunc_dims = options.trunc_dims
|
|
96
|
+
display = options.display
|
|
97
|
+
mlsvd_method = options.mlsvd_method
|
|
98
|
+
tol_mlsvd = options.tol_mlsvd
|
|
99
|
+
mkl_dot = options.mkl_dot
|
|
100
|
+
svd_n_components = options.svd_n_components
|
|
101
|
+
if type(tol_mlsvd) == list:
|
|
102
|
+
if L > 3:
|
|
103
|
+
tol_mlsvd = tol_mlsvd[0]
|
|
104
|
+
else:
|
|
105
|
+
tol_mlsvd = tol_mlsvd[1]
|
|
106
|
+
gpu = options.gpu
|
|
107
|
+
if gpu:
|
|
108
|
+
import pycuda.gpuarray as gpuarray
|
|
109
|
+
import pycuda.autoinit
|
|
110
|
+
from skcuda import linalg, rlinalg
|
|
111
|
+
|
|
112
|
+
# tol_mlsvd = -1 means no truncation and no compression, that is, the original tensor.
|
|
113
|
+
if tol_mlsvd == -1:
|
|
114
|
+
T1 = cnv.unfold(T, 1)
|
|
115
|
+
U = [identity(dims[l]) for l in range(L)]
|
|
116
|
+
sigmas = [ones(dims[l]) for l in range(L)]
|
|
117
|
+
if display > 2 or display < -1:
|
|
118
|
+
return T, U, T1, sigmas, 0.0
|
|
119
|
+
else:
|
|
120
|
+
return T, U, T1, sigmas
|
|
121
|
+
|
|
122
|
+
# T is sparse.
|
|
123
|
+
elif type(T) == list:
|
|
124
|
+
for l in range(L):
|
|
125
|
+
if display != 0:
|
|
126
|
+
print(' Compressing unfolding mode', l+1)
|
|
127
|
+
Tl = cnv.sparse_unfold(data, idxs, dims, l+1)
|
|
128
|
+
if l == 0:
|
|
129
|
+
T1 = cnv.sparse_unfold(data, idxs, dims, l+1)
|
|
130
|
+
mlsvd_method = 'sparse'
|
|
131
|
+
U, sigmas, Vlt, dim = compute_svd(Tl, U, sigmas, dims, R, mlsvd_method, tol_mlsvd, gpu, mkl_dot, L, l, svd_n_components, display)
|
|
132
|
+
|
|
133
|
+
# Compute (U_1^T,...,U_L^T)*T = S.
|
|
134
|
+
UT = [U[l].T for l in range(L)]
|
|
135
|
+
if display != 0:
|
|
136
|
+
print(' Computing sparse multilinear multiplication')
|
|
137
|
+
S = mlinalg.sparse_multilin_mult(UT, data, idxs, dims)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
# Compute MLSVD base on sequentially truncated method.
|
|
141
|
+
elif mlsvd_method == 'seq':
|
|
142
|
+
S_dims = copy(dims)
|
|
143
|
+
S = T
|
|
144
|
+
for l in range(L):
|
|
145
|
+
if display != 0:
|
|
146
|
+
print(' Compressing unfolding mode', l+1)
|
|
147
|
+
Sl = cnv.unfold(S, l+1)
|
|
148
|
+
if l == 0:
|
|
149
|
+
T1 = cnv.unfold_C(S, l+1)
|
|
150
|
+
U, sigmas, Vlt, dim = compute_svd(Sl, U, sigmas, dims, R, mlsvd_method, tol_mlsvd, gpu, mkl_dot, L, l, svd_n_components, display)
|
|
151
|
+
|
|
152
|
+
# Compute l-th unfolding of S truncated at the l-th mode.
|
|
153
|
+
Sl = (Vlt.T * sigmas[-1]).T
|
|
154
|
+
S_dims[l] = dim
|
|
155
|
+
S = empty(S_dims, dtype=float64)
|
|
156
|
+
S = cnv.foldback(S, Sl, l+1)
|
|
157
|
+
|
|
158
|
+
# Compute MLSVD based on classic method.
|
|
159
|
+
elif mlsvd_method == 'classic':
|
|
160
|
+
for l in range(L):
|
|
161
|
+
if display != 0:
|
|
162
|
+
print(' Compressing unfolding mode', l+1)
|
|
163
|
+
Tl = cnv.unfold(T, l+1)
|
|
164
|
+
if l == 0:
|
|
165
|
+
T1 = cnv.unfold_C(T, l+1)
|
|
166
|
+
U, sigmas, Vlt, dim = compute_svd(Tl, U, sigmas, dims, R, mlsvd_method, tol_mlsvd, gpu, mkl_dot, L, l, svd_n_components, display)
|
|
167
|
+
|
|
168
|
+
# Compute (U_1^T,...,U_L^T)*T = S.
|
|
169
|
+
UT = [U[l].T for l in range(L)]
|
|
170
|
+
S = mlinalg.multilin_mult(UT, T1, dims)
|
|
171
|
+
|
|
172
|
+
# Specific truncation is given by the user.
|
|
173
|
+
if type(trunc_dims) == list:
|
|
174
|
+
slices = []
|
|
175
|
+
for l in range(L):
|
|
176
|
+
slices.append(slice(0, trunc_dims[l]))
|
|
177
|
+
U[l] = U[l][:, :min(trunc_dims[l], U[l].shape[1])] # trunc_dims[l] can't be higher than U[l].shape[1]
|
|
178
|
+
S = S[tuple(slices)]
|
|
179
|
+
|
|
180
|
+
# Compute error of compressed tensor.
|
|
181
|
+
if display > 2 or display < -1:
|
|
182
|
+
if type(T) == list:
|
|
183
|
+
best_error = mlinalg.compute_error(T, Tsize, S, U, dims)
|
|
184
|
+
else:
|
|
185
|
+
S1 = cnv.unfold(S, 1)
|
|
186
|
+
best_error = mlinalg.compute_error(T, Tsize, S1, U, S.shape)
|
|
187
|
+
return S, U, T1, sigmas, best_error
|
|
188
|
+
|
|
189
|
+
return S, U, T1, sigmas
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def compute_svd(Tl, U, sigmas, dims, R, mlsvd_method, tol_mlsvd, gpu, mkl_dot, L, l, svd_n_components, display):
|
|
193
|
+
"""
|
|
194
|
+
Subroutine of the function mlsvd. This function performs the SVD of a given unfolding.
|
|
195
|
+
"""
|
|
196
|
+
|
|
197
|
+
low_rank = min(R, dims[l])
|
|
198
|
+
|
|
199
|
+
if gpu:
|
|
200
|
+
if mlsvd_method == 'gpu' or mlsvd_method == 'sparse':
|
|
201
|
+
tmp = array(dot(Tl, Tl.T), dtype=float32, order='F')
|
|
202
|
+
Tl_gpu = gpuarray.to_gpu(tmp)
|
|
203
|
+
Ul, sigma_l, Vlt = rlinalg.rsvd(Tl_gpu, k=low_rank, p=10, q=2, method='standard')
|
|
204
|
+
sigma_l = sqrt(sigma_l)
|
|
205
|
+
else:
|
|
206
|
+
tmp = array(Tl.T, dtype=float32)
|
|
207
|
+
Tl_gpu = gpuarray.to_gpu(tmp)
|
|
208
|
+
Ul, sigma_l, Vlt = rlinalg.rsvd(Tl_gpu, k=low_rank, p=10, q=2, method='standard')
|
|
209
|
+
|
|
210
|
+
else:
|
|
211
|
+
if mlsvd_method == 'sparse':
|
|
212
|
+
if mkl_dot:
|
|
213
|
+
Tl = sparse_dot_mkl_call(Tl, display)
|
|
214
|
+
else:
|
|
215
|
+
Tl = mlinalg.sparse_dot(Tl)
|
|
216
|
+
# Number of singular values and vectors to extract.
|
|
217
|
+
if svd_n_components is None:
|
|
218
|
+
n_components = int(max(L, log10(dims[l]*dims[l]*L)) * low_rank)
|
|
219
|
+
else:
|
|
220
|
+
n_components = svd_n_components
|
|
221
|
+
Ul, sigma_l, Vlt = randomized_svd(Tl, n_components, mkl_dot, n_oversamples=10, n_iter=2)
|
|
222
|
+
sigma_l = sqrt(sigma_l)
|
|
223
|
+
|
|
224
|
+
else:
|
|
225
|
+
# Number of singular values and vectors to extract.
|
|
226
|
+
if svd_n_components is None:
|
|
227
|
+
n_components = low_rank
|
|
228
|
+
else:
|
|
229
|
+
n_components = svd_n_components
|
|
230
|
+
Ul, sigma_l, Vlt = randomized_svd(Tl, n_components, mkl_dot, n_oversamples=10, n_iter=2)
|
|
231
|
+
|
|
232
|
+
# Truncate more based on energy.
|
|
233
|
+
Ul, sigma_l, Vlt, dim = clean_compression(Ul, sigma_l, Vlt, tol_mlsvd, L)
|
|
234
|
+
sigmas.append(sigma_l)
|
|
235
|
+
U.append(Ul)
|
|
236
|
+
|
|
237
|
+
return U, sigmas, Vlt, dim
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def sparse_dot_mkl_call(Tl, display):
|
|
241
|
+
"""
|
|
242
|
+
This function tries to compute the product dot(Tl, Tl.T) with the method from the package sparse_dot_mkl. If this
|
|
243
|
+
method fails, the program calls the function sparse_dot.
|
|
244
|
+
"""
|
|
245
|
+
|
|
246
|
+
# Defines environment variable and make import.
|
|
247
|
+
try:
|
|
248
|
+
# Set MKL interface layer to int64 before importing the package. This handles bigger tensors.
|
|
249
|
+
import os
|
|
250
|
+
os.environ["MKL_INTERFACE_LAYER"] = "ILP64"
|
|
251
|
+
from sparse_dot_mkl import dot_product_mkl
|
|
252
|
+
TlT = Tl.T
|
|
253
|
+
Tl = dot_product_mkl(Tl, TlT, copy=False, dense=True)
|
|
254
|
+
except Exception as e:
|
|
255
|
+
print('-> ', str(e) + '.\n-> Using sparse-sparse dot from Tensor Fox.', file=sys.stderr)
|
|
256
|
+
Tl = mlinalg.sparse_dot(Tl)
|
|
257
|
+
|
|
258
|
+
return Tl
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def clean_compression(U, sigma, Vt, tol_mlsvd, L):
|
|
262
|
+
"""
|
|
263
|
+
This function try different threshold values to truncate the mlsvd. The conditions to accept a truncation are
|
|
264
|
+
defined by the parameter level. Higher level means harder constraints, which translates to bigger tensors after the
|
|
265
|
+
truncation.
|
|
266
|
+
|
|
267
|
+
Inputs
|
|
268
|
+
------
|
|
269
|
+
U, sigma, Vt: float arrays
|
|
270
|
+
Arrays of some SVD of the form M = U * diag(sigma) * V^T.
|
|
271
|
+
tol_mlsvd: float
|
|
272
|
+
Tolerance criterion for the truncation. The idea is to obtain a truncation (U_1,...,U_L)*S such that
|
|
273
|
+
|T - (U_1,...,U_L)*S| / |T| < tol_mlsvd.
|
|
274
|
+
L: int
|
|
275
|
+
Number of modes of the original tensor.
|
|
276
|
+
|
|
277
|
+
Outputs
|
|
278
|
+
-------
|
|
279
|
+
U, sigma, Vt: float ndarrays
|
|
280
|
+
Arrays of the SVD after truncation.
|
|
281
|
+
dim: int
|
|
282
|
+
New dimension size after truncation.
|
|
283
|
+
"""
|
|
284
|
+
|
|
285
|
+
# INITIALIZE RELEVANT VARIABLES.
|
|
286
|
+
eps = tol_mlsvd/L
|
|
287
|
+
|
|
288
|
+
# COMPUTE TRUNCATION.
|
|
289
|
+
sigma_len = len(sigma)
|
|
290
|
+
for i in range(1, sigma_len):
|
|
291
|
+
Tl_trunc_error = np.sum(sigma[i:]**2)/np.sum(sigma**2)
|
|
292
|
+
if Tl_trunc_error < eps:
|
|
293
|
+
sigma = sigma[:i]
|
|
294
|
+
U = U[:, :i]
|
|
295
|
+
Vt = Vt[:i, :]
|
|
296
|
+
break
|
|
297
|
+
|
|
298
|
+
# Size of truncation.
|
|
299
|
+
dim = sigma.size
|
|
300
|
+
|
|
301
|
+
return U, sigma, Vt, dim
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def safe_sparse_dot(a, b, mkl_dot):
|
|
305
|
+
"""
|
|
306
|
+
Dot product that handle the sparse matrix case correctly
|
|
307
|
+
|
|
308
|
+
Parameters
|
|
309
|
+
----------
|
|
310
|
+
a : array or sparse matrix
|
|
311
|
+
b : array or sparse matrix
|
|
312
|
+
|
|
313
|
+
Returns
|
|
314
|
+
-------
|
|
315
|
+
dot_product : array or sparse matrix
|
|
316
|
+
sparse if a and b are sparse.
|
|
317
|
+
"""
|
|
318
|
+
|
|
319
|
+
if mkl_dot:
|
|
320
|
+
try:
|
|
321
|
+
from sparse_dot_mkl import dot_product_mkl
|
|
322
|
+
except:
|
|
323
|
+
mkl_dot = False
|
|
324
|
+
if mkl_dot:
|
|
325
|
+
if (sparse.issparse(a) and a.getformat() == 'csr') or (sparse.issparse(b) and b.getformat() == 'csr'):
|
|
326
|
+
ret = dot_product_mkl(a, b, copy=False, dense=True)
|
|
327
|
+
else:
|
|
328
|
+
ret = a @ b
|
|
329
|
+
else:
|
|
330
|
+
ret = a @ b
|
|
331
|
+
else:
|
|
332
|
+
ret = a @ b
|
|
333
|
+
|
|
334
|
+
return ret
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def randomized_range_finder(A, mkl_dot, size, n_iter, random_state=None):
|
|
338
|
+
"""
|
|
339
|
+
Computes an orthonormal matrix whose range approximates the range of A.
|
|
340
|
+
|
|
341
|
+
Parameters
|
|
342
|
+
----------
|
|
343
|
+
A : 2D array
|
|
344
|
+
The input data matrix
|
|
345
|
+
|
|
346
|
+
size : integer
|
|
347
|
+
Size of the return array
|
|
348
|
+
|
|
349
|
+
n_iter : integer
|
|
350
|
+
Number of power iterations used to stabilize the result
|
|
351
|
+
|
|
352
|
+
.. versionadded:: 0.18
|
|
353
|
+
|
|
354
|
+
random_state : int, RandomState instance or None, optional (default=None)
|
|
355
|
+
The seed of the pseudo random number generator to use when shuffling
|
|
356
|
+
the data, i.e. getting the random vectors to initialize the algorithm.
|
|
357
|
+
Pass an int for reproducible results across multiple function calls.
|
|
358
|
+
See :term:`Glossary <random_state>`.
|
|
359
|
+
|
|
360
|
+
Returns
|
|
361
|
+
-------
|
|
362
|
+
Q : 2D array
|
|
363
|
+
A (size x size) projection matrix, the range of which
|
|
364
|
+
approximates well the range of the input matrix A.
|
|
365
|
+
|
|
366
|
+
Notes
|
|
367
|
+
-----
|
|
368
|
+
|
|
369
|
+
Follows Algorithm 4.3 of
|
|
370
|
+
Finding structure with randomness: Stochastic algorithms for constructing
|
|
371
|
+
approximate matrix decompositions
|
|
372
|
+
Halko, et al., 2009 (arXiv:909) https://arxiv.org/pdf/0909.4061.pdf
|
|
373
|
+
|
|
374
|
+
An implementation of a randomized algorithm for principal component
|
|
375
|
+
analysis
|
|
376
|
+
A. Szlam et al. 2014
|
|
377
|
+
"""
|
|
378
|
+
|
|
379
|
+
random_state = np.random.mtrand._rand
|
|
380
|
+
|
|
381
|
+
# Generating normal random vectors with shape: (A.shape[1], size)
|
|
382
|
+
Q = random_state.normal(size=(A.shape[1], size))
|
|
383
|
+
if A.dtype.kind == 'f':
|
|
384
|
+
# Ensure f32 is preserved as f32
|
|
385
|
+
Q = Q.astype(A.dtype, copy=False)
|
|
386
|
+
|
|
387
|
+
# Perform power iterations with Q to further 'imprint' the top
|
|
388
|
+
# singular vectors of A in Q
|
|
389
|
+
for i in range(n_iter):
|
|
390
|
+
Q = safe_sparse_dot(A, Q, mkl_dot)
|
|
391
|
+
Q = safe_sparse_dot(A.T, Q, mkl_dot)
|
|
392
|
+
|
|
393
|
+
# Sample the range of A using by linear projection of Q
|
|
394
|
+
# Extract an orthonormal basis
|
|
395
|
+
Q, _ = qr(safe_sparse_dot(A, Q, mkl_dot), mode='economic')
|
|
396
|
+
|
|
397
|
+
return Q
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def randomized_svd(M, n_components, mkl_dot, n_oversamples=10, n_iter='auto', transpose='auto', flip_sign=True, random_state=0):
|
|
401
|
+
"""
|
|
402
|
+
Computes a truncated randomized SVD
|
|
403
|
+
|
|
404
|
+
Parameters
|
|
405
|
+
----------
|
|
406
|
+
M : ndarray or sparse matrix
|
|
407
|
+
Matrix to decompose
|
|
408
|
+
|
|
409
|
+
n_components : int
|
|
410
|
+
Number of singular values and vectors to extract.
|
|
411
|
+
|
|
412
|
+
n_oversamples : int (default is 10)
|
|
413
|
+
Additional number of random vectors to sample the range of M so as
|
|
414
|
+
to ensure proper conditioning. The total number of random vectors
|
|
415
|
+
used to find the range of M is n_components + n_oversamples. Smaller
|
|
416
|
+
number can improve speed but can negatively impact the quality of
|
|
417
|
+
approximation of singular vectors and singular values.
|
|
418
|
+
|
|
419
|
+
n_iter : int or 'auto' (default is 'auto')
|
|
420
|
+
Number of power iterations. It can be used to deal with very noisy
|
|
421
|
+
problems. When 'auto', it is set to 4, unless `n_components` is small
|
|
422
|
+
(< .1 * min(X.shape)) `n_iter` in which case is set to 7.
|
|
423
|
+
This improves precision with few components.
|
|
424
|
+
|
|
425
|
+
.. versionchanged:: 0.18
|
|
426
|
+
|
|
427
|
+
power_iteration_normalizer : 'auto' (default), 'QR', 'none'
|
|
428
|
+
Whether the power iterations are normalized with step-by-step
|
|
429
|
+
QR factorization (the slowest but most accurate), 'none'
|
|
430
|
+
(the fastest but numerically unstable when `n_iter` is large, e.g.
|
|
431
|
+
typically 5 or larger). The 'auto' mode applies no normalization
|
|
432
|
+
if `n_iter` <= 2.
|
|
433
|
+
|
|
434
|
+
.. versionadded:: 0.18
|
|
435
|
+
|
|
436
|
+
transpose : True, False or 'auto' (default)
|
|
437
|
+
Whether the algorithm should be applied to M.T instead of M. The
|
|
438
|
+
result should approximately be the same. The 'auto' mode will
|
|
439
|
+
trigger the transposition if M.shape[1] > M.shape[0] since this
|
|
440
|
+
implementation of randomized SVD tend to be a little faster in that
|
|
441
|
+
case.
|
|
442
|
+
|
|
443
|
+
.. versionchanged:: 0.18
|
|
444
|
+
|
|
445
|
+
flip_sign : boolean, (True by default)
|
|
446
|
+
The output of a singular value decomposition is only unique up to a
|
|
447
|
+
permutation of the signs of the singular vectors. If `flip_sign` is
|
|
448
|
+
set to `True`, the sign ambiguity is resolved by making the largest
|
|
449
|
+
loadings for each component in the left singular vectors positive.
|
|
450
|
+
|
|
451
|
+
random_state : int, RandomState instance or None, optional (default=None)
|
|
452
|
+
The seed of the pseudo random number generator to use when shuffling
|
|
453
|
+
the data, i.e. getting the random vectors to initialize the algorithm.
|
|
454
|
+
Pass an int for reproducible results across multiple function calls.
|
|
455
|
+
See :term:`Glossary <random_state>`.
|
|
456
|
+
|
|
457
|
+
Notes
|
|
458
|
+
-----
|
|
459
|
+
This algorithm finds a (usually very good) approximate truncated
|
|
460
|
+
singular value decomposition using randomization to speed up the
|
|
461
|
+
computations. It is particularly fast on large matrices on which
|
|
462
|
+
you wish to extract only a small number of components. In order to
|
|
463
|
+
obtain further speed up, `n_iter` can be set <=2 (at the cost of
|
|
464
|
+
loss of precision).
|
|
465
|
+
|
|
466
|
+
References
|
|
467
|
+
----------
|
|
468
|
+
* Finding structure with randomness: Stochastic algorithms for constructing
|
|
469
|
+
approximate matrix decompositions
|
|
470
|
+
Halko, et al., 2009 https://arxiv.org/abs/0909.4061
|
|
471
|
+
|
|
472
|
+
* A randomized algorithm for the decomposition of matrices
|
|
473
|
+
Per-Gunnar Martinsson, Vladimir Rokhlin and Mark Tygert
|
|
474
|
+
|
|
475
|
+
* An implementation of a randomized algorithm for principal component
|
|
476
|
+
analysis
|
|
477
|
+
A. Szlam et al. 2014
|
|
478
|
+
"""
|
|
479
|
+
|
|
480
|
+
random_state = np.random.mtrand._rand
|
|
481
|
+
n_random = n_components + n_oversamples
|
|
482
|
+
n_samples, n_features = M.shape
|
|
483
|
+
|
|
484
|
+
if n_iter == 'auto':
|
|
485
|
+
# Checks if the number of iterations is explicitly specified
|
|
486
|
+
# Adjust n_iter. 7 was found a good compromise for PCA.
|
|
487
|
+
n_iter = 7 if n_components < .1 * min(M.shape) else 4
|
|
488
|
+
|
|
489
|
+
if transpose == 'auto':
|
|
490
|
+
transpose = n_samples < n_features
|
|
491
|
+
if transpose:
|
|
492
|
+
# this implementation is a bit faster with smaller shape[1]
|
|
493
|
+
M = M.T
|
|
494
|
+
|
|
495
|
+
Q = randomized_range_finder(M, mkl_dot, size=n_random, n_iter=n_iter, random_state=random_state)
|
|
496
|
+
|
|
497
|
+
# project M to the (k + p) dimensional space using the basis vectors
|
|
498
|
+
B = safe_sparse_dot(Q.T, M, mkl_dot)
|
|
499
|
+
|
|
500
|
+
# compute the SVD on the thin matrix: (k + p) wide
|
|
501
|
+
Uhat, s, V = svd(B, full_matrices=False)
|
|
502
|
+
|
|
503
|
+
del B
|
|
504
|
+
U = dot(Q, Uhat)
|
|
505
|
+
|
|
506
|
+
if flip_sign:
|
|
507
|
+
if not transpose:
|
|
508
|
+
U, V = svd_flip(U, V)
|
|
509
|
+
else:
|
|
510
|
+
# In case of transpose u_based_decision=false
|
|
511
|
+
# to actually flip based on u and not v.
|
|
512
|
+
U, V = svd_flip(U, V, u_based_decision=False)
|
|
513
|
+
|
|
514
|
+
if transpose:
|
|
515
|
+
# transpose back the results according to the input convention
|
|
516
|
+
return V[:n_components, :].T, s[:n_components], U[:, :n_components].T
|
|
517
|
+
else:
|
|
518
|
+
return U[:, :n_components], s[:n_components], V[:n_components, :]
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
def svd_flip(u, v, u_based_decision=True):
|
|
522
|
+
"""
|
|
523
|
+
Sign correction to ensure deterministic output from SVD.
|
|
524
|
+
|
|
525
|
+
Adjusts the columns of u and the rows of v such that the loadings in the
|
|
526
|
+
columns in u that are largest in absolute value are always positive.
|
|
527
|
+
|
|
528
|
+
Parameters
|
|
529
|
+
----------
|
|
530
|
+
u : ndarray
|
|
531
|
+
u and v are the output of `linalg.svd` or
|
|
532
|
+
:func:`~sklearn.utils.extmath.randomized_svd`, with matching inner
|
|
533
|
+
dimensions so one can compute `np.dot(u * s, v)`.
|
|
534
|
+
|
|
535
|
+
v : ndarray
|
|
536
|
+
u and v are the output of `linalg.svd` or
|
|
537
|
+
:func:`~sklearn.utils.extmath.randomized_svd`, with matching inner
|
|
538
|
+
dimensions so one can compute `np.dot(u * s, v)`.
|
|
539
|
+
|
|
540
|
+
u_based_decision : boolean, (default=True)
|
|
541
|
+
If True, use the columns of u as the basis for sign flipping.
|
|
542
|
+
Otherwise, use the rows of v. The choice of which variable to base the
|
|
543
|
+
decision on is generally algorithm dependent.
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
Returns
|
|
547
|
+
-------
|
|
548
|
+
u_adjusted, v_adjusted : arrays with the same dimensions as the input.
|
|
549
|
+
|
|
550
|
+
"""
|
|
551
|
+
|
|
552
|
+
if u_based_decision:
|
|
553
|
+
# columns of u, rows of v
|
|
554
|
+
max_abs_cols = argmax(np.abs(u), axis=0)
|
|
555
|
+
signs = sign(u[max_abs_cols, range(u.shape[1])])
|
|
556
|
+
u *= signs
|
|
557
|
+
v *= signs[:, newaxis]
|
|
558
|
+
else:
|
|
559
|
+
# rows of v, columns of u
|
|
560
|
+
max_abs_rows = argmax(np.abs(v), axis=1)
|
|
561
|
+
signs = sign(v[range(v.shape[0]), max_abs_rows])
|
|
562
|
+
u *= signs
|
|
563
|
+
v *= signs[:, newaxis]
|
|
564
|
+
return u, v
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def test_truncation(T, trunc_list, mkl_dot=True, display=True, n_iter=2):
|
|
568
|
+
"""
|
|
569
|
+
This function test one or several possible truncations for the MLSVD of T, showing the error of each one. It is
|
|
570
|
+
possible to accomplish the same results calling the function mlsvd with display=3 but this is not advisable since
|
|
571
|
+
each call recomputes the same unfolding SVD's.
|
|
572
|
+
The variable trunc_list must be a list of truncations. Even if it is only one truncation, it must be a list with one
|
|
573
|
+
truncation only.
|
|
574
|
+
"""
|
|
575
|
+
|
|
576
|
+
# Set the main variables about T.
|
|
577
|
+
dims = T.shape
|
|
578
|
+
L = len(dims)
|
|
579
|
+
Tsize = norm(T)
|
|
580
|
+
|
|
581
|
+
# Transform list into array and get the maximum for each dimension.
|
|
582
|
+
max_trunc_dims = np.max(array(trunc_list), axis=0)
|
|
583
|
+
|
|
584
|
+
# Compute truncated SVD of all unfoldings of T.
|
|
585
|
+
sigmas = []
|
|
586
|
+
U = []
|
|
587
|
+
T1 = empty((dims[0], mlinalg.multiply_dims(dims) // dims[0]), dtype=float64)
|
|
588
|
+
for l in range(L):
|
|
589
|
+
Tl = cnv.unfold(T, l+1)
|
|
590
|
+
if l == 0:
|
|
591
|
+
T1 = cnv.unfold_C(T, l+1)
|
|
592
|
+
low_rank = min(dims[l], max_trunc_dims[l])
|
|
593
|
+
Ul, sigma_l, Vlt = randomized_svd(Tl, low_rank, mkl_dot, n_iter=n_iter)
|
|
594
|
+
sigmas.append(sigma_l)
|
|
595
|
+
U.append(Ul)
|
|
596
|
+
|
|
597
|
+
# Save errors in a list.
|
|
598
|
+
trunc_error = []
|
|
599
|
+
|
|
600
|
+
# Truncated MLSVD.
|
|
601
|
+
for trunc in trunc_list:
|
|
602
|
+
# S, U and UT truncated.
|
|
603
|
+
current_dims = trunc
|
|
604
|
+
current_U = []
|
|
605
|
+
current_sigmas = []
|
|
606
|
+
for l in range(L):
|
|
607
|
+
current_U.append(U[l][:, :current_dims[l]])
|
|
608
|
+
current_sigmas.append(sigmas[l][:current_dims[l]])
|
|
609
|
+
current_UT = [current_U[l].T for l in range(L)]
|
|
610
|
+
S = mlinalg.multilin_mult(current_UT, T1, dims)
|
|
611
|
+
|
|
612
|
+
# Error of truncation.
|
|
613
|
+
S1 = cnv.unfold(S, 1)
|
|
614
|
+
current_error = mlinalg.compute_error(T, Tsize, S1, current_U, current_dims)
|
|
615
|
+
trunc_error.append(current_error)
|
|
616
|
+
|
|
617
|
+
# Display results.
|
|
618
|
+
if display:
|
|
619
|
+
print('Truncation:', current_dims)
|
|
620
|
+
print('Error:', current_error)
|
|
621
|
+
print()
|
|
622
|
+
|
|
623
|
+
return trunc_error
|
|
624
|
+
|