Tensorflow.js:简单的线性回归效果不佳

时间:2019-07-02 15:14:02

标签: javascript tensorflow machine-learning neural-network tensorflow.js

我有一个example video的基本代码(视频的前半部分是这样做的,但是数据集不同)。

代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
        <title>Website</title>
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script>
        <style>

        </style>
        <script>
            var linearModel = tf.sequential();
            linearModel.add(tf.layers.dense({units: 1, inputShape: [1]}));
            linearModel.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

            var xs = tf.tensor1d([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]);
            var ys = tf.tensor1d([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]);

            linearModel.fit(xs, ys);

            function linearPrediction(val) {
                var output = linearModel.predict(tf.tensor2d([val], [1,1]));
                var prediction = Array.from(output.dataSync())[0];
                console.log(prediction);
            }

            linearPrediction(50);
        </script>
    </head>
    <body>
        Welcome to my website.
    </body>
</html>

我使用100值训练它,其中输入与输出相同。然后,当我在训练后尝试使用50作为输入运行它时,得到的结果范围从-5060

这是正常现象吗?我希望值接近50

此外,当我训练一个具有11000的值的数组并输入500时,我什至会得到从-600开始的输出。

1 个答案:

答案 0 :(得分:0)

您的代码中有两个主要问题。

问题1:不等待承诺完成

linearModel.fit返回一个Promise,将在培训结束后解决。这意味着,该模型现在将开始训练,但是在训练结束之前,您已经在要求预测。

您必须等待Promise解决。最简单的方法是将代码放入async函数中,然后像这样使用await

(async () => {
  var linearModel = tf.sequential();
  // ...
  await linearModel.fit(xs, ys);
  // ...
})();

问题2:学习速度不佳

sgd的默认学习率是0.01,在您的情况下该值太高。用该值训练模型对我来说,每次训练迭代的loss值都增加了,表明学习率很高。如果您需要有关该主题的更多信息,请查阅"estimating an optimal learning rate"上的本指南。

您可以通过使用tf.train.sgd函数(而不是使用字符串)并按如下所示传递学习率来更改学习率:

linearModel.compile({ loss: 'meanSquaredError', optimizer: tf.train.sgd(0.0001) });

进一步的改进:训练不止一个纪元

尽管以上技巧应该已经产生接近50的结果,但是您可以通过训练一个以上的时期来进一步改善模型。您可以像这样传递epochs参数来训练模型更长的时间:

await linearModel.fit(xs, ys, {
  epochs: 10
});

自己尝试

我在以下代码段中为您的代码添加了改进。您可以更改epochslearning rate的值,以查看其如何影响50的预测结果。

document.querySelector('button').addEventListener('click', async () => {
    const learningRate = document.querySelector('#learning_rate').value;
    const epochs = document.querySelector('#epochs').value;
    
    const linearModel = tf.sequential();
    linearModel.add(tf.layers.dense({ units: 1, inputShape: [1] }));
    linearModel.compile({ loss: 'meanSquaredError', optimizer: tf.train.sgd(learningRate) });

    const xs = tf.tensor1d([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]);
    const ys = tf.tensor1d([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]);

    await linearModel.fit(xs, ys, {
        epochs,
        callbacks: {
            onEpochEnd: (epoch, logs) => console.log(`Loss, epoch ${epoch}: ${logs.loss}`),
        },
    });

    function linearPrediction(val) {
        const output = linearModel.predict(tf.tensor1d([val]));
        const prediction = Array.from(output.dataSync())[0];
        console.log(`Prediction for 50: ${prediction}`);
    }

    linearPrediction(50);
});
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script>

epochs: <input type="number" id="epochs" value="1" />
learning rate: <input type="number" id="learning_rate" value="0.0001" />
<button id="train">Train</button>

相关问题