Advertisement
Guest User

uczenie

a guest
Nov 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. import * as Utils from './Utils.js';
  2.  
  3. const LEARNING_CONST = 0.1;
  4.  
  5. class Perceptron {
  6.  
  7. constructor(number, wages) {
  8. this.number = number;
  9. this.wages = [...wages];
  10. }
  11.  
  12. teachPerceptron(examples) {
  13. let pocket_record = {
  14. record: 0,
  15. wages: this.wages
  16. };
  17.  
  18. let iterationsWithoutError = 0;
  19.  
  20. let pocket = Object.assign({}, pocket_record);
  21.  
  22. for (let i = 0; i < 10000; i++) {
  23. let example_number = Utils.getRandomInt(10) + 1;
  24. let values = examples[example_number];
  25. let correctAnswer = values[this.number] === 1 ? 1 : -1;
  26. let ERR = correctAnswer - this.getTresholdFunctionResult('learn', example_number, examples);
  27.  
  28. if (ERR === 0) {
  29. pocket.record++;
  30. iterationsWithoutError++;
  31. if (iterationsWithoutError > 1000) {
  32.  
  33. if (pocket.record > pocket_record.record) {
  34. Object.assign(pocket_record, pocket);
  35. }
  36.  
  37. break;
  38. }
  39. } else {
  40. this.updateWages(ERR, examples, example_number);
  41.  
  42. //Pocket replace after error for better performance
  43. if (pocket.record > pocket_record.record) {
  44. Object.assign(pocket_record, pocket);
  45. }
  46.  
  47. pocket.record = 0;
  48. pocket.wages = this.wages;
  49. iterationsWithoutError = 0;
  50. }
  51. }
  52. this.wages = pocket_record.wages;
  53. }
  54.  
  55. updateWages(ERR, examples, example_number) {
  56. let values = examples[example_number];
  57.  
  58. for (let j = 0; j <= 2500; j++) {
  59. this.wages[j] = this.wages[j] + (0.1 * ERR * values[j]);
  60. }
  61. }
  62.  
  63. checkResultFromDisplay(examples, displayValues_rightArea, displayValues_leftArea) {
  64. let flag = false;
  65. if (this.getTresholdFunctionResult('check', null, examples, displayValues_leftArea) === 1) {
  66. flag = true;
  67. }
  68.  
  69. const cells_RightArea = document.getElementsByClassName('grid-item-result');
  70.  
  71. let self = this;
  72. Array.from(cells_RightArea).forEach(cell => {
  73. cell.classList.forEach(function(className) {
  74. if (className.startsWith('number')) {
  75. let tmp_num = parseInt(className.substring(className.indexOf('-') + 1));
  76. if (tmp_num === self.number) {
  77. if (flag) {
  78. displayValues_rightArea[tmp_num] = 1;
  79. cell.style = 'background-color: pink;';
  80. } else {
  81. displayValues_rightArea[tmp_num] = 0;
  82. cell.style = 'background-color: black;';
  83. }
  84.  
  85. }
  86. }
  87. });
  88. });
  89.  
  90. }
  91.  
  92. recursion(displayValues_rightArea) {
  93. for (let i = 1; i <= 3; i++) {
  94. let flag = false;
  95. if (this.getTresholdFunctionResultForRecursion(displayValues_rightArea) === 1) {
  96. flag = true;
  97. }
  98.  
  99. const cells_RightArea = document.getElementsByClassName('grid-item-result');
  100. let self = this;
  101. Array.from(cells_RightArea).forEach(cell => {
  102. cell.classList.forEach(function(className) {
  103. if (className.startsWith('number')) {
  104. let tmp_num = parseInt(className.substring(className.indexOf('-') + 1));
  105. if (tmp_num === self.number) {
  106. if (flag) {
  107. displayValues_rightArea[tmp_num] = 1;
  108. cell.style = 'background-color: pink;';
  109. } else {
  110. displayValues_rightArea[tmp_num] = 0;
  111. cell.style = 'background-color: black;';
  112. }
  113.  
  114. }
  115. }
  116. });
  117. });
  118.  
  119. }
  120. }
  121.  
  122. getTresholdFunctionResult(action, example_number, examples, displayValues_leftArea) {
  123. let summary = 0.0;
  124. let values = examples[example_number];
  125. for (let s = 0; s <= 2500; s++) {
  126. if (action === 'learn') {
  127. summary += (this.wages[s] * values[s]);
  128. } else {
  129. summary += (this.wages[s] * displayValues_leftArea[s]);
  130. }
  131. }
  132. return (summary < 0 ? -1 : 1);
  133. }
  134.  
  135. getTresholdFunctionResultForRecursion(displayValues_rightArea) {
  136. let summary = 0.0;
  137. for (let s = 0; s <= 2500; s++) {
  138. summary += (this.wages[s] * displayValues_rightArea[s]);
  139. }
  140. return (summary < 0 ? -1 : 1);
  141. }
  142. }
  143.  
  144. export default Perceptron;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement