-
Notifications
You must be signed in to change notification settings - Fork 95
/
NonNegativeMatrixFactorization.m
168 lines (140 loc) · 6.53 KB
/
NonNegativeMatrixFactorization.m
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
163
164
165
166
167
168
(*
Implementation of the Non-Negative Matrix Factorization algorithm in Mathematica
Copyright (C) 2013 Anton Antonov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written by Anton Antonov,
7320 Colbury Ave,
Windermere, Florida, USA.
*)
(*
Mathematica is (C) Copyright 1988-2013 Wolfram Research, Inc.
Protected by copyright law and international treaties.
Unauthorized reproduction or distribution subject to severe civil
and criminal penalties.
Mathematica is a registered trademark of Wolfram Research, Inc.
*)
(* Version 1.0 *)
(* This package contains definitions for the application of Non-Negative Matrix Factorization (NNMF). *)
(*
The implementation follows the description of the hybrid algorithm GD-CLS (Gradient Descent with Constrained Least Squares) in the article:
Shahnaz, F., Berry, M., Pauca, V., Plemmons, R., 2006.
Document clustering using nonnegative matrix factorization. Information Processing & Management 42 (2), 373-386.
*)
BeginPackage["NonNegativeMatrixFactorization`"]
GDCLS::usage = "GDCLS[V_?MatrixQ,k_Integer,opts] returns the pair of matrices {W,H} such that V = W H and the number of the columns of W and the number of rows of H are k. The method used is Gradient Descent with Constrained Least Squares."
GDCLSGlobal::usage = "GDCLSGlobal[V_?MatrixQ,W_?MatrixQ,H_?MatrixQ,opts] continues the GDCLS iterations over the matrices W and H in the execution context and returns {W,H} as a result."
NormalizeMatrixProduct::usage = "NormalizeMatrixProduct[W_?MatrixQ,H_?MatrixQ] returns a pair of matrices {W1,H1} such that W1 H1 = W H and the norms of the columns of W1 are 1."
LeftNormalizeMatrixProduct::usage = "Same as NormalizeMatrixProduct."
RightNormalizeMatrixProduct::usage = "RightNormalizeMatrixProduct[W_?MatrixQ,H_?MatrixQ] returns a pair of matrices {W1,H1} such that W1 H1 = W H and the norms of the rows of H1 are 1."
BasisVectorInterpretation::usage = "BasisVectorInterpretation[vec_?VectorQ,n_Integer,interpretationItems_List] takes the n largest coordinates of vec, finds the corresponding elements in interpretationItems, and returns a list of coordinate-item pairs."
Begin["`Private`"]
Clear[GDCLS]
Options[GDCLS] = {"MaxSteps" -> 200, "NonNegative" -> True, "Epsilon" -> 10^-9., "RegularizationParameter" -> 0.01, PrecisionGoal -> Automatic, "PrintProfilingInfo" -> False};
GDCLS[V_?MatrixQ, k_?IntegerQ, opts:OptionsPattern[]] :=
Block[{t, fls, A, W, H, T, m, n, b, diffNorm, normV, nSteps = 0,
nonnegQ = OptionValue[GDCLS,"NonNegative"],
maxSteps = OptionValue[GDCLS,"MaxSteps"],
eps = OptionValue[GDCLS,"Epsilon"],
lbd = OptionValue[GDCLS,"RegularizationParameter"],
pgoal = OptionValue[GDCLS,PrecisionGoal],
PRINT = If[TrueQ[OptionValue[GDCLS,"PrintProfilingInfo"]], Print, None]},
{m, n} = Dimensions[V];
W = RandomReal[{0, 1}, {m, k}];
H = ConstantArray[0, {k, n}];
normV = Norm[V, "Frobenius"]; diffNorm = 10 normV;
If[ pgoal === Automatic, pgoal = 4 ];
While[nSteps < maxSteps && TrueQ[! NumberQ[pgoal] || NumberQ[pgoal] && (normV > 0) && diffNorm/normV > 10^(-pgoal)],
nSteps++;
t =
Timing[
A = Transpose[W].W + lbd*IdentityMatrix[k];
T = Transpose[W];
fls = LinearSolve[A];
H = Table[(b = T.V[[All, i]]; fls[b]), {i, 1, n}];
H = SparseArray[Transpose[H]];
If[nonnegQ,
H = Clip[H, {0, Max[H]}]
];
W = W*(V.Transpose[H])/(W.(H.Transpose[H]) + eps);
];
If[NumberQ[pgoal],
diffNorm = Norm[V - W.H, "Frobenius"];
If[nSteps < 100 || Mod[nSteps, 100] == 0, PRINT["step:", nSteps, ", iteration time:", t, " relative error:", diffNorm/normV]],
If[nSteps < 100 || Mod[nSteps, 100] == 0, PRINT["step:", nSteps, ", iteration time:", t]]
];
];
{W, H}
];
Clear[GDCLSGlobal]
Options[GDCLSGlobal] = Options[GDCLS];
SetAttributes[GDCLSGlobal,HoldAll]
GDCLSGlobal[V_, W_, H_, opts:OptionsPattern[]] :=
Block[{t, fls, A, k, T, m, n, b, diffNorm, normV, nSteps = 0,
nonnegQ = OptionValue[GDCLSGlobal,"NonNegative"],
maxSteps = OptionValue[GDCLSGlobal,"MaxSteps"],
eps = OptionValue[GDCLSGlobal,"Epsilon"],
lbd = OptionValue[GDCLSGlobal,"RegularizationParameter"],
pgoal = OptionValue[GDCLSGlobal,PrecisionGoal],
PRINT = If[TrueQ[OptionValue[GDCLSGlobal,"PrintProfilingInfo"]], Print, None]},
{m, n} = Dimensions[V];
k = Dimensions[H][[1]];
normV = Norm[V, "Frobenius"]; diffNorm = 10 normV;
While[nSteps < maxSteps && TrueQ[! NumberQ[pgoal] || NumberQ[pgoal] && (normV > 0) && diffNorm/normV > 10^(-pgoal)],
nSteps++;
t =
Timing[
A = Transpose[W].W + lbd*IdentityMatrix[k];
T = Transpose[W];
fls = LinearSolve[A];
H = Table[(b = T.V[[All, i]]; fls[b]), {i, 1, n}];
H = SparseArray[Transpose[H]];
If[nonnegQ,
H = Clip[H, {0, Max[H]}]
];
W = W*(V.Transpose[H])/(W.(H.Transpose[H]) + eps);
];
If[NumberQ[pgoal],
diffNorm = Norm[V - W.H, "Frobenius"];
If[nSteps < 100 || Mod[nSteps, 100] == 0, PRINT[nSteps, " ", t, " relative error=", diffNorm/normV]],
If[nSteps < 100 || Mod[nSteps, 100] == 0, PRINT[nSteps, " ", t]]
];
];
{W, H}
] /; MatrixQ[W] && MatrixQ[H] && Dimensions[W][[2]] == Dimensions[H][[1]];
(* ::Subsection:: *)
(*Normalize matrices*)
Clear[NormalizeMatrixProduct]
NormalizeMatrixProduct[W_?MatrixQ,H_?MatrixQ]:=
Block[{d,S,SI},
d=Table[Norm[W[[All,i]]],{i,Length[W[[1]]]}];
S=DiagonalMatrix[d];
SI=DiagonalMatrix[Map[If[#!=0,1/#,0]&,d]];
{W.(SI),S.H}
];
LeftNormalizeMatrixProduct = NormalizeMatrixProduct;
Clear[RightNormalizeMatrixProduct]
RightNormalizeMatrixProduct[W_?MatrixQ,H_?MatrixQ]:=
Block[{d,S,SI},
d=Table[Norm[H[[i]]],{i,Length[H]}];
S=DiagonalMatrix[d];
SI=DiagonalMatrix[1/d];
{W.S,SI.H}
];
Clear[BasisVectorInterpretation]
BasisVectorInterpretation[vec_,n_Integer,terms_]:=
Block[{t},
t=Reverse@Ordering[vec,-n];
Transpose[{vec[[t]],terms[[t]]}]
];
End[]
EndPackage[]