Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript tensorflow.js多元回归sgd中的错误最小化_Javascript_Node.js_Tensorflow_Linear Regression_Tensorflow.js - Fatal编程技术网

Javascript tensorflow.js多元回归sgd中的错误最小化

Javascript tensorflow.js多元回归sgd中的错误最小化,javascript,node.js,tensorflow,linear-regression,tensorflow.js,Javascript,Node.js,Tensorflow,Linear Regression,Tensorflow.js,这是我的第一篇文章。我在tensorflow.js中拟合曲线时遇到了一个错误,似乎无法修复。到目前为止,我已经花了两天时间。由于tensorflow.js是一个非常新的问题,对于这类问题没有太多的答案,所以我相信很多人都对此感兴趣。我尝试复制tensorflow.js项目网站上的示例: 不同之处在于我使用多个预测因子来预测结果变量。我有20个价格,我用前面的4个价格来预测第五个。所以我从价格数字5开始,到价格数字20,在加权时间序列预测模型中,价格5由价格1到4预测,以此类推。我使用的是多元线

这是我的第一篇文章。我在tensorflow.js中拟合曲线时遇到了一个错误,似乎无法修复。到目前为止,我已经花了两天时间。由于tensorflow.js是一个非常新的问题,对于这类问题没有太多的答案,所以我相信很多人都对此感兴趣。我尝试复制tensorflow.js项目网站上的示例:

不同之处在于我使用多个预测因子来预测结果变量。我有20个价格,我用前面的4个价格来预测第五个。所以我从价格数字5开始,到价格数字20,在加权时间序列预测模型中,价格5由价格1到4预测,以此类推。我使用的是多元线性回归框架,其中我设置了4个随机参数(之前四个价格中的每一个都有一个权重)。我的目标是训练变量使损失函数最小化(使用最小平方准则)。我试着尽可能地从链接中学习这个例子。每当我运行代码时,我都会得到:

错误:传入的variableGrads(f)中的f必须是函数

这是由第59行中的.minimize调用生成的(在末尾的train函数返回之前)。基本上,我所做的是拟合一个线性回归,这在R中更容易实现,但我们的目标是非常大的数据集和更复杂的机器学习过程。我相信这对其他很多开始使用tensorflow.js的人来说是很有趣的

以下是我的代码和一些注释:

const tf = require('@tensorflow/tfjs');

require('@tensorflow/tfjs-node');

module.exports = function tensorFlow() {
//the trainable variable with initial random numbers
let lag = tf.variable(tf.tensor([Math.random(), Math.random(), Math.random(), Math.random()], [4])); 

//20 observed prices
let priceData = [21.00397, 21.29068, 22.80492, 23.40646, 24.06598, 23.89722, 25.40211, 24.63436, 25.83449, 26.44832, 26.25194, 27.34009, 27.90455, 27.14175, 28.12549, 29.99411, 30.43631, 30.39753, 30.16104, 31.14931]; 

//the prices from price 5 on that are to be predicted
let toBePredictedList = [24.06598, 23.89722, 25.40211, 24.63436, 25.83449, 26.44832, 26.25194, 27.34009, 27.90455, 27.14175, 28.12549, 29.99411, 30.43631, 30.39753, 30.16104, 31.14931];

//set up tensor of labels to compare predictions with
let toBePredicted = tf.tensor(toBePredictedList, [16]);

//a list of predictors with 16 rows and four columns for 16 predictions to be made using 4 previous prices each
let predictorsList = [];

for (let predictorIndex = 0; predictorIndex < 16; predictorIndex++) {
    for (let predictionsIndex = 0; predictionsIndex < 4; predictionsIndex++) {
        predictorsList.push(priceData[predictorIndex + predictionsIndex]);
    }
}

//make it a tensor
let predictors = tf.tensor(predictorsList, [16, 4]);

//predict multiplies all predictors in all lines with the parameters from lag to be trained and adds up the four elements to generate an estimate of the fifth price
function predict(predictors) {
    function modelMaker() {
        let modelList = [];

        for (let rowIndex = 0; rowIndex < 16; rowIndex++) {
            let prediction = 0;

            for (let colIndex = 0; colIndex < 4; colIndex++) {
                prediction += lag.get(colIndex) * predictors.get(rowIndex, colIndex);
                console.log({prediction});
            }
            modelList.push(prediction);
        }
        return tf.tensor(modelList, [16]);
    }

    return tf.tidy(modelMaker);
}

//means square error of my prediction when compared to actual outcome price
function loss(predictions, toBePredicted) {
    return tf.losses.meanSquaredError(toBePredicted, predictions);
}

function train(predictors, toBePredicted, numIterations) {
    function computeLoss (predictors, toBePredicted) {
        let predictions = predict(predictors);
        return loss(predictions, toBePredicted);
    }
    let learningRate = 0.5; //suggested by Google Developers
    const OPTIMIZER = tf.train.sgd(learningRate); //suggested by Google Developers

    for (let iter = 0; iter < numIterations; iter++) {
        OPTIMIZER.minimize(computeLoss(predictors, toBePredicted));
    }
    return {
        a: lag.get(0),
        b: lag.get(1),
        c: lag.get(2),
        d: lag.get(3)
    };
};
//75 suggested by google developers
return train(predictors, toBePredicted, 75);
};
const tf=require('@tensorflow/tfjs');
需要(“@tensorflow/tfjs节点”);
module.exports=函数tensorFlow(){
//具有初始随机数的可训练变量
设lag=tf.variable(tf.tensor([Math.random(),Math.random(),Math.random(),Math.random(),[4]);
//20观察价格
价格数据=[21.00397,21.29068,22.80492,23.40646,24.06598,23.89722,25.40211,24.63436,25.83449,26.44832,26.25194,27.34009,27.90455,27.14175,28.12549,29.99411,30.43631,30.39753,30.16104,31.14931];
//从价格5开始的价格是可以预测的
让toBePredictedList=[24.06598,23.89722,25.40211,24.63436,25.83449,26.44832,26.25194,27.34009,27.90455,27.14175,28.12549,29.99411,30.43631,30.39753,30.16104,31.14931];
//设置标签的张量以将预测与
设toBePredicted=tf.张量(toBePredictedList[16]);
//16行4列的预测值列表,用于16个预测,每个预测值使用4个以前的价格
让预测器列表=[];
对于(让Predictor Index=0;Predictor Index<16;Predictor Index++){
for(让predictionsIndex=0;predictionsIndex<4;predictionsIndex++){
PredictsList.push(价格数据[PredictrIndex+predictionsIndex]);
}
}
//把它变成张量
设预测器=tf.张量(预测器列表[16,4]);
//predict将所有行中的所有预测器与待训练滞后的参数相乘,并将四个元素相加以生成第五个价格的估计值
功能预测(预测器){
函数建模器(){
让modelList=[];
对于(让rowIndex=0;rowIndex<16;rowIndex++){
设预测=0;
对于(让colIndex=0;colIndex<4;colIndex++){
预测+=滞后.get(colIndex)*预测因子.get(rowIndex,colIndex);
log({prediction});
}
推送(预测);
}
返回tf.tensor(modelList[16]);
}
返回tf.tidy(modelMaker);
}
//指我的预测与实际结果价格相比的平方误差
功能损失(预测,待预测){
返回tf.损失.均方误差(待预测,预测);
}
功能训练(预测、预测、限制){
函数computeLoss(预测值,待预测){
让预测=预测(预测);
回报损失(预测,待预测);
}
let learningRate=0.5;//由谷歌开发者建议
const OPTIMIZER=tf.train.sgd(learningRate);//由Google开发者建议
对于(让iter=0;iter
正如我所说,问题在于Minimize的最终效果。上面的代码运行良好,可以计算它应该计算的所有内容

谢谢你的建议! Chris

optimizer.minimize()
在每个训练周期更新权重。要更新权重,需要使用
tf.variable
创建权重。使用
tf.variable
创建的变量是可变的,而tf.tensor创建的变量是不可变的


还值得注意的是,predict()应该返回一个函数,该函数的系数是使用tf.variable创建的,该函数将被更新以最小化损失函数。

结果表明,最小化函数需要一个带有可训练变量的predict函数返回。因此,所有的模型规范都应该使用张量操作来实现,比如直接在返回中使用.add(),.mul()。然后它就起作用了。