-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvd_tests.py
37 lines (28 loc) · 1.09 KB
/
svd_tests.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
import numpy as np
X = np.load('X_t_test_noise_float32.npy')
# X = np.load('X_t_test_noise_float64.npy')
print('Original data shape:', X.shape)
print('X mean:', np.mean(X))
print('X max:', np.max(X))
print('X min:', np.min(X))
U, S, Vh = np.linalg.svd(X, full_matrices=False)
print(U.shape)
print(S.shape)
print(Vh.shape)
X_reconstructed = np.dot(U*S, Vh)
print('Reconstructed data shape:', X_reconstructed.shape)
print('X_reconstructed mean:', np.mean(X_reconstructed))
print('X_reconstructed max:', np.max(X_reconstructed))
print('X_reconstructed min:', np.min(X_reconstructed))
X_diff = X - X_reconstructed
print('Max discrepancy:', np.max(np.abs(X_diff)))
print(np.allclose(X, X_reconstructed))
smat = np.diag(S)
X_reconstructed = np.dot(U, np.dot(smat, Vh))
print('Reconstructed data shape:', X_reconstructed.shape)
print('X_reconstructed mean:', np.mean(X_reconstructed))
print('X_reconstructed max:', np.max(X_reconstructed))
print('X_reconstructed min:', np.min(X_reconstructed))
X_diff = X - X_reconstructed
print('Max discrepancy:', np.max(np.abs(X_diff)))
print(np.allclose(X, X_reconstructed))