-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_nmf_fro_mu.py
101 lines (89 loc) · 3.5 KB
/
test_nmf_fro_mu.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
import numpy as np
import scipy
import scipy.sparse
from TELF.factorization.decompositions import nmf_fro_mu
from TELF.factorization.decompositions.utilities.math_utils import fro_norm
from TELF.factorization.decompositions.utilities.resample import uniform_product
import pytest
def test_H_update_numpy():
np.random.seed(0)
m, k, n = 3, 2, 4
W0 = np.random.rand(m, k)
H0 = np.random.rand(k, n)
X0 = W0@H0
for dtype in [np.float32, np.float64]:
for typ in [np.array, scipy.sparse.csr_matrix, scipy.sparse.coo_matrix]:
X = typ(X0.astype(dtype))
H = nmf_fro_mu.H_update(X, W0, uniform_product(H0, 0.1), use_gpu=False)
assert H.dtype == dtype
assert np.allclose(H, H0, rtol=1e-3, atol=1e-3)
def test_W_update_numpy():
np.random.seed(0)
m, k, n = 3, 2, 4
W0 = np.random.rand(m, k)
H0 = np.random.rand(k, n)
X0 = W0@H0
for dtype in [np.float32, np.float64]:
for typ in [np.array, scipy.sparse.csr_matrix, scipy.sparse.coo_matrix]:
X = typ(X0.astype(dtype))
W = nmf_fro_mu.W_update(X, uniform_product(W0, 0.1), H0, use_gpu=False)
assert W.dtype == dtype
assert np.allclose(W, W0, rtol=1e-3, atol=1e-3)
def test_nmf_numpy():
np.random.seed(0)
m, k, n = 3, 2, 4
W0 = np.random.rand(m, k)
H0 = np.random.rand(k, n)
X0 = W0@H0
for dtype in [np.float32, np.float64]:
for typ in [np.array, scipy.sparse.csr_matrix]:
X = typ(X0.astype(dtype))
W, H, _ = nmf_fro_mu.nmf(X, uniform_product(
W0, 0.1), uniform_product(H0, 0.1), use_gpu=False)
assert W.dtype == dtype
assert H.dtype == dtype
assert fro_norm(X-W@H)/fro_norm(X) < 1e-5
def test_H_update_cupy():
cp = pytest.importorskip("cupy")
cupyx = pytest.importorskip("cupyx")
cp.random.seed(0)
m, k, n = 3, 2, 4
W0 = cp.random.rand(m, k)
H0 = cp.random.rand(k, n)
X0 = W0@H0
for dtype in [np.float32, np.float64]:
for typ in [cp.array, cupyx.scipy.sparse.csr_matrix]:
X = typ(X0.astype(dtype))
H = nmf_fro_mu.H_update(X, W0, uniform_product(H0, 0.1, use_gpu=True), use_gpu=True)
assert H.dtype == dtype
assert cp.allclose(H, H0, rtol=1e-3, atol=1e-3)
def test_W_update_cupy():
cp = pytest.importorskip("cupy")
cupyx = pytest.importorskip("cupyx")
cp.random.seed(0)
m, k, n = 3, 2, 4
W0 = cp.random.rand(m, k)
H0 = cp.random.rand(k, n)
X0 = W0@H0
for dtype in [np.float32, np.float64]:
for typ in [cp.array, cupyx.scipy.sparse.csr_matrix]:
X = typ(X0.astype(dtype))
W = nmf_fro_mu.W_update(X, uniform_product(W0, 0.1, use_gpu=True), H0, use_gpu=True)
assert W.dtype == dtype
assert cp.allclose(W, W0, rtol=1e-3, atol=1e-3)
def test_nmf_cupy():
cp = pytest.importorskip("cupy")
cupyx = pytest.importorskip("cupyx")
cp.random.seed(0)
m, k, n = 3, 2, 4
W0 = cp.random.rand(m, k)
H0 = cp.random.rand(k, n)
X0 = W0@H0
for dtype in [np.float32, np.float64]:
for typ in [cp.array, cupyx.scipy.sparse.csr_matrix]:
X = typ(X0.astype(dtype))
W, H, _ = nmf_fro_mu.nmf(X, uniform_product(
W0, 0.1, use_gpu=True), uniform_product(H0, 0.1, use_gpu=True), use_gpu=True)
assert W.dtype == dtype
assert H.dtype == dtype
assert fro_norm(X-W@H, use_gpu=True)/fro_norm(X, use_gpu=True) < 1e-5