84 for (
int x = 0; x <
outputs; x++)
87 weight[x].resize(newSize+1);
91 else if (newSize ==
inputs)
96 for (
int x = 0; x <
outputs; x++)
98 weight[x].resize(newSize+1);
100 for (
int y =
inputs; y < newSize; y++)
113 for (
int x = 0; x <
outputs; x++)
116 weight[x].resize(newSize+1);
120 else if (newSize ==
inputs)
125 for (
int x = 0; x <
outputs; x++)
127 weight[x].resize(newSize+1);
129 for (
int y =
inputs; y < newSize; y++)
130 weight[x][y] = ((
double)2*random()/RAND_MAX-1)/(double)newSize;
144 for (
int x = 0; x <
outputs; x++)
148 for (
int y = 0; y <=
inputs; y++)
149 weight[x][y] = ((
double)2*random()/RAND_MAX-1)/(double)
inputs;
171 f = fopen(fname,
"r");
174 fprintf(stderr,
"REGR Error: could not open file '%s' for loading; trying once more.\n", fname);
176 f = fopen(fname,
"r");
179 fprintf(stderr,
"REGR Error: could not open file '%s' for loading.\n", fname);
190 int inputs1, outputs1;
192 int res = fscanf(f,
"REGR %f %d %d\n", &version, &inputs1, &outputs1);
195 printf(
"Error: unrecognized LinearRegression file. Expected header 'PERP <version> <inputs> <outputs>'.");
200 printf(
"Error: loaded network is %1.2f, but code can only handle %1.2f to %1.2f.",
207 res = fscanf(f,
"%s\n", text);
209 if (strcmp(text,
"binary") == 0)
223 for (
int y = 0; y <
outputs; y++)
225 for (
int x = 0; x <=
inputs; x++)
230 fread(&shrunk,
sizeof(
float), 1, f);
236 fscanf(f,
"%le ", &
weight[y][x]);
255 for (
int y = 0; y <
outputs; y++)
257 for (
int x = 0; x <=
inputs; x++)
268 f = fopen(fname,
"r");
275 int res = fscanf(f,
"REGR %f %d %d\n", &version, &finput, &foutput);
291 f = fopen(fname,
"w+");
294 fprintf(stderr,
"Error: could not open file for saving.\n");
305 fprintf(f,
"binary\n");
307 fprintf(f,
"text\n");
309 for (
int y = 0; y <
outputs; y++)
311 for (
int x = 0; x <=
inputs; x++)
317 fwrite(&val,
sizeof(
float), 1, f);
320 fprintf(f,
"%le ",
weight[y][x]);
330 double err = (out[which]-expected[which]);
339 for (
int x = 0; x <
outputs; x++)
343 double rateTimesError =
rate*err;
344 for (
unsigned int y = 0; y < input.size(); y++)
346 weight[x][input[y]] -= rateTimesError;
350 double delta = rateTimesError-val.
mean;
352 val.
S += delta*(err-val.
mean);
353 val.
totErr += rateTimesError*rateTimesError;
364 for (
int x = 0; x <
outputs; x++)
368 double rateTimesError =
rate*err;
369 for (
int y = 0; y <
inputs; y++)
371 weight[x][y] -= rateTimesError*input[y];
376 double delta = rateTimesError-val.
mean;
378 val.
S += delta*(err-val.
mean);
379 val.
totErr += rateTimesError*rateTimesError;
388 for (
int y = 0; y <
outputs; y++)
391 for (
unsigned int x = 0; x < input.size(); x++)
401 for (
int y = 0; y <
outputs; y++)
404 for (
int x = 0; x <
inputs; x++)
414 assert(which <
weight.size());
415 var.resize(
weight[which].size()-1);
416 for (
unsigned int x = 0; x <
weight[which].size()-1; x++)
420 var[x] = val.
S/(val.
n-1);
428 assert(whichOutput <
weight.size());
429 assert(weightNum <
weight[whichOutput].size());
433 return val.
S/(val.
n-1);
439 assert(which <
weight.size());
440 var.resize(
weight[which].size()-1);
441 for (
unsigned int x = 0; x <
weight[which].size()-1; x++)
453 assert(whichOutput <
weight.size());
454 assert(weightNum <
weight[whichOutput].size());
464 assert(which <
weight.size());
465 var.resize(
weight[which].size()-1);
466 for (
unsigned int x = 0; x <
weight[which].size()-1; x++)
468 var[x] =
updates[which][x].totErr;
474 assert(whichOutput <
weight.size());
475 assert(weightNum <
weight[whichOutput].size());
477 return updates[whichOutput][weightNum].totErr;
482 updates[whichOutput][weightNum].reset();
487 for (
unsigned int y = 0; y <
updates.size(); y++)
489 for (
unsigned int x = 0; x <
updates[y].size(); x++)
498 return updates[whichOutput][weightNum].n;
503 updates[whichOutput][weightNum].reset();
504 weight[whichOutput][weightNum] = value;
509 for (
int y = 0; y <
outputs; y++)
510 for (
int x = 0; x <=
inputs; x++)
512 printf(
"%1.3f ",
weight[y][x]);