83 for (
int x = 0; x <
outputs; x++)
86 weight[x].resize(newSize+1);
90 else if (newSize ==
inputs)
95 for (
int x = 0; x <
outputs; x++)
97 weight[x].resize(newSize+1);
99 for (
int y =
inputs; y < newSize; y++)
112 for (
int x = 0; x <
outputs; x++)
115 weight[x].resize(newSize+1);
119 else if (newSize ==
inputs)
124 for (
int x = 0; x <
outputs; x++)
126 weight[x].resize(newSize+1);
128 for (
int y =
inputs; y < newSize; y++)
129 weight[x][y] = ((
double)2*random()/RAND_MAX-1)/(double)newSize;
143 for (
int x = 0; x <
outputs; x++)
147 for (
int y = 0; y <=
inputs; y++)
171 f = fopen(fname,
"r");
174 fprintf(stderr,
"LOG_REGR Error: could not open file '%s' for loading; trying once more.\n", fname);
176 f = fopen(fname,
"r");
179 fprintf(stderr,
"LOG_REGR Error: could not open file '%s' for loading.\n", fname);
190 int inputs1, outputs1;
192 int res = fscanf(f,
"LOG_REGR %f %d %d\n", &version, &inputs1, &outputs1);
195 printf(
"Error: unrecognized LogisticRegression 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,
"LOG_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]);
329 return (1/(1+exp(-a)));
340 double err = (expected[which]-out[which]);
341 err *=
dg(out[which]);
353 for (
int x = 0; x <
outputs; x++)
357 double rateTimesError =
rate*err;
358 for (
unsigned int y = 0; y < input.size(); y++)
360 weight[x][input[y]] -= rateTimesError;
364 double delta = rateTimesError-val.
mean;
366 val.
S += delta*(err-val.
mean);
367 val.
totErr += rateTimesError*rateTimesError;
378 for (
int x = 0; x <
outputs; x++)
382 double rateTimesError =
rate*err;
383 for (
int y = 0; y <
inputs; y++)
385 weight[x][y] += rateTimesError*input[y];
390 double delta = rateTimesError-val.
mean;
392 val.
S += delta*(err-val.
mean);
393 val.
totErr += rateTimesError*rateTimesError;
402 for (
int y = 0; y <
outputs; y++)
405 for (
unsigned int x = 0; x < input.size(); x++)
416 for (
int y = 0; y <
outputs; y++)
419 for (
int x = 0; x <
inputs; x++)
430 assert(which <
weight.size());
431 var.resize(
weight[which].size()-1);
432 for (
unsigned int x = 0; x <
weight[which].size()-1; x++)
436 var[x] = val.
S/(val.
n-1);
444 assert(whichOutput <
weight.size());
445 assert(weightNum <
weight[whichOutput].size());
449 return val.
S/(val.
n-1);
455 assert(which <
weight.size());
456 var.resize(
weight[which].size()-1);
457 for (
unsigned int x = 0; x <
weight[which].size()-1; x++)
469 assert(whichOutput <
weight.size());
470 assert(weightNum <
weight[whichOutput].size());
480 assert(which <
weight.size());
481 var.resize(
weight[which].size()-1);
482 for (
unsigned int x = 0; x <
weight[which].size()-1; x++)
484 var[x] =
updates[which][x].totErr;
490 assert(whichOutput <
weight.size());
491 assert(weightNum <
weight[whichOutput].size());
493 return updates[whichOutput][weightNum].totErr;
498 updates[whichOutput][weightNum].reset();
503 for (
unsigned int y = 0; y <
updates.size(); y++)
505 for (
unsigned int x = 0; x <
updates[y].size(); x++)
514 return updates[whichOutput][weightNum].n;
519 updates[whichOutput][weightNum].reset();
520 weight[whichOutput][weightNum] = value;
525 for (
int y = 0; y <
outputs; y++)
526 for (
int x = 0; x <=
inputs; x++)
528 printf(
"%1.3f ",
weight[y][x]);