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

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