3. 加载输入数据、设置其格式并直观呈现

我们首先加载要用于训练模型的数据、设置其格式并让数据直观呈现出来。

我们将从为您托管的 JSON 文件中加载“cars”数据集。它包含与每辆指定汽车有关的多个不同特征。在本教程中,我们只希望提取有关马力和每加仑英里数的数据。

将以下代码添加到您的
script.js 文件中

  1. /**
  2. * Get the car data reduced to just the variables we are interested
  3. * and cleaned of missing data.
  4. */
  5. async function getData() {
  6. const carsDataResponse = await fetch('https://storage.googleapis.com/tfjs-tutorials/carsData.json');
  7. const carsData = await carsDataResponse.json();
  8. const cleaned = carsData.map(car => ({
  9. mpg: car.Miles_per_Gallon,
  10. horsepower: car.Horsepower,
  11. }))
  12. .filter(car => (car.mpg != null && car.horsepower != null));
  13. return cleaned;
  14. }

这样还会移除尚未定义每加仑的英里数或马力的所有条目。让我们以散点图的形式绘制这些数据,以查看其呈现样式。

将以下代码添加到
script.js 文件的底部。

  1. async function run() {
  2. // Load and plot the original input data that we are going to train on.
  3. const data = await getData();
  4. const values = data.map(d => ({
  5. x: d.horsepower,
  6. y: d.mpg,
  7. }));
  8. tfvis.render.scatterplot(
  9. {name: 'Horsepower v MPG'},
  10. {values},
  11. {
  12. xLabel: 'Horsepower',
  13. yLabel: 'MPG',
  14. height: 300
  15. }
  16. );
  17. // More code will be added below
  18. }
  19. document.addEventListener('DOMContentLoaded', run);

刷新页面时,您应该会在页面左侧看到一个面板,其中显示了数据的散点图。应如下所示。
image.png
此面板称为 visor,由 tfjs-vis 提供,可便利地直观显示数据形状。

通常,在处理数据时,建议您设法查看数据并在必要时清理这些数据。在本例,我们必须从 carsData 中移除某些不包含所有必填字段的条目。直观呈现数据可让我们了解模型可以学习的数据是否存在任何结构。
从上图中可以看出,马力与 MPG 之间成反比,也就是说,随着马力越大,汽车耗用一加仑汽油能行使的英里数通常越少。

注意:如果数据中不存在任何结构(模式),也就是说数据是随机的,那么模型实际上将学不到任何东西。

形成任务的概念

现在,输入数据将如下所示。

  1. ...
  2. {
  3. "mpg":15,
  4. "horsepower":165,
  5. },
  6. {
  7. "mpg":18,
  8. "horsepower":150,
  9. },
  10. {
  11. "mpg":16,
  12. "horsepower":150,
  13. },
  14. ...

我们的目标是训练一个模型,该模型将获取一个一个数字“马力”,并学习预测一个数字“每加仑的英里数”。请注意,这是一对一的映射,该映射对于下一部分非常重要。

我们将这些样本(马力和 MPG)提供给神经网络,神经网络将通过这些样本学习一个公式(或函数),以预测汽车在指定马力下的 MPG。这种通过从我们提供正确答案的样本来学习的方式称为监督式学习