-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.cpp
More file actions
259 lines (215 loc) · 7.82 KB
/
interface.cpp
File metadata and controls
259 lines (215 loc) · 7.82 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
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
#include "module.h"
#include <emscripten.h>
#include <emscripten/bind.h>
// Takes a javascript function name and calls it if it exists
#define CALL_JS_FUNC(FUNC_NAME) \
EM_ASM({ \
var funcName = UTF8ToString($0); \
if (typeof window[funcName] === 'function') { \
window[funcName](); \
} \
}, FUNC_NAME);
class NetworkController {
private:
nn::vi_t dimensions;
double alpha{};
std::string actFunction;
std::string lossFunction;
nn::Module module;
static nn::vvd_t pairToVector(const nn::vpd_t &data) {
nn::vvd_t res;
for (const auto &[i, j]: data) { res.push_back({i, j}); }
return res;
}
static nn::act::Function stringToActivationFunction(const std::string &function) {
if (function == "sigmoid") {
return nn::act::sigmoid;
} else if (function == "tanh") {
return nn::act::tanh;
} else {
return nn::act::relu;
}
}
static nn::loss::function_t stringToLossFunction(const std::string &function) {
if (function == "mse") {
return nn::loss::mse;
} else {
return nn::loss::sse;
}
}
void _setDimensions(const nn::vi_t &networkDimensions) {
this->dimensions = networkDimensions;
CALL_JS_FUNC("onDimensionsSet")
}
void _setLearningRate(double learningRate) {
this->alpha = learningRate;
CALL_JS_FUNC("onLearningRateSet")
}
void _setActivationFunction(const std::string &function) {
this->actFunction = function;
CALL_JS_FUNC("onActivationFunctionSet")
}
void _setLossFunction(const std::string &function) {
this->lossFunction = function;
CALL_JS_FUNC("onLossFunctionSet")
}
public:
/**
* On Construction, no events are triggered.
*/
NetworkController() = default;
/**
* On initialization, events are triggered sequentially:
* - `onDimensionsSet`
* - `onLearningRateSet`
* - `onActivationFunctionSet`
* - `onLossFunctionSet`
* - `onNetworkBuilt`
*/
void init() {
_setDimensions({4, 3, 4});
_setLearningRate(0.01);
_setActivationFunction("tanh");
_setLossFunction("sse");
build();
}
/**
* Builds the module and triggers `onNetworkBuilt` event.
*/
void build() {
nn::act::Function act = stringToActivationFunction(actFunction);
nn::loss::function_t loss = stringToLossFunction(lossFunction);
module.setNetwork(nn::make::network(dimensions, act, loss));
module.setLearningRate(alpha);
CALL_JS_FUNC("onNetworkBuilt")
}
void setDimensions(const nn::vi_t &networkDimensions) {
_setDimensions(networkDimensions);
build();
}
[[nodiscard]] nn::vi_t getDimensions() const {
return dimensions;
}
void setLearningRate(double learningRate) {
_setLearningRate(learningRate);
module.setLearningRate(alpha);
}
[[nodiscard]] double getLearningRate() const {
return alpha;
}
void setActivationFunction(const std::string &function) {
_setActivationFunction(function);
build();
}
[[nodiscard]] std::string getActivationFunction() const {
return actFunction;
}
void setLossFunction(const std::string &function) {
_setLossFunction(function);
build();
}
[[nodiscard]] std::string getLossFunction() const {
return lossFunction;
}
[[nodiscard]] nn::vvvd_t getWeights() const {
return module.getWeights();
}
[[nodiscard]] nn::vvd_t getBiases() const {
return module.getBiases();
}
void setTrainInput(const nn::vvd_t &data) {
module.setTrainInput(data);
CALL_JS_FUNC("onTrainInputSet")
}
[[nodiscard]] nn::vvd_t getTrainInput() const {
return module.getTrainInput();
}
void clearTrainInput() {
module.setTrainInput({});
CALL_JS_FUNC("onTrainInputCleared")
}
void setTrainOutput(const nn::vvd_t &data) {
module.setTrainOutput(data);
CALL_JS_FUNC("onTrainOutputSet")
}
[[nodiscard]] nn::vvd_t getTrainOutput() const {
return module.getTrainOutput();
}
void clearTrainOutput() {
module.setTrainOutput({});
CALL_JS_FUNC("onTrainOutputCleared")
}
void setTestInput(const nn::vvd_t &data) {
module.setTestInput(data);
CALL_JS_FUNC("onTestInputSet")
}
[[nodiscard]] nn::vvd_t getTestInput() const {
return module.getTestInput();
}
void clearTestInput() {
module.setTestInput({});
CALL_JS_FUNC("onTestInputCleared")
}
void setTestOutput(const nn::vvd_t &data) {
module.setTestOutput(data);
CALL_JS_FUNC("onTestOutputSet")
}
[[nodiscard]] nn::vvd_t getTestOutput() const {
return module.getTestOutput();
}
void clearTestOutput() {
module.setTestOutput({});
CALL_JS_FUNC("onTestOutputCleared")
}
nn::vd_t trainFor(std::size_t epochs) {
return module.train(epochs);
}
nn::vvd_t trainAndTestFor(std::size_t epochs) {
return pairToVector(module.trainAndTest(epochs));
}
[[nodiscard]] nn::vvd_t getPredictions() const {
return module.predict();
}
[[nodiscard]] nn::vvd_t getCustomPredictions(const nn::vvd_t &data) const {
return module.predict(data);
}
};
EMSCRIPTEN_BINDINGS(my_module) {
using namespace emscripten;
register_vector<int>("VecInt");
register_vector<nn::ui_t>("VecUInt");
register_vector<double>("VecNum");
register_vector<nn::vd_t>("VecVecNum");
register_vector<nn::vvd_t>("VecVecVecNum");
class_<NetworkController>("Network")
.constructor<>()
.function("init", &NetworkController::init)
.function("build", &NetworkController::build)
.function("setDimensions", &NetworkController::setDimensions)
.function("getDimensions", &NetworkController::getDimensions)
.function("setLearningRate", &NetworkController::setLearningRate)
.function("getLearningRate", &NetworkController::getLearningRate)
.function("setActivationFunction", &NetworkController::setActivationFunction)
.function("getActivationFunction", &NetworkController::getActivationFunction)
.function("setLossFunction", &NetworkController::setLossFunction)
.function("getLossFunction", &NetworkController::getLossFunction)
.function("getWeights", &NetworkController::getWeights)
.function("getBiases", &NetworkController::getBiases)
.function("setTrainInput", &NetworkController::setTrainInput)
.function("getTrainInput", &NetworkController::getTrainInput)
.function("clearTrainInput", &NetworkController::clearTrainInput)
.function("setTrainOutput", &NetworkController::setTrainOutput)
.function("getTrainOutput", &NetworkController::getTrainOutput)
.function("clearTrainOutput", &NetworkController::clearTrainOutput)
.function("setTestInput", &NetworkController::setTestInput)
.function("getTestInput", &NetworkController::getTestInput)
.function("clearTestInput", &NetworkController::clearTestInput)
.function("setTestOutput", &NetworkController::setTestOutput)
.function("getTestOutput", &NetworkController::getTestOutput)
.function("clearTestOutput", &NetworkController::clearTestOutput)
.function("trainFor", &NetworkController::trainFor)
.function("trainAndTestFor", &NetworkController::trainAndTestFor)
.function("getPredictions", &NetworkController::getPredictions)
.function("getCustomPredictions", &NetworkController::getCustomPredictions);
}
int main() {}