brain-js.test-02-RNN.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Brain JS Test</title>
  5. <meta charset="utf-8" />
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <style>
  8. #network {
  9. background: #ccc;
  10. border: 1px solid red;
  11. width: 640px;
  12. height: 480px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <h1>Brain JS Test - RNN</h1>
  18. <p>
  19. Brain.js is a machine learning program in Javascript.
  20. </p>
  21. <h2>Test results</h2>
  22. <div id="output"></div>
  23. <h2>Network</h2>
  24. <div id="network"></div>
  25. </body>
  26. <script src="brain-browser.js"></script>
  27. <script type="text/javascript">
  28. // provide optional config object, defaults shown.
  29. const config = {
  30. inputSize: 5,
  31. hiddenLayers: [6,6],
  32. outputSize: 5,
  33. learningRate: 0.01,
  34. decayRate: 0.999,
  35. };
  36. const svg_config = {
  37. height: 480,
  38. width: 640,
  39. };
  40. // create a simple recurrent neural network
  41. const net = new brain.recurrent.RNN(config);
  42. net.train([
  43. { input: [0, 0], output: [0] },
  44. { input: [0, 1], output: [1] },
  45. { input: [1, 0], output: [1] },
  46. { input: [1, 1], output: [0] },
  47. ]);
  48. let input = [1, 0];
  49. let output = net.run([0, 0]); // [0]
  50. document.getElementById("output").innerHTML =
  51. "Input:" + input + "<br/>" + "output is " + output;
  52. document.getElementById("network").innerHTML = brain.utilities.toSVG(
  53. net,
  54. svg_config
  55. );
  56. </script>
  57. </html>