forked from microsoft/EdgeML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtoNNIngestTest.cpp
221 lines (184 loc) · 6.3 KB
/
ProtoNNIngestTest.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "ProtoNN.h"
using namespace EdgeML;
using namespace EdgeML::ProtoNN;
int main()
{
ProtoNNModel::ProtoNNHyperParams hyperParam;
hyperParam.problem_type = ProblemFormat::multiclass;
hyperParam.initialization_type = InitializationFormat::overall_kmeans;
hyperParam.dataformat_type = DataFormat::interface_ingest_format;
hyperParam.normalization_type = NormalizationFormat::none;
hyperParam.seed = 41;
hyperParam.batch_size = 100;
hyperParam.iters = 5;
hyperParam.epochs = 2;
hyperParam.D = 2;
hyperParam.d = 2;
hyperParam.m = 4;
hyperParam.k = 0;
hyperParam.l = 3;
hyperParam.ntest = 0;
hyperParam.gammaNumerator = 1.0;
hyperParam.lambda_W = 1.0;
hyperParam.lambda_Z = 1.0;
hyperParam.lambda_B = 1.0;
hyperParam.finalizeHyperParams();
// trivial data set
{
auto trainer = new ProtoNNTrainer(DataIngestType::InterfaceIngest, hyperParam);
FP_TYPE trainPts[2*16] = {-1.1, -1.1,
-0.9, -1.1,
-1.1, -0.9,
-0.9, -0.9,
1.1, 1.1,
0.9, 1.1,
1.1, 0.9,
0.9, 0.9,
-1.1, 1.1,
-0.9, 1.1,
-1.1, 0.9,
-0.9, 0.9,
1.1, -1.1,
0.9, -1.1,
1.1, -0.9,
0.9, -0.9};
labelCount_t labels[3] = {0,1,2};
for (int i=0; i<4; ++i)
trainer->feedDenseData (trainPts + 2*i, labels, 1);
for (int i=4; i<8; ++i)
trainer->feedDenseData (trainPts + 2*i, labels, 1);
for (int i=8; i<12; ++i)
trainer->feedDenseData (trainPts + 2*i, labels+1, 1);
for (int i=12; i<16; ++i)
trainer->feedDenseData (trainPts + 2*i, labels+2, 1);
trainer->finalizeData();
trainer->train();
auto modelBytes = trainer->getModelSize();
auto model = new char[modelBytes];
trainer->exportModel(modelBytes, model);
auto predictor = new ProtoNNPredictor(modelBytes, model);
FP_TYPE scoreArray[3] = {0.0, 0.0, 0.0};
FP_TYPE testPts[2*4] = {-1.0, -1.0,
1.0, 1.0,
-1.0, 1.0,
1.0, -1.0};
for (int t=0; t<4; ++t) {
predictor->scoreDenseDataPoint(scoreArray, testPts + 2*t);
for(int i=0;i<3;++i) std::cout<<scoreArray[i]<<" ";std::cout<<std::endl;
}
delete[] model;
delete trainer, predictor;
}
// Slightly less trivial example
{
auto trainer = new ProtoNNTrainer(DataIngestType::InterfaceIngest, hyperParam);
FP_TYPE trainPts[2*17] = {-1.1, -1.1,
-0.9, -1.1,
-1.1, -0.9,
-0.9, -0.9,
1.1, 1.1,
0.9, 1.1,
1.1, 0.9,
0.9, 0.9,
-1.1, 1.1,
-0.9, 1.1,
-1.1, 0.9,
-0.9, 0.9,
1.1, -1.1,
0.9, -1.1,
1.1, -0.9,
0.9, -0.9,
0.0, 0.0}; // Outlier
labelCount_t labels[3] = {0,1,2};
for (int i=0; i<3; ++i)
trainer->feedDenseData (trainPts + 2*i, labels, 1);
trainer->feedDenseData (trainPts + 6, labels + 1, 1);
for (int i=4; i<7; ++i)
trainer->feedDenseData (trainPts + 2*i, labels, 1);
trainer->feedDenseData (trainPts + 14, labels + 2, 1);
for (int i=8; i<11; ++i)
trainer->feedDenseData (trainPts + 2*i, labels+1, 1);
trainer->feedDenseData (trainPts + 22, labels + 2, 1);
for (int i=12; i<15; ++i)
trainer->feedDenseData (trainPts + 2*i, labels+2, 1);
trainer->feedDenseData (trainPts + 30, labels + 1, 1);
trainer->feedDenseData (trainPts + 32, labels+2, 1);
trainer->finalizeData();
trainer->train();
auto modelBytes = trainer->getModelSize();
auto model = new char[modelBytes];
trainer->exportModel(modelBytes, model);
auto predictor = new ProtoNNPredictor(modelBytes, model);
FP_TYPE scoreArray[3] = {0.0, 0.0, 0.0};
FP_TYPE testPts[2*5] = {-1.0, -1.0,
1.0, 1.0,
-1.0, 1.0,
1.0, -1.0,
0.5, 0.5};
for (int t=0; t<5; ++t) {
predictor->scoreDenseDataPoint(scoreArray, testPts + 2*t);
for(int i=0;i<3;++i) std::cout<<scoreArray[i]<<" ";std::cout<<std::endl;
}
delete[] model;
delete trainer, predictor;
}
// Slightly less trivial example for sparse data
{
auto trainer = new ProtoNNTrainer(DataIngestType::InterfaceIngest, hyperParam);
featureCount_t indices[2] = {0, 1};
int numIndices = 2;
FP_TYPE trainPts[2*17] = {-1.1, -1.1,
-0.9, -1.1,
-1.1, -0.9,
-0.9, -0.9,
1.1, 1.1,
0.9, 1.1,
1.1, 0.9,
0.9, 0.9,
-1.1, 1.1,
-0.9, 1.1,
-1.1, 0.9,
-0.9, 0.9,
1.1, -1.1,
0.9, -1.1,
1.1, -0.9,
0.9, -0.9,
0.0, 0.0}; // Outlier
labelCount_t labels[3] = {0,1,2};
for (int i=0; i<3; ++i)
trainer->feedSparseData (trainPts + 2*i, indices, numIndices, labels, 1);
trainer->feedSparseData (trainPts + 6, indices, numIndices, labels + 1, 1);
for (int i=4; i<7; ++i)
trainer->feedSparseData (trainPts + 2*i, indices, numIndices, labels, 1);
trainer->feedSparseData (trainPts + 14, indices, numIndices, labels + 2, 1);
for (int i=8; i<11; ++i)
trainer->feedSparseData (trainPts + 2*i, indices, numIndices, labels+1, 1);
trainer->feedSparseData (trainPts + 22, indices, numIndices, labels + 2, 1);
for (int i=12; i<15; ++i)
trainer->feedSparseData (trainPts + 2*i, indices, numIndices, labels+2, 1);
trainer->feedSparseData (trainPts + 30, indices, numIndices, labels + 1, 1);
trainer->feedSparseData (trainPts + 32, indices, numIndices, labels+2, 1);
trainer->finalizeData();
trainer->train();
auto modelBytes = trainer->getModelSize();
auto model = new char[modelBytes];
trainer->exportModel(modelBytes, model);
auto predictor = new ProtoNNPredictor(modelBytes, model);
FP_TYPE scoreArray[3] = {0.0, 0.0, 0.0};
FP_TYPE testPts[2*5] = {-1.0, -1.0,
1.0, 1.0,
-1.0, 1.0,
1.0, -1.0,
0.5, 0.5};
for (int t=0; t<5; ++t) {
//predictor->scoreDenseDataPoint(scoreArray, testPts + 2*t);
// both dense and sparse scoring work
predictor -> scoreSparseDataPoint(scoreArray, testPts + 2*t, indices, 2);
for(int i=0;i<3;++i) std::cout<<scoreArray[i]<<" ";std::cout<<std::endl;
}
delete[] model;
delete trainer, predictor;
}
}