brain-js.test-01.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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: 80%;
  12. height: 400px;
  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. // provide optional config object (or undefined). Defaults shown.
  29. const config = {
  30. binaryThresh: 0.5,
  31. hiddenLayers: [5], // array of ints for the sizes of the hidden layers in the network
  32. activation: "relu", // supported activation types: ['sigmoid', 'relu', 'leaky-relu', 'tanh'],
  33. leakyReluAlpha: 0.01, // supported for activation type 'leaky-relu'
  34. // inputSize: 2,
  35. // outputSize: 2,
  36. };
  37. // create a simple feed forward neural network with backpropagation
  38. const net = new brain.NeuralNetwork(config);
  39. net.train([
  40. { input: [0, 0], output: [0] },
  41. { input: [0, 1], output: [1] },
  42. { input: [1, 0], output: [1] },
  43. { input: [1, 1], output: [0] },
  44. ]);
  45. const input = [1, 0];
  46. const output = net.run(input); // [0.987]
  47. document.getElementById("output").innerHTML =
  48. "Input:" + input + "<br/>" + "output is " + output;
  49. document.getElementById("network").innerHTML = brain.utilities.toSVG(
  50. net,
  51. config
  52. );
  53. </script>
  54. </html>