forked from MingjieQian/LAML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadme.txt
2344 lines (1838 loc) · 56.1 KB
/
Readme.txt
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
LAML (Linear Algebra and Machine Learning)
LAML is a stand-alone pure Java library for linear algebra and machine learning. The goal is to build efficient and easy-to-use linear algebra and machine learning libraries. The reason why linear algebra and machine learning are built together is that full control of the basic data structures for matrices and vectors is required to have fast implementation for machine learning methods. Additionally, LAML provides a lot of commonly used matrix functions in the same signature to MATLAB, thus can also be used to manually convert MATLAB code to Java code.
The built-in linear algebra library supports well-designed dense and sparse matrices and vectors. Standard compressed sparse column (CSC) and compressed sparse row (CSR) are used to design and implement sparse matrices. The matrix multiplication is competitive or even faster than well known linear algebra libraries in Java such as Commons-Math and Colt. Unlike other linear algebra libraries in Java, the built-in linear algebra library in LAML gives users full control of sparse matrices and vectors (e.g., the interior arrays in sparse matrices), which is crucial to make efficient high level implementations.
Carefully designed linear algebra library is the basis for machine learning library. ML library aims to provide fast implementation of mature machine learning methods. For instance, the LinearBinarySVM class re-implements the popular liblinear (in Java). For heart_scale data with C = 1.0 and eps = 1e-2, the average running time is 0.04 seconds using an Intel(R) Core(TM) i7 CPU M620 @ 2.67GHz with 4.00GB memory and 64-bit Windows 7 operating system, even a little faster than liblinear, which costs 0.06 seconds in average given the same parameter.
JML v.s. LAML
LAML is much faster than JML (more than 3 times faster) due to two implementation considerations. First, LAML allows full control of dense and sparse matrices and vectors. Second, LAML extensively uses in-place matrix and vector operations thus avoids too much memory allocation and garbage collection.
JML relies on third party linear algebra library, i.e. Apache Commons-math. Sparse matrices and vectors have been deprecated in Commons-math 3.0+, and will be ultimately eliminated. Whereas LAML has its own built-in linear algebra library.
Like JML, LAML also provides a lot of commonly used matrix functions in the same signature to MATLAB, thus can also be used to manually convert MATLAB code to Java code.
In short, JML has been replaced by LAML.
SourceForge
https://sourceforge.net/projects/lamal
Github:
https://github.com/MingjieQian/LAML
Documentation
http://web.engr.illinois.edu/~mqian2/upload/projects/java/LAML/doc/index.html
Features
Stand-alone Java library, completely cross-platform
Built-in Linear Algebra (LA) library
Full control of matrices and vectors
Many general-purpose optimization algorithms
Fast implementation of Machine Learning (ML) methods
Matrix functions with almost the same signature to MATLAB
Well documented source code and friendly API, very easy to use
Packages
la.decomposition
LU, QR, eigenvalue decomposition, and SVD
la.matrix
Sparse and dense matrix implementation
la.vector
Sparse and dense vector implementation
la.io
Functions of saving and loading a matrix, a vector, or a data set
ml.utils
Efficient functions for array and matrix operations, Matlab-style functions, and Printer
ml.classification
Linear SVM, Linear multi-class SVM, regularized logistic regression, maximum entropy modeling, and AdaBoost
ml.clustering
K-means, L1NMF, NMF, and spectral clustering
ml.optimization
L-BFGS, BoundConstrainedPLBFGS, NonnegativePLBFGS, Projection, ProximalMapping, ShrinkageOperator, accelerated proximal gradient, accelerated gradient descent, general quadratic programming, nonlinear conjugate gradient, LBFGS on simplex, quadratic programming with bound constraint, primal-dual interior-point method
ml.sequence
Hidden Markov Models (HMM) and Conditional Random Fields (CRF)
ml.kernel
Commonly used kernel functions ('linear' | 'poly' | 'rbf' | 'cosine')
ml.manifold
Commonly used manifold learning functions such as computing adjacency matrix, Laplacian matrix, and local learning regularization matrix
ml.subspace
PCA, kernel PCA, Multi-dimensional Scaling (MDS), Isomap, and Locally Linear Embedding (LLE)
ml.regression
LASSO and linear regression
ml.random
Multivariate Gaussian distribution
ml.recovery
Matrix completion and robust PCA
ml.topics
LDA
ml.graph
Minimum spanning tree using Prim's algorithm, shortest path using Dijkstra's algorithm, topological order, all-pairs shortest path using Floyd-Warshall algorithm, Huffman codes, and maximum flow using Ford-Fulkerson algorithm
ml.recommendation
Factorization machines and structured sparse regression (STSR)
Code Examples
# Eigenvalue Decomposition (For Real Symmetric Matrices)
int m = 4;
int n = 4;
Matrix A = hilb(m, n);
fprintf("A:%n");
disp(A);
long start = 0;
start = System.currentTimeMillis();
Matrix[] VD = EigenValueDecomposition.decompose(A);
System.out.format("Elapsed time: %.4f seconds.%n", (System.currentTimeMillis() - start) / 1000.0);
fprintf("*****************************************%n");
Matrix V = VD[0];
Matrix D = VD[1];
fprintf("V:%n");
printMatrix(V);
fprintf("D:%n");
printMatrix(D);
fprintf("VDV':%n");
disp(V.mtimes(D).mtimes(V.transpose()));
fprintf("A:%n");
printMatrix(A);
fprintf("V'V:%n");
printMatrix(V.transpose().mtimes((V)));
# Output
A:
1 0.5000 0.3333 0.2500
0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0.1667
0.2500 0.2000 0.1667 0.1429
Elapsed time: 0.0100 seconds.
*****************************************
V:
0.7926 0.5821 -0.1792 -0.0292
0.4519 -0.3705 0.7419 0.3287
0.3224 -0.5096 -0.1002 -0.7914
0.2522 -0.5140 -0.6383 0.5146
D:
1.5002
0.1691
0.0067
0.0001
VDV':
1 0.5000 0.3333 0.2500
0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0.1667
0.2500 0.2000 0.1667 0.1429
A:
1 0.5000 0.3333 0.2500
0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0.1667
0.2500 0.2000 0.1667 0.1429
V'V:
1.0000 -0.0000 -0.0000 -0.0000
-0.0000 1.0000 0.0000 0
-0.0000 0.0000 1.0000 -0.0000
-0.0000 0 -0.0000 1.0000
# -------------------------------------------------------------------------- #
# LU Decomposition
double[][] data = new double[][] {
{1, -2, 3},
{2, -5, 12},
{0, 2, -10}
};
Matrix A = new DenseMatrix(data);
fprintf("A:%n");
printMatrix(A);
Matrix[] LUP = LUDecomposition.decompose(A);
Matrix L = LUP[0];
Matrix U = LUP[1];
Matrix P = LUP[2];
fprintf("L:%n");
printMatrix(L);
fprintf("U:%n");
printMatrix(U);
fprintf("P:%n");
printMatrix(P);
fprintf("PA:%n");
printMatrix(P.mtimes(A));
fprintf("LU:%n");
printMatrix(L.mtimes(U));
long start = 0;
start = System.currentTimeMillis();
LUDecomposition LUDecomp = new LUDecomposition(A);
Vector b = new DenseVector(new double[] {2, 3, 4});
Vector x = LUDecomp.solve(b);
fprintf("Solution for Ax = b:%n");
printVector(x);
fprintf("b = %n");
printVector(b);
fprintf("Ax = %n");
printVector(A.operate(x));
fprintf("A^{-1}:%n");
printMatrix(LUDecomp.inverse());
fprintf("det(A) = %.2f%n", LUDecomp.det());
System.out.format("Elapsed time: %.2f seconds.%n", (System.currentTimeMillis() - start) / 1000F);
fprintf("**********************************%n");
A = sparse(A);
fprintf("A:%n");
printMatrix(A);
LUP = LUDecomposition.decompose(A);
L = LUP[0];
U = LUP[1];
P = LUP[2];
fprintf("L:%n");
printMatrix(L);
fprintf("U:%n");
printMatrix(U);
fprintf("P:%n");
printMatrix(P);
fprintf("PA:%n");
printMatrix(P.mtimes(A));
fprintf("LU:%n");
printMatrix(L.mtimes(U));
start = System.currentTimeMillis();
LUDecomp = new LUDecomposition(sparse(A));
b = new DenseVector(new double[] {2, 3, 4});
x = LUDecomp.solve(b);
fprintf("Solution for Ax = b:%n");
printVector(x);
fprintf("Ax = %n");
printVector(A.operate(x));
fprintf("b = %n");
printVector(b);
Matrix B = new DenseMatrix(new double[][] {
{2, 4},
{3, 3},
{4, 2} }
);
Matrix X = LUDecomp.solve(B);
fprintf("Solution for AX = B:%n");
printMatrix(X);
fprintf("AX = %n");
printMatrix(A.mtimes(X));
fprintf("B = %n");
printMatrix(B);
fprintf("A^{-1}:%n");
printMatrix(LUDecomp.inverse());
fprintf("det(A) = %.2f%n", LUDecomp.det());
System.out.format("Elapsed time: %.2f seconds.%n", (System.currentTimeMillis() - start) / 1000F);
# Output
A:
1 -2 3
2 -5 12
0 2 -10
L:
1 0 0
0 1 0
0.5000 0.2500 1
U:
2 -5 12
0 2 -10
0 0 -0.5000
P:
0 1 0
0 0 1
1 0 0
PA:
2 -5 12
0 2 -10
1 -2 3
LU:
2 -5 12
0 2 -10
1 -2 3
Solution for Ax = b:
13
7
1
b =
2
3
4
Ax =
2
3
4
A^{-1}:
-13 7 4.5000
-10 5 3
-2 1 0.5000
det(A) = -2.00
Elapsed time: 0.02 seconds.
**********************************
A:
1 -2 3
2 -5 12
2 -10
L:
1
1
0.5000 0.2500 1
U:
2 -5 12
2 -10
-0.5000
P:
1
1
1
PA:
2 -5 12
2 -10
1 -2 3
LU:
2 -5 12
2 -10
1 -2 3
Solution for Ax = b:
13
7
1
Ax =
2
3
4
b =
2
3
4
Solution for AX = B:
13 -22
7 -19
1 -4
AX =
2 4
3 3
4 2
B =
2 4
3 3
4 2
A^{-1}:
-13 7 4.5000
-10 5 3
-2 1 0.5000
det(A) = -2.00
Elapsed time: 0.02 seconds.
# -------------------------------------------------------------------------- #
# QR Decomposition
int m = 4;
int n = 3;
Matrix A = hilb(m, n);
fprintf("When A is full:%n");
fprintf("A:%n");
printMatrix(A);
long start = 0;
start = System.currentTimeMillis();
Matrix[] QRP = QRDecomposition.decompose(A);
Matrix Q = QRP[0];
Matrix R = QRP[1];
Matrix P = QRP[2];
fprintf("Q:%n");
printMatrix(Q);
fprintf("R:%n");
printMatrix(R);
fprintf("P:%n");
printMatrix(P);
fprintf("AP:%n");
printMatrix(A.mtimes(P));
fprintf("QR:%n");
printMatrix(Q.mtimes(R));
fprintf("Q'Q:%n");
printMatrix(Q.transpose().mtimes(Q));
System.out.format("Elapsed time: %.2f seconds.%n", (System.currentTimeMillis() - start) / 1000F);
fprintf("**********************************%n");
// fprintf("|AP - QR| = ");
A = sparse(hilb(m, n));
fprintf("When A is sparse:%n");
fprintf("A:%n");
printMatrix(A);
start = System.currentTimeMillis();
QRP = QRDecomposition.decompose(A);
Q = QRP[0];
R = QRP[1];
P = QRP[2];
fprintf("Q:%n");
printMatrix(Q);
fprintf("R:%n");
printMatrix(R);
fprintf("P:%n");
printMatrix(P);
fprintf("AP:%n");
printMatrix(A.mtimes(P));
fprintf("QR:%n");
printMatrix(Q.mtimes(R));
fprintf("Q'Q:%n");
printMatrix(Q.transpose().mtimes(Q));
System.out.format("Elapsed time: %.2f seconds.%n", (System.currentTimeMillis() - start) / 1000F);
QRDecomposition QRDecomp = new QRDecomposition((A));
Vector b = new DenseVector(new double[] {2, 3, 4, 9});
Vector x = QRDecomp.solve(b);
fprintf("Solution for Ax = b:%n");
printVector(x);
fprintf("b = %n");
printVector(b);
fprintf("Ax = %n");
printVector(A.operate(x));
# Output
When A is full:
A:
1 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000
0.2500 0.2000 0.1667
Q:
-0.8381 0.5144 -0.1796 -0.0263
-0.4191 -0.4052 0.7487 0.3157
-0.2794 -0.5351 -0.1132 -0.7892
-0.2095 -0.5338 -0.6280 0.5261
R:
-1.1932 -0.4749 -0.6705
0 -0.1258 -0.1184
0 0 0.0059
0 0 0
P:
1
1
1
AP:
1 0.3333 0.5000
0.5000 0.2500 0.3333
0.3333 0.2000 0.2500
0.2500 0.1667 0.2000
QR:
1.0000 0.3333 0.5000
0.5000 0.2500 0.3333
0.3333 0.2000 0.2500
0.2500 0.1667 0.2000
Q'Q:
1.0000 0.0000 -0.0000 0.0000
0.0000 1 -0.0000 0
-0.0000 -0.0000 1.0000 0
0.0000 0 0 1
Elapsed time: 0.05 seconds.
**********************************
When A is sparse:
A:
1 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000
0.2500 0.2000 0.1667
Q:
-0.8381 0.5144 -0.1796 -0.0263
-0.4191 -0.4052 0.7487 0.3157
-0.2794 -0.5351 -0.1132 -0.7892
-0.2095 -0.5338 -0.6280 0.5261
R:
-1.1932 -0.4749 -0.6705
-0.1258 -0.1184
0.0059
P:
1
1
1
AP:
1 0.3333 0.5000
0.5000 0.2500 0.3333
0.3333 0.2000 0.2500
0.2500 0.1667 0.2000
QR:
1.0000 0.3333 0.5000
0.5000 0.2500 0.3333
0.3333 0.2000 0.2500
0.2500 0.1667 0.2000
Q'Q:
1.0000 0.0000 -0.0000 0.0000
0.0000 1 -0.0000 0
-0.0000 -0.0000 1.0000 0
0.0000 0 0 1
Elapsed time: 0.04 seconds.
Solution for Ax = b:
117.2346
-719.5017
733.7439
b =
2
3
4
9
Ax =
2.0651
2.2194
5.9516
7.6990
# -------------------------------------------------------------------------- #
# Singular Value Decomposition
Matrix A = new DenseMatrix(new double[][] { {1d, 2d}, {2d, 0d}, {1d, 7d}});
/*A = new DenseMatrix(new double[][] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
});*/
// printMatrix(SingularValueDecomposition.bidiagonalize(A)[1]);
// A = IO.loadMatrix("SVDInput");
/*fprintf("When A is full:%n%n");
fprintf("A:%n");
printMatrix(A);*/
long start = 0;
start = System.currentTimeMillis();
boolean computeUV = !false;
Matrix[] USV = SingularValueDecomposition.decompose(A, computeUV);
System.out.format("Elapsed time: %.4f seconds.%n", (System.currentTimeMillis() - start) / 1000.0);
fprintf("*****************************************%n");
Matrix U = USV[0];
Matrix S = USV[1];
Matrix V = USV[2];
if (computeUV) {
fprintf("USV':%n");
disp(U.mtimes(S).mtimes(V.transpose()));
fprintf("A:%n");
printMatrix(A);
fprintf("U'U:%n");
printMatrix(U.transpose().mtimes((U)));
fprintf("V'V:%n");
printMatrix(V.transpose().mtimes((V)));
fprintf("U:%n");
printMatrix(U);
fprintf("V:%n");
printMatrix(V);
}
fprintf("S:%n");
printMatrix(S);
fprintf("rank(A): %d%n", rank(A));
# Output
Elapsed time: 0.0100 seconds.
*****************************************
USV':
1.0000 2.0000
2.0000 -0.0000
1.0000 7.0000
A:
1 2
2 0
1 7
U'U:
1.0000 0.0000 0.0000
0.0000 1.0000 0.0000
0.0000 0.0000 1.0000
V'V:
1.0000 -0.0000
-0.0000 1.0000
U:
-0.2906 -0.2976 -0.9094
-0.0492 -0.9445 0.3248
-0.9556 0.1391 0.2598
V:
-0.1819 -0.9833
-0.9833 0.1819
S:
7.3935
2.0822
rank(A): 2
# -------------------------------------------------------------------------- #
# Linear Binary SVM
double C = 1.0;
double eps = 1e-4;
Classifier linearBinarySVM = new LinearBinarySVM(C, eps);
int[] pred_labels = null;
double[][] data = {
{3.5, 4.4, 1.3, 2.3},
{5.3, 2.2, 0.5, 4.5},
{0.2, 0.3, 4.1, -3.1},
{-1.2, 0.4, 3.2, 1.6}
};
int[] labels = new int[] {1, 1, -1, -1};
linearBinarySVM.feedData(data);
linearBinarySVM.feedLabels(labels);
linearBinarySVM.train();
fprintf("W:%n");
printMatrix(linearBinarySVM.W);
fprintf("b:%n");
printVector(linearBinarySVM.b);
pred_labels = linearBinarySVM.predict(data);
getAccuracy(pred_labels, labels);
# Output
W:
0.2143
0.1312
-0.2407
0.0150
b:
-0.0490
Accuracy: 100.00%
# -------------------------------------------------------------------------- #
# Linear Multi-Class SVM
double C = 1.0;
double eps = 1e-4;
Classifier linearMCSVM = new LinearMCSVM(C, eps);
double[][] data = {
{3.5, 4.4, 1.3, 2.3},
{5.3, 2.2, 0.5, 4.5},
{0.2, 0.3, 4.1, -3.1},
{-1.2, 0.4, 3.2, 1.6}
};
int[] labels = new int[] {1, 2, 3, 4};
linearMCSVM.feedData(data);
linearMCSVM.feedLabels(labels);
linearMCSVM.train();
fprintf("W:%n");
printMatrix(linearMCSVM.W);
fprintf("b:%n");
printVector(linearMCSVM.b);
int[] pred_labels = linearMCSVM.predict(data);
getAccuracy(pred_labels, labels);
# Output
..
W:
-0.0482 0.1314 0.0630 -0.1462
0.2825 -0.2241 -0.0053 -0.0531
-0.0819 -0.0836 0.0599 0.1056
-0.0838 0.1711 -0.2301 0.1428
b:
-0.0248 -0.0089 0.0043 0.0295
Accuracy: 100.00%
# -------------------------------------------------------------------------- #
# Multi-Class Logistic Regression with Multiple Choices of Regularization
double[][] data = {
{3.5, 5.3, 0.2, -1.2},
{4.4, 2.2, 0.3, 0.4},
{1.3, 0.5, 4.1, 3.2}
};
int[] labels = new int[] {1, 2, 3};
/*
* Regularization type.
* 0: No regularization
* 1: L1 regularization
* 2: L2^2 regularization
* 3: L2 regularization
* 4: Infinity norm regularization
*/
int regularizationType = 1;
double lambda = 0.1;
Classifier logReg = new LogisticRegression(regularizationType, lambda);
logReg.epsilon = 1e-5;
logReg.feedData(data);
logReg.feedLabels(labels);
// Get elapsed time in seconds
tic();
logReg.train();
fprintf("Elapsed time: %.3f seconds.%n", toc());
fprintf("W:%n");
printMatrix(logReg.W);
fprintf("b:%n");
printVector(logReg.b);
double[][] dataTest = data;
fprintf("Ground truth:%n");
printMatrix(logReg.Y);
fprintf("Predicted probability matrix:%n");
Matrix Prob_pred = logReg.predictLabelScoreMatrix(dataTest);
disp(Prob_pred);
fprintf("Predicted label matrix:%n");
Matrix Y_pred = logReg.predictLabelMatrix(dataTest);
printMatrix(Y_pred);
int[] pred_labels = logReg.predict(dataTest);
getAccuracy(pred_labels, labels);
# Output
# Without regularization
L-BFGS converges with norm(Grad) 0.000006
Elapsed time: 0.060 seconds.
W:
-1.8522 3.1339 -1.2817
3.4138 -1.7282 -1.6856
-1.2455 -1.2630 2.5084
-2.8311 0.5558 2.2753
b:
-0.3616 0.2534 0.1082
Ground truth:
1
1
1
Predicted probability matrix:
1.0000 0.0000 0.0000
0.0000 1.0000 0.0000
0.0000 0.0000 1.0000
Predicted label matrix:
1
1
1
Accuracy: 100.00%
# L1-norm regularization
Accelerated proximal gradient method converges with norm(G_Y_k) 0.000004
Elapsed time: 0.430 seconds.
W:
0 0.8678 0
0.9579 0 0
0 0 0.9894
0 0 0
b:
0 0 0
Ground truth:
1
1
1
Predicted probability matrix:
0.8790 0.1143 0.0067
0.1493 0.8263 0.0244
0.0258 0.0494 0.9247
Predicted label matrix:
1
1
1
Accuracy: 100.00%
# squared Frobenius norm regularization
Accelerated proximal gradient method converges with norm(G_Y_k) 0.000009
Elapsed time: 0.242 seconds.
W:
-0.2817 0.5368 -0.2551
0.5298 -0.3269 -0.2029
-0.1181 -0.3004 0.4185
-0.3814 0.0449 0.3364
b:
-0.0417 0.0266 0.0151
Ground truth:
1
1
1
Predicted probability matrix:
0.8872 0.1028 0.0100
0.1241 0.8299 0.0461
0.0137 0.0514 0.9349
Predicted label matrix:
1
1
1
Accuracy: 100.00%
# Frobenius norm regularization
Accelerated proximal gradient method converges with norm(G_Y_k) 0.000008
Elapsed time: 0.358 seconds.
W:
-0.4333 0.7669 -0.3336
0.7402 -0.4956 -0.2447
-0.1262 -0.4227 0.5489
-0.5135 0.0810 0.4326
b:
-0.0587 0.0375 0.0212
Ground truth:
1
1
1
Predicted probability matrix:
0.9509 0.0461 0.0029
0.0558 0.9249 0.0192
0.0039 0.0221 0.9740
Predicted label matrix:
1
1
1
Accuracy: 100.00%
# Infinity norm regularization
Accelerated proximal gradient method converges with norm(G_Y_k) 0.000010
Elapsed time: 0.631 seconds.
W:
-0.7162 0.7162 -0.7162
0.7162 -0.7162 -0.7162
-0.7162 -0.7162 0.7162
-0.7162 0.7162 0.7162
b:
-0.6125 -0.3405 0.5728
Ground truth:
1
1
1
Predicted probability matrix:
0.9821 0.0175 0.0004
0.0179 0.9752 0.0068
0.0000 0.0072 0.9928
Predicted label matrix:
1
1
1
Accuracy: 100.00%
# -------------------------------------------------------------------------- #
# Maximum Entropy Modeling
long start = System.currentTimeMillis();
/*
* a 3D {@code double} array, where data[n][i][k]
* is the i-th feature value on the k-th class
* for the n-th sample
*/
double[][][] data = new double[][][] {
{{1, 0, 0}, {2, 1, -1}, {0, 1, 2}, {-1, 2, 1}},
{{0, 2, 0}, {1, 0, -1}, {0, 1, 1}, {-1, 3, 0.5}},
{{0, 0, 0.8}, {2, 1, -1}, {1, 3, 0}, {-0.5, -1, 2}},
{{0.5, 0, 0}, {1, 1, -1}, {0, 0.5, 1.5}, {-2, 1.5, 1}},
};
/*double [][] labels = new double[][] {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
{1, 0, 0}
};*/
int[] labels = new int[] {1, 2, 3, 1};
MaxEnt maxEnt = new MaxEnt();
maxEnt.feedData(data);
maxEnt.feedLabels(labels);
maxEnt.train();
double elapsedTime = (System.currentTimeMillis() - start) / 1000d;
System.out.format("Elapsed time: %.3f seconds\n", elapsedTime);
fprintf("MaxEnt parameters:\n");
display(maxEnt.W);
String modelFilePath = "MaxEnt-Model.dat";
maxEnt.saveModel(modelFilePath);
maxEnt = new MaxEnt();
maxEnt.loadModel(modelFilePath);
fprintf("Predicted probability matrix:\n");
display(maxEnt.predictLabelScoreMatrix(data));
fprintf("Predicted label matrix:\n");
display(full(maxEnt.predictLabelMatrix(data)));
fprintf("Predicted labels:\n");