javascript – 如何使用Node.js和convnetjs深入学习一行数字并预测一个新值?

我尝试使用convnetjs来使Node.js从x,y coordiinates中的一行数字中学习.目标是在简单的数字行中预测下一个值.

首先,一个非常简单的行[0,1,0,2,0,3,0,4,0,5,0,6]可能是后来的sin和cos数字行.

我不想深入深入学习材料,所以我使用的是convnetjs.

到目前为止我试过:

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]);
}

为了实现学习,我想预测下一个值,但我想知道(知道下一个值[9])如何告诉训练师他做了一个坏的,好的或非常好的工作?

这是什么正确的方法来训练x更多来预测价值?我想这不是那么简单,因为预测值不会进入值9 ^^的方向.

最佳答案
您需要为数据定义域空间输入.之后按照以下步骤操作:

>根据先前定义的域创建网络(请阅读本文档以调整网络中的参数:convnetjs-doc).
>训练网络,请参考convnetjs-doc,以便为训练器的参数选择合适的值.

以下示例显示网络,假设域空间为9(网络必须预测一行大小为9的下一个值).我正在使用相同的数据集进行训练(my_data),因此为了满足每个数据项中的域空间要求,我在训练过程的每个步骤中从my_data(使用切片函数)获取大小为9的数组并假设在获取大小为9的数组之后,每行的实际值是my_data中的下一个值(如果数据集发生更改,则应采用不同的方法来创建满足相同域空间要求的项目).

函数learn学习上述学习过程,var data = my_data.slice(i,i d);从i开始,从my_data中获取一个大小为d的数组(在本例中为9),因此我们将遍历数据训练集并获取大小为9的切片(以满足域空间要求).之后我们用这个获得数据的实际值:var real_value = [my_data [i d]];这是数据中最后一个的值,请注意,由于我们正在使用回归,因此real_value必须是LIST(有关详细信息,请参阅convnetjs-doc).然后我们创建一个Vol class var x = new convnetjs.Vol(data);存储数据,最后我们训练网络设置为先前创建的Vol class trainer.train(x,real_value);的实际值real_value.

当我们完成学习过程时,我们已准备好预测一些值,我们所要做的就是创建一个新的输入,使用Vol class并使用经过训练的网络进行预测.

这是代码:

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));

这些是一些示例输出:

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

转载注明原文:javascript – 如何使用Node.js和convnetjs深入学习一行数字并预测一个新值? - 代码日志