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

I am trying to use convnetjs to make Node.js learn from a string of numbers in x,y coordinates. The goal is to predict the next value in a simple string of numbers.

First of all, a very simple string [0,1,0,2,0,3,0,4,0,5,0,6] may be later a sin and cos numeric string.

I do not want to delve into deep learning matter, so I use convnetjs.

So far I have 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++) { xw[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 implement the training, I want to predict the following meaning, but I wonder (knowing the following meaning [9]) how to tell the coach that he did a bad, good or very good job?

What is the right way to train x more to predict the value? I think this is not so simple, because the predicted value does not go in the direction of the value 9 ^^.

+5
source share
1 answer

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 
+6
source

Source: https://habr.com/ru/post/1245375/


All Articles