Brain.js

Brain.js是一个 JavaScript 库,它隐藏了数学的复杂性,因此可以轻松理解神经网络。

构建神经网络

使用 Brain.js 构建神经网络:

例子:

// Create a Neural Network
const network = new brain.NeuralNetwork();

// Train the Network with 4 input objects
network.train([
 {input:[0,0], output:{zero:1}},
 {input:[0,1], output:{one:1}},
 {input:[1,0], output:{one:1},
 {input:[1,1], output:{zero:1},
]);

// What is the expected output of [1,0]?
result = network.run([1,0]);

// Display the probability for "zero" and "one"
... result["one"] + " " + result["zero"];
亲自试一试 »

示例解释:

神经网络是通过以下方式创建的:new brain.NeuralNetwork()

该网络经过训练network.train([examples])

这些示例代表 4 个输入值以及相应的输出值。

network.run([1,0]),你问"What is the likely output of [1,0]?"

来自网络的回答是:

  • 一:93%(接近1)
  • 零:6%(接近0)


如何预测对比度

使用 CSS,可以通过 RGB 设置颜色:

示例

Color RGB
Black RGB(0,0,0)
Yellow RGB(255,255,0)
Red RGB(255,0,0)
White RGB(255,255,255)
Light Gray RGB(192,192,192)
Dark Gray RGB(65,65,65)
亲自试一试 »

下面的示例演示了如何预测颜色的暗度:

例子:

// Create a Neural Network
const net = new brain.NeuralNetwork();

// Train the Network with 4 input objects
net.train([
// White RGB(255, 255, 255)
{input:[255/255, 255/255, 255/255], output:{light:1}},
// Light grey (192,192,192)
{input:[192/255, 192/255, 192/255], output:{light:1}},
// Darkgrey (64, 64, 64)
{ input:[65/255, 65/255, 65/255], output:{dark:1}},
// Black (0, 0, 0)
{ input:[0, 0, 0], output:{dark:1}},
]);

// What is the expected output of Dark Blue (0, 0, 128)?
let result = net.run([0, 0, 128/255]);

// Display the probability of "dark" and "light"
... result["dark"] + " " + result["light"];
亲自试一试 »

示例解释:

神经网络是通过以下方式创建的:new brain.NeuralNetwork()

该网络经过训练network.train([examples])

这些示例代表 4 个输入值和相应的输出值。

network.run([0,0,128/255]),你问"What is the likely output of dark blue?"

来自网络的回答是:

  • 深色:95%
  • 光:4%

为什么不编辑示例来测试黄色或红色的可能输出?