console.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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+9="
  33. ]);
  34. netColor = new brain.NeuralNetwork();
  35. netColor.train([
  36. { input: { r: 0.62, g: 0.72, b: 0.88 }, output: { light: 1 } },
  37. { input: { r: 0.1, g: 0.84, b: 0.72 }, output: { light: 1 } },
  38. { input: { r: 0.74, g: 0.78, b: 0.86 }, output: { light: 1 } },
  39. { input: { r: 1, g: 0.99, b: 0 }, output: { light: 1 } },
  40. { input: { r: 0.33, g: 0.24, b: 0.29 }, output: { dark: 1 } },
  41. { input: { r: 0.31, g: 0.35, b: 0.41 }, output: { dark: 1 } },
  42. { input: { r: 1, g: 0.42, b: 0.52 }, output: { dark: 1 } }
  43. ]);
  44. runTests("Color match", netColor, [
  45. { r: 0.8, g: 0.7, b: 0.2 },
  46. { r: 0.5, g: 0.2, b: 0.2 }
  47. ]);