2
0

gates.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. float sigmoidf(float x)
  6. {
  7. return 1.f / (1.f + expf(-x));
  8. }
  9. typedef float sample[3];
  10. // NAND-gate
  11. sample or_train[] = {
  12. {0, 0, 0},
  13. {1, 0, 1},
  14. {0, 1, 1},
  15. {1, 1, 1},
  16. };
  17. sample and_train[] = {
  18. {0, 0, 0},
  19. {1, 0, 0},
  20. {0, 1, 0},
  21. {1, 1, 1},
  22. };
  23. sample nand_train[] = {
  24. {0, 0, 1},
  25. {1, 0, 1},
  26. {0, 1, 1},
  27. {1, 1, 0},
  28. };
  29. sample xor_train[] = {
  30. {0, 0, 0},
  31. {1, 0, 1},
  32. {0, 1, 1},
  33. {1, 1, 0},
  34. };
  35. sample *train = and_train;
  36. size_t train_count = 4;
  37. float cost(float w1, float w2, float b)
  38. {
  39. float result = 0.0f;
  40. for (size_t i = 0; i < train_count; ++i) {
  41. float x1 = train[i][0];
  42. float x2 = train[i][1];
  43. float y = sigmoidf(x1*w1 + x2*w2 + b);
  44. float d = y - train[i][2];
  45. result += d*d;
  46. }
  47. result /= train_count;
  48. return result;
  49. }
  50. void dcost(float eps,
  51. float w1, float w2, float b,
  52. float *dw1, float *dw2, float *db)
  53. {
  54. float c = cost(w1, w2, b);
  55. *dw1 = (cost(w1 + eps, w2, b) - c)/eps;
  56. *dw2 = (cost(w1, w2 + eps, b) - c)/eps;
  57. *db = (cost(w1, w2, b + eps) - c)/eps;
  58. }
  59. void gcost(float w1, float w2, float b,
  60. float *dw1, float *dw2, float *db)
  61. {
  62. *dw1 = 0;
  63. *dw2 = 0;
  64. *db = 0;
  65. size_t n = train_count;
  66. for (size_t i = 0; i < n; ++i) {
  67. float xi = train[i][0];
  68. float yi = train[i][1];
  69. float zi = train[i][2];
  70. float ai = sigmoidf(xi*w1 + yi*w2 + b);
  71. float di = 2*(ai - zi)*ai*(1 - ai);
  72. *dw1 += di*xi;
  73. *dw2 += di*yi;
  74. *db += di;
  75. }
  76. *dw1 /= n;
  77. *dw2 /= n;
  78. *db /= n;
  79. }
  80. float rand_float(void)
  81. {
  82. return (float) rand()/ (float) RAND_MAX;
  83. }
  84. int main2(void)
  85. {
  86. // (x|y) & ~(x&y)
  87. for (size_t x = 0; x < 2; ++x) {
  88. for (size_t y = 0; y < 2; ++y) {
  89. printf("%zu ^ %zu = %zu\n", x, y, (x|y) & (~(x&y)));
  90. }
  91. }
  92. return 0;
  93. }
  94. int main(void)
  95. {
  96. srand(time(0));
  97. float w1 = rand_float();
  98. float w2 = rand_float();
  99. float b = rand_float();
  100. float rate = 1e-1;
  101. for (size_t i = 0; i < 10*1000; ++i) {
  102. float c = cost(w1, w2, b);
  103. printf("c = %f, w1 = %f, w2 = %f, b = %f\n", c, w1, w2, b);
  104. float dw1, dw2, db;
  105. #if 1
  106. float eps = 1e-1;
  107. dcost(eps, w1, w2, b, &dw1, &dw2, &db);
  108. #else
  109. gcost(w1, w2, b, &dw1, &dw2, &db);
  110. #endif
  111. w1 -= rate*dw1;
  112. w2 -= rate*dw2;
  113. b -= rate*db;
  114. }
  115. printf("c = %f, w1 = %f, w2 = %f, b = %f\n", cost(w1, w2, b), w1, w2, b);
  116. for (size_t i = 0; i < 2; ++i) {
  117. for (size_t j = 0; j < 2; ++j) {
  118. printf("%zu | %zu = %f\n", i, j, sigmoidf(i*w1 + j*w2 + b));
  119. }
  120. }
  121. return 0;
  122. }