Trying out Node JS with Synaptic

A few weeks ago I subscribed to the Javascript Weekly newsletter as an attempt to immerse myself more into the frontiers of software development! It worked because I ended up with a list of really interesting articles that I never would’ve read otherwise. I’d recommend it! Check it out here.

One of the articles which caught my eye was this lovely tutorial on Synaptic.js: How to create a Neural Network in JavaScript in only 30 lines of code. As an applications developer, Javascript seems really exciting so I wanted to know more, and this tutorial seemed like a really small piece of code to do something quite cool. So I dove in.

The Tutorial

I worked through the tutorial at various points over the course of a day, just reading through the article and retyping out the elements; making a mental note as I went along. It was a really cool and surprisingly simple experience. The library’s website, Synaptic, gives a slightly more exciting demo of the possibilities of the library, with a neural network for traversing a screen as a group. It was the first time I’ve really tried to look into anything javascript specific that wasn’t basic website functionality and it was pretty exciting to see something relatively complex expressed so simply.

SynapticCapture

The synaptic page, with the demo up and running

You specify neurons and connect them up in layers to form a network. Then you activate and backpropagate that network with two simple calls, helpfully named activate and propagate. I still need to spend some time understanding react, node, angular and things like that. But it’s a nice introduction. But I had a problem. I’m a complete Node newbie. So I needed to spend some time working out how to run javascript with libraries from Node to be able to test.

Running Node Apps

So, I bumbled about for a while trying to work out what I’d need to get this to run. The documentation for synaptic actually does a really good job of telling you once you have the context to understand the things its saying. And it didn’t take me that long to find it out.

First things first, was to install Node js. The Node site is real simple and installs npm at the same time. Npm is a package manager for javascript libraries. What that means, practically, is that people will upload their modules to npm and, when you have it installed, you can type a single command in the terminal to download all the library files to your computer. No searching for links. No find the right versions or distributions. Just a line on the command line. Nice.

So once Node and npm were installed, I did some reading up and rememberes about client and server side code. Node JS is a server side technology. That means that scripts you write are naturally meant to exist on the machine where the web page lives. For Node, that means you can pretty easily run the script on your machine using the terminal. On the command line I just typed

‘node E:\Personal\xorNeuralNet.js’

This should work. But I got nothing. It took me some puzzling before I realized I had Node but I didn’t have the Synaptic library code! The following npm command installs it for you; “npm install synaptic –save” and you’re good to go. You need to provide the node command “require()” to inform the runtime that it needs to grab the Synaptic module. Rerunning the node command worked like a charm!

NeuralNetCapture

But this was only half the battle. I wanted it running in my browser. This is a little trickier (though still pretty darn easy as it turns out). Because Node is a server side technology, your browser isn’t able to compile it, because that’s a client side technology. But! Libraries like Synaptic can be minified/browserified to javascript and the browser CAN run that. In fact, this is even distributed with the lirary itself. Convenient!

Running in the Browser

So to get it running in the browser (I use chrome), I had to create a simple html page:

NeuralNetHTMLCapture

Which includes the Synaptic library code ahead of my script. The script I wrote did need a slight modification though. The “require” line is actually a Node function, which means the browser can’t interpret it. But, we’ve already included the Synaptic library from our script html tag, so we actually don’t even need it. A quick delete and a refresh and I had the code running. You can see this by right clicking and selecting Inspect. The panel that pops open has a tab at the top for console, and with that open you should see the output from the script if all went well.

So there you have it. My experiences with running Node for the first time, using the Synaptic library. I learnt a lot from doing it. And for any beginners out there, I hope this helps!

Leave a comment