Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Neural networks in JavaScript – free 19-part course (scrimba.com)
329 points by mrborgen on Dec 23, 2018 | hide | past | favorite | 69 comments


Hey guys! I'm the creator of the course and lead developer of brain.js and would love to answer any questions you may have.


It took me a while to realize I wasn't just watching a static video and that I could actually interact with the code! Very well done and nice pacing/voiceover.


Thanks for this. It is certainly a lot simpler than grappling with jupyter notebooks & numpy and the like. The course is fairly short but covers off a bunch of stuff.

Couple of thoughts:

- The "hello world" of ML seems to have kinda settled on CNN "cat detector" or MNIST type things now (for better or worse). It does not appear that brain.js has support for CNNs yet(?), but it might be nice to address this in the course somehow - even if it is just "this is coming soon". Lots of people are visual learners - it would be neat for this to be supported somehow, e.g. for brain.js to support loading training data via URLs or grabbing a frame from a webcam and doing some basic stuff with images. This kinda goes for the brain.js webpage too - the demo there is a bit underwhelming compared to the fun tensorflow playground @ https://playground.tensorflow.org/

- It might be nice to see a few more "real world" examples of doing things that real people might want to do, e.g. a comment toxicity/spam detector for their sites that runs real-time in the browser. From this course I am not sure how to load in a pre-trained model for instance.

- I tried to use the GPU NeuralNetwork in a couple of the sessions but I got a "TypeError: array is undefined (1.6.0/browser.js:18548)" error.


Hey Axel! Just started watching but the interactive guide is incredible. It's like having Screenhero with a private tutor.

Only thing is, I kind of wish there were something like the time-coded comments SoundCloud has. At the end of the second guide, we're asked to play around with the tests.

I added: console.log(net.run([0, 4])); console.log(net.run([3, 3])); console.log(net.run([8, 4]));

Based on the training data, I would expect this to resolve to ~4(or 1), ~0, ~8(or 1) by standard logic expectancies (if same return 0, else return the higher number or 1). But instead I received ~0, ~0, ~0.

It's not immediately obvious what is causing this. But it seems like the model created is inherently ignorant of basic logic (at least by my narrow definition), and there isn't any immediate discussion of caveats as to error margin.

I'll admit this might be a n00bish concern based on never programing neural nets before, but as this guide seems focused on introducing NN's to n00bs like me: a way to discuss concerns with other viewers/the author would be amazing.

Aside from that, incredible work! I'll keep watching to see if I can figure out my misunderstandings.

Update: Just discovered the Q&A tab, this should likely be adequate for my concerns. Well done. This may be the best online demo/tutorial I've ever seen.


Ty! This means a lot! Can you point me to the tutorial you added new tests with?


Hey sorry for the delay, it was at the end of the second tutorial "Our First Neural Net!" I think you were instructing us to try testing the outputs for the other sets in the training data which all work as expected, but I took the "play around with the outputs" instruction to mean, see how the NN responds to novel inputs like the ones I mentioned.


Thanks Robert for putting this together. Coming from PyTorch ecosystem. What is the actual requirement for this course? Would a smattering knowledge of ES6 do? Been leching at browser based implementations since Karpathy demoed his CNNs a few years ago but just havent had enough motivation and courage to pickup JS. Perhaps my main reson for anxiety is the confusing JS ecosystem.Also what are some real world use cases for training and deploying NNs on the browser rather than training and deploying it on a tradional cloud backend environment?


afaik it's mostly for cool demos and educational purposes, or extremely latency intensive applications that funny require large models.

for most deep learning models, training on the client is completely unreasonable, as they require weeks of training even on multi thousand $ pro equipement.

For training, tiny metalearning models are the only reasonable thing to train on the clients in most useful scenarios, as they are pretrained to take as few examples as possible to train on a specific task (see MAML).

For inference, aside from educational applications, the only potential advantage of in browser over in server is the lower latency. the main disadvantages are that you need to send the model to the client (multiple MB), with the longer loading time and potential problems with intellectual property this entails. maybe for some extremely small models in very latency driven applications, it can be worth it.

So, overall, either educational purposes, training and use of latency hungry tiny metalearning models or inference with tiny pretrained latency hungry models, like computer vision on webcam sort of deal


Keep in mind that while the tutorial is in javascript, in the web browser, the neural network easily apply for node based solutions as well.

We're adding GPU support that use either client side OR server side GPU, so that any case you mention can be handled.


yes, but afaik node with webgl is strictly slower than cuda/cudnn with Python or whatever serving


unless you also support cuda/cudnn directly now ofc


Real world use cases might be; Classifying the users mood from mouse movement, classifying the microphone audio. I.e. process real time data that might be too large to upload.

I personally haven't seen any NNs being used in browser apps, but there are plenty of existing mobile apps that has NNs to classify audio/video/etc directly on the device.


All you need know are basic function calls, objects and arrays. I share the same fears you have about the JS ecosystem, especially when they overcomplicate something simple. By keeping the course thought out and simple we can adhere to a wide audience, and show off that the priciples are the same.


This is fantastic! The tutorials are very simple to understand and can be actually used in your very familiar JavaScript env and the Scrimba editor is great coupled with the author editing things in real time that you can play with, brilliant. Thanks Robert!


I just watched the reinforcement learning video and was very confused -- where did the reinforcement learning happen? I thought reinforcement learning was giving an agent positive / negative signals to learn without example output data, but in the video the network was just retrained with more data? Is it an overloaded term maybe?


Ty for asking this question! "Reinforcement learning" has a wide definition, but in this case because we are using a simple feed forward neural network, "reinforce" is more principled by dynamic programming with supervised learning. We are not actually using a "Deep reinforcement learning" algorithm.

The idea here is that the net can continue to train, reinforcing its previous understanding with new understandings, if new training data is provided.

It may be that we need to clarify the tutorial, as your point is based around unsupervised learning, not having training data.

Is it an overloaded term? YES!


Thanks for the clarification!


Totally add to the collaborative list of interactive Machine Learning, Deep Learning and Statistics websites https://github.com/stared/interactive-machine-learning-list (waiting for a PR! :))


why would you do ML in JS?


Why would you do web dev in python?


Because you don't know Ruby


because you distrust microsoft fundamentally enough to avoid .net core?


exactly, JS for ML sounds like a stretch indeed.


Is there a Numpy equivalent for JS yet? How easy is it to use (considering that JS doesn't have operator overloading)?


Not effectively. From what I've read, JS lacks SIMD support which makes Numpy-like vectorizations unachievable. Would be nice though...


I think Numpy is written in C, so in principle they could use the same approach for JS (and invoke SIMD functions from C). The problem, however, is that there is no operator overloading in JS. So you can't write things like a[:,:,3] *= 2.


This seems to exactly follow the examples from the readme. Guessing this is by someone that worked on the project. Either way great tutorial.

One complaint... `#` THIS IS NOT AN ASTERISK! (he said it like 5 times in one video)

Otherwise great tutorial


Doh! You found a mistake. I'll try and take care of that asap. Ty for pointing this out.


interesting - it seems to me, anecdotally, that more and more ML/AI emerging in JS (vs python as the default goto).

Anyone know if anyone is tracking this (similar to github language usage stats)?


Awesome! Bonus point for doing a js course


Thank you for doing this! Can't wait to check it out.


Why the heck Javascript?


One very practical reason is to save on computation costs. GPU servers are not cheap and if you're going to run on CPU anyway, you might as well run it locally on the user's browser.

Some demos such as real time object detection isn't possible at all if you had to pay roundtrip server latency (not to mention the complexity of streaming video to and from your server): https://github.com/ModelDepot/tfjs-yolo-tiny

And a lot of web demos such https://affinelayer.com/pixsrv/ I don't believe will be up for such a long time if the author had to pay ongoing server costs.


There is NO save on computation cost. CPU is slow for NN whatever language you are using.

OP has a point, if I want to learn PRACTICAL front end programming, I will choose javascript, not Python. Same for Neural networks, just switch the place.


Cost as in money.

To deploy a model in js (as a web page) all you need is a static S3 or GCS bucket. You don't even need a webserver and it can automatically handle infinite scale. Show me a python solution that can do the same.


There is currently no NN learning algorithm that can handle massively paralleled training. We can use some simple fixes such as mean gradient but they have severe limitations, and their limitation grows as you scale up. Currently even with a handful paralleled GPU training, the gradient computation needs to wait for all GPU batch to complete and then return to CPU before the next batch, so your idea of infinite scaling is just a pipe dream at the moment. Sure Python can't be run on a million pages, but heck, no NN architecture can even handle more than a dozen paralleled computation in a stable manner yet.


Yea the infinite scaling was referring to deployment of inference, not training.


> To deploy a model in js (as a web page) all you need is a static S3 or GCS bucket.

First of all, what you have described is far from the reality.

Had this come true, only inference will be in javascript, through some language agnostic standardized model format, not training. The model is just a blackbox function for the js runtime to call. The amount of javascript to make this happen will be surprisingly slim anyway.


> First of all, what you have described is far from the reality.

Did you try any of the links I included? This is the reality for all of them and they are a few years old. They have a model file loaded from bucket url and never make another network request thereafter.

It's actually the recommended workflow from https://js.tensorflow.org/ where you'd find tons of other examples.

But you're right, this is for inference only. I would not do training in JS.


What I mean reality here is the running nn model in browser is possible, but not practically efficient, so even with its perceived simplicity, people would under most occasions, run the inference in the cloud, with its controllability and performance, like using customized hardware. After all, running inference is about to run it reliably and fast, until the day when nn operations are ubiquitous and common enough to be standardized and shipped in performant runtime that come out-of-box, browser inference is still a dream that is too good to be true.


What's wrong with js for neural networks? JS/V8 is faster than python, not accounting for python bindings to native NN libs that may as well be bound to js.


JS/V8 may beat Python in general operations but definitely not in math specialized libraries. A lot of python packages support OpenBLAS and AVX/FMA by default which gives huge boosts to math/tensor operations. Correct me if I'm wrong, I don't think the js ecosystem is quite there yet except for experimental projects.


There is nothing wrong to use js, like there is nothing wrong to call it impractical.

JS is faster than Python, but in the land of DL, there is C++ and everyone else. No one is using Python to do the actual computation anyway.


"No one is using Python to do the actual computation anyway."

Depends what "actual" computation is. If you're definition of "actual computation" is something which requires extreme optimization then your definition precludes the question. In the real world there is an extraordinary amount of computation done with inefficient languages though simply because development time costs very often outweighs run time costs.


Python is used because there are C/C++ bindings for working with NNs.

The point is that none of computations are done in pure Python. Python just provides a convenient wrapper over non-Python code.


Are you suggesting distributed NN training through user's browsers? This is a very sketchy area along the lines of browser based bitcoin mining. You don't want to run any computation heavy code on client side without user's knowledge & approval.

Though if you only meant performing inference in user's browsers, then the challenge would be to find a way for TF/pytorch pre-trained models to port and perform accurately in js. If successfully done, I can see some use cases here.


See https://js.tensorflow.org/ for automatically porting tensorflow models to the browser


Are you under the impression that js is the only language that can run locally?


in a client's web browser... yeah.


Well there is web assembly, but why is running in a web browser a requirement for creating a neural network anyway?


so you can offset server costs to the client which usually means bloated laggy apps until someone comes along and figures out an optimised way of doing things, for eg react.js vs phonegap's comparative shitshow


Personally I've found that Python - or perhaps more Numpy et al - are impenetrable for a learner. I am sure the data structures that are used in Python + Numpy et al are powerful and well-suited for the task, but as a learner coming to this with minimal knowledge trying to ALSO learn the idiosyncrasies and weirdness (IMHO) of how Numpy does things and the weirdness (IMHO) of how Numpy even names things (e.g. in decades of programming, the term "shape" for an array was new on me) was an extra burden.

Doing the same thing in Javascript with good old-fashioned arrays using good old-fashioned terminology clears the fog and makes things simpler for people who are not already fluent in numpy's data structures and terminology

Javascript is - in my view - the equivalent of a "business english" of programming, i.e. even if you aren't fluent, its syntax and terminology is familiar to C/C++/Java/C#/Golang/ObjectiveC/Perl/etc that most people will at least be able to understand what is going on in the same way that business people who might not be fluent in English will at least be able to understand and basically communicate with each other even if they perhaps will not be writing Sonnets. Python feels like a niche language that developed in isolation and is only readable to people who have actually gone out of their way to learn it.


shape makes sense if you think of arrays in terms of linear algebra concepts(which you should if you're working with ML concepts imo)


Why not? It's one of the most widely known languages among developers in many disciplines, which removes a barrier to learning the concepts. Someone who just wants to familiarize themselves with basic implementations could do so in any language if their goal doesn't include shipping production-quality code. There's also the added bonus that the concepts taught here are already covered by courses in other languages, so anyone is free to use those as well.


Why not? It's extremely popular, fast and portable.


Also, it's hard to beat the browser for creating a quick visualization/UI for your code. Especially for an online course like this.

I think we're at a point where you'd have to justify why you didn't use Javascript for a learning course.



Usage: Javascript is everywhere and can be learned quickly. Speed: GPU can easily be tapped into inside Javascript with projects like GPU.js, which brain.js uses.


coz its the best language for prototyping and learning


It really, really super isn’t. It’s a Frankenstein mashup of programming insanity that never should have come into being in the first place, much less become the de facto language of the web.


it actually isnt a mashup... if u look at the data types... they are some of the simplest... the apis might be a bit weird... i agree


So, what language is not an insanity?


Any particular reasons or is this just based on what you heard 10 years ago?


From the top of my head:

- `this`

- the whole prototypal inheritance thing

- class inheritance bolted on top of prototypal inheritance

- arrow functions vs `function` functions


But none of that is going to affect our ability to do machine learning work with JavaScript


doesn't it affect complexity on an exponential scale though? codebases aren't paragons of perfection when even 2 devs are working on them


There are many languages that are very popular for prototyping and learning what makes JavaScript the best?


also for better or for worse... the javascript ecosystem is amazingly wide... from brain as in this example to p5js for creative programming etc... i think we can agree that no other language and ecosystem has that much breadth and reach...


for one its available in any browser console... there is no setup per se.




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

Search: