-
Notifications
You must be signed in to change notification settings - Fork 1
/
NMFnumpy.py
143 lines (113 loc) · 5.34 KB
/
NMFnumpy.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
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
# NMF algorithms in normal numpy without parallelization
import numpy as np
import NMFbase
class NMF(NMFbase.NMFbase):
def run(self, iterations):
"""Run the nmf for given number of iterations or until converged"""
for i in range(0, iterations):
# update H
self.H = self.H * np.dot(self.W.T,self.X) \
/ np.dot(np.dot(self.W.T,self.W),self.H)
# update W
self.W = self.W * np.dot(self.X,self.H.T) \
/ np.dot(np.dot(self.W, self.H), self.H.T)
# test for convergence
if (i % self.niter_test_conv == 0) and self.checkConvergence():
print "NMF converged after %i iterations" % i
break
class NMFsparse(NMFbase.NMFsparse, NMFbase.NMFbase):
def run(self, iterations):
for i in range(0, iterations):
# A = W, X = H, Y = X, Yhat = WH
WH = np.dot(self.W,self.H)
self.W = self.W * np.dot(self.X, self.H.T) \
/ (np.dot(WH, self.H.T) + self.sparseW)
self.W = self.W / self.W.sum(0) #[:,None]
# alternative: Wn = (W.T / W.sum(1)).T
self.H = self.H * np.dot(self.W.T, self.X) \
/ (np.dot(self.W.T, WH) + self.sparseH)
# test for convergence
if (i % self.niter_test_conv == 0) and self.checkConvergence():
print "NMF converged after %i iterations" % i
break
class NMFaffine(NMFbase.NMFaffine, NMFbase.NMFbase):
def run(self, iterations):
Xsum = self.X.sum(1)
for i in range(0, iterations):
# A = W, X = H, Y = X, Yhat = WH
WH = np.dot(self.W,self.H) + self.W0[:,None]
self.W = self.W * np.dot(self.X, self.H.T) \
/ (np.dot(WH, self.H.T) + self.sparseW)
self.W = self.W / self.W.sum(0) #[:,None]
# alternative: Wn = (W.T / W.sum(1)).T
self.H = self.H * np.dot(self.W.T, self.X) \
/ (np.dot(self.W.T, WH) + self.sparseH)
self.W0 = self.W0 * Xsum / WH.sum(1)
# test for convergence
if (i % self.niter_test_conv == 0) and self.checkConvergence():
print "NMF converged after %i iterations" % i
break
class NMFsemi(NMFbase.NMFsemi, NMFbase.NMFbase):
def run(self, iterations):
for i in range(iterations):
try:
self.F = np.dot(np.dot(self.X, self.G),
np.linalg.inv(np.dot(self.G.T, self.G))
)
except LinAlgError:
self.F = np.dot(np.dot(self.X, self.G),
np.linalg.pinv(np.dot(self.G.T, self.G))
)
XTF = np.dot(self.X.T, self.F)
FTF = np.dot(self.F.T, self.F)
XTF_pos = (np.abs(XTF) + XTF) / 2
XTF_neg = XTF_pos - XTF
FTF_pos = (np.abs(FTF) + FTF) / 2
FTF_neg = FTF_pos - FTF
self.G = self.G * np.sqrt((XTF_pos + np.dot(self.G, FTF_neg) \
+ 10**-9) \
/ (XTF_neg + np.dot(self.G, FTF_pos) \
+ 10**-9)
)
# test for convergence
if (i % self.niter_test_conv == 0) and self.checkConvergence():
print "NMF converged after %i iterations" % i
break
class NMFconvex(NMFbase.NMFconvex, NMFbase.NMFbase):
def run(self, iterations):
XTX = np.dot(self.X.T, self.X)
XTXpos = (np.abs(XTX) + XTX) / 2
XTXneg = XTXpos - XTX
for i in range(0, iterations):
# Update G
XTXnegW = np.dot(XTXneg, self.W)
XTXposW = np.dot(XTXpos, self.W)
GWT = np.dot(self.G, self.W.T)
self.G *= np.sqrt((XTXposW + np.dot(GWT, XTXnegW)) \
/ (XTXnegW + np.dot(GWT, XTXposW) \
+ 10**-9)
)
# Update W
GTG = np.dot(self.G.T, self.G)
self.W *= np.sqrt( (np.dot(XTXpos, self.G) \
+ np.dot(XTXnegW, GTG)) \
/ (np.dot(XTXneg, self.G) \
+ np.dot(XTXposW, GTG) + 10**-9)
)
# test for convergence
if (i % self.niter_test_conv == 0) and self.checkConvergence():
print "NMF converged after %i iterations" % i
break
class NMFKL(NMFbase.NMFbase):
def run(self, iterations):
for i in range(0, iterations):
self.H = self.H * np.dot(self.W.T,self.X \
/ np.dot(self.W,self.H)) \
/ np.sum(self.W,0)[:,None]
self.W = self.W * np.dot(self.X \
/ np.dot(self.W,self.H),self.H.T) \
/ np.sum(self.H,1)
# test for convergence
if (i % self.niter_test_conv == 0) and self.checkConvergence():
print "NMF converged after %i iterations" % i
break