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.

test.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * GENANN - Minimal C Artificial Neural Network
  3. *
  4. * Copyright (c) 2015-2018 Lewis Van Winkle
  5. *
  6. * http://CodePlea.com
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. *
  12. * Permission is granted to anyone to use this software for any purpose,
  13. * including commercial applications, and to alter it and redistribute it
  14. * freely, subject to the following restrictions:
  15. *
  16. * 1. The origin of this software must not be misrepresented; you must not
  17. * claim that you wrote the original software. If you use this software
  18. * in a product, an acknowledgement in the product documentation would be
  19. * appreciated but is not required.
  20. * 2. Altered source versions must be plainly marked as such, and must not be
  21. * misrepresented as being the original software.
  22. * 3. This notice may not be removed or altered from any source distribution.
  23. *
  24. */
  25. #include "genann.h"
  26. #include "minctest.h"
  27. #include <stdio.h>
  28. #include <math.h>
  29. #include <stdlib.h>
  30. void basic() {
  31. genann *ann = genann_init(1, 0, 0, 1);
  32. lequal(ann->total_weights, 2);
  33. double a;
  34. a = 0;
  35. ann->weight[0] = 0;
  36. ann->weight[1] = 0;
  37. lfequal(0.5, *genann_run(ann, &a));
  38. a = 1;
  39. lfequal(0.5, *genann_run(ann, &a));
  40. a = 11;
  41. lfequal(0.5, *genann_run(ann, &a));
  42. a = 1;
  43. ann->weight[0] = 1;
  44. ann->weight[1] = 1;
  45. lfequal(0.5, *genann_run(ann, &a));
  46. a = 10;
  47. ann->weight[0] = 1;
  48. ann->weight[1] = 1;
  49. lfequal(1.0, *genann_run(ann, &a));
  50. a = -10;
  51. lfequal(0.0, *genann_run(ann, &a));
  52. genann_free(ann);
  53. }
  54. void xor() {
  55. genann *ann = genann_init(2, 1, 2, 1);
  56. ann->activation_hidden = genann_act_threshold;
  57. ann->activation_output = genann_act_threshold;
  58. lequal(ann->total_weights, 9);
  59. /* First hidden. */
  60. ann->weight[0] = .5;
  61. ann->weight[1] = 1;
  62. ann->weight[2] = 1;
  63. /* Second hidden. */
  64. ann->weight[3] = 1;
  65. ann->weight[4] = 1;
  66. ann->weight[5] = 1;
  67. /* Output. */
  68. ann->weight[6] = .5;
  69. ann->weight[7] = 1;
  70. ann->weight[8] = -1;
  71. double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  72. double output[4] = {0, 1, 1, 0};
  73. lfequal(output[0], *genann_run(ann, input[0]));
  74. lfequal(output[1], *genann_run(ann, input[1]));
  75. lfequal(output[2], *genann_run(ann, input[2]));
  76. lfequal(output[3], *genann_run(ann, input[3]));
  77. genann_free(ann);
  78. }
  79. void backprop() {
  80. genann *ann = genann_init(1, 0, 0, 1);
  81. double input, output;
  82. input = .5;
  83. output = 1;
  84. double first_try = *genann_run(ann, &input);
  85. genann_train(ann, &input, &output, .5);
  86. double second_try = *genann_run(ann, &input);
  87. lok(fabs(first_try - output) > fabs(second_try - output));
  88. genann_free(ann);
  89. }
  90. void train_and() {
  91. double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  92. double output[4] = {0, 0, 0, 1};
  93. genann *ann = genann_init(2, 0, 0, 1);
  94. int i, j;
  95. for (i = 0; i < 50; ++i) {
  96. for (j = 0; j < 4; ++j) {
  97. genann_train(ann, input[j], output + j, .8);
  98. }
  99. }
  100. ann->activation_output = genann_act_threshold;
  101. lfequal(output[0], *genann_run(ann, input[0]));
  102. lfequal(output[1], *genann_run(ann, input[1]));
  103. lfequal(output[2], *genann_run(ann, input[2]));
  104. lfequal(output[3], *genann_run(ann, input[3]));
  105. genann_free(ann);
  106. }
  107. void train_or() {
  108. double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  109. double output[4] = {0, 1, 1, 1};
  110. genann *ann = genann_init(2, 0, 0, 1);
  111. genann_randomize(ann);
  112. int i, j;
  113. for (i = 0; i < 50; ++i) {
  114. for (j = 0; j < 4; ++j) {
  115. genann_train(ann, input[j], output + j, .8);
  116. }
  117. }
  118. ann->activation_output = genann_act_threshold;
  119. lfequal(output[0], *genann_run(ann, input[0]));
  120. lfequal(output[1], *genann_run(ann, input[1]));
  121. lfequal(output[2], *genann_run(ann, input[2]));
  122. lfequal(output[3], *genann_run(ann, input[3]));
  123. genann_free(ann);
  124. }
  125. void train_xor() {
  126. double input[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  127. double output[4] = {0, 1, 1, 0};
  128. genann *ann = genann_init(2, 1, 2, 1);
  129. int i, j;
  130. for (i = 0; i < 500; ++i) {
  131. for (j = 0; j < 4; ++j) {
  132. genann_train(ann, input[j], output + j, 3);
  133. }
  134. /* printf("%1.2f ", xor_score(ann)); */
  135. }
  136. ann->activation_output = genann_act_threshold;
  137. lfequal(output[0], *genann_run(ann, input[0]));
  138. lfequal(output[1], *genann_run(ann, input[1]));
  139. lfequal(output[2], *genann_run(ann, input[2]));
  140. lfequal(output[3], *genann_run(ann, input[3]));
  141. genann_free(ann);
  142. }
  143. void persist() {
  144. genann *first = genann_init(1000, 5, 50, 10);
  145. FILE *out = fopen("persist.txt", "w");
  146. genann_write(first, out);
  147. fclose(out);
  148. FILE *in = fopen("persist.txt", "r");
  149. genann *second = genann_read(in);
  150. fclose(in);
  151. lequal(first->inputs, second->inputs);
  152. lequal(first->hidden_layers, second->hidden_layers);
  153. lequal(first->hidden, second->hidden);
  154. lequal(first->outputs, second->outputs);
  155. lequal(first->total_weights, second->total_weights);
  156. int i;
  157. for (i = 0; i < first->total_weights; ++i) {
  158. lok(first->weight[i] == second->weight[i]);
  159. }
  160. genann_free(first);
  161. genann_free(second);
  162. }
  163. void copy() {
  164. genann *first = genann_init(1000, 5, 50, 10);
  165. genann *second = genann_copy(first);
  166. lequal(first->inputs, second->inputs);
  167. lequal(first->hidden_layers, second->hidden_layers);
  168. lequal(first->hidden, second->hidden);
  169. lequal(first->outputs, second->outputs);
  170. lequal(first->total_weights, second->total_weights);
  171. int i;
  172. for (i = 0; i < first->total_weights; ++i) {
  173. lfequal(first->weight[i], second->weight[i]);
  174. }
  175. genann_free(first);
  176. genann_free(second);
  177. }
  178. void sigmoid() {
  179. double i = -20;
  180. const double max = 20;
  181. const double d = .0001;
  182. while (i < max) {
  183. lfequal(genann_act_sigmoid(NULL, i), genann_act_sigmoid_cached(NULL, i));
  184. i += d;
  185. }
  186. }
  187. int main(int argc, char *argv[])
  188. {
  189. printf("GENANN TEST SUITE\n");
  190. srand(100); //Repeatable test results.
  191. lrun("basic", basic);
  192. lrun("xor", xor);
  193. lrun("backprop", backprop);
  194. lrun("train and", train_and);
  195. lrun("train or", train_or);
  196. lrun("train xor", train_xor);
  197. lrun("persist", persist);
  198. lrun("copy", copy);
  199. lrun("sigmoid", sigmoid);
  200. lresults();
  201. return lfails != 0;
  202. }