Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Brain.js (github.com/brainjs)
273 points by hliyan on Dec 28, 2018 | hide | past | favorite | 51 comments


A 19-part interactive neural network course that uses brain.js was posted to HN last week: https://scrimba.com/g/gneuralnetworks


I just went through the 19-part interactive neural network and this is the best tutorial for beginners I've encountered. Each video is < 10 minutes and contains a practical example that motivated me to try implementing / prototype something quickly.

And doing all the examples directly on the browser creates a very low barrier to entry as a front end developer.


For reference, NodeJS support for GPUs will only drop under version 2, so if you're planning on GPU acceleration, this is not for you.


This is Robert, the lead developer for brain.js. Ty for the plug! I was going to mention this, nice to see people listening and helping spread the word. Working on it hard guys! When spare time cannot be had, just stay up late, and get up early!


This is tangential, but I highly disapprove of any encouragement to stay up late. Sleep is incredibly important for the entirety of the body. It's always better to get well-rested over a small amount more work done (which you'll eventually lose anyway since you won't be well-rested).


Different strokes for different folks. If it works, don’t fix it. Etc.

Some of us succeed not just despite our bad habits, but because of them.


Lack of sleep might not stop you from succeeding, but it can make you think slower, have poorer memory, get sick easier, eat more than you should, look older, and be more prone to depression and anxiety.

Please read "Why We Sleep" by neuroscientist Matthew Walker. Sleep is essential. Don't give it up lightly.


This is not to mean that it's not a useful tool for teaching people simplified neural networks without having to whip out a full tensorflow environment.

Edit: When you have a working 2.0 beta, do you mind if I build a docker-jupyter notebook kinda like the Tensorflow docker? Something with nvidia-docker runtime on jupyter-nodejs? I really just wanna bring this into a more mainstream audience.



Even as someone who has only superficial knowledge of the topic, that course was very underwhelming.


As someone with absolutely no knowledge of the topic, I found that course to be very digestible and pleasant.


Same. It was so accessible (voiceover in an online code editor) that I just did it instead of bookmarking it for a day that never arrives.


That is really cool to hear!


Ty, I hope my voice is/was not very irritating. I obsess over relaying ideas in a _very simple_ manner.

Things should be as simple as possible, but not simpler...


Ty for your thought. I'd love to know more specifics.


Most of the arguments either don't add up (e.g. saying it's mathematically proven while ML doesn't have rigorous theory) or are too generic to add much value. The course feels more like a verbal introduction to the library API rather than teaching anything related to ML.


Ty for your feedback. We'll be sure to use it if we make another.


The sample app (the color contrast stuff) hurt my feelings a bit. I used to work on an app that needed to do something similar and I ended up fighting with the YIQ formula (it really doesn't work for a lot of cases). Playing with the sample just a bit and I ended up replicating what had taken me weeks to do back in the days...


This was good, correct? I'd love to hear what your use case was, like which colors didn't work, etc.


Haha, sorry if it was unclear, my tone was in jest.

Yeah, basically what Im trying to say is that this is awesome. It was several years ago so I don't have my old code anymore (different job), but it was in the printing industry, where we wanted to warn customers that their choices of color were...dubious.

However, it's extremely subjective. An example is certain shades of red/orange. The formula might tell you that using white text is a bad idea, but they're really the same to read, and white looks better.

One thing I'd have to play around more than my free time allows, but I had to deal with back then, was backgrounds with multiple color. Where it got really tricky is when the text overlaps multiple colors, you have to decide if you're seeing enough of the text that one foreground color is ok, or if it's too much and you have to say "no". It was incredibly tricky to do, especially with things like antialiasing involved. Training a model with that data probably would give much better results than my tweaked formulas of old.


Can someone comment on performance -- speed vs training vs other frameworks. Most of our work is in TF/Pytorch and I have not used any JS implementations would love to hear some peoples insights into how it compares.


I just did the maths one.

1 to 10 addition, 2000 iterations took about 5 mins on repl.it

It got the maths wrong so will need more training


From the README:

    brain.js is a library of Neural Networks written in JavaScript.


Saw this and thought, "Huh, I wonder if harthur is back at it", but nope. Nice to see someone picked it up though!


I followed the progress back then and felt very bad for both, that such an awesome library was going down and the bad situation she found herself into. Hope she is doing well! And happy to see brain being reborn.


I think she followed up a few years later and said that the bad situation she was in was unrelated to her leaving Github. It was mostly the time commitment of maintaining many open source projects.


What happened to the author? I'm not aware of the history...


What happened was essentially bullies. I think it is apparent now that the library and, more importantly, the idea of a data first neural network architecture in javascript isn't going away.


What do you mean by "data-first"? What other forms could a JS NN architecture take?


A lot of neural networks start by first constructing the neural network structure, and eventually getting around to running the data through it. Where in brain.js, line 2 in most examples (just after instantiation) is train with data.


JS nails it again even with Neuron Networks. What saddens me in latest years is the direction of library development, where people put more focus on Python instead of Javascript. Javascript is universal, you can reuse your code in both browser, native apps and server. Python ? I don't think so. My dream of "getting the job done" is a universal language, universal ecosystem/libraries. We need universal developers, no need for full-stack developers anymore.


I am a full stack developer using python for everything but what's in my HTML pages. And I'd replace that javascript with python as soon as it would be viable. Javascript is fine for what it is, but the idea of it as a universal language is gross.


Yeah! Making poorly designed and "weakly" typed language with enormous WTF's as a universal language! I think if they put more effort on webassembly, market share and library support for js will have a huge dropout. For more universal language how about have a actual sane language other than javascript or even python?


Type script is what will likely take over.


>Neuron Networks

This really sets the tone!

>We need universal developers, no need for full-stack developers anymore.

What if the “universality” of a developer has less to do with their language of choice, and more with their reasoning and abstraction capabilities?


I couldn't really understand how the JS community is so vibrant in some areas and flocks to libraries every other day, but the python community gravitated around all the big data stuff

Not saying it shouldn't, I just don't get the aberration given the computational resources available today since compute instances mitigate single event loop issues

Is there a JS version of numpy and panda?

edit: yes, there are

https://github.com/nicolaspanel/numjs

https://github.com/StratoDem/pandas-js


Also: https://github.com/gpujs/gpu.js

Note: this is what brain.js is shifting over to. It uses the... GPU.


Great. From this source code, I can find well that the speed is optimized even simple for-loop levels in JavaScript.



I'm aware of tensorflow.js, but what really struck me with brain.js was this snippet from the very beginning of the readme file:

how to approximate the XOR function using brain.js

  const net = new brain.NeuralNetwork(config);

  net.train([{input: [0, 0], output: [0]},
           {input: [0, 1], output: [1]},
           {input: [1, 0], output: [1]},
           {input: [1, 1], output: [0]}]);

  const output = net.run([1, 0]);  // [0.987]

This is how you win over newcomers -- quick, accessible demonstrations of the product/library's capabilities. A sample is worth a thousand words. Tensorflow may have a lot of great, detailed examples, but its documentation fails in this regard.


Great point.

This is why I used p2.js for javascript game physics over franken-emscripten Box2d ports.

The readme and source code are extremely accessible (https://github.com/schteppe/p2.js#sample-code).

When I was trying to optimize my collision detection, I stepped in to p2.js source to read narrowphase vs broadphase implementations. Last I used Box2d, I was reading c++ pdf documentation.


Really cool example!


What is the equivalent in tensorflow.js? Can someone translate?


1. Tensorflow.js closely (not exactly) mirrors the python counterpart, and its api's, and thinking. We feel it does not reflect node ideologies well. And not the shify, what is in today is out tomorrow, thinking. The thinking that is practical, and applies well to business tastics. Brain.js started in node, and was original in its way it tackled the problem. Being data first, easy api, and then speed. It just works. 2. The vast majority of machine learning needs are encompassed within the library, and are only getting better. 3. The library can be learned in a matter of hours rather than weeks or months. 4. We can do it faster, simpler, than tensorflow.js. GPU support is coming in NodeJS, but is already partially implemented for browser, with fallback to CPU. 5. Brain.js was measured, where its benchmark was even better than industry standards, including IBM's Watson: https://github.com/jesus-seijas-sp/demo-nlp (note: project uses brain.js). Tensorflow.js isn't mentioned in this link, but I talked with the author about using Tensorflow.js, which he did try for this exact project, and his exact reply was: "with tensorflow.js same implementation I got a 0.91, but taked more time to train with CPU" (complete with typo) 6. Any corporation as powerful as those who maintain tensorflow should not have all the say in something as powerful as machine learning. Everyone should, and can. 7. We choose to.


There's room for more than one, and it's good to have alternatives to Google-run projects.


Y not tho?


I share the concern about mono-cultures in tech, but if you are serious about getting into data science or AI I have to agree with the OP -- stick to TensorFlow. TensorFlow.js is not a first class citizen of that ecosystem, but its close enough to get real work done.

Also... there are some groups that use Javascript for this kind of work (We use Typescript for our projects), but the vast majority of it is in Python. As much as I dislike that language, it completely dominates AI and data science.


> if you are serious about getting into data science or AI

Then you won't be using Javascript anyways. Like you said, Python is the way to go: PyTorch, TF, PyMC3, scikit-learn, etc.


There are several firms that I personally know of that use alternative languages for building production systems around AI. My two largest clients have systems built in Typescript and Java. If you are going to work in this field you have to know Python as it is a common language for practitioners, but the language choice for production systems is driven by many factors. Python is almost never an appropriate choice for building production systems. Even in cases where we use Python for models, the models are exported to a different language for integration with the deployed application. In many cases there are substantial benefits to building models in the language that will used for building the final application as well. As with all technical choices, the needs of the project should be the ultimate basis for making a selection -- not fanboi attachments to particular languages.


> but the language choice for production systems is driven by many factors. Python is almost never an appropriate choice for building production systems

And JS is? Even with TS papering over the top of it, JS is not really an ideal language for this. If you want a type system, performance and ease of use, why not write it in Julia?


> As much as I dislike that language

Can you elaborate why? It dominates AI and data science for a reason




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: