Пример #1
0
int main()
{
  NN::MathInit();

  typedef NN::Iris CONTENT;
  const char* fpath = "iris.nn";

  int mid_layer = 4;
  NN::Network::InitParam
    init_param[] =
  {
    { {CONTENT::DataSize, mid_layer}, NN::Network::LogisticLayer },
    { {mid_layer, CONTENT::LabelSize}, NN::Network::SoftMaxLayer },
  };
  int layer_num = ARRAY_NUM(init_param);

  const int batch_size = 5;
  const int train_count = 10;

  NN::Network net_train(layer_num, init_param, batch_size);
  net_train.load(fpath);
  NN::Network net_test(layer_num, init_param, 1);

  CONTENT::Content trainData, testData;
  CONTENT::LoadTrainData(trainData);
  CONTENT::LoadTestData(testData);

  int count = 50;
  while (--count) {
    DWORD tick = GetTickCount();
    std::cout << "start.." << std::endl;
    NN::Train(net_train, trainData, batch_size, train_count);
    std::cout << "tick = " << (GetTickCount() - tick) << std::endl;

    net_train.save(fpath);

    net_test.load(fpath);
    NN::Test(net_test, testData);
  }

  getchar();
  return 0;
}
Пример #2
0
int
main (int argc, char **argv)
{
  network_t *net;
  int no_of_pairs;
  int no_of_inputs;
  int no_of_outputs;
  float input[MAX_SIZE];
  float target[MAX_SIZE];
  float output[MAX_SIZE];
  float error, total_error;
  int t;
  int i;

  srand (time (0));

  parse_options (argc, argv);
  read_specification (spec_filename, &no_of_inputs, &no_of_outputs,
                      &no_of_pairs, input, target);

  if (strlen (input_filename) == 0) {
    if (hidden_nodes == 0)
      hidden_nodes = no_of_inputs;
    net = net_allocate (3, no_of_inputs, hidden_nodes, no_of_outputs);
  } else {
    net = net_load (input_filename);
  }

  if (!use_bias) {
    net_use_bias(net, 0);
  }

/* See spec.c for the way the input/target pairs are stored
 * in the input[] and target[] arrays. */
#define inputs(i) (input + i * no_of_inputs)
#define targets(i) (target + i* no_of_outputs)

  t = 0;
  total_error = 0;
  while ((t < max_trainings) && ((total_error >= max_error) || (t <= 10))) {
    /* choose one of the input/target pairs: inputs(i), targets(i) */
    i = rand () % no_of_pairs;

    /* compute the outputs for inputs(i) */
    net_compute (net, inputs (i), output);

    /* find the error with respect to targets(i) */
    error = net_compute_output_error (net, targets (i));

    /* train the network one step */
    net_train (net);

    /* keep track of (moving) average of the error */
    if (t == 0) {
      total_error = error;
    } else {
      total_error = 0.9 * total_error + 0.1 * error;
    }

    /* next */
    t++;
  }

  if (strlen (output_filename) == 0) {
    net_print (net);
  } else {
    net_save (output_filename, net);
  }

  printf ("Number of training performed: %i (max %i)\n", t, max_trainings);
  printf ("Average output error: %f (max %f)\n", total_error, max_error);

  return 0;
}