brain-js.test-01.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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</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. const svg_config = {
  29. height: 480,
  30. width: 640,
  31. };
  32. // create a simple feed forward neural network with backpropagation
  33. const net = new brain.NeuralNetwork();
  34. net.train([
  35. { input: [0, 0], output: [0] },
  36. { input: [0, 1], output: [1] },
  37. { input: [1, 0], output: [1] },
  38. { input: [1, 1], output: [0] },
  39. ]);
  40. const input = [1, 0];
  41. const output = net.run(input); // [0.987]
  42. document.getElementById("output").innerHTML =
  43. `Input: [${input}] <br/>Output is ${output}`;
  44. document.getElementById("network").innerHTML = brain.utilities.toSVG(
  45. net,
  46. svg_config
  47. );
  48. </script>
  49. </html>