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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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: 20,
  31. inputRange: 20,
  32. hiddenLayers: [2, 5],
  33. outputSize: 20,
  34. learningRate: 0.01,
  35. decayRate: 0.999,
  36. };
  37. const svg_config = {
  38. height: 480,
  39. width: 640,
  40. radius: 10,
  41. inputs:{
  42. labels:['1','2','3', '4','5' ],
  43. color:'#333',
  44. }
  45. };
  46. // create a simple recurrent neural network
  47. const net = new brain.recurrent.RNN(config);
  48. net.train([
  49. { input: [0, 0], output: [0] },
  50. { input: [0, 1], output: [1] },
  51. { input: [1, 0], output: [1] },
  52. { input: [1, 1], output: [0] },
  53. ]);
  54. let input = [1, 0];
  55. let output = net.run([0, 0]); // [0]
  56. document.getElementById("output").innerHTML =
  57. "Input:" + input + "<br/>" + "output is " + output;
  58. document.getElementById("network").innerHTML = brain.utilities.toSVG(
  59. net,
  60. svg_config
  61. );
  62. </script>
  63. </html>