Guest User

Untitled

a guest
Aug 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include "Layer1Neuron.h"
  3.  
  4.  
  5. int main()
  6. {
  7. bool answer = false;
  8. int get[2];
  9. int count[4] = { 0,0,0,0 };
  10. int result;
  11. char input;
  12. Layer1Neuron L1Neuron[4];
  13. do {
  14. std::cout << "Give me your first input:";
  15. std::cin >> get[0];
  16. std::cout << std::endl;
  17. std::cout << "Give me your second input:";
  18. std::cin >> get[1];
  19. std::cout << std::endl;
  20. if (get[0] == 0 && get[1] == 0)
  21. {
  22. std::cout << "The target result is:";
  23. std::cin >> result;
  24. std::cout << std::endl;
  25. std::cout << "The machine generated answer is:"<<L1Neuron[0].MachineResult(result)<<std::endl;
  26. if (count[0] < 5)
  27. {
  28. L1Neuron[0].TrainFunction(result);
  29. count[0]++;
  30. }
  31. }
  32. if (get[0] == 0 && get[1] == 1)
  33. {
  34. std::cout << "The target result is:";
  35. std::cin >> result;
  36. std::cout << std::endl;
  37. std::cout << "The machine generated answer is:" << L1Neuron[1].MachineResult(result) << std::endl;
  38. if (count[1] < 5)
  39. {
  40. L1Neuron[1].TrainFunction(result);
  41. count[1]++;
  42. }
  43. }
  44. if (get[0] == 1 && get[1] == 0)
  45. {
  46. std::cout << "The target result is:";
  47. std::cin >> result;
  48. std::cout << std::endl;
  49. std::cout << "The machine generated answer is:" << L1Neuron[2].MachineResult(result) << std::endl;
  50. if (count[2] < 5)
  51. {
  52. L1Neuron[2].TrainFunction(result);
  53. count[2]++;
  54. }
  55. }
  56. if (get[0] == 1 && get[1] == 1)
  57. {
  58. std::cout << "The target result is:";
  59. std::cin >> result;
  60. std::cout << std::endl;
  61. std::cout << "The machine generated answer is:" << L1Neuron[3].MachineResult(result) << std::endl;
  62. if (count[3] < 5)
  63. {
  64. L1Neuron[3].TrainFunction(result);
  65. count[3]++;
  66. }
  67. }
  68. std::cout << "Do you want to exit the program? Type y for yes or n for no:";
  69. std::cin >> input;
  70. std::cout << std::endl;
  71. if (input == 'y')
  72. {
  73. answer = true;
  74. }
  75. else if (input == 'n')
  76. {
  77. answer = false;
  78. }
  79. } while (answer == false);
  80. }
  81.  
  82. #pragma once
  83.  
  84.  
  85. class Layer1Neuron
  86. {
  87. public:
  88. void TrainFunction(int result);
  89. int MachineResult(int result);
  90. private:
  91. double Layer1R[2] = { 0.5,0.5 };
  92. };
  93.  
  94. #include "Layer1Neuron.h"
  95. #include <stdlib.h>
  96. #include <time.h>
  97. #include <math.h>
  98.  
  99. void Layer1Neuron::TrainFunction(int result)
  100. {
  101. if (result == 0)
  102. {
  103. Layer1R[0] = Layer1R[0] + 0.1;//0.1 is the learning rate.You can change it.
  104. Layer1R[1] = Layer1R[1] - 0.1;
  105. }
  106. else if(result ==1)
  107. {
  108. Layer1R[1] = Layer1R[1] + 0.1;
  109. Layer1R[0] = Layer1R[0] - 0.1;
  110. }
  111. }
  112.  
  113. int Layer1Neuron::MachineResult(int result)
  114. {
  115. double val = (double)rand() / RAND_MAX;
  116. if (result == 0)
  117. {
  118. if (val < Layer1R[1])
  119. return 1;
  120. else if (val < Layer1R[result])
  121. return result;
  122. }
  123. else if(result ==1)
  124. {
  125. if (val < Layer1R[0])
  126. return 0;
  127. else if (val < Layer1R[result])
  128. return result;
  129. }
  130. }
Add Comment
Please, Sign In to add comment