File: projectorGHSOM.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 3,336 bytes parent folder | download
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
#include "projectorGHSOM.h"
#include <mymaths.h>
#include <QDebug>
#include <GHSOM/globals.h>
#include <map>

using namespace std;

ProjectorGHSOM::ProjectorGHSOM()
{
    Globals::EXPAND_CYCLES = 10; // after how many iterations through the data we check if we want to expand
    Globals::MAX_CYCLES = 0; // maximum number of iterations through data (0: ignored)
    Globals::TAU_1 = 0.01; // percentage of remaining error that needs to be explained by each map. 1.0 == standard SOM
    Globals::TAU_2 = 1.0; // granularity of the last layer of maps 1.0 == non-hierarchical SOM (1 map only)
    Globals::INITIAL_LEARNRATE = 0.7; // how strongly the winner and neighboring units are adapted initially, decreases over time
    Globals::NR = 0.0009; // Neighborhood radius
    Globals::randomSeed = 1; // change ad libitum
    Globals::normInputVectors=NORM_LENGTH; // NONE, LENGTH, INTERVAL. Normalize data by their length or clamp in a 0-1 interval
    Globals::INITIAL_X_SIZE = 2; // initial size in the x direction
    Globals::INITIAL_Y_SIZE = 2; // initial size in the y direction
    Globals::LABELS_NUM = 0; // max number of labels per unit. 0: no labels
}

void ProjectorGHSOM::SetParams(float tau1, float tau2, int xSize, int ySize, int expandCycles, int normalizationType, float learningRate, float neighborhoodRadius)
{
    Globals::TAU_1 = tau1;
    Globals::TAU_2 = tau2;
    Globals::INITIAL_X_SIZE = xSize;
    Globals::INITIAL_Y_SIZE = ySize;
    Globals::EXPAND_CYCLES = expandCycles;
    Globals::normInputVectors = normalizationType;
    Globals::INITIAL_LEARNRATE = learningRate;
    Globals::NR = neighborhoodRadius;
}

void ProjectorGHSOM::Train(std::vector< fvec > samples, ivec labels)
{
    if(!samples.size()) return;
    source = samples;
    projected = samples;
    dim = samples[0].size();

    Data_Vector *res = new Data_Vector();
    if(Globals::layers) Globals::layers->removeAllElements();

    Globals::vectorlength = dim;
    Globals::numofallvecs = samples.size();
    //Globals::currentDataLoader = new DataLoader();

    FOR(i, samples.size())
    {
        float *data = new float[dim];
        FOR(d, dim) data[d] = samples[i][d];

        if (Globals::normInputVectors==NORM_LENGTH)
        {
            data = Globals::normVec(data);
        }
        char *id = new char[150];
        sprintf(id,"sample%d", i+1);
        DataItem *di = new DataItem(id,data,dim);
        res->addElement(di);
        delete [] data;
        delete [] id;
    }
    if (Globals::normInputVectors==NORM_INTERVAL) res = Globals::normIntervalVector(res);
    float *mean = Globals::meanVector(res, dim);
    qDebug() << "mean" << mean[0] << mean[1];

    Globals::dataItems = res;

    char **vecDescr = new char *[dim];
    FOR(d, dim)
    {
        vecDescr[d] = new char[150];
        sprintf(vecDescr[d], "dim%d", d+1);
    }

    Globals::vectorDescription = vecDescr;

    Globals::initHFM();

    Globals::trainHFM();

//    Globals::savePath = "/Users/basilio/Desktop";
//    Globals::saveHFMAs(HTML);

    //    if (Globals::SAVE_AS_HTML) Globals::saveHFMAs(HTML);
    //    if(Globals::SAVE_AS_SOMLIB) Globals::saveHFMAs(SOMLIB);

}

fvec ProjectorGHSOM::Project(const fvec &sample)
{
    return sample;
}