forked from mikerabat/mrmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNNMF.pas
511 lines (434 loc) · 16.5 KB
/
NNMF.pas
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
unit NNMF;
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2014, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
interface
// ###########################################
// #### Non-Negative Matrix factorization
// ###########################################
uses SysUtils, Classes, Matrix, MatrixConst, BaseMathPersistence;
type
TNNMFPropsMethod = (nnmfDivergence, nnmfEukledian, nnmfAlternateLeastSquare);
TNNMFProps = record
MaxIter : integer;
tolUpdate : double; // algorithm converges if reconstruction error
// does not change by this tolerance. If set to 0 the Maximum iterations are used
method : TNNMFPropsMethod; // if false use euclidian update
RankOfBasis : integer;
UseLastResIfFail : boolean; // in case we have problems with the floating point precission use the last valid result
DoUpdateWithEPSMtx : boolean; // before division the matrix a matrix close to floating point precission is added
end;
type
ENMFException = class(EBaseMatrixException);
TNMFProgress = procedure(Sender : TObject; progress : integer) of Object;
TNMFRes = (nmOk, nmFloatingPointPrecReached, nmFailed);
TNNMF = class(TBaseMathPersistence)
private
fProps : TNNMFProps;
fH: TDoubleMatrix;
fW: TDoubleMatrix;
fWInv : TDoubleMatrix;
fOnProgress: TNMFProgress;
procedure Clear;
// procedures applied to each element of a matrix:
procedure epsFunc(var value: double);
procedure RandFunc(var value : double);
procedure maxFunc(var value : double);
protected
procedure DefineProps; override;
function PropTypeOfName(const Name : string) : TPropType; override;
procedure OnLoadIntProperty(const Name : String; Value : integer); override;
procedure OnLoadDoubleProperty(const Name : String; const Value : double); override;
function OnLoadObject(const Name : String; Obj : TBaseMathPersistence) : boolean; override;
public
property OnProgress : TNMFProgress read fOnProgress write fOnProgress;
property H : TDoubleMatrix read fH;
property W : TDoubleMatrix read fW;
property WInv : TDoubleMatrix read fWInv;
function CalcNMF(V : TDoubleMatrix) : TNMFRes;
procedure InitProjectionmatrix; // calculates pseudoinverse of W
function ProjectToFeatureSpace(Example: TDoubleMatrix): TDoubleMatrix;
function Reconstruct(Features: TDoubleMatrix): TDoubleMatrix;
procedure SetProperties(const props : TNNMFProps);
constructor Create;
destructor Destroy; override;
end;
implementation
uses Math, MathUtilFunc;
const cNNMFH = 'NNMFH';
cNNMFW = 'NNMFW';
cNNMFWInv = 'NNMFWinf';
cNNMFMaxIter = 'NNMFMaxIter';
cNNMFTolerance = 'NNMFTolerance';
cNNMFMethod = 'NNMFMethod';
cNNMFRank = 'NNMFRank';
cNNMFUpdateEPS = 'NNMFEpsUpdate';
cNNMFUseLastRes = 'NNMFUseLastRes';
{ TNMF }
// ###########################################
// #### Create Free
// ###########################################
procedure TNNMF.Clear;
begin
FreeAndNil(fH);
FreeAndNil(fW);
FreeAndNil(fWInv);
end;
constructor TNNMF.Create;
begin
fProps.MaxIter := 100;
fProps.method := nnmfDivergence;
fProps.RankOfBasis := -1; // use all
fProps.tolUpdate := 1e-4; // use a tolerance
fProps.UseLastResIfFail := True;
fProps.DoUpdateWithEPSMtx := True;
inherited Create;
end;
destructor TNNMF.Destroy;
begin
Clear;
inherited;
end;
// ###########################################
// #### NNMF algorithms
// ###########################################
function TNNMF.CalcNMF(V: TDoubleMatrix) : TNMFRes;
var lenOfEigVec : integer;
NumOfEigVec : integer;
RankOfBasis : integer;
sumW : IMatrix;
counter: Integer;
wh : IMatrix;
hht : IMatrix;
temp1 : IMatrix;
temp2 : IMatrix;
iter : integer;
nm : integer;
dnorm0, dnorm : double;
lastW, lastH : TDoubleMatrix;
epsMtx : IMatrix;
begin
Result := nmOk;
Clear;
// ###########################################
// #### Preprocess params
lenOfEigVec := V.Height;
NumOfEigVec := V.Width;
RankOfBasis := V.Width;
if fProps.RankOfBasis > 0 then
RankOfBasis := Min(RankOfBasis, fProps.RankOfBasis);
dnorm0 := 0;
nm := V.Height*V.Width;
lastW := nil;
lastH := nil;
// ###########################################
// #### Randomly initialize matrices
try
fW := TDoubleMatrix.Create(RankOfBasis, lenOfEigVec);
fW.ElementwiseFuncInPlace({$IFDEF FPC}@{$ENDIF}randFunc);
sumW := fW.Sum(False);
for counter := 0 to fW.Height - 1 do
begin
fW.SetSubMatrix(0, counter, fW.Width, 1);
fW.ElementWiseDivInPlace(sumW);
end;
fW.UseFullMatrix;
sumW := nil;
fH := TDoubleMatrix.Create(NumOfEigVec, RankOfBasis);
fH.ElementwiseFuncInPlace({$IFDEF FPC}@{$ENDIF}randFunc);
if ( fProps.UseLastResIfFail ) and (fProps.method <> nnmfAlternateLeastSquare ) then
begin
lastW := fW.Clone;
lastH := fH.Clone;
end;
// ###########################################
// #### Iterative update of NMF matrix
for iter := 0 to fProps.MaxIter - 1 do
begin
if fProps.method = nnmfDivergence then
begin
// implements:
// H = H.*(W'*(V./(W*H)));
wh := fW.Mult(fH);
if fProps.DoUpdateWithEPSMtx then
begin
epsMtx := wh.ElementwiseFunc({$IFDEF FPC}@{$ENDIF}epsFunc);
wh.AddInPlace(epsMtx);
end;
temp1 := V.ElementWiseDiv(wh);
temp2 := fW.MultT1(temp1);
fH.ElementWiseMultInPlace(temp2);
// W = W.*((V./(W*H))*H');
wh := fW.Mult(fH);
if fProps.DoUpdateWithEPSMtx then
begin
epsMtx := wh.ElementwiseFunc({$IFDEF FPC}@{$ENDIF}epsFunc);
wh.AddInPlace(epsMtx);
end;
temp1 := V.ElementWiseDiv(wh);
temp2 := temp1.MultT2(fH);
fW.ElementWiseMultInPlace(temp2);
// W = W./(ones(LenOfEigVec,1)*sum(W));
sumW := fW.Sum(False);
for counter := 0 to fW.Height - 1 do
begin
fW.SetSubMatrix(0, counter, fW.Width, 1);
fW.ElementWiseDivInPlace(sumW);
end;
fW.UseFullMatrix;
end
else if fProps.method = nnmfEukledian then
begin
// euclidian update
// implements
// W = W.*(V*H')./(W*(H*H'));
hht := fH.MultT2(fH);
wh := fW.Mult(hht);
temp1 := V.MultT2(fH);
if fProps.DoUpdateWithEPSMtx then
begin
epsMtx := temp1.ElementwiseFunc({$IFDEF FPC}@{$ENDIF}epsFunc);
wh.AddInplace(epsMtx);
end;
temp1.ElementWiseDivInPlace(wh);
fW.ElementWiseMultInPlace(temp1);
// H = H.*(W'*V)./((W'*W)*H);
hht := fW.MultT1(fW);
wh := hht.Mult(fH);
temp1 := fW.MultT1(V);
if fProps.DoUpdateWithEPSMtx then
begin
epsMtx := temp1.ElementwiseFunc({$IFDEF FPC}@{$ENDIF}epsFunc);
wh.AddInplace(epsMtx);
end;
temp1.ElementWiseDivInPlace(wh);
fH.ElementWiseMultInPlace(temp1);
end
else
begin
// alternating Least Squares
// h = max(0, w0\a);
// w = max(0, a/h);
if fW.PseudoInversion(temp1) = srNoConvergence then
begin
Result := nmFloatingPointPrecReached;
exit;
end;
temp1.MultInPlace(V);
temp1.ElementwiseFuncInPlace({$IFDEF FPC}@{$ENDIF}maxFunc);
if temp1.PseudoInversion(temp2) = srNoConvergence then
begin
Result := nmFloatingPointPrecReached;
exit;
end;
temp2 := V.Mult(temp2);
temp2.ElementwiseFuncInPlace({$IFDEF FPC}@{$ENDIF}maxFunc);
fH.Assign(temp1);
fW.Assign(temp2);
end;
// ###########################################
// #### check for convergence (very simple one!)
// checks if the change in the reconstruction error is smaller than the given tolerance
if fProps.tolUpdate > 0 then
begin
temp2 := fW.Mult(fH);
temp2.SubInPlace(V);
dNorm := sqrt( sqr(temp2.ElementwiseNorm2) / nm );
if iter > 0 then
begin
// check if change in norm is smaller than the given tolerance
if Abs(dnorm0 - dnorm) < fProps.tolUpdate then
begin
if Assigned(fOnProgress) then
fOnProgress(self, 100);
break;
end;
end;
dNorm0 := dNorm;
end;
// make clones in case an update fails due to floating point precission
// problems -> use the last valid matrix
if (fProps.UseLastResIfFail) and (fProps.method <> nnmfAlternateLeastSquare) then
begin
lastW.Free;
lastH.Free;
lastW := fW.Clone;
lastH := fH.Clone;
end;
if Assigned(fOnProgress) then
fOnProgress(self, (iter + 1)*100 div fProps.MaxIter);
end;
lastW.Free;
lastH.Free;
except
// if update is divergent (or made too often)
on E : EInvalidOp do
begin
Clear;
if fProps.method = nnmfAlternateLeastSquare
then
Result := nmFailed
else
begin
Result := nmFloatingPointPrecReached;
if fProps.UseLastResIfFail then
begin
fW := lastW;
fH := lastH;
end
else
Result := nmFailed; // in this case we don't have a valid result
end;
end;
else
Clear;
Result := nmFailed;
end;
end;
// ###########################################
// #### Projection and reconstruction
// ###########################################
procedure TNNMF.InitProjectionmatrix;
begin
if not Assigned(fW) then
raise ENMFException.Create('NMF object not initialized');
if not Assigned(fWInv) then
begin
if fW.PseudoInversion(fWInv) <> srOk then
raise ENMFException.Create('Error initilaizing projection matrix - no convergence');
end;
end;
function TNNMF.ProjectToFeatureSpace(
Example: TDoubleMatrix): TDoubleMatrix;
var exmplTransp : IMatrix;
begin
if not Assigned(fW) then
raise ENMFException.Create('NMF object not initialized');
InitProjectionmatrix;
// check dimensions -> transpose if necessary
if (Example.Width = fW.Height) and (Example.Height = 1) then
begin
exmplTransp := Example.Transpose;
Result := fWInv.Mult(exmplTransp);
end
else
Result := fWInv.Mult(exmplTransp);
end;
function TNNMF.Reconstruct(Features: TDoubleMatrix): TDoubleMatrix;
begin
if not Assigned(fW) then
raise ENMFException.Create('NMF object not initialized');
Result := fW.Mult(features);
end;
// ###########################################
// #### Functions applied to the matrix
// ###########################################
procedure TNNMF.epsFunc(var value : double);
begin
value := eps(value);
end;
procedure TNNMF.RandFunc(var value: double);
begin
value := random;
end;
procedure TNNMF.maxFunc(var value: double);
begin
if Value < 0 then
Value := 0;
end;
// ###########################################
// #### Persistence
// ###########################################
procedure TNNMF.DefineProps;
begin
AddIntProperty(cNNMFMaxIter, fProps.MaxIter);
AddDoubleProperty(cNNMFTolerance, fProps.tolUpdate);
AddIntProperty(cNNMFMethod, Integer(fProps.method));
AddIntProperty(cNNMFRank, fProps.RankOfBasis);
AddIntProperty(cNNMFUpdateEPS, Integer(fProps.DoUpdateWithEPSMtx));
AddIntProperty(cNNMFUseLastRes, Integer(fProps.UseLastResIfFail));
if Assigned(fW) then
AddObject(cNNMFW, fW);
if Assigned(fH) then
AddObject(cNNMFH, fH);
if Assigned(fWInv) then
AddObject(cNNMFWInv, fWInv);
end;
function TNNMF.PropTypeOfName(const Name: string): TPropType;
begin
if (CompareText(Name, cNNMFMaxIter) = 0) or (CompareText(Name, cNNMFMethod) = 0) or
(CompareText(Name, cNNMFRank) = 0) or (CompareText(Name, cNNMFUpdateEPS) = 0) or
(CompareText(Name, cNNMFUseLastRes) = 0)
then
Result := ptInteger
else if CompareText(Name, cNNMFTolerance) = 0
then
Result := ptDouble
else if (CompareText(Name, cNNMFW) = 0) or (CompareText(Name, cNNMFH) = 0) or
(CompareText(Name, cNNMFWInv) = 0)
then
Result := ptObject
else
Result := inherited PropTypeOfName(Name);
end;
procedure TNNMF.OnLoadDoubleProperty(const Name: String; const Value: double);
begin
if SameText(Name, cNNMFTolerance)
then
fProps.tolUpdate := Value
else
inherited OnLoadDoubleProperty(Name, Value);
end;
procedure TNNMF.OnLoadIntProperty(const Name: String; Value: integer);
begin
if SameText(Name, cNNMFMaxIter)
then
fProps.MaxIter := Value
else if SameText(Name, cNNMFMethod)
then
fProps.method := TNNMFPropsMethod( Value )
else if SameText(Name, cNNMFRank)
then
fProps.RankOfBasis := Value
else if SameText(Name, cNNMFUpdateEPS)
then
fProps.DoUpdateWithEPSMtx := Value <> 0
else if SameText(Name, cNNMFUseLastRes)
then
fProps.UseLastResIfFail := Value <> 0
else
inherited OnLoadIntProperty(Name, Value);
end;
function TNNMF.OnLoadObject(const Name: String;
Obj: TBaseMathPersistence): boolean;
begin
Result := True;
if SameText(Name, cNNMFH)
then
fH := obj as TDoubleMatrix
else if SameText(Name, cNNMFW)
then
fW := obj as TDoubleMatrix
else if SameText(Name, cNNMFWInv)
then
fWInv := obj as TDoubleMatrix
else
Result := inherited OnLoadObject(Name, obj);
end;
procedure TNNMF.SetProperties(const props: TNNMFProps);
begin
fProps := props;
end;
initialization
RegisterMathIO(TNNMF);
end.