forked from xlladdins/xll_ml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxll_ml.cpp
More file actions
208 lines (183 loc) · 4.97 KB
/
xll_ml.cpp
File metadata and controls
208 lines (183 loc) · 4.97 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
// xll_ml.cpp
#include "fms_perceptron.h"
#include "xll_ml.h"
using namespace xll;
using namespace fms::perceptron;
AddIn xai_perceptron_update(
Function(XLL_FP, L"xll_perceptron_update", L"PERCEPTRON.UPDATE")
.Arguments({
Arg(XLL_FP, L"w", L"is an array of weights."),
Arg(XLL_FP, L"x", L"is an array representing the input vector."),
Arg(XLL_BOOL, L"y", L"is the label"),
Arg(XLL_DOUBLE, L"alpha", L"is the learning rate. (default=1.0)", 1.0)
})
.Category(CATEGORY)
.FunctionHelp(L"Update perceptron weights input vector and label.")
);
_FP12* WINAPI xll_perceptron_update(_FP12* pw, _FP12* px, BOOL y, double alpha)
{
#pragma XLLEXPORT
try {
ensure(size(*pw) == size(*px) || !"weight and input vector size mismatch");
alpha = alpha ? alpha : 1;
update(size(*pw), pw->array, px->array, y, alpha);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR(__FUNCDNAME__ ": unknown exception");
}
return pw;
}
AddIn xai_perceptron_train(
Function(XLL_FP, L"xll_perceptron_train", L"PERCEPTRON.TRAIN")
.Arguments({
Arg(XLL_FP, L"w", L"is an array of weights."),
Arg(XLL_FP, L"x", L"is an array representing the input vector."),
Arg(XLL_BOOL, L"y", L"is the label."),
Arg(XLL_DOUBLE, L"alpha", L"is the learning rate. (default=1.0)", 1.0),
Arg(XLL_UINT, L"n", L"is the maximum number of iterations. (default=100)", 100),
})
.Category(CATEGORY)
.FunctionHelp(L"Train perceptron weights on single input vector and label.")
);
_FP12* WINAPI xll_perceptron_train(_FP12* pw, _FP12* px, BOOL y, double alpha, UINT n)
{
#pragma XLLEXPORT
try {
ensure(size(*pw) == size(*px) || !"weight and input vector size mismatch");
alpha = alpha ? alpha : 1;
n = n ? n : 100;
train(size(*pw), pw->array, px->array, y, alpha, n);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR(__FUNCDNAME__ ": unknown exception");
}
return pw;
}
AddIn xai_neuron_(
Function(XLL_HANDLEX, L"xll_neuron_", L"\\NEURON")
.Arguments({
Arg(XLL_FP, L"w", L"is an array of initial weights."),
})
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return handle to a neuron with given weights.")
);
HANDLEX WINAPI xll_neuron_(_FP12* pw)
{
#pragma XLLEXPORT
HANDLEX h = INVALID_HANDLEX;
try {
handle<neuron<>> h_(new neuron<>(size(*pw), pw->array));
ensure(h_);
h = h_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR(__FUNCDNAME__ ": unknown exception");
}
return h;
}
AddIn xai_neuron(
Function(XLL_FP, L"xll_neuron", L"NEURON")
.Arguments({
Arg(XLL_HANDLEX, L"h", L"is a handle returned by \\NEURON."),
})
.Category(CATEGORY)
.FunctionHelp(L"Return array of weights.")
);
_FP12* WINAPI xll_neuron(HANDLEX h)
{
#pragma XLLEXPORT
static FPX w;
try {
handle<neuron<>> h_(h);
ensure(h_);
std::span<double> s = h_->span();
FPX w_((int)s.size(), 1, s.data());
w.swap(w_);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return 0; //
}
catch (...) {
XLL_ERROR(__FUNCDNAME__ ": unknown exception");
return 0;
}
return w.get();
}
AddIn xai_neuron_update(
Function(XLL_HANDLEX, L"xll_neuron_update", L"NEURON.UPDATE")
.Arguments({
Arg(XLL_HANDLEX, L"h", L"is a handle to a neuron."),
Arg(XLL_FP, L"x", L"is an array representing the input vector."),
Arg(XLL_BOOL, L"y", L"is the label."),
Arg(XLL_DOUBLE, L"alpha", L"is the learning rate. (defaul 1.0)", 1.0),
})
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return handle of updated neuron.")
);
HANDLEX WINAPI xll_neuron_update(HANDLEX h, _FP12* px, BOOL y, double alpha)
{
#pragma XLLEXPORT
try {
handle<neuron<>> h_(h);
ensure(h_);
alpha = alpha ? alpha : 1;
h_->update(px->array, y, alpha);
}
catch (const std::exception& ex) {
h = INVALID_HANDLEX;
XLL_ERROR(ex.what());
}
catch (...) {
h = INVALID_HANDLEX;
XLL_ERROR(__FUNCDNAME__ ": unknown exception");
}
return h;
}
AddIn xai_neuron_train(
Function(XLL_FP, L"xll_neuron_train", L"NEURON.TRAIN")
.Arguments({
Arg(XLL_HANDLEX, L"h", L"is a handle to a neuron."),
Arg(XLL_FP, L"x", L"is an array representing the input vector."),
Arg(XLL_BOOL, L"y", L"is the label."),
Arg(XLL_DOUBLE, L"alpha", L"is the learning rate. Default 1.", 1.0),
Arg(XLL_UINT, L"n", L"is the maximum number of iterations. Default 100.", 100),
})
.Category(CATEGORY)
.FunctionHelp(L"Return {handle, steps} after training a point.")
);
_FP12* WINAPI xll_neuron_train(HANDLEX h, _FP12* px, BOOL y, double alpha, UINT n)
{
#pragma XLLEXPORT
static FPX w(1,2); // 1 x 2 array of doubles
try {
handle<neuron<>> h_(h);
ensure(h_);
ensure(size(*px) == h_->span().size() || !"input vector size mismatch");
alpha = alpha ? alpha : 1.0;
n = n ? n : 100;
auto m = h_->train(px->array, y, alpha, n);
w[0] = h;
w[1] = static_cast<double>(m);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return 0;
}
catch (...) {
XLL_ERROR(__FUNCDNAME__ ": unknown exception");
return 0;
}
return w.get();
}