-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgplvm.cpp
933 lines (897 loc) · 31.5 KB
/
gplvm.cpp
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
#include "gplvm.h"
int main(int argc, char* argv[])
{
CClgplvm command(argc, argv);
command.setFlags(true);
command.setVerbosity(2);
command.setMode("gplvm");
try {
while(command.isFlags())
{
string argument = command.getCurrentArgument();
if(argv[command.getCurrentArgumentNo()][0]=='-')
{
if (command.isCurrentArg("-?", "--?"))
{
command.helpInfo();
command.exitNormal();
}
else if(command.isCurrentArg("-h", "--help"))
{
command.helpInfo();
command.exitNormal();
}
else if(command.isCurrentArg("-v", "--verbosity"))
{
command.incrementArgument();
command.setVerbosity(command.getIntFromCurrentArgument());
}
else if(command.isCurrentArg("-s", "--seed"))
{
command.incrementArgument();
command.setSeed(command.getIntFromCurrentArgument());
}
else
command.unrecognisedFlag();
}
else if(argument=="learn") // learning a model.
command.learn();
else if(argument=="display")
command.display();
else if(argument=="gnuplot")
command.gnuplot();
else
command.exitError("Invalid gplvm command provided.");
command.incrementArgument();
}
command.exitError("No gplvm command provided.");
}
catch(ndlexceptions::FileFormatError err)
{
command.exitError(err.getMessage());
}
catch(ndlexceptions::FileReadError err)
{
command.exitError(err.getMessage());
}
catch(ndlexceptions::FileWriteError err)
{
command.exitError(err.getMessage());
}
catch(ndlexceptions::FileError err)
{
command.exitError(err.getMessage());
}
catch(ndlexceptions::Error err)
{
command.exitError(err.getMessage());
}
catch(std::bad_alloc err)
{
command.exitError("Out of memory.");
}
catch(std::exception err)
{
command.exitError("Unhandled exception.");
}
}
CClgplvm::CClgplvm(int arc, char** arv) : CClctrl(arc, arv)
{
}
void CClgplvm::learn()
{
incrementArgument();
setMode("learn");
enum {
KERNEL_USAGE_BACK,
KERNEL_USAGE_FWD,
KERNEL_USAGE_DYN
};
double tol=1e-6;
vector<string> kernelTypes;
vector<unsigned int> kernelUsageFlag;
vector<double> rbfInvWidths;
vector<double> weightVariances;
vector<double> biasVariances;
vector<double> variances;
vector<double> degrees;
vector<bool> selectInputs;
string labelledIndicesFile;
bool labelledIndicesFlag=false;
string optimiser="scg";
bool centreData=true;
bool scaleData=false;
bool regulariseLatent=true;
bool inputScaleLearnt=false;
string initialisationType="pca";
bool labelsProvided = true;
bool dynamicsUsed = false;
bool wangWeighting = false;
bool dynamicScaling = false;
double dynamicsRatio = 20;
double dynamicsScale = 0.5;
double signalVariance = 0.0;
double dynamicsMParameter = 1.0;
vector<int> labels;
int iters=1000;
int latentDim=2;
string modelFileName="gplvm_model";
while(isFlags())
{
if(isCurrentArgumentFlag())
{
if (isCurrentArg("-?", "--?")) {
helpInfo();
exitNormal();
}
else if (isCurrentArg("-h", "--help")) {
helpInfo();
exitNormal();
}
else if (isCurrentArg("-x", "--latent-dim")) {
incrementArgument();
latentDim = getIntFromCurrentArgument();
}
else if (isCurrentArg("-c", "--constrained")) {
incrementArgument();
kernelTypes.push_back(getCurrentArgument());
kernelUsageFlag.push_back(KERNEL_USAGE_BACK);
rbfInvWidths.push_back(-1.0);
weightVariances.push_back(-1.0);
biasVariances.push_back(-1.0);
variances.push_back(-1.0);
degrees.push_back(-1.0);
selectInputs.push_back(false);
}
else if (isCurrentArg("-D", "--dynamics-kernel")) {
incrementArgument();
dynamicsUsed = true;
kernelTypes.push_back(getCurrentArgument());
kernelUsageFlag.push_back(KERNEL_USAGE_DYN);
rbfInvWidths.push_back(-1.0);
weightVariances.push_back(-1.0);
biasVariances.push_back(-1.0);
variances.push_back(-1.0);
degrees.push_back(-1.0);
selectInputs.push_back(false);
}
// else if (isCurrentArg("-dh", "--dynamics-hyperparameter")) {
// incrementArgument();
// if(!dynamicsUsed)
// exitError("You need to declare a dynamics kernel before setting the parameter M.");
// dynamicsMParameter = getDoubleFromCurrentArgument();
// if(dynamicsRatio!=-1.0)
// exitError("It doesn't make sense to use Wang-weighting when the dynamics signal to noise ratio is fixed.");
// wangWeighting=true;
// }
// else if (isCurrentArg("-ds", "--dynamics-scaling")) {
// incrementArgument();
// if(!dynamicsUsed)
// exitError("You need to declare a dynamics kernel before setting dynamics scaling.");
// if(dynamicsRatio!=-1.0)
// exitError("It doesn't make sense to use log-likelihood scaling on the dynamics when the dynamics signal to noise ratio is fixed.");
// dynamicScaling = getBoolFromCurrentArgument();
// }
else if (isCurrentArg("-dr", "--dynamics-ratio")) {
incrementArgument();
if(!dynamicsUsed)
exitError("You need to declare a dynamics kernel before setting the dynamics signal to noise ratio. Default is 10.");
dynamicsRatio = getDoubleFromCurrentArgument();
if(wangWeighting)
exitError("It doesn't make sense to use Wang-weighting with the dynamics ratio paratmeter set.");
}
else if (isCurrentArg("-ds", "--dynamics-scale")) {
incrementArgument();
if(!dynamicsUsed)
exitError("You need to declare a dynamics kernel before setting the dynamics scale.");
dynamicsScale = getDoubleFromCurrentArgument();
if(wangWeighting)
exitError("It doesn't make sense to use Wang-weighting with the dynamics scale paratmeter set.");
}
else if (isCurrentArg("-C", "--Centre-data")) {
incrementArgument();
centreData=getBoolFromCurrentArgument();
}
else if (isCurrentArg("-I", "--Initialise")) {
incrementArgument();
initialisationType=getStringFromCurrentArgument();
}
else if (isCurrentArg("-L", "--Learn-scales")) {
incrementArgument();
inputScaleLearnt=getBoolFromCurrentArgument();
}
else if (isCurrentArg("-R", "--Regularise")) {
incrementArgument();
regulariseLatent=getBoolFromCurrentArgument();
}
else if (isCurrentArg("-S", "--Scale-data")) {
incrementArgument();
scaleData=getBoolFromCurrentArgument();
}
else if (isCurrentArg("-l", "--labelled-indices-file")) {
incrementArgument();
labelledIndicesFile=getCurrentArgument();
labelledIndicesFlag = true;
}
else if (isCurrentArg("-O", "--optimiser")) {
incrementArgument();
optimiser=getCurrentArgument();
}
else if (isCurrentArg("-k", "--kernel")) {
incrementArgument();
kernelTypes.push_back(getCurrentArgument());
kernelUsageFlag.push_back(KERNEL_USAGE_FWD);
rbfInvWidths.push_back(-1.0);
weightVariances.push_back(-1.0);
biasVariances.push_back(-1.0);
variances.push_back(-1.0);
degrees.push_back(-1.0);
selectInputs.push_back(false);
}
else if (isCurrentArg("-g", "--gamma")) {
incrementArgument();
if(kernelTypes.size()==0)
exitError("Inverse width specification must come after kernel type is specified.");
if(kernelTypes[kernelTypes.size()-1]!="rbf")
exitError("Inverse width parameter only valid for RBF kernel.");
rbfInvWidths[rbfInvWidths.size()-1]=2*getDoubleFromCurrentArgument();
}
else if (isCurrentArg("-d", "--degree")) {
incrementArgument();
if(kernelTypes.size()==0)
exitError("Polynomial degree specification must come after kernel type is specified.");
if(kernelTypes[kernelTypes.size()-1]!="poly")
exitError("Polynomial degree parameter only valid for poly kernel.");
degrees[degrees.size()-1]=getDoubleFromCurrentArgument();
}
else if (isCurrentArg("-w", "--weight")) {
incrementArgument();
if(kernelTypes.size()==0)
exitError("`Weight variance' parameter specification must come after kernel type is specified.");
if(kernelTypes[kernelTypes.size()-1]!="poly"
&& kernelTypes[kernelTypes.size()-1]!="mlp")
exitError("`Weight variance' parameter only valid for polynomial and MLP kernel.");
weightVariances[weightVariances.size()-1]=getDoubleFromCurrentArgument();
}
else if (isCurrentArg("-b", "--bias")) {
incrementArgument();
if(kernelTypes.size()==0)
exitError("`Bias variance' parameter specification must come after kernel type is specified.");
if(kernelTypes[kernelTypes.size()-1]!="poly"
&& kernelTypes[kernelTypes.size()-1]!="mlp")
exitError("`Bias variance' parameter only valid for polynomial and MLP kernel.");
biasVariances[biasVariances.size()-1]=getDoubleFromCurrentArgument();
}
else if (isCurrentArg("-v", "--variance")) {
incrementArgument();
if(kernelTypes.size()==0)
exitError("Variance parameter specification must come after kernel type is specified.");
variances[variances.size()-1]=getDoubleFromCurrentArgument();
}
else if (isCurrentArg("-i", "--input-select")) {
incrementArgument();
if(kernelTypes.size()==0)
exitError("Input selection flag must come after kernel type is specified.");
selectInputs[selectInputs.size()-1]=getBoolFromCurrentArgument();
}
else if (isCurrentArg("-#", "--#iterations")) {
incrementArgument();
iters=getIntFromCurrentArgument();
}
else if (isCurrentArg("-f", "--file-format")) {
incrementArgument();
setFileFormat(getIntFromCurrentArgument());
}
else {
unrecognisedFlag();
}
incrementArgument();
}
else
setFlags(false);
}
if(getCurrentArgumentNo()>=argc)
exitError("There are not enough input parameters.");
string trainDataFileName=getCurrentArgument();
if((getCurrentArgumentNo()+1)<argc)
modelFileName=argv[getCurrentArgumentNo()+1];
string dataFile="";
CMatrix Y;
CMatrix labs;
readData(Y, labs, trainDataFileName);
vector<int> labelledIndices;
if(labelledIndicesFlag)
{
ifstream in(labelledIndicesFile.c_str());
string line;
if(!in) throw ndlexceptions::FileReadError(labelledIndicesFile);
while(getline(in, line))
{
int index = atol(line.c_str())-1;
if(index<0 || index>labs.getRows()) throw ndlexceptions::FileFormatError(labelledIndicesFile);
labelledIndices.push_back(index);
}
in.close();
}
if(labelledIndicesFlag)
{
CMatrix newlabs(labelledIndices.size(), labs.getCols());
CMatrix newY(labelledIndices.size(), Y.getCols());
for(int i=0; i<labelledIndices.size(); i++)
{
newlabs.copyRowRow(i, labs, labelledIndices[i]);
newY.copyRowRow(i, Y, labelledIndices[i]);
}
labs = newlabs;
Y = newY;
if(getVerbosity()>0)
cout << "Reduced data set ... contains " << labs.getRows() << " points." << endl;
}
if(labs.getCols()>1)
{
cout << "Ignoring data labels." << endl;
labelsProvided=false;
}
if(labelsProvided)
{
for(int i=0; i<labs.getRows(); i++)
{
double val = labs.getVal(i);
int intVal = (int)val;
if((val - (double)intVal)!=0)
{
cout << "Ignoring data labels." << endl;
labelsProvided=false;
break;
}
else
labels.push_back(intVal);
}
}
CMatrix X(Y.getRows(), latentDim);
// create kernel.
CCmpndKern kern(X);
CCmpndKern backKern(X);
CCmpndKern dynKern(X);
vector<CKern*> kernels;
for(int i=0; i<kernelTypes.size(); i++)
{
CMatrix *M = 0;
if(kernelUsageFlag[i]!=KERNEL_USAGE_BACK)
M = &X;
else
M = &Y;
if(kernelTypes[i]=="lin")
{
if(selectInputs[i])
kernels.push_back(new CLinardKern(*M));
else
kernels.push_back(new CLinKern(*M));
if(variances[i]!=-1.0)
kernels[i]->setParam(variances[i], 0); // set variance parameter as specified.
}
else if(kernelTypes[i]=="poly")
{
if(selectInputs[i])
{
kernels.push_back(new CPolyardKern(*M));
if(degrees[i]!=-1.0)
((CPolyardKern*)kernels[i])->setDegree(degrees[i]);
}
else
{
kernels.push_back(new CPolyKern(*M));
if(degrees[i]!=-1.0)
((CPolyKern*)kernels[i])->setDegree(degrees[i]);
}
if(weightVariances[i]!=-1.0)
kernels[i]->setParam(weightVariances[i], 0); // set `weight variance' parameter as specified.
if(biasVariances[i]!=-1.0)
kernels[i]->setParam(biasVariances[i], 1); // set `bias variance' parameter as specified.
if(variances[i]!=-1.0)
kernels[i]->setParam(variances[i], 2); // set variance parameter as specified.
}
else if(kernelTypes[i]=="rbf")
{
if(selectInputs[i])
kernels.push_back(new CRbfardKern(*M));
else
kernels.push_back(new CRbfKern(*M));
if(rbfInvWidths[i]!=-1.0)
kernels[i]->setParam(rbfInvWidths[i], 0); /// set rbf inverse width as specified.
if(variances[i]!=-1.0)
kernels[i]->setParam(variances[i], 1); /// set variance parameter as specified.
}
else if(kernelTypes[i] == "mlp")
{
if(selectInputs[i])
kernels.push_back(new CMlpardKern(*M));
else
kernels.push_back(new CMlpKern(*M));
if(weightVariances[i]!=-1.0)
kernels[i]->setParam(weightVariances[i], 0); // set `weight variance' parameter as specified.
if(biasVariances[i]!=-1.0)
kernels[i]->setParam(biasVariances[i], 1); // set `bias variance' parameter as specified.
if(variances[i]!=-1.0)
kernels[i]->setParam(variances[i], 2); // set variance parameter as specified.
}
else if(kernelTypes[i] == "bias" && kernelUsageFlag[i]!=KERNEL_USAGE_FWD)
{
// fwd kernel always has bias component
kernels.push_back(new CBiasKern(*M));
if(variances[i]!=-1.0)
kernels[i]->setParam(variances[i], 0); // set variance parameter as specified.
}
else if(kernelTypes[i] == "white" && kernelUsageFlag[i]!=KERNEL_USAGE_FWD)
{
// fwd kernel always includes a white noise component
kernels.push_back(new CWhiteKern(*M));
if(variances[i]!=-1.0)
kernels[i]->setParam(variances[i], 0); // set variance parameter as specified.
}
else
{
exitError("Unknown kernel type: " + kernelTypes[i]);
}
switch (kernelUsageFlag[i])
{
case KERNEL_USAGE_FWD:
if(wangWeighting)
addWangPrior(kernels[i], dynamicsMParameter);
kern.addKern(kernels[i]);
break;
case KERNEL_USAGE_BACK:
backKern.addKern(kernels[i]);
break;
case KERNEL_USAGE_DYN:
if(wangWeighting)
addWangPrior(kernels[i], 1.0);
signalVariance+=kernels[i]->getVariance();
dynKern.addKern(kernels[i]);
break;
}
}
// if no kernel was specified, add an RBF.
if(kern.getNumKerns()==0)
{
CKern* defaultKern = new CRbfKern(X);
if(wangWeighting)
addWangPrior(defaultKern, dynamicsMParameter);
kern.addKern(defaultKern);
}
CKern* biasKern = new CBiasKern(X);
if(wangWeighting)
addWangPrior(biasKern, dynamicsMParameter);
CKern* whiteKern = new CWhiteKern(X);
if(wangWeighting)
addWangPrior(whiteKern, dynamicsMParameter);
kern.addKern(biasKern);
kern.addKern(whiteKern);
// if a dynamics kernel was specified add bias and white
if(dynKern.getNumKerns()>0)
{
biasKern = new CBiasKern(X);
if(wangWeighting)
addWangPrior(biasKern, 1.0);
whiteKern = new CWhiteKern(X);
if(wangWeighting)
addWangPrior(whiteKern, 1.0);
dynKern.addKern(biasKern);
signalVariance+=biasKern->getVariance();
dynKern.setVariance(dynamicsScale*dynamicsScale);
if(dynamicsRatio!=-1.0)
whiteKern->setParam(dynamicsScale/(dynamicsRatio*dynamicsRatio), 0);
dynKern.addKern(whiteKern);
}
CScaleNoise noise(&Y);
// Remove scales and center if necessary.
CMatrix params(1, 2*Y.getCols());
noise.getParams(params);
if(!centreData)
for(int j=0; j<Y.getCols(); j++)
params.setVal(0.0, j);
if(!scaleData)
for(int j=0; j<Y.getCols(); j++)
params.setVal(1.0, j+Y.getCols());
noise.setParams(params);
CGplvm* pmodel;
CMatrix bK(1,1,0.0);
bool hasBack = (backKern.getNumKerns()!=0);
bool hasDyn = (dynKern.getNumKerns()!=0);
if(!hasBack && !hasDyn) {
pmodel = new CGplvm(&kern, &noise, latentDim, getVerbosity());
}
else if(!hasBack && hasDyn) {
pmodel = new CGplvm(&kern, &dynKern, &noise, latentDim, getVerbosity());
}
else if(hasBack)
{
bK.resize(Y.getRows(), Y.getRows());
backKern.compute(bK, Y);
bK.setSymmetric(true);
if (hasDyn) {
pmodel = new CGplvm(&kern, &dynKern, &bK, &noise, latentDim, getVerbosity());
}
else {
pmodel = new CGplvm(&kern, &bK, &noise, latentDim, getVerbosity());
}
}
else
{
exitError("Unsupported learning mode");
}
pmodel->setLatentRegularised(regulariseLatent);
pmodel->setDynamicScaling(dynamicScaling);
pmodel->setInputScaleLearnt(inputScaleLearnt);
if(dynamicsRatio!=-1.0)
pmodel->setDynamicKernelLearnt(false);
if(initialisationType=="rand")
pmodel->initXrand();
else if(initialisationType=="pca")
{}// do nothing, this is normal.
else
exitError("Unknown initialisation type: " + initialisationType);
cout << "Optimiser is " << optimiser;
if(optimiser=="scg")
{
pmodel->setDefaultOptimiser(CGplvm::SCG);
}
else if(optimiser=="conjgrad")
{
pmodel->setDefaultOptimiser(CGplvm::CG);
}
else if(optimiser=="graddesc")
{
pmodel->setDefaultOptimiser(CGplvm::GD);
pmodel->setLearnRate(1e-4);
pmodel->setMomentum(0.9);
}
else if(optimiser=="quasinew")
{
pmodel->setDefaultOptimiser(CGplvm::BFGS);
}
else
{
exitError("Unrecognised model optimiser type.");
}
// Optimise the GP-LVM
pmodel->optimise(iters);
if(labelsProvided)
// set labels after optimisation (to show they aren't being used!)
pmodel->setLabels(labels);
string comment="";
switch(getFileFormat())
{
case 0: /// GPLVM file format.
comment = "Run as:";
for(int i=0; i<argc; i++)
{
comment+=" ";
comment+=argv[i];
}
comment += " with seed " + ndlstrutil::itoa(getSeed()) + ".";
writeGplvmToFile(*pmodel, modelFileName, comment);
break;
case 1: /// Matlab file format.
#ifdef _NDLMATLAB
// Write matlab output.
pmodel->writeMatlabFile(modelFileName, "gplvmInfo");
pmodel->pkern->updateMatlabFile(modelFileName, "kern");
X.updateMatlabFile(modelFileName, "X");
Y.updateMatlabFile(modelFileName, "Y");
#else
exitError("Error MATLAB not incorporated at compile time.");
#endif
break;
default:
exitError("Unrecognised file format number.");
}
exitNormal();
}
void CClgplvm::display()
{
incrementArgument();
setMode("display");
while(isFlags())
{
if(isCurrentArgumentFlag())
{
if(getCurrentArgumentLength()!=2)
unrecognisedFlag();
if (isCurrentArg("-?", "--?") ||isCurrentArg("-h", "--help"))
{
helpInfo();
exitNormal();
}
else {
unrecognisedFlag();
}
incrementArgument();
}
else
setFlags(false);
}
if(getCurrentArgumentNo()>=argc)
exitError("There are not enough input parameters.");
string modelFileName=getCurrentArgument();
CGplvm* pmodel=readGplvmFromFile(modelFileName, getVerbosity());
pmodel->display(cout);
exitNormal();
}
void CClgplvm::gnuplot()
{
incrementArgument();
setMode("gnuplot");
double pointSize = 2;
double lineWidth = 2;
int resolution = 80;
string name = "gplvm";
string modelFileName="gplvm_model";
string labelFileName="";
while(isFlags())
{
if(isCurrentArgumentFlag())
{
int j=1;
if(getCurrentArgumentLength()!=2)
unrecognisedFlag();
else if(isCurrentArg("-?","--?") || isCurrentArg("-h","--help")) {
helpInfo();
exitNormal();
}
else if (isCurrentArg("-l", "--labels")) {
incrementArgument();
labelFileName=getStringFromCurrentArgument();
}
else if (isCurrentArg("-p", "--point-size")) {
incrementArgument();
pointSize=getDoubleFromCurrentArgument();
}
else if (isCurrentArg("-r", "--resolution")) {
incrementArgument();
resolution=getIntFromCurrentArgument();
}
else {
unrecognisedFlag();
}
incrementArgument();
}
else
setFlags(false);
}
if(getCurrentArgumentNo()<argc)
modelFileName=argv[getCurrentArgumentNo()];
if((getCurrentArgumentNo()+1)<argc)
name=argv[getCurrentArgumentNo()+1];
string outputFileName=name+"_plot_data";
if((getCurrentArgumentNo()+3)<argc)
outputFileName=argv[getCurrentArgumentNo()+2];
CGplvm* pmodel=readGplvmFromFile(modelFileName, getVerbosity());
if(pmodel->getLatentDim()!=2)
{
exitError("Plotting is only implemented for 2 dimensional latent spaces.");
}
// If there is a labels file prepare files separately.
vector<int> labels;
vector<bool> labelFileWritten;
int maxVal=0;
int minVal=0;
if(labelFileName.length()>0)
{
ifstream in(labelFileName.c_str());
string line;
if(!in) throw ndlexceptions::FileReadError(labelFileName);
while(getline(in, line))
{
int index = atol(line.c_str());
if(index<0) throw ndlexceptions::FileFormatError(labelFileName);
labels.push_back(index);
}
in.close();
if(labels.size() != pmodel->pX->getRows()) throw ndlexceptions::Error("Incorrect number of labels");
pmodel->setLabels(labels);
}
if(pmodel->isLabels())
{
vector<int> storeIndex;
int currentLabel = 0;
maxVal = pmodel->getMaxLabelVal();
minVal = pmodel->getMinLabelVal();
for(int j=minVal; j<=maxVal; j++)
{
storeIndex.clear();
for(int i=0; i<pmodel->getNumData(); i++)
if(pmodel->getLabel(i)==j)
storeIndex.push_back(i);
if(storeIndex.size()>0)
{
// create a temporary storage matrix for writing.
CMatrix Xtemp(storeIndex.size(), pmodel->pX->getCols()+1, 0.1);
for(int i=0; i<storeIndex.size(); i++)
for(int k=0; k<2; k++)
Xtemp.setVal(pmodel->pX->getVal(storeIndex[i], k), i, k);
Xtemp.toFile(name+"_latent_data" + ndlstrutil::itoa(j) + ".dat");
labelFileWritten.push_back(true);
}
else
labelFileWritten.push_back(false);
}
}
else
{
CMatrix Xext(pmodel->pX->getRows(), 3, 0.1);
for(int i=0; i<pmodel->pX->getRows(); i++)
{
for(int j=0; j<2; j++)
Xext.setVal(pmodel->pX->getVal(i, j), i, j);
}
Xext.toFile(name+"_latent_data.dat");
}
// Prepare the background variance plot.
CMatrix minVals(1, pmodel->pX->getCols());
CMatrix maxVals(1, pmodel->pX->getCols());
pmodel->pX->maxRow(maxVals);
pmodel->pX->minRow(minVals);
int numx=resolution;
int numy=resolution;
double xspan=maxVals.getVal(0, 0)-minVals.getVal(0, 0);
double xdiff=xspan/(numx-1);
xdiff*=1.1;
double yspan= maxVals.getVal(0, 1)-minVals.getVal(0, 1);
double ydiff=yspan/(numy-1);
ydiff*=1.1;
CMatrix Xgrid(numx*numy, 2);
vector<CMatrix*> matrixOut;
double x;
double y;
int i;
int j;
for(i=0, y=minVals.getVal(0, 1)-0.05*yspan; i<numy; y+=ydiff, i++)
{
matrixOut.push_back(new CMatrix(numx, 3));
for(j=0, x=minVals.getVal(0, 0)-0.05*xspan; j<numx; x+=xdiff, j++)
{
Xgrid.setVal(x, i*numy+j, 0);
matrixOut[i]->setVal(x, j, 0);
Xgrid.setVal(y, i*numy+j, 1);
matrixOut[i]->setVal(y, j, 1);
}
}
CMatrix yTest(Xgrid.getRows(), pmodel->getNumProcesses());
yTest.ones();
CMatrix variances(yTest.getRows(), yTest.getCols());
CMatrix means(yTest.getRows(), yTest.getCols());
pmodel->posteriorMeanVar(means, variances, Xgrid);
for(int i=0; i<numy; i++)
for(int j=0; j<numx; j++)
{
matrixOut[i]->setVal(-log(variances.getVal(i*numy+j)), j, 2);
}
string matrixFile = name + "_variance_matrix.dat";
ofstream out(matrixFile.c_str());
if(!out) throw ndlexceptions::FileWriteError(matrixFile);
out << setiosflags(ios::scientific);
out << setprecision(17);
out << "# Prepared plot of model file " << endl;
for(int i=0; i<numy; i++)
{
matrixOut[i]->toStream(out);
out << endl;
}
out.close();
// prepare the plot file
string plotFileName = name + "_plot.gp";
ofstream outGnuplot(plotFileName.c_str());
if(!outGnuplot) throw ndlexceptions::FileWriteError(plotFileName);
outGnuplot << "set palette gray" << endl;
outGnuplot << "set palette gamma 2.5" << endl;
outGnuplot << "set pm3d map" << endl;
outGnuplot << "set pm3d explicit" << endl;
outGnuplot << "splot \"" << name << "_variance_matrix.dat\" with pm3d";
if(pmodel->isLabels())
{
for(int i=minVal, j=0; i<=maxVal; i++, j++)
if(labelFileWritten[j])
outGnuplot << ", \"" << name << "_latent_data" << i << ".dat\" with points ps " << pointSize;
}
else
outGnuplot << ", \"" << name << "_latent_data.dat\" with points ps " << pointSize;
outGnuplot << endl;
outGnuplot << "pause -1";
outGnuplot.close();
exitNormal();
}
void CClgplvm::helpInfo()
{
string command = getMode();
if(command=="gplvm")
{
helpHeader();
helpUsage("gplvm [options] command [command-options]");
cout << "Commands:" << endl;
helpArgument("learn", "For learning the GPLVM model.");
helpArgument("display", "For displaying the parameters of a learned GPLVM model.");
helpArgument("gnuplot", "For plotting the results in gnuplot.");
helpDescriptor("For help on a specific command type gplvm command --h");
cout << endl;
cout << "Options:" << endl;
helpArgument("-?, -h, --help", "This help.");
helpArgument("-v, --verbosity [0..3]", "Verbosity level (default is set to 2).");
helpArgument("-s, --seed long", "Set random seed (default is to use current time in seconds).");
}
else if(command=="learn")
{
helpHeader();
helpUsage("gplvm [options] learn example_file [model_file]");
helpDescriptor("This file is for learning a data set with an GPLVM. By default 1000 iterations of scaled conjugate gradient are used.");
cout << "Arguments:" << endl;
helpArgument("example_file", "File with the training data. The data file is in the format used by SVM light.");
helpArgument("model_file", "File to store the resulting model in. By default this is set to gplvm_model.");
cout << endl;
cout << "Command Options:" << endl;
helpArgument("-?, -h, --help", "This help.");
helpArgument("-l, --labelled-indices-file string", "File containing indices of labelled points. The index starts from 1.");
helpArgument("-#, --#iterations int", "Number of iterations for optimisation by scaled conjugate gradient. Default is 1000.");
helpArgument("-O, --optimiser string", "Optimiser to be used, valid options are scg, conjgrad, quasinew and graddesc. Default is scg.");
helpArgument("-x, --latent-dim int", "Dimension for latent variable space. Deafult is 2.");
helpArgument("-C, --Centre-data bool", "Centre the data by removing the mean value. Default value is 1 (true).");
helpArgument("-I, --Initialise string", "How to initialise the latent positions. Options are pca (principal component analysis) or rand (small values drawn from a normal distribution). Default setting is pca.");
helpArgument("-L, --Learn-scales bool", "Whether or not to learn the scaling on the input data. Default value is 0 (false).");
helpArgument("-R, --Regularise bool", "Whether or not to regularise the latent space by using an L2 penalty (equivalent to the MAP solution for a Gaussian prior) over the latent space. The default value is 1 (true).");
helpArgument("-S, --Scale-data bool", "Scale the data by setting the standard deviation in each direction to 1.0. Default value is 0 (false).");
helpArgument("-c, --constrained string", "Use a back constraint kernel matrix with the given kernel type (see --kernel for options). Default is no constraint.");
helpArgument("-D, --dynamics-kernel string", "Specify kernel for dynamics in the latent space. Default value is no dynamics kernel and therefore no learning of dynamics.");
// helpArgument("-dh, --dynamics-hyperparameter double", "Specify weighting of the prior on the dynamics hyperparameters.");
helpArgument("-ds, --dynamics-scale true", "Specify standard deviations of the move in latent space associated with dynamics, default is 0.1.");
helpArgument("-dr, --dynamics-ratio double", "Ratio between signal and noise in the dynamics kernel.");
string m=getMode();
setMode("kern");
helpInfo();
setMode(m);
}
else if(command=="kern")
{
cout << endl << "Kernel options:" << endl;
helpArgument("-k, --kernel string", "Type of kernel function. Currently available options are lin (linear), poly (polynomial -- not recommended), rbf (radial basis function) and mlp (multi-layer perceptron otherwise known as arcsin). If the kernel is not specified it defaults to rbf.");
helpArgument("-g, --gamma float", "Inverse width parameter in RBF kernel.");
helpArgument("-v, --variance float", "Variance parameter for kernel.");
helpArgument("-w, --weight float", "Weight parameter for polynomial and MLP kernel.");
helpArgument("-b, --bias float", "Bias parameter for polynomial and MLP kernel.");
helpArgument("-d, --degree int", "Degree parameter for polynomial kernel.");
}
else if(command=="display")
{
helpHeader();
helpUsage("gplvm [options] display [model_file]");
helpDescriptor("Summarise the contents of a model file for quick viewing. The model is loaded in and the parameters of the GPLVM are given.");
cout << "Arguments:" << endl;
helpArgument("model_file", "File containing the model to be tested. By default this is set to gplvm_model.");
cout << "Command Options:" << endl;
helpArgument("-?, -h, --help", "This help.");
}
else if(command=="gnuplot")
{
helpHeader();
helpUsage("gplvm [options] gnuplot [model_file] [name]");
helpDescriptor("This command creates files for displaying the visualisation in gnuplot.");
cout << "Arguments:" << endl;
helpArgument("model_file", "The GPLVM model you want to use for generating the plot. The default is gplvm_model");
helpArgument("name", "The gnuplot script will be output as \"name_plot.gp\". Data files needed etc. will also be preceded by \"name\". Default is \"gplvm\". Requires gnuplot vs 4.0 or later.");
cout << "Command Options:" << endl;
helpArgument("-?, -h, --help", "This help.");
helpArgument("-l, --labels string", "Label file name for visualisation of the data. If you wish to add labels to the data you can specify a file containing a vector of those labels here. By default any labels in the original data file are used.");
helpArgument("-p, --point-size float", "Size of the point markers in gnuplot.");
helpArgument("-r, --resolution int", "Resolution of the mesh grid in 3-d plots. The grid will have the given number of points in each direction. Default is 80.");
}
}
void CClgplvm::helpHeader()
{
cout << endl << "GPLVM Code: Version " << GPLVMVERSION << endl;
}
void addWangPrior(CKern* kern, double parameter)
{
CWangDist* weightingPrior = new CWangDist();
weightingPrior->setParam(parameter, 0);
for(int j=0; j<kern->getNumParams(); j++)
kern->addPrior(weightingPrior, j);
}