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.
@@ -0,0 +1,264 @@
1
+ """
2
+ Alternating Least Squares Module
3
+ ===============================
4
+ This module implement the Alternating Least Squares algorithm.
5
+ """
6
+
7
+ # Python modules
8
+ import numpy as np
9
+ from numpy import inf, mean, copy, concatenate, empty, float64, dot
10
+ from numpy.linalg import norm, pinv
11
+
12
+ # Tensor Fox modules
13
+ import TensorFox.Conversion as cnv
14
+ import TensorFox.Critical as crt
15
+ import TensorFox.MultilinearAlgebra as mlinalg
16
+
17
+
18
+ def als(T, factors, R, options):
19
+ """
20
+ This function uses the ALS method to compute an approximation of T with rank R.
21
+
22
+ Inputs
23
+ ------
24
+ T: float array
25
+ factors: list of float 2-D array
26
+ The factor matrices to be used as starting point.
27
+ R: int.
28
+ The desired rank of the approximating tensor.
29
+ options: class
30
+ See the function cpd for more information about the options available.
31
+
32
+ Outputs
33
+ -------
34
+ factors: list of float 2-D array
35
+ The factor matrices of the CPD of T.
36
+ step_sizes: float 1-D array
37
+ Distance between the computed points at each iteration.
38
+ errors: float 1-D array
39
+ Error of the computed approximating tensor at each iteration.
40
+ improv: float 1-D array
41
+ Improvement of the error at each iteration. More precisely, the difference between the relative error of the
42
+ current iteration and the previous one.
43
+ gradients: float 1-D array
44
+ Gradient of the error function at each iteration.
45
+ stop: 0, 1, 2, 3, 4, 5, 6 or 7
46
+ This value indicates why the function stopped. See the function dGN for more details.
47
+ """
48
+
49
+ # INITIALIZE RELEVANT VARIABLES
50
+
51
+ # Extract all relevant variables from the class of options.
52
+ maxiter = options.maxiter
53
+ tol = options.tol
54
+ tol_step = options.tol_step
55
+ tol_improv = options.tol_improv
56
+ tol_grad = options.tol_grad
57
+ symm = options.symm
58
+ display = options.display
59
+ factors_norm = options.factors_norm
60
+
61
+ # Verify if some factor should be fixed or not. This only happens when the bicpd function was called.
62
+ L = len(factors)
63
+ fix_mode = -1
64
+ orig_factors = [[] for l in range(L)]
65
+ for l in range(L):
66
+ if type(factors[l]) == list:
67
+ fix_mode = l
68
+ orig_factors[l] = factors[l][0].copy()
69
+ factors[l] = factors[l][0]
70
+
71
+ # Set the other variables.
72
+ Tsize = norm(T)
73
+ error = 1
74
+ best_error = inf
75
+ stop = 5
76
+ const = 1 + int(maxiter/10)
77
+
78
+ # INITIALIZE RELEVANT ARRAYS
79
+
80
+ x = concatenate([factors[l].flatten('F') for l in range(L)])
81
+ step_sizes = empty(maxiter)
82
+ errors = empty(maxiter)
83
+ improv = empty(maxiter)
84
+ gradients = empty(maxiter)
85
+ best_factors = [copy(factors[l]) for l in range(L)]
86
+
87
+ # Compute unfoldings.
88
+ Tl = [cnv.unfold(T, l+1) for l in range(L)]
89
+ T1_approx = empty(Tl[0].shape, dtype=float64)
90
+
91
+ if display > 1:
92
+ if display == 4:
93
+ print(' ',
94
+ '{:^9}'.format('Iteration'),
95
+ '| {:^11}'.format('Rel error'),
96
+ '| {:^11}'.format('Step size'),
97
+ '| {:^11}'.format('Improvement'),
98
+ '| {:^11}'.format('norm(grad)'))
99
+ else:
100
+ print(' ',
101
+ '{:^9}'.format('Iteration'),
102
+ '| {:^9}'.format('Rel error'),
103
+ '| {:^11}'.format('Step size'),
104
+ '| {:^10}'.format('Improvement'),
105
+ '| {:^10}'.format('norm(grad)'))
106
+
107
+ # START ALS ITERATIONS
108
+
109
+ for it in range(maxiter):
110
+ # Keep the previous value of x and error to compare with the new ones in the next iteration.
111
+ old_x = x
112
+ old_error = error
113
+
114
+ # ALS iteration call.
115
+ factors = als_iteration(Tl, factors, fix_mode)
116
+ x = concatenate([factors[l].flatten('F') for l in range(L)])
117
+
118
+ # Transform factors.
119
+ factors = cnv.transform(factors, symm, factors_norm)
120
+ # Some mode may be fixed when the bicpd is called.
121
+ if L == 3:
122
+ for l in range(L):
123
+ if fix_mode == l:
124
+ factors[l] = copy(orig_factors[l])
125
+
126
+ # Compute error.
127
+ T1_approx = cnv.cpd2unfold1(T1_approx, factors)
128
+ error = crt.fastnorm(Tl[0], T1_approx) / Tsize
129
+
130
+ # Update best solution.
131
+ if error < best_error:
132
+ best_error = error
133
+ for l in range(L):
134
+ best_factors[l] = copy(factors[l])
135
+
136
+ # Save relevant information about the current iteration.
137
+ step_sizes[it] = norm(x - old_x)
138
+ errors[it] = error
139
+ gradients[it] = np.abs(old_error - error)/step_sizes[it]
140
+ if it == 0:
141
+ improv[it] = errors[it]
142
+ else:
143
+ improv[it] = np.abs(errors[it-1] - errors[it])
144
+
145
+ # Show information about current iteration.
146
+ if display > 1:
147
+ if display == 4:
148
+ print(' ',
149
+ '{:^8}'.format(it + 1),
150
+ '| {:^10.5e}'.format(errors[it]),
151
+ '| {:^10.5e}'.format(step_sizes[it]),
152
+ '| {:^10.5e}'.format(improv[it]),
153
+ '| {:^11.5e}'.format(gradients[it]))
154
+ else:
155
+ print(' ',
156
+ '{:^9}'.format(it + 1),
157
+ '| {:^9.2e}'.format(errors[it]),
158
+ '| {:^11.2e}'.format(step_sizes[it]),
159
+ '| {:^11.2e}'.format(improv[it]),
160
+ '| {:^10.2e}'.format(gradients[it]))
161
+
162
+ # Stopping conditions.
163
+ if it > 1:
164
+ if errors[it] < tol:
165
+ stop = 0
166
+ break
167
+ if step_sizes[it] < tol_step:
168
+ stop = 1
169
+ break
170
+ if improv[it] < tol_improv:
171
+ stop = 2
172
+ break
173
+ if gradients[it] < tol_grad:
174
+ stop = 3
175
+ break
176
+ if it > 2*const and it % const == 0:
177
+ # Let const=1+int(maxiter/10). Comparing the average errors of const consecutive iterations prevents
178
+ # the program to continue iterating when the error starts to oscillate without decreasing.
179
+ mean1 = mean(errors[it - 2 * const: it - const])
180
+ mean2 = mean(errors[it - const: it])
181
+ if mean1 - mean2 <= tol_improv:
182
+ stop = 4
183
+ break
184
+ # If the average improvements is too small compared to the average errors, the program stops.
185
+ mean3 = mean(improv[it - const: it])
186
+ if mean3 < 1e-3 * mean2:
187
+ stop = 7
188
+ break
189
+ # Prevent blow ups.
190
+ if error > max(1, Tsize ** 2) / (1e-16 + tol):
191
+ stop = 6
192
+ break
193
+
194
+ # SAVE LAST COMPUTED INFORMATION
195
+
196
+ errors = errors[0: it+1]
197
+ step_sizes = step_sizes[0: it+1]
198
+ improv = improv[0: it+1]
199
+ gradients = gradients[0: it+1]
200
+
201
+ return factors, step_sizes, errors, improv, gradients, stop
202
+
203
+
204
+ def als_iteration(Tl, factors, fix_mode):
205
+ """
206
+ This function the ALS iterations, that is, it computes the pseudoinverse with respect to the modes. This
207
+ implementation is simple and not intended to be optimal.
208
+ """
209
+
210
+ # Initialize first variables.
211
+ L = len(factors)
212
+ R = factors[0].shape[1]
213
+ dims = [factors[l].shape[0] for l in range(L)]
214
+
215
+ # Main computations for the general case.
216
+ if fix_mode == -1:
217
+ for l in range(L):
218
+ itr = [l for l in reversed(range(L))]
219
+ itr.remove(l)
220
+ M = factors[itr[0]]
221
+
222
+ # Compute Khatri-Rao products W^(L) ⊙ ... ⊙ W^(l+1) ⊙ W^(l-1) ⊙ ... ⊙ W^(1).
223
+ for ll in range(L-2):
224
+ tmp = M
225
+ dim1, dim2 = tmp.shape[0], dims[itr[ll+1]]
226
+ M = empty((dim1*dim2, R), dtype=float64)
227
+ M = mlinalg.khatri_rao(tmp, factors[itr[ll+1]], M)
228
+
229
+ factors[l] = dot(Tl[l], pinv(M.T))
230
+
231
+ return factors
232
+
233
+ # If fix_mode != -1, it is assumed that the program is using the bicpd function.
234
+ # This part is only used for third order tensors.
235
+ X, Y, Z = factors
236
+ T1, T2, T3 = Tl
237
+
238
+ if fix_mode == 0:
239
+ M = empty((Z.shape[0] * X.shape[0], R))
240
+ M = mlinalg.khatri_rao(Z, X, M)
241
+ Y = dot( T2, pinv(M.T) )
242
+ M = empty((Y.shape[0] * X.shape[0], R))
243
+ M = mlinalg.khatri_rao(Y, X, M)
244
+ Z = dot( T3, pinv(M.T) )
245
+
246
+ elif fix_mode == 1:
247
+ X, Y, Z = factors
248
+ M = empty((Z.shape[0] * Y.shape[0], R))
249
+ M = mlinalg.khatri_rao(Z, Y, M)
250
+ X = dot( T1, pinv(M.T) )
251
+ M = empty((Y.shape[0] * X.shape[0], R))
252
+ M = mlinalg.khatri_rao(Y, X, M)
253
+ Z = dot( T3, pinv(M.T) )
254
+
255
+ elif fix_mode == 2:
256
+ X, Y, Z = factors
257
+ M = empty((Z.shape[0] * Y.shape[0], R))
258
+ M = mlinalg.khatri_rao(Z, Y, M)
259
+ X = dot( T1, pinv(M.T) )
260
+ M = empty((Z.shape[0] * X.shape[0], R))
261
+ M = mlinalg.khatri_rao(Z, X, M)
262
+ Y = dot( T2, pinv(M.T) )
263
+
264
+ return [X, Y, Z]