【问题标题】:Simple linear regression with TensorFlowJSTensorFlowJS 的简单线性回归
【发布时间】:2018-06-26 13:15:10
【问题描述】:

我正在尝试为一个项目进行一些线性回归。 由于我习惯了 Javascript,因此我决定尝试使用 TensorFlowJS。

我正在关注他们网站上的教程,并观看了一些解释其工作原理的视频,但我仍然无法理解为什么我的算法没有返回我期望的结果。

这是我正在做的事情:

// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// Generate some synthetic data for training.
const xs = tf.tensor1d([1, 2, 3, 4]);
const ys = tf.tensor1d([1, 2, 3, 4]);

// Train the model using the data.
model.fit(xs, ys).then(() => {
  // Use the model to do inference on a data point the model hasn't seen before:
  // Open the browser devtools to see the output
  const output = model.predict(tf.tensor2d([5], [1,1]));
  console.log(Array.from(output.dataSync())[0]);
});

我在这里尝试制作一个线性图,其中输入应始终等于输出。

我试图预测输入5 会得到什么,但输出似乎是随机的。

这里在 codepen 上,你可以试试:https://codepen.io/anon/pen/RJJNeO?editors=0011

【问题讨论】:

    标签: javascript tensorflow tensorflow.js


    【解决方案1】:

    您的模型仅在 一个 epoch(一个训练周期)后进行预测。结果损失仍然很大,导致预测不准确。

    模型的权重随机初始化。所以只有一个时期,预测是非常随机的。这就是为什么需要训练一个以上的时期,或者在每批之后更新权重(这里你也只有一批)。 要查看训练期间的损失,您可以这样更改拟合方法:

    model.fit(xs, ys, { 
      callbacks: {
          onEpochEnd: (epoch, log) => {
            // display loss
            console.log(epoch, log.loss);
          }
        }}).then(() => {
      // make the prediction after one epoch
    })
    

    为了得到准确的预测,你可以增加 epoch 的数量

     model.fit(xs, ys, {
          epochs: 50,  
          callbacks: {
              onEpochEnd: (epoch, log) => {
                // display loss
                console.log(epoch, log.loss);
              }
            }}).then(() => {
          // make the prediction after one epoch
        })
    

    这是一个 sn-p,它显示了增加 epoch 的数量将如何帮助模型表现良好

    // Define a model for linear regression.
    const model = tf.sequential();
    model.add(tf.layers.dense({units: 1, inputShape: [1]}));
    
    // Prepare the model for training: Specify the loss and the optimizer.
    model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
    
    // Generate some synthetic data for training.
    const xs = tf.tensor1d([1, 2, 3, 4]);
    const ys = tf.tensor1d([1, 2, 3, 4]);
    
    // Train the model using the data.
    model.fit(xs, ys, {
      epochs: 50,
      callbacks: {
          onEpochEnd: (epoch, log) => {
            console.log(epoch, log.loss);
          }
        }}).then(() => {
      // Use the model to do inference on a data point the model hasn't seen before:
      // Open the browser devtools to see the output
      const output = model.predict(tf.tensor2d([6], [1,1]));
      output.print();
    });
    <html>
      <head>
        <!-- Load TensorFlow.js -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.12.4/tf.js"> </script>
      </head>
    
      <body>
      </body>
    </html>

    【讨论】:

      【解决方案2】:

      您的训练数据需要比模型形状多一维,以反映训练批次。所以 x 和 y 至少需要 2D。

      【讨论】:

        猜你喜欢
        • 2021-09-14
        • 2016-04-14
        • 1970-01-01
        • 2014-05-04
        • 1970-01-01
        • 2013-03-15
        • 2018-12-13
        • 1970-01-01
        • 2018-10-03
        相关资源
        最近更新 更多