Guest User

Untitled

a guest
Apr 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. class LetterModel {
  2.  
  3. constructor() {
  4. this._adj_step = 0.001;
  5. this._x = 5;
  6. this._y = 5;
  7. this._tries = 3000;
  8. this._models = [];
  9. }
  10.  
  11. _createPlayGround(x,y) {
  12. let playground = [];
  13. for (let i=0; i < x; i++) {
  14. let vector =[];
  15. for (let j=0; j < y; j++) {
  16. vector.push(0);
  17. }
  18. playground.push(vector);
  19. }
  20. return playground;
  21. }
  22.  
  23. _getRndInteger(min, max) {
  24. return Math.floor(Math.random() * (max - min) ) + min;
  25. }
  26.  
  27. _generateRandomCase(){
  28. //let version = JSON.parse(JSON.stringify(this.playground)); //deep copy array
  29. let version = this._createPlayGround(this._x, this._y);
  30. for(let i = 0; i < (this._x * this._y); i++) {
  31. let a = this._getRndInteger(0,this._x);
  32. let b = this._getRndInteger(0,this._y);
  33. version[a][b] = 1;
  34. }
  35. return version;
  36. }
  37.  
  38. _adjustWeights(sample, weights, caseMatrix) {
  39.  
  40. for (let i = 0; i < this._x; i++) {
  41. for (let j = 0; j < this._y; j++) {
  42. if (caseMatrix[i][j] && sample[i][j] && weights[i][j] < 1) {
  43. weights[i][j] += this._adj_step;
  44. }
  45. }
  46. }
  47.  
  48. return false;
  49. }
  50.  
  51. _learn(sample, weights) {
  52. for (let i=0; i < this._tries; i++) {
  53. let inputRandomCaseses = this._generateRandomCase();
  54. this._adjustWeights(sample, weights, inputRandomCaseses);
  55. }
  56. }
  57.  
  58. _getCountPixels(letter_mx) {
  59. let count = 0;
  60. letter_mx.forEach(vector => {
  61. vector.forEach(pixel => {
  62. count += pixel ? 1 : 0;
  63. })
  64. })
  65.  
  66. return count;
  67. }
  68.  
  69. addNewModel(sample,lable) {
  70.  
  71.  
  72.  
  73. const model = {
  74. "lable": lable,
  75. "weights": this._createPlayGround(this._x, this._y),
  76. "pixels_count": this._getCountPixels(sample)
  77. }
  78.  
  79. this._learn(sample, model.weights);
  80. this._models.push(model);
  81.  
  82. }
  83.  
  84. printModel(lable) {
  85. return this._models.find(o => {
  86. if(o.lable === lable)
  87. return true;
  88. })
  89. }
  90.  
  91. predictLetter(example) {
  92.  
  93. const statistic = [];
  94.  
  95. let max_prediction = 0;
  96. let letter = '';
  97.  
  98. this._models.forEach(model =>{
  99. let weights = model.weights;
  100. let p_count = model.pixels_count;
  101. let m_weight = 0;
  102. for (let i = 0; i < this._x; i++) {
  103. for (let j = 0; j < this._y; j++) {
  104. if(example[i][j] && weights[i][j]){
  105. m_weight += weights[i][j];
  106. }
  107. }
  108. }
  109.  
  110. statistic[model.lable] = m_weight/p_count;
  111.  
  112. if (max_prediction < statistic[model.lable]) {
  113. max_prediction = statistic[model.lable];
  114. letter = model.lable;
  115. }
  116.  
  117. });
  118.  
  119. console.log(statistic);
  120.  
  121. return letter;
  122.  
  123. }
  124.  
  125. }
  126.  
  127. const modelSage = new LetterModel();
  128.  
  129. const p = [
  130. [1,1,1,1,0],
  131. [1,0,0,1,1],
  132. [1,1,1,1,0],
  133. [1,0,0,0,0],
  134. [1,0,0,0,0]
  135. ];
  136. modelSage.addNewModel(p, 'p');
  137.  
  138. const x = [
  139. [1,0,0,0,1],
  140. [0,1,0,1,0],
  141. [0,0,1,0,0],
  142. [0,1,0,1,0],
  143. [1,0,0,0,1]
  144. ];
  145. modelSage.addNewModel(x, 'x');
  146.  
  147. const o = [
  148. [0,0,1,0,0],
  149. [0,1,0,1,0],
  150. [1,0,0,0,1],
  151. [0,1,0,1,0],
  152. [0,0,1,0,0]
  153. ];
  154. modelSage.addNewModel(o, 'o');
  155.  
  156. const q = [
  157. [0,0,1,0,0],
  158. [0,1,0,1,0],
  159. [1,0,0,0,1],
  160. [0,1,0,1,0],
  161. [0,0,1,0,1]
  162. ];
  163. modelSage.addNewModel(q, 'q');
  164.  
  165. const a = [
  166. [0,0,1,0,0],
  167. [0,1,0,1,0],
  168. [0,1,1,1,0],
  169. [1,1,0,1,1],
  170. [1,0,1,0,1]
  171. ];
  172. modelSage.addNewModel(a, 'a');
  173.  
  174. const t = [
  175. [1,1,1,1,1],
  176. [0,0,1,0,0],
  177. [0,0,1,0,0],
  178. [0,0,1,0,0],
  179. [0,0,1,0,0]
  180. ];
  181. modelSage.addNewModel(t, 't');
  182.  
  183. const one = [
  184. [0,1,0,0,0],
  185. [1,1,0,0,0],
  186. [0,1,0,0,0],
  187. [0,1,0,0,0],
  188. [0,1,0,0,0]
  189. ];
  190. modelSage.addNewModel(one, 'one');
  191.  
  192. const two = [
  193. [0,1,1,0,0],
  194. [1,0,0,1,1],
  195. [0,1,1,1,0],
  196. [1,1,0,0,0],
  197. [1,1,1,1,1]
  198. ];
  199. modelSage.addNewModel(two, 'two');
  200.  
  201. const inputCase = [
  202. [0,0,0,0,0],
  203. [0,1,0,0,0],
  204. [0,1,0,0,0],
  205. [0,0,0,0,0],
  206. [0,0,0,0,0]
  207. ];
  208.  
  209. console.log(modelSage.predictLetter(inputCase));
  210.  
  211. const inputCaseA = [
  212. [0,0,0,0,0],
  213. [1,1,1,1,1],
  214. [0,0,1,0,0],
  215. [0,0,1,0,0],
  216. [0,0,1,0,0]
  217. ];
  218.  
  219. /*const inputCaseA = [
  220. [' ',' ','*',' ',' '],
  221. [' ','*','*','*',' '],
  222. [' ','*',' ','*',' '],
  223. ['*','*','*','*','*'],
  224. ['*',' ',' ',' ','*']
  225. ];*/
  226.  
  227. console.log(modelSage.predictLetter(inputCaseA));
  228.  
  229. //console.log(modelSage.printModel('q'));
  230. //console.log(modelSage.printModel('o'));
Add Comment
Please, Sign In to add comment