Blame simple/neural/nntrain.inc.cpp

56d550
#ifndef NNTRAIN_INC_CPP
56d550
#define NNTRAIN_INC_CPP
56d550
56d550
56d550
#include "nnlayer.inc.cpp"
56d550
56d550
56d550
class Trainer {
56d550
public:
025224
  double trainRatio;
56d550
  int sizeX, sizeY, count;
56d550
  double *x, *y;
56d550
56d550
025224
  explicit Trainer(double trainRatio = 0.5): trainRatio(trainRatio), sizeX(), sizeY(), count(), x(), y() { }
56d550
025224
  Trainer(double trainRatio, int sizeX, int sizeY, int count):
025224
    Trainer(trainRatio)
56d550
    { init(sizeX, sizeY, count); }
56d550
56d550
  ~Trainer()
025224
    { deinit(); }
56d550
56d550
56d550
  void init(int sizeX, int sizeY, int count) {
025224
    deinit();
56d550
    assert(sizeX > 0);
56d550
    assert(sizeY > 0);
56d550
    assert(count > 0);
56d550
    this->sizeX = sizeX;
56d550
    this->sizeY = sizeY;
56d550
    this->count = count;
56d550
    x = new double[(sizeX + sizeY)*count];
56d550
    y = x + sizeX*count;
56d550
    memset(x, 0, sizeof(*x)*(sizeX + sizeY)*count);
56d550
  }
56d550
56d550
  void deinit() {
56d550
    if (!count) return;
56d550
    delete[] x;
56d550
    sizeX = sizeY = count = 0;
56d550
    x = y = nullptr;
56d550
  }
56d550
56d550
025224
  double trainSimple(Layer &l, int successCount, double qmin, int reportStep) {
025224
    assert(count);
025224
    assert(!l.prev);
025224
    assert(sizeX == l.size);
025224
    assert(sizeY == l.back().size);
025224
    assert(successCount > 0);
025224
    assert(qmin > 0);
025224
025224
    printf("training: %d, %lf\n", successCount, qmin);
025224
    double *res = new double[successCount];
025224
    double *rp = res, *re = res + successCount;
025224
    double rsum = 0;
025224
    int rcount = 0;
025224
    memset(res, 0, sizeof(*res)*successCount);
025224
025224
    int success = 0, total = 0, nextReport = reportStep;
025224
    double avg = 0;
025224
    for(int i = 0; i < 1000000000; ++i) {
025224
      int index = rand() % count;
025224
025224
      double target = avg*0.5;
025224
      double q = 0;
025224
      for(int i = 0; i < 10; ++i) {
025224
        double qq = l.trainPass(trainRatio, x + sizeX*index, y + sizeY*index, target);
025224
        if (q < qq) q = qq;
025224
        ++total;
025224
        if (qq <= target) break;
025224
        break;
025224
      }
025224
025224
025224
      rcount += (q > qmin) - (*rp > qmin);
025224
      rsum += q - *rp;
025224
      *rp++ = q;
025224
      if (rp == re) rp = res;
025224
025224
      int cnt = i+1 < successCount ? i+1 : successCount;
025224
      avg = rsum/cnt;
025224
025224
      if (q > qmin) success = 0; else ++success;
025224
025224
      if (total >= nextReport || success >= successCount) {
025224
        printf("  iterations: %d, error rate: %lf, avg res: %lf\n", total, rcount/(double)cnt, avg);
025224
        nextReport = total + reportStep;
025224
      }
025224
      if (success >= successCount) break;
025224
    }
025224
025224
    delete[] res;
025224
    printf("done\n");
025224
    return rsum/successCount;
025224
  }
025224
025224
025224
  double trainBlock(Layer &l, int successCount, int blockSize, double qmin, int reportStep = 0) {
56d550
    assert(count);
56d550
    assert(!l.prev);
56d550
    assert(sizeX == l.size);
56d550
    assert(sizeY == l.back().size);
56d550
    assert(blockSize > 0 && qmin > 0);
025224
    assert(reportStep >= 0);
56d550
56d550
    printf("training: %d, %lf\n", blockSize, qmin);
56d550
    double **blockXY = new double*[blockSize*2];
56d550
    double qmin2 = qmin*0.9;
56d550
    double qmin3 = qmin2*0.9;
56d550
56d550
    int success = 0;
025224
    int total = 0, nextReport = reportStep;
56d550
    int repeats, blockRepeats;
56d550
    double qmax0, qsum0, qmax, qsum;
025224
    for(int i = 0; i < 1000000; ++i) {
56d550
      for(int i = 0; i < blockSize; ++i) {
56d550
        int index = rand() % count;
56d550
        blockXY[i*2 + 0] = x + sizeX*index;
56d550
        blockXY[i*2 + 1] = y + sizeY*index;
56d550
      }
56d550
56d550
      repeats = blockRepeats = 0;
56d550
      qmax0 = qsum0 = 0;
56d550
      for(int i = 0; i < 1000; ++i) {
56d550
        double **xy = blockXY;
56d550
        qmax = 0, qsum = 0;
56d550
        for(int i = 0; i < blockSize; ++i, xy += 2) {
56d550
          double q0 = 0;
56d550
          for(int i = 0; i < 100; ++i) {
025224
            double q = l.trainPass(trainRatio, xy[0], xy[1], qmin3);
56d550
            if (!i) q0 = q;
56d550
            ++repeats;
56d550
            if (q < qmin3) break;
56d550
          }
56d550
          qsum += q0;
56d550
          if (qmax < q0) qmax = q0;
56d550
        }
56d550
        if (!i) { qmax0 = qmax; qsum0 = qsum; }
56d550
        ++blockRepeats;
56d550
        if (qmax <= qmin2) break;
56d550
      }
56d550
      total += repeats;
56d550
56d550
025224
      if (qmax0 > qmin) success = 0; else ++success;
025224
025224
      if (total >= nextReport || success >= successCount) {
025224
        nextReport = total + reportStep;
025224
        printf("  blocks %d (samples: %d, total: %d, repeats: %3d (%lf)): %lf -> %lf, %lf -> %lf\n",
025224
          i+1, (i+1)*blockSize, total, blockRepeats-1, repeats/(double)(blockRepeats*blockSize) - 1, qmax0, qmax, qsum0/blockSize, qsum/blockSize);
025224
      }
025224
      if (success >= successCount) break;
56d550
    }
56d550
025224
    delete[] blockXY;
56d550
    printf("done\n");
56d550
    return qmax0;
56d550
  }
56d550
56d550
56d550
  bool loadSymbolMap(const char *filename, int sizeX, int sizeY) {
56d550
    deinit();
56d550
56d550
    FILE *f = fopen(filename, "rb");
56d550
    if (!f)
56d550
      return printf("cannot open file '%s' for read\n", filename), false;
56d550
    fseek(f, 0, SEEK_END);
56d550
    size_t fs = ftell(f);
56d550
    fseek(f, 0, SEEK_SET);
56d550
56d550
    size_t testSize = sizeX + 1;
56d550
    int count = fs/testSize;
56d550
    if (!count)
56d550
      return printf("file '%s' is lesser minimal size\n", filename), fclose(f), false;
56d550
56d550
    unsigned char *data = new unsigned char[testSize*count];
56d550
    memset(data, 0, testSize*count);
56d550
    if (!fread(data, testSize*count, 1, f))
025224
      return printf("cannot read from file '%s'\n", filename), delete[] data, fclose(f), false;
56d550
56d550
    fclose(f);
56d550
56d550
    init(sizeX, sizeY, count);
56d550
    const unsigned char *pd = data;
56d550
    const double delta = 0;
56d550
    double *ey = y + sizeY*count;
025224
    for(double *py = y; py < ey; ++py)
025224
      *py = delta;
56d550
    for(double *px = x, *py = y; py < ey; py += sizeY) {
56d550
      for(double *ex = px + sizeX; px < ex; ++px, ++pd)
56d550
        *px = *pd/255.0;
56d550
      assert(*pd < sizeY);
56d550
      py[*pd++] = 1 - delta;
56d550
    }
56d550
    delete[] data;
56d550
56d550
    return true;
56d550
  }
56d550
56d550
56d550
  void printSymbol(int index, int width) {
56d550
    assert(index >= 0 && index < count);
56d550
    assert(width > 0);
56d550
    for(int i = 0; i < sizeX; ++i) {
56d550
      if (i && !(i % width)) printf("\n");
56d550
      printf("%c", x[sizeX*index + i] > 0 ? '#' : '.');
56d550
    }
56d550
    printf("\n");
56d550
    for(int i = 0; i < sizeY; ++i)
56d550
      printf(" %4.1lf", y[sizeY*index + i]);
56d550
    printf("\n");
56d550
  }
56d550
};
56d550
56d550
56d550
#endif