Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deep learn from a row of numbers using Node.js and convnetjs and predicted a new value?

I try to to use convnetjs to make Node.js learn from a row of numbers in x,y coordiinates. The goal is to predicted next value in a simple number row.

First of all a very simple row [0,1,0,2,0,3,0,4,0,5,0,6] maybe later sin and cos number row.

I do not want to go to deep into the deep learning materia so I am using convnetjs.

So far I tried :

var convnetjs = require("./convnet-min.js");

// create a net out of it
var net = new convnetjs.Net();

var layer_defs = [];
layer_defs.push({type:'input', out_sx:1, out_sy:1, out_depth:1});
layer_defs.push({type:'fc', num_neurons:5, activation:'sigmoid'});
layer_defs.push({type:'regression', num_neurons:1});
var net = new convnetjs.Net();
net.makeLayers(layer_defs);

var my_data = [
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8
];

var x = new convnetjs.Vol(my_data);

var trainer = new convnetjs.SGDTrainer(net, {learning_rate:1.1, momentum:0.0, batch_size:1, l2_decay:0.001});

var think = function () {
    for (var i = 0; i < my_data.length; i++) {
        x.w[i] = my_data[i]; // Vol.w is just a list, it holds your data
        trainer.train(x, my_data[i]);
    }
}

for (var i = 0; i < 100; i++) {
    think();
    var predicted_values = net.forward(x);
    console.log('predicted value: ' + predicted_values.w[0]);
}

To realize learning I want to predict the next value, but I wonder (knowing the next value [9]) how to tell the trainer he did a bad, good or very good job?

What this the right way to train x more to predict a value? I guess this is not trivial as that because the predicted value goes not into direction of value 9 ^^.

like image 300
Stephan Ahlf Avatar asked Mar 19 '16 09:03

Stephan Ahlf


1 Answers

You need to define a domain space input for your data. After that follow this steps:

  • Create a network according to the domain previously defined (please read this documentation to tune the parameter in the network: convnetjs-doc).
  • Train the network, please also refer to the convnetjs-doc in order of select proper values for the parameters of the trainer.

The follow example show you a network assuming that the domain space is 9 (the network must predict the next value for one row of size 9). I'm using the same data set for training (my_data), so to meet the domain space requirement in each data item I'm taking arrays of size 9 in each step in the training process from my_data (using the slice function) and assuming that the real value for each row is the next value in my_data after take an array of size 9 (if the data set change you should take a different approach to create items that meet the same domain space requirement).

The function learn do the learning process described above, var data = my_data.slice(i, i + d); take an array of size d (9 in this example) from my_data starting at i, so we are moving through the data training set and taking slices of size 9 (to meet the domain space requirement). After that we get the real value for data with this: var real_value = [my_data[i + d]]; which is the value next to the last in data, note that since we are working with regression the real_value must be a LIST (see convnetjs-doc for more details). Then we create a Vol class var x = new convnetjs.Vol(data); to storage the data, and finally we train the net setting the real value real_value for the previously created Vol class trainer.train(x, real_value);.

When we finish the learning process we are ready for predict some values, all we have to do is create a new input, using a Vol class and predict with the trained net.

This is the code:

var convnetjs = require('convnetjs');

// create a net out of it
var net = new convnetjs.Net();
var d = 9;
var layer_defs = [];
layer_defs.push({type:'input', out_sx:1, out_sy:1, out_depth:d});
layer_defs.push({type:'fc', num_neurons:10, activation:'sigmoid'});
layer_defs.push({type:'regression', num_neurons:1});
var net = new convnetjs.Net();
net.makeLayers(layer_defs);

var my_data = [
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8,9,10,
    0,1,2,3,4,5,6,7,8
];



var trainer = new convnetjs.SGDTrainer(net, {learning_rate:0.01, momentum:0.2, batch_size:1, l2_decay:0.001});

var learn = function () {
  for(var j = 0; j < 100; j++){
    for (var i = 0; i < my_data.length - d; i++) {
        var data = my_data.slice(i, i + d);
        var real_value = [my_data[i + d]];
        var x = new convnetjs.Vol(data);
        trainer.train(x, real_value);
        var predicted_values = net.forward(x);
        console.log("data: [" + data + "] -> value: " + real_value);
        console.log("prediction in learn stage is: " + predicted_values.w[0]);
    }
  }

}

var predict = function(data){
  var x = new convnetjs.Vol(data);
  var predicted_value = net.forward(x);
  return predicted_value.w[0];
}

learn();
var item = [0,1,2,3,4,5,6,7,8];
console.log("predicted value for [" + item + "] is: " + predict(item));

These are some example output:

predicted value for [3,4,5,6,7,8,9,10,0] is: 1.0789064579041727
predicted value for [0,1,2,3,4,5,6,7,8] is: 9.223386915148865
predicted value for [10,0,1,2,3,4,5,6,7] is: 8.430232430080627
predicted value for [1,2,3,4,5,6,7,8,9] is: 9.020852169040044
predicted value for [5,6,7,8,9,10,0,1,2] is: 3.0623065881421674
predicted value for [4,5,6,7,8,9,10,0,1] is: 2.208646113846295
like image 173
leobelizquierdo Avatar answered Sep 28 '22 12:09

leobelizquierdo