console.js 940 B

123456789101112131415161718192021222324252627282930313233343536
  1. runTests = (label, network, tests) => {
  2. console.log(`--- ${label}`);
  3. for (test of tests) {
  4. console.log(`${label} test ${JSON.stringify(test)}: ${network.run(test)}`);
  5. }
  6. }
  7. netXOR = new brain.NeuralNetwork();
  8. netXOR.train([
  9. { input: [0, 0], output: [0] },
  10. { input: [0, 1], output: [1] },
  11. { input: [1, 0], output: [1] },
  12. { input: [1, 1], output: [0] }
  13. ]);
  14. runTests("XOR gate", netXOR, [
  15. [0,0],
  16. [0,1],
  17. [1,0],
  18. [1,1]
  19. ]);
  20. netSum = new brain.recurrent.LSTM({ hiddenLayer: [20] });
  21. netSum.train([
  22. "0+0=0", "0+1=1", "0+2=2", "0+3=3", "0+4=4", "0+5=5",
  23. "1+0=1", "1+1=2", "1+2=3", "1+3=4", "1+4=5", "1+5=6",
  24. "2+0=2", "2+1=3", "2+2=4", "2+3=5", "2+4=6", "2+5=7",
  25. "3+0=3", "3+1=4", "3+2=5", "3+3=6", "3+4=7", "3+5=8",
  26. "4+0=4", "4+1=5", "4+2=6", "4+3=7", "4+4=8", "4+5=9",
  27. "5+0=5", "5+1=6", "5+2=7", "5+3=8", "5+4=9", "5+5=10"
  28. ], { errorThresh: 0.025 });
  29. runTests("Sum", netSum, [
  30. "1+2=",
  31. "4+2=",
  32. "3+3="
  33. ]);