You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example1.c 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "genann.h"
  5. int main(int argc, char *argv[])
  6. {
  7. printf("GENANN example 1.\n");
  8. printf("Train a small ANN to the XOR function using backpropagation.\n");
  9. /* This will make the neural network initialize differently each run. */
  10. /* If you don't get a good result, try again for a different result. */
  11. srand(time(0));
  12. /* Input and expected out data for the XOR function. */
  13. const double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  14. const double output[4] = {0, 1, 1, 0};
  15. int i;
  16. /* New network with 2 inputs,
  17. * 1 hidden layer of 2 neurons,
  18. * and 1 output. */
  19. genann *ann = genann_init(2, 1, 2, 1);
  20. /* Train on the four labeled data points many times. */
  21. for (i = 0; i < 300; ++i) {
  22. genann_train(ann, input[0], output + 0, 3);
  23. genann_train(ann, input[1], output + 1, 3);
  24. genann_train(ann, input[2], output + 2, 3);
  25. genann_train(ann, input[3], output + 3, 3);
  26. }
  27. /* Run the network and see what it predicts. */
  28. printf("Output for [%1.f, %1.f] is %1.f.\n", input[0][0], input[0][1], *genann_run(ann, input[0]));
  29. printf("Output for [%1.f, %1.f] is %1.f.\n", input[1][0], input[1][1], *genann_run(ann, input[1]));
  30. printf("Output for [%1.f, %1.f] is %1.f.\n", input[2][0], input[2][1], *genann_run(ann, input[2]));
  31. printf("Output for [%1.f, %1.f] is %1.f.\n", input[3][0], input[3][1], *genann_run(ann, input[3]));
  32. genann_free(ann);
  33. return 0;
  34. }