I’m mod for neural networks (Chapter 1)

Exploring the neural-redis module with Node.js

Kyle
5 min readDec 19, 2016

This is part 27.1 of my Node / Redis series. The previous part was That Syncing Feeling.

Let’s jump into the way-back machine for a minute. This will date me, I guess but my first introduction to a neural network was playing with a public domain implementation back in the early 1990s. For me, it was pre-internet days and I was learning Turbo Pascal 6 by trial-and-error. To many, this will seem laughable, but it used to be common to order CD-ROMs loaded with collections of public domain stuff from Walnut Creek Software. I had one for games (obvious) but I also had one that was labeled something like “SuperCode” that was loaded down with source code. The collection must have been scoured from some old-school BBS because most stuff dated from around 1983–1988 or so. In this collection, I found a pascal implementation of a neural network. I played with the neural network because, well, it sounded cool and futuristic. I plugged numbers into the demo software for a few hours to figure out that, while cool, it didn’t do much — definitely not my fevered teenage dream of turning my computer into a sentient being. So, let’s fast-forward to the present.

Things have, uh, advanced since the early 90’s and AI is all the rage these days and when I learned that a neural network was implemented as a Redis module, I had to give neural networks another go. Let me first start by saying that I’m not an expert on this topic. This is more of a “learn with me” than “learn from me” post. If you’re not familiar with neural networks, a decent layman’s explanation is in this video:

It’s worth a watch an doesn’t have a single mathematical symbol.

If this looks interesting, let’s first start by setting up our environment. At the time of writing Redis 4 is not production ready and most modules are alpha stage. I’m working on a few other Redis projects, so I didn’t want to sully my current Redis install up with some non-production version for now. This is a job for a VM.

VM for a NN

First, I went to Ubuntu and downloaded an ISO file for a server-install. I used VirtualBox to host the VM. It’s a remarkably easy wizard-based install process.

On a side note, I’d be interested to know how you manage to keep your Redis projects separated on your dev machine— I’ve tended to use SELECT/databases (in dev, not production), but I know that other people just live wildly and keep everything in one database or instance and are just careful to use prefixing (I’m not that much of a wild man). Others still standup a new Redis server instance per project, which is often the preferred method. Let me know what you do in a response.

Once you’ve got your clean VM up and running, SSH into it from your host machine and login. Once logged in, grab the unstable version of Redis and build it with the following steps:

$ wget https://github.com/antirez/redis/archive/unstable.tar.gz
$ tar xzf unstable.tar.gz
$ cd redis-unstable

(You may need to also install make and gcc, depending what image you originally downloaded)

$ cd deps
$ make hiredis lua jemalloc linenoise
$ cd ..
$ make

(You may also need to install tcl)

$ make test

With a little luck you should have this guy to greet you:

\o/ All tests passed without errors!

I’m sure there are a billion variations on building an unstable version of Redis. The article below has a great deal of detail on how to just build Redis from source. If I missed something in your situation, the article is a good place to start looking.

Now you probably want to develop in your nice environment with whatever tools you’re used to, but to access the Redis as if it was running on the host machine. To do this, we’ll need to make the VM talk to the host machine. Without the VM running, execute the following command from your terminal:

$ VBoxManage modifyvm "Redis 4" --natpf1 "redis,tcp,,6370,,6379"

(“Redis 4” is the name of my VM, adjust accordingly)

What we’re doing here is telling Virtual Box to change the configuration to map the VM’s TCP port (6379) to the host machine’s port 6370. Really, you can use any port for the host machine.

Next up on the virtual machine, you’ll need to adjust redis.conf. Remove the comment (#) in front of Bind 127.0.0.1 . This will make your Redis instance accessible to the host machine. Next add a password with the requirepass line. This is fine for a VM residing on your development machine, its an proceed carefully on a server or otherwise less sheltered environment. You can really open yourself up if you’re not careful. Test your config by using redis-cli on your host machine:

$ redis-cli -p 6370 -a yourpassword

Once you’re in, run some basic Redis commands to confirm all is well.

Now back on your virtual machine, let’s go ahead and grab the neural-redis module and install it.

$ wget https://github.com/antirez/neural-redis/archive/master.zip
$ unzip master.zip
$ cd neural-redis-master
$ make generic

Now edit the redis.conf file again.

loadmodule /path/to/neuralredis.so

Once you’ve got all that done, restart Redis server and you should have a few new commands to play with:

  • NR.RUN
  • NR.CREATE
  • NR.OBSERVE
  • NR.TRAIN
  • NR.CLASS
  • NR.INFO
  • NR.RESET
  • NR.THREADS

Fire up redis-cli again and test them out. How exciting! Modules are really quite cool — you can get more over at Redis Modules.

Coding for the neural-redis module in Node.js

Now, moving forward with getting the module to work with Node.js. Out-of-the-box, node_redis doesn’t have support for the neural redis commands aside from the generic send_command function which is cumbersome and ugly. Don’t worry I’ve got you covered. I made a pull-request to node_redis that allows for simple adding of commands. Until that goes through, feel free to use my fork of node_redis (I’ll update this space when it is added to node_redis proper). To add a command, you need to tell node_redis that it exists. To do this, execute the addCommand function with the module’s command.

redis.addCommand('nr.run') //adds the nr.run command

In Javascript, nr.run wouldn’t be a valid function name. node_redis converts the dot in the redis command into an underscore so nr.run is transformed into nr_run.

To ease this conversion, I wrote a tiny adapter node module. All it does is adds the Neural Redis API. To enable node_redis, just pass-in your node_redis module instance (not the client):

var
redis = require('redis'),
redisnn = require('redis-nn');
redisnn(redis);

After this, you’ll have the full suite of Neural Redis commands available for both singular and multi execution.

In the next part we will build a Neural Redis playground where you can interactively explore the commands in the API and how they work.

--

--

Kyle

Developer of things. Node.js + all the frontend jazz. Also, not from Stockholm, don’t do UX. Long story.