Blame simple/neural/nn-trainer3.cpp

53488e
53488e
#include <ctime></ctime>
53488e
#include <cstdlib></cstdlib>
53488e
53488e
#include <chrono></chrono>
53488e
53488e
#include "nnlayer3.inc.cpp"
53488e
#include "nnlayer3.mt.inc.cpp"
53488e
53488e
53488e
long long timeUs() {
53488e
  static std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
53488e
  return (long long)std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now() - begin ).count();</std::chrono::microseconds>
53488e
}
53488e
53488e
53488e
bool train(const char *infile, const char *outfile, Layer &l, int blockSize, int totalCount, Real trainRatio) {
53488e
  assert(blockSize > 0);
53488e
  int blockCount = totalCount/blockSize;
53488e
  assert(blockCount > 0);
53488e
  assert(!l.prev);
53488e
  assert(l.countNeurons() && l.back().countNeurons());
53488e
53488e
  printf("load training data\n");
53488e
53488e
  FILE *f = fopen(infile, "rb");
53488e
  if (!f)
53488e
    return printf("cannot open file '%s' for read\n", infile), false;
53488e
  fseek(f, 0, SEEK_END);
53488e
  int fs = ftell(f);
53488e
  fseek(f, 0, SEEK_SET);
53488e
53488e
  int sizeX = l.countNeurons();
53488e
  int sizeY = l.back().countNeurons();
53488e
  int count = fs/(sizeX+1);
53488e
  if (count < blockSize)
53488e
    return printf("file '%s' is lesser minimal size\n", infile), fclose(f), false;
53488e
53488e
  unsigned char *data = new unsigned char[(sizeX + sizeY)*count];
53488e
  memset(data, 0, (sizeX + sizeY)*count);
53488e
  for(int i = 0; i < count; ++i) {
53488e
    unsigned char *d = data + (sizeX + sizeY)*i;
53488e
    if (!fread(d, sizeX+1, 1, f) || d[sizeX] >= sizeY)
53488e
      return printf("cannot read from file '%s'\n", infile), delete[] data, fclose(f), false;
53488e
    d += sizeX;
53488e
    unsigned char c = *d;
53488e
    *d = 0;
53488e
    d[c] = 255;
53488e
  }
53488e
  fclose(f);
53488e
53488e
  printf("train %d x %d = %d, ratio: %f\n", blockCount, blockSize, blockCount*blockSize, trainRatio);
53488e
53488e
  int *shuffle = new int[blockSize];
53488e
  TrainMT tmt;
53488e
  tmt.layer = &l;
53488e
  tmt.dataX = data;
53488e
  tmt.dataY = data + sizeX;
53488e
  tmt.strideX = tmt.strideY = sizeX + sizeY;
53488e
  tmt.shuffle = shuffle;
53488e
  tmt.count = blockSize;
53488e
  tmt.trainRatio = trainRatio;
53488e
53488e
  long long timeStartUs = timeUs();
53488e
  for(int i = 0; i < blockCount; ++i) {
53488e
    long long timeBlockStartUs = timeUs();
53488e
53488e
    for(int j = 0; j < blockSize; ++j)
53488e
      shuffle[j] = rand()%count;
53488e
    double res = tmt.train(4);
53488e
53488e
    long long dt = timeUs() - timeBlockStartUs;
53488e
53488e
    printf("%4d, total %7d, avg.result %f, time: %f\n", i+1, (i+1)*blockSize, res, dt*0.000001);
53488e
    if ( ((i+1)%100) == 0 || i+1 == blockCount ) {
53488e
      if (!l.saveAll(outfile)) return delete[] data, delete[] shuffle, false;
53488e
      printf("  saved\n");
53488e
    }
53488e
  }
53488e
53488e
  long long dt = timeUs() - timeStartUs;
53488e
  printf("finished int time: %f\n", dt*0.000001);
53488e
53488e
  delete[] shuffle;
53488e
  delete[] data;
53488e
  return true;
53488e
}
53488e
53488e
53488e
int main() {
53488e
  srand(time(NULL));
53488e
53488e
  const char *infile = "data/symbols-data.bin"; // 28x28
53488e
  const char *outfile = "data/output/weights.bin";
53488e
53488e
  printf("create neural network\n");
53488e
  Layer l(nullptr, 28, 28,  1);
53488e
  new Layer(&l, 14, 14,  1, 28);
53488e
  new Layer(&l,  7,  7,  1, 14);
53488e
  new Layer(&l,  1,  1, 10,  7);
53488e
53488e
  printf("  neurons: %d, links %d, memSize: %llu\n", l.totalNeurons(), l.totalLinks(), (unsigned long long)l.totalMemSize());
53488e
53488e
  //printf("try load previously saved network\n");
53488e
  //l.loadAll(outfile);
53488e
53488e
  printf("train\n");
53488e
  train(infile, outfile, l, 10000, 1000000, 0.01);
53488e
53488e
  return 0;
53488e
}
53488e