brain-js.test-03-LSTM-math.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Brain JS - LSTM Math</title>
  5. <meta charset="utf-8" />
  6. <meta http-equiv="Content-Type"
  7. content="text/html; charset=utf-8" />
  8. <style>
  9. #network {
  10. background: #ccc;
  11. border: 1px solid red;
  12. width: 640px;
  13. height: 480px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <h1>Brain JS - LSTM Math</h1>
  19. <p>
  20. In this case, we test the math adding calcuation by LSTM model.
  21. </p>
  22. <h2>Test results</h2>
  23. <div id="input">
  24. <p>Input a math problem, like "1+2=" or "9+7=" in the below input box.</p>
  25. <input type="text"
  26. id="problem"></input>
  27. <button id="calcuate">Calculate</button>
  28. <button id="testall">Test all once</button>
  29. </div>
  30. <div id="output"></div>
  31. <h2>Network</h2>
  32. <div id="network"></div>
  33. </body>
  34. <script src="brain-browser.js"></script>
  35. <script type="text/javascript">
  36. const svg_config = {
  37. height: 480,
  38. width: 640,
  39. radius: 10,
  40. };
  41. // create a simple recurrent neural network
  42. const net = new brain.recurrent.LSTM();
  43. // used to build list below
  44. const mathProblemsSet = new Set();
  45. for (let i = 0; i < 10; i++) {
  46. for (let j = 0; j < 10; j++) {
  47. mathProblemsSet.add(`${i}+${j}=${i + j}`);
  48. mathProblemsSet.add(`${j}+${i}=${i + j}`);
  49. }
  50. }
  51. const mathProblems = Array.from(mathProblemsSet);
  52. console.log(mathProblems);
  53. net.train(mathProblems, { log: true, errorThresh: 0.03 });
  54. let errors = 0;
  55. document.getElementById("calcuate").onclick = () => {
  56. const input = document.getElementById("problem").value;
  57. const output = net.run(input)
  58. const predictedMathProblem = input + output;
  59. const error = mathProblems.indexOf(predictedMathProblem) <= -1 ? 1 : 0;
  60. errors += error;
  61. document.getElementById("output").innerHTML =
  62. `Output :${input}${output}` +
  63. `<br/>${error?'Error':'Correct'}, total error ${errors}`;
  64. };
  65. // Batch test for all problems
  66. document.getElementById("testall").onclick = () => {
  67. let results = 'Test all items:<br/><ul>';
  68. for (let i = 0; i < mathProblems.length; i++) {
  69. const input = mathProblems[i].split('=')[0] + '=';
  70. const output = net.run(input);
  71. const predictedMathProblem = input + output;
  72. const error = mathProblems.indexOf(predictedMathProblem) <= -1 ? 1 : 0;
  73. errors += error;
  74. results += '<li>';
  75. results += input + output;
  76. results += error ? ' - error' : '';
  77. results += '</li>';
  78. }
  79. results +='</ul>';
  80. results = `Batch test with ${errors} errors <br/>`+ results;
  81. document.getElementById("output").innerHTML = results;
  82. };
  83. document.getElementById("network").innerHTML = brain.utilities.toSVG(
  84. net,
  85. svg_config
  86. );
  87. </script>
  88. </html>