TensorFlow.js - 根据 2D 数据进行预测

目录

index.html

script.js


index.html

<!DOCTYPE html>
<html>
<head>
  <title>TensorFlow.js Tutorial</title>

  <!-- Import TensorFlow.js -->
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>
  <!-- Import tfjs-vis -->
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis@1.0.2/dist/tfjs-vis.umd.min.js"></script>

  <!-- Import the main script file -->
  <script src="script.js"></script>

</head>

<body>
</body>
</html>


script.js

/**
 * Get the car data reduced to just the variables we are interested
 * and cleaned of missing data.     */

 // async function:https://www.runoob.com/w3cnote/es6-async.html
 async function getData() {

    /* const:https://www.runoob.com/js/js-let-const.html
     * fetch:发起请求   */
    const carsDataResponse = await fetch('https://storage.googleapis.com/tfjs-tutorials/carsData.json');

    // 数据格式转换为json文本
    const carsData = await carsDataResponse.json();

    // map() 方法返回一个新数组
    const cleaned = carsData.map(car => ({
      mpg: car.Miles_per_Gallon,
      horsepower: car.Horsepower,
    }))
    // filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素
    .filter(car => (car.mpg != null && car.horsepower != null));
  
    return cleaned;
  }

/*------------------------------------------------------------------------------------------------------------------*/

  async function run() {
    // Load and plot the original input data that we are going to train on.

    // 调用自己定义的getData()函数
    const data = await getData();

    const values = data.map(d => ({
      x: d.horsepower,
      y: d.mpg,
    }));
  
    // 绘制原始图像
    tfvis.render.scatterplot(
      {name: 'Horsepower v MPG'},
      {values},     // values上面定义的x、y数据
      {
        xLabel: 'Horsepower',
        yLabel: 'MPG',
        height: 300
      }
    );
  
    // Create the model
    const model = createModel();
    tfvis.show.modelSummary({name: 'Model Summary'}, model);

    // Convert the data to a form we can use for training.
    const tensorData = convertToTensor(data);
    const {inputs, labels} = tensorData;

    // Train the model
    await trainModel(model, inputs, labels);
    console.log('Done Training');

    // Make some predictions using the model and compare them to the original data 
    testModel(model, data, tensorData);
  }

  document.addEventListener('DOMContentLoaded', run);

/*------------------------------------------------------------------------------------------------------------------*/

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

/*------------------------------------------------------------------------------------------------------------------*/

  /**
 * Convert the input data to tensors that we can use for machine
 * learning. We will also do the important best practices of _shuffling_
 * the data and _normalizing_ the data
 * MPG on the y-axis.
 */
function convertToTensor(data) {
    // Wrapping these calculations in a tidy will dispose any
    // intermediate tensors.
  
    return tf.tidy(() => {
      // Step 1. 重排数据
      tf.util.shuffle(data);
  
      // Step 2. 转换为张量。张量:即有标签的数据
      const inputs = data.map(d => d.horsepower)
      const labels = data.map(d => d.mpg);
      const inputTensor = tf.tensor2d(inputs, [inputs.length, 1]);
      const labelTensor = tf.tensor2d(labels, [labels.length, 1]);
  
      //Step 3. 对数据进行0-1归一化
      const inputMax = inputTensor.max();
      const inputMin = inputTensor.min();
      const labelMax = labelTensor.max();
      const labelMin = labelTensor.min();
      const normalizedInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin));
      const normalizedLabels = labelTensor.sub(labelMin).div(labelMax.sub(labelMin));
  
      return {
        inputs: normalizedInputs,
        labels: normalizedLabels,
        // Return the min/max bounds so we can use them later.
        inputMax,
        inputMin,
        labelMax,
        labelMin,
      }
    });
  }

/*------------------------------------------------------------------------------------------------------------------*/

  async function trainModel(model, inputs, labels) {
    // Prepare the model for training.
    model.compile({

      // 这是用于控制模型更新的算法,我们选择了 Adam 优化器,因为它在实际使用中非常有效,无需进行任何配置。
      optimizer: tf.train.adam(),    

      // 这是一个函数,用于告知模型在学习所显示的各个批次(数据子集)时的表现如何。我们使用 meanSquaredError 将模型所做的预测与真实值进行比较
      loss: tf.losses.meanSquaredError, 

      metrics: ['mse'],
    });
  
    // 是指模型在每次训练迭代时会看到的数据子集的大小。常见的批次大小通常介于 32-512 之间
    const batchSize = 32;        
    
    // 表示模型查看您提供的整个数据集的次数。我们将对数据集执行 50 次迭代
    const epochs = 50;                  
  
    return await model.fit(inputs, labels, {
      batchSize,
      epochs,
      shuffle: true,
      callbacks: tfvis.show.fitCallbacks(
        { name: 'Training Performance' },
        ['loss', 'mse'],
        { height: 200, callbacks: ['onEpochEnd'] }
      )
    });
  }

/*------------------------------------------------------------------------------------------------------------------*/

  function testModel(model, inputData, normalizationData) {
    const {inputMax, inputMin, labelMin, labelMax} = normalizationData;
  
    // Generate predictions for a uniform range of numbers between 0 and 1;
    // We un-normalize the data by doing the inverse of the min-max scaling
    // that we did earlier.

    // .tidy() 函数用于执行给定的函数
    const [xs, preds] = tf.tidy(() => {
  
      // 生成 100 个新“样本”,以提供给模型
      const xs = tf.linspace(0, 1, 100);

      const preds = model.predict(xs.reshape([100, 1]));
  
      // 将数据恢复到原始范围
      const unNormXs = xs
        .mul(inputMax.sub(inputMin))
        .add(inputMin);
      const unNormPreds = preds
        .mul(labelMax.sub(labelMin))
        .add(labelMin);
  
      // Un-normalize the data
      return [unNormXs.dataSync(), unNormPreds.dataSync()];
    });
  
    const predictedPoints = Array.from(xs).map((val, i) => {
      return {x: val, y: preds[i]}
    });
  
    const originalPoints = inputData.map(d => ({
      x: d.horsepower, y: d.mpg,
    }));
  
    // 使用 tfjs-vis 来绘制 原始数据 和 模型的预测
    tfvis.render.scatterplot(
      {name: 'Model Predictions vs Original Data'},
      {values: [originalPoints, predictedPoints], series: ['original', 'predicted']},
      {
        xLabel: 'Horsepower',
        yLabel: 'MPG',
        height: 300
      }
    );
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow.js可以使用类似Keras的代码语法来训练模型,因此可以使用它来训练自己的图片数据集。以下是一个简单的例子,展示如何使用TensorFlow.js训练一个图像分类器: 1.首先,需要准备好自己的图片数据集,并将其转换为TensorFlow.js可以使用的格式。可以使用TensorFlow.js提供的ImageDataGenerator类来进行数据增强和格式转换。 2.然后,需要定义模型的结构和参数。可以使用TensorFlow.js提供的各种层和优化器来构建模型。 3.接下来,需要编写代码来训练模型。可以使用TensorFlow.js提供的fit方法来进行模型训练。 4.最后,可以使用训练好的模型来进行预测。 以下是一个简单的代码示例,展示如何使用TensorFlow.js训练一个图像分类器: ``` // 准备数据集 const imageDataset = tf.data.generator(function* () { // 加载图片数据集 const img1 = yield { src: 'path/to/image1.jpg', label: 'cat' }; const img2 = yield { src: 'path/to/image2.jpg', label: 'dog' }; // 将图片转换为TensorFlow.js可以使用的格式 const xs = tf.browser.fromPixels(img1).resizeNearestNeighbor([224, 224]).toFloat().div(tf.scalar(255)); const ys = tf.oneHot(tf.tensor1d([0]), 2); yield { xs, ys }; const xs = tf.browser.fromPixels(img2).resizeNearestNeighbor([224, 224]).toFloat().div(tf.scalar(255)); const ys = tf.oneHot(tf.tensor1d([1]),2); yield { xs, ys }; }); // 定义模型 const model = tf.sequential(); model.add(tf.layers.conv2d({ filters: 32, kernelSize: 3, activation: 'relu', inputShape: [224, 224, 3] })); model.add(tf.layers.maxPooling2d({ poolSize: 2 })); model.add(tf.layers.flatten()); model.add(tf.layers.dense({ units: 2, activation: 'softmax' })); model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] }); // 训练模型 const batchSize = 2; const epochs = 10; await model.fit(imageDataset.batch(batchSize), { epochs }); // 使用模型进行预测 const img3 = document.getElementById('image3'); const xs = tf.browser.fromPixels(img3).resizeNearestNeighbor([224, 224]).toFloat().div(tf.scalar(255)); const ys = model.predict(xs.reshape([1, 224, 224, 3])); console.log(ys); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值