You need to determine the space input in space for your data. After that, do the following:
- Create a network according to a previously defined domain (see this documentation to configure the network setting: convnetjs-doc ).
- Build a network, please also refer to convnetjs-doc in order to select the correct values ββfor the trainer's parameters.
The following example shows a network assuming that the space in the domain is 9 (the network should predict the next value for one row of size 9). I use the same training my_data
( my_data
), so to meet the space requirements in each data element, I take arrays of size 9 at each stage of the training process with my_data
(using slice
) and assuming that the real value for each row is the next value in my_data
after accepting an array of size 9 (if changing the dataset you should use a different approach to create elements that satisfy the same space space requirement).
The learn
function performs the learning process described above, var data = my_data.slice(i, i + d);
accepts an array of size d
(9 in this example) from my_data
starting with i
, so we go through data training to install and take slices of size 9 (to satisfy the need for domain space). After that, we get the real value for data
as follows: var real_value = [my_data[i + d]];
, which is the value next to the last one in data
, note that since we are working with regression
, real_value should be a LIST (see convnetjs-doc for more information). Then we create the class Vol var x = new convnetjs.Vol(data);
to store data, and finally, we train network settings for the real_value
real value for the previously created class Vol trainer.train(x, real_value);
.
When we finish the learning process, we are ready to predict some values, all we need to do is create a new input using the Vol class and predict using the trained network.
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));
Here is an 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