Storing a Trained Neural Network- How I Beat Mongo's Limitations

javascript neural networks databases

The Problem

We wrote an app that needed a personalized neural network for different users. Training a neural network with user data takes a long time. A trained neural network is a function. Most databases won’t store functions.

The Solution

The neural network library we were using—Synaptic.js— provides a function that allows you to store a trained network as a standalone function. This allows you to repeatedly use the trained function, as opposed to continuously training the network each time it is used.

We then called toString() on the resulting function so we could store it in MongoDB, and then used Javascript’s notorious eval function when we wanted to use it later:

//First, we create a PantryItem class and a train function
var PantryItem = function(item){
  //create the network using a long short term memory architecture
  // that has one input neuron, one hidden neuron, and one output neuron = new Architect.LSTM(1, 2, 1);

  //create a trainer for the network
  this.trainer = new Trainer(;

PantryItem.prototype.train = function(trainingSet){
  //train the network using the given trainingSet
  //return a standalone function for the network that 
  //does not rely on any dependencies

//Next we instantiate and train a network for that item, and then store it in our Mongo database
var pantryItem = new PantryItem(item);
var trained = pantryItem.train(data);

//household is the Mongo table where we stored each user's information
household.pantry[item] = {
    // Store the standalone function inside the database by converting it to a string
    network: trained.toString(),
    data: additionaData

//Then, use eval() when we want to use the network 
eval("var network = "+household.pantry[item].network);

And that’s that! We did not choose to use eval lightly, as we are well aware of the potential security risks it can introduce. I’d love to hear if anyone has ideas for an alternate (potentially more secure) solution too!

Written on December 6, 2015