-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleCode.cpp
More file actions
71 lines (55 loc) · 1.81 KB
/
Copy pathSampleCode.cpp
File metadata and controls
71 lines (55 loc) · 1.81 KB
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
#include <iostream>
#include "SOM.h"
using namespace std;
void printPoints(vector<vector<double>> &weights) {
for (int i = 0; i < weights.size(); ++i) {
cout << weights[i][0] << "," << weights[i][1] << endl;
}
}
int main() {
// training data
vector<vector<double>> data = {
{2,2},
{5,5},
{2,1},
{1,2},
{6,5},
{5,6},
{10,4},
{11,3},
{9,2},
{6,7}
};
// initial weights
vector<vector<double>> weights = {
{1, 3},
{6, 4},
{10, 1}
};
// print the adjusted weights
cout << "Initial weights (x,y)" << endl;
printPoints(weights);
// Create a new SOM object and set the parameters
SOM som = SOM::builder()
.setEpochs(100) // number if iterations > 0
.setLearningRate(1) // learning rate <= 1 and > 0
.setNeighborhoodSize(0) // neighborhood function size
.build();
// train the network using the provided training data and initial weights
clock_t tStart = clock(); // track training time
som.trainData(data, weights);
double time = (double) (clock() - tStart) / CLOCKS_PER_SEC;
// get the adjusted weights and print them
vector<vector<double>> adjusted = som.getWeights();
cout << "\nAdjusted weights (x,y)" << endl;
printPoints(adjusted);
// Print points and their clusters
cout << "\nPoints clusters" << endl;
for (int i = 0; i < data.size(); ++i) {
int clusterIndex = som.getCluster(data[i]);
cout << data[i][0] << "," << data[i][1] << " belongs to cluster " <<
adjusted[clusterIndex][0] << "," << adjusted[clusterIndex][1] << endl;
}
// print training time
printf("\nTime taken: %.2fs\n", time);
}