Repository navigation
Expand file tree
/
Copy pathalgorithm.py
More file actions
executable file
·162 lines (120 loc) · 5.37 KB
/
Copy pathalgorithm.py
File metadata and controls
executable file
·162 lines (120 loc) · 5.37 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import numpy as np
import matplotlib.pyplot as plt
from evaluation import rre_score
class L2NMF(object):
def __init__(self, n_components, random_seed=2):
self.n_components = n_components
np.random.seed(random_seed)
def initialize_WH(self, X):
n_components = self.n_components
n_samples, n_features = X.shape
avg = np.sqrt(X.mean() / n_components)
W = avg * np.abs(np.random.randn(n_samples, n_components))
H = avg * np.abs(np.random.randn(n_components, n_features))
return W, H
def fit(self, X, max_iter=1000, print_iter=None):
# -------------- Objective --------------------
# || X - WH ||_F^2
# -------------- Initialization --------------------
W, H = self.initialize_WH(X)
# -------------- Optimization --------------------
error_list = []
for iter in range(max_iter):
# Update W
W = W * (X.dot(H.T) / W.dot(H.dot(H.T)))
# Update H
H = H * (W.T.dot(X) / W.T.dot(W).dot(H))
if not (print_iter is None) and (iter+1) % print_iter == 0:
error = rre_score(X, W.dot(H))
print(" iter = {}, error = {}".format(iter+1, error))
error_list.append(error)
# Plot the learning curve
if not (print_iter is None):
plt.figure(figsize=(4,2))
plt.title('#iterations vs. RRE')
plt.plot(error_list)
plt.show()
return W, H
class L1NMF(object):
def __init__(self, n_components, random_seed=2):
self.n_components = n_components
np.random.seed(random_seed)
def initialize_DR(self, X):
n_components = self.n_components
n_samples, n_features = X.shape
avg = np.sqrt(X.mean() / n_components)
W = avg * np.abs(np.random.randn(n_samples, n_components))
H = avg * np.abs(np.random.randn(n_components, n_features))
return W, H
def fit(self, X, max_iter=1000, print_iter=None):
# -------------- Initialization --------------------
W, H = self.initialize_DR(X)
# -------------- Optimization --------------------
error_list = []
for iter in range(max_iter):
e = 1e-5
Q = ((X-np.dot(W, H))**2 + e**2)**(-1/2)
W = W * ( (X*Q).dot(H.T) / (W.dot(H)*Q).dot(H.T) )
H = H * ( (W.T.dot(X*Q)) / (W.T.dot(W.dot(H)*Q)) )
if not (print_iter is None) and (iter + 1) % print_iter == 0:
error = rre_score(X, W.dot(H))
print(" iter = {}, error = {}".format(iter + 1, error))
error_list.append(error)
# Plot the learning curve
if not (print_iter is None):
plt.figure(figsize=(4,2))
plt.title('#iterations vs. RRE')
plt.plot(error_list)
plt.show()
return W, H
class L1RegularizationNMF(object):
def __init__(self, n_components, regularization_factor=0.05, random_seed=2):
self.n_components = n_components
self.regularization_factor = regularization_factor
np.random.seed(random_seed)
def initialize_UVE(self, X):
n_components = self.n_components
n_samples, n_features = X.shape
avg = np.sqrt(X.mean() / n_components)
U = avg * np.abs(np.random.randn(n_samples, n_components))
V = avg * np.abs(np.random.randn(n_components, n_features))
E = avg * np.abs(np.random.randn(n_samples, n_features))
E = np.minimum(E, X)
return U, V, E
def fit(self, X, max_iter=1000, print_iter=None):
# -------------- Initialization --------------------
n_components = self.n_components
n_samples, n_features = X.shape
U, V, E = self.initialize_UVE(X)
# -------------- Optimization --------------------
error_list = []
for iter in range(max_iter):
# Update U
X_hat = X - E
U = U * (X_hat.dot(V.T)) / U.dot(V.dot(V.T))
# Update V, E
E_p = np.abs(E) + E / 2
E_n = np.abs(E) - E / 2
V_hat = np.concatenate([V, E_p, E_n], axis=0)
X_hat = np.concatenate([X, np.zeros((1, n_features))], axis=0)
U_hat = np.concatenate([U, np.eye(n_samples), -np.eye(n_samples)], axis=1)
padding = np.concatenate([np.zeros((1, n_components)), np.sqrt(self.regularization_factor) * np.exp(1) * np.ones((1, 2 * n_samples))], axis=1)
U_hat = np.concatenate([U_hat, padding], axis=0)
SV_hat = np.abs(U_hat.T.dot(U_hat.dot(V_hat)))
temp = ((U_hat.T.dot(U_hat.dot(V_hat))) - (U_hat.T.dot(X_hat))) / SV_hat
V_hat = np.maximum(0, V_hat - V_hat * temp)
V = V_hat[0:n_components,:]
E_p = V_hat[n_components:n_components+n_samples,:]
E_n = V_hat[n_components+n_samples:,:]
E = E_p - E_n
if not (print_iter is None) and (iter+1) % print_iter == 0:
error = rre_score(X, U.dot(V) + E)
print(" iter = {}, error = {}".format(iter+1, error))
error_list.append(error)
# Plot the learning curve
if not (print_iter is None):
plt.figure(figsize=(4,2))
plt.title('#iterations vs. RRE')
plt.plot(error_list)
plt.show()
return U, V, E