-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparacortex.cc
More file actions
235 lines (177 loc) · 3.74 KB
/
paracortex.cc
File metadata and controls
235 lines (177 loc) · 3.74 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
#define __MAKEMORE_PARACORTEX_CC__ 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <vector>
#include "cortex.hh"
#include "youtil.hh"
#include "paracortex.hh"
#include "colonel.hh"
namespace makemore {
Paracortex::Paracortex() {
prepared = false;
rmul = 1.0;
_kinp = NULL;
_kout = NULL;
iw = ih = ic = iwhc = 0;
ow = oh = oc = owhc = 0;
}
Paracortex::~Paracortex() {
for (auto ctx : ctxv)
delete ctx;
if (_kinp)
kfree(_kinp);
if (_kout)
kfree(_kout);
}
void Paracortex::push(const std::string &fn, int mode) {
Cortex *ctx = new Cortex(fn, mode);
info("adding " + fn + " to paracortex");
ctx->rmul = rmul;
ctxv.push_back(ctx);
ic += ctx->ic;
}
void Paracortex::prepare(int _iw, int _ih) {
assert(!prepared);
int n = ctxv.size();
assert(n > 0);
iw = _iw;
ih = _ih;
ctxv[0]->prepare(iw, ih);
ic = ctxv[0]->ic;
ow = ctxv[0]->ow;
oh = ctxv[0]->oh;
oc = ctxv[0]->oc;
if (n == 1) {
iwhc = iw * ih * ic;
owhc = ow * oh * oc;
prepared = true;
return;
}
for (int i = 1; i < n; ++i) {
ctxv[i]->prepare(iw, ih);
ic += ctxv[i]->ic;
assert(ctxv[i]->ow == ow);
assert(ctxv[i]->oh == oh);
oc += ctxv[i]->oc;
}
iwhc = iw * ih * ic;
owhc = ow * oh * oc;
kmake(&_kinp, iwhc);
kmake(&_kout, owhc);
prepared = true;
}
double *Paracortex::kinp() {
assert(prepared);
int n = ctxv.size();
assert(n > 0);
if (n == 1) {
return ctxv[0]->kinp;
}
assert(_kinp);
return _kinp;
}
double *Paracortex::kout() {
assert(prepared);
int n = ctxv.size();
assert(n > 0);
if (n == 1) {
return ctxv[0]->kout;
}
assert(_kout);
return _kout;
}
double *Paracortex::synth(double *kinp) {
assert(prepared);
int n = ctxv.size();
assert(n > 0);
if (n == 1) {
ctxv[0]->synth(kinp);
return ctxv[0]->kout;
}
if (kinp)
kcopy(kinp, iwhc, _kinp);
int off = 0;
for (int i = 0; i < n; ++i) {
ksplice(_kinp, iw * ih, ic, off, ctxv[i]->ic, ctxv[i]->kinp, ctxv[i]->ic, 0);
off += ctxv[i]->ic;
ctxv[i]->synth();
}
assert(off == ic);
off = 0;
for (int i = 0; i < n; ++i) {
ksplice(ctxv[i]->kout, ow * oh, ctxv[i]->oc, 0, ctxv[i]->oc, _kout, oc, off);
off += ctxv[i]->oc;
}
assert(off == oc);
return _kout;
}
void Paracortex::target(const double *ktgt) {
assert(prepared);
int n = ctxv.size();
assert(n > 0);
if (n == 1) {
ctxv[0]->target(ktgt);
return;
}
ksubvec(ktgt, _kout, owhc, _kout);
int off = 0;
for (int i = 0; i < n; ++i) {
ksplice(_kout, ow * oh, oc, off, ctxv[i]->oc, ctxv[i]->kout, ctxv[i]->oc, 0);
off += ctxv[i]->oc;
}
assert(off == oc);
}
void Paracortex::learn(double mul) {
assert(prepared);
int n = ctxv.size();
assert(n > 0);
if (n == 1) {
ctxv[0]->learn(mul);
return;
}
int off = 0;
for (int i = 0; i < n; ++i) {
ksplice(_kout, ow * oh, oc, off, ctxv[i]->oc, ctxv[i]->kout, ctxv[i]->oc, 0);
off += ctxv[i]->oc;
ctxv[i]->learn(mul);
}
assert(off == oc);
off = 0;
for (int i = 0; i < n; ++i) {
ksplice(ctxv[i]->kinp, iw * ih, ctxv[i]->ic, 0, ctxv[i]->ic, _kinp, ic, off);
off += ctxv[i]->ic;
}
assert(off == ic);
}
void Paracortex::load() {
for (auto ctx : ctxv)
ctx->load();
}
void Paracortex::save() {
for (auto ctx : ctxv)
ctx->save();
}
unsigned long Paracortex::rounds() {
assert(prepared);
int n = ctxv.size();
assert(n > 0);
return ctxv[0]->rounds;
}
std::string Paracortex::fn() {
assert(prepared);
int n = ctxv.size();
assert(n > 0);
return ctxv[0]->fn;
}
double Paracortex::rms() {
assert(prepared);
double z = 0;
for (auto ctx : ctxv)
z += ctx->rms * ctx->rms * ctx->oc;
z /= (double)oc;
z = sqrt(z);
return z;
}
}