#ifndef NNTRAIN_INC_C
#define NNTRAIN_INC_C
#include "nnlayer.inc.c"
typedef struct NeuralTrainer {
int sizeX, sizeY, count;
double *x, *y;
} NeuralTrainer;
NeuralTrainer* ntNew(int sizeX, int sizeY, int count) {
assert(sizeX > 0);
assert(sizeY > 0);
assert(count > 0);
NeuralTrainer *nt = calloc(sizeof(NeuralTrainer), 1);
nt->sizeX = sizeX;
nt->sizeY = sizeY;
nt->count = count;
nt->x = calloc(sizeof(double)*(sizeX + sizeY)*count, 1);
nt->y = nt->x + sizeX*count;
return nt;
}
void ntFree(NeuralTrainer *nt) {
free(nt->x);
free(nt);
}
double ntTrain(NeuralTrainer *nt, NeuralLayer *nl, int successCount, int blockSize, double qmin) {
assert(!nl->prev);
assert(nt->sizeX == nl->size);
assert(nt->sizeY == nlBack(nl)->size);
assert(blockSize > 0 && qmin > 0);
printf("training: %d, %lf\n", blockSize, qmin);
double **blockXY = calloc(sizeof(double)*2, blockSize);
double qmin2 = qmin*0.75;
double qmin3 = qmin2*0.75;
int success = 0;
int total = 0;
int repeats, blockRepeats;
double qmax0, qsum0, qmax, qsum;
for(int i = 0; i < 10000; ++i) {
for(int i = 0; i < blockSize; ++i) {
int index = rand() % nt->count;
blockXY[i*2 + 0] = nt->x + nt->sizeX*index;
blockXY[i*2 + 1] = nt->y + nt->sizeY*index;
}
repeats = blockRepeats = 0;
qmax0 = qsum0 = 0;
for(int i = 0; i < 1000; ++i) {
double **xy = blockXY;
qmax = 0, qsum = 0;
for(int i = 0; i < blockSize; ++i, xy += 2) {
double q0 = 0;
for(int i = 0; i < 100; ++i) {
double q = nlTrainPass(nl, xy[0], xy[1], qmin3);
if (!i) q0 = q;
++repeats;
if (q < qmin3) break;
}
qsum += q0;
if (qmax < q0) qmax = q0;
}
if (!i) { qmax0 = qmax; qsum0 = qsum; }
++blockRepeats;
if (qmax <= qmin2) break;
}
total += repeats;
printf(" blocks %d (samples: %d, total: %d, repeats: %3d (%lf)): %lf -> %lf, %lf -> %lf\n",
i+1, (i+1)*blockSize, total, blockRepeats-1, repeats/(double)(blockRepeats*blockSize) - 1, qmax0, qmax, qsum0/blockSize, qsum/blockSize);
if (qmax0 <= qmin) {
if (++success == successCount) break;
} else {
success = 0;
}
}
free(blockXY);
printf("done\n");
return qmax0;
}
NeuralTrainer* ntNewSymbolMap(const char *filename, int sizeX, int sizeY) {
FILE *f = fopen(filename, "rb");
if (!f)
return printf("cannot open file '%s' for read\n", filename), NULL;
fseek(f, 0, SEEK_END);
size_t fs = ftell(f);
fseek(f, 0, SEEK_SET);
size_t testSize = sizeX + 1;
int count = fs/testSize;
if (!count)
return printf("file '%s' is lesser minimal size\n", filename), fclose(f), NULL;
unsigned char *data = calloc(testSize, count);
if (count != fread(data, testSize, count, f))
return printf("cannot read from file '%s'\n", filename), free(data), fclose(f), NULL;
fclose(f);
NeuralTrainer *nt = ntNew(sizeX, sizeY, count);
const unsigned char *d = data;
double *x = nt->x, *y = nt->y, *ey = y + sizeY*count;
const double delta = 0;
for(double *p = y; p < ey; ++p) *p = delta;
while(y < ey) {
for(double *e = x + sizeX; x < e; ++x, ++d)
*x = *d/255.0;
assert(*d < sizeY);
y[*d++] = 1 - delta;
y += sizeY;
}
return nt;
}
void ntPrintSymbol(NeuralTrainer *nt, int index, int width) {
assert(index >= 0 && index < nt->count);
assert(width > 0);
for(int i = 0; i < nt->sizeX; ++i) {
if (i && !(i % width)) printf("\n");
printf("%c", nt->x[nt->sizeX*index + i] > 0 ? '#' : '.');
}
printf("\n");
for(int i = 0; i < nt->sizeY; ++i)
printf(" %4.1lf", nt->y[nt->sizeY*index + i]);
printf("\n");
}
#endif