【问题标题】:Linear regression model: Why are the predicted values always wrong?线性回归模型:为什么预测值总是错误的?
【发布时间】:2019-08-13 00:37:59
【问题描述】:

我正在尝试做一个简单的线性回归模型,我的数据集是从 1 到 10 的数字。我正在尝试训练模型以预测对于任何给定的输出,例如 3,输出应该是值输入 (y = x)。

预测总是错误的。有人可以向我解释我做错了什么吗?

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

const xArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const yArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const createModel = () => {
  const model = tf.sequential();
  model.add(tf.layers.dense({ inputShape: [1], units: 1, useBias: true })); //input layer
  model.add(tf.layers.dense({ units: 1, useBias: true })); //output layer
  return model;
};

const convertToTensor = () => {
  return tf.tidy(() => {
    const inputTensor = tf.tensor2d(xArray, [xArray.length, 1]);
    const outputTensor = tf.tensor2d(yArray, [yArray.length, 1]);

    return {
      inputs: inputTensor,
      outputs: outputTensor,
    };
  });
};

async function trainModel(model, inputs, trueValues) {
  model.compile({
    optimizer: tf.train.adam(),
    loss: tf.losses.meanSquaredError,
    metrics: ["mse"]
  });

  return await model.fit(inputs, trueValues, {
    batchSize: 2,
    epochs: 5,
    learningRate: 0.04
  });
}

function testModel(model, testValue) {
  return tf.tidy(() => model.predict(tf.tensor2d([testValue], [1, 1]));
}

const run = async testValue => {
  const model = createModel();
  const tensorData = convertToTensor();
  await trainModel(model, tensorData.inputs, tensorData.outputs);
  const prediction = testModel(model, testValue);
  console.log(prediction.toString());
};

run(5);

【问题讨论】:

  • 这是如何作为重复关闭的?链接的问题是关于图像的分类问题。这是一个回归问题。他们唯一的共同点是两个模型都做出了错误的预测。甚至解决方案也完全不同(错误的分类函数与未使用正确的 API 且训练的 epoch 不足)。
  • 即使一个是回归问题,第二个是分类问题,答案仍然是相同的——调整答案中突出显示的模型,该模型与其重复。我只是通过提供上一个链接回答了这个问题,后来意识到我应该标记它
  • @edkeveked 不,答案不一样,看看吧!这个问题/答案是关于错误地使用 API(这与调优无关!)并且没有训练足够的 epoch。另一个答案是关于类别预测、混淆矩阵和错误的激活函数,这些都与这个问题无关。

标签: javascript machine-learning tensorflow.js


【解决方案1】:

你的代码有两个问题:

  • 您没有正确设置learningRate 值。您需要将其作为第一个参数传递给 tf.train.adam() 函数
  • 您只学习5 epochs,这对您的情况来说还不够。

自己试试

我从您下面的代码中删除了不必要的代码。您可以更改epochslearning rate 值,以查看它如何影响5 的结果预测。我将纪元的默认值更改为50。预测结果非常接近5

document.querySelector('button').addEventListener('click', async () => {
  const learningRate = document.querySelector('#learning_rate').value;
  const epochs = document.querySelector('#epochs').value;

  const xArray = [0,1,2,3,4,5,6,7,8,9];
  const yArray = [0,1,2,3,4,5,6,7,8,9];

  const createModel = () => {
    const model = tf.sequential();
    model.add(tf.layers.dense({ inputShape: [1], units: 1, useBias: true }));
    model.add(tf.layers.dense({ units: 1, useBias: true }));
    return model;
  };

  const convertToTensor = () => {
    return tf.tidy(() => {
      const inputTensor = tf.tensor2d(xArray, [xArray.length, 1]);
      const outputTensor = tf.tensor2d(yArray, [yArray.length, 1]);
      return {
        inputs: inputTensor,
        outputs: outputTensor,
      };
    });
  };

  async function trainModel(model, inputs, trueValues) {
    model.compile({
      optimizer: tf.train.adam(learningRate),
      loss: tf.losses.meanSquaredError,
      metrics: ["mse"]
    });

    const batchSize = 2;

    return await model.fit(inputs, trueValues, {
      batchSize,
      epochs,
    });
  }

  function testModel(model, testValue) {
    return tf.tidy(() => model.predict(tf.tensor2d([testValue], [1, 1])));
  }

  const run = async testValue => {
    const model = createModel();
    const tensorData = convertToTensor();
    await trainModel(model, tensorData.inputs, tensorData.outputs);
    const prediction = testModel(model, testValue);
    console.log(`Predction for 5: ${prediction.toString()}`);
  };

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

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

【讨论】:

    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2022-07-29
    • 1970-01-01
    相关资源
    最近更新 更多