【机器学习】在浏览器中训练模型

K210学习笔记——MaixHub在线训练模型(新版) 最近sipeed推出了新版的maixhub在线训练平台,因为前端时间比较忙没有时间来体验新版的功能,最近闲下来后赶忙来体验一下。新版maixhub对浏览器有要求的,比如不支持qq浏览器,推荐使用Chrome、MicrosoftEdge、Firefox、Safari等浏览器。点击这里跳转新版maixhub。新版maixhub主要有三大功能,分别是模型库(下载模型)、模型训练、和模型转换,模型库可以下载一些大佬训练效果比较好的模型,比如人脸识别、数字识别等,下载即用。httpshttpshttps。... 阅读详情

⭐️ 本文首发自 前端修罗场(点击加入),是一个由资深开发者独立运行的专业技术社区,我专注 Web 技术、答疑解惑、面试辅导以及职业发展。现在加入,私聊我即可获取一次免费的模拟面试机会,帮你评估知识点的掌握程度,获得更全面的学习指导意见,交个朋友,不走弯路,少吃亏!

最近公司在研发分布式高性能的云计算平台,其中涉及到了 AI 方面的处理。所以我也在自学 Machine Learning。不过在 AI 方面的知识却是需要花功夫花时间学习的。在学习的过程中我发现了一个不错的学习教程(点击收藏),推荐给大伙😋,我个人觉得这个教程讲解的通俗易懂,帮我省去了自己苦苦专研的时间,能够得到快速的进步。下一阶段,我也会在这里和大家分享我的学习笔记。


识别鸢尾花

本文将在浏览器中定义、训练和运行模型。 为了实现这一功能,我将构建一个识别鸢尾花的案例。

接下来,我们将创建一个神经网络。同时,根据开源数据集我们将鸢尾花分为三类:Setosa、Virginica 和 Versicolor。

每个机器学习项目的核心都是数据集。 我们需要采取的第一步是将这个数据集拆分为训练集和测试集。

这样做的原因是我们将使用我们的训练集来训练我们的算法和我们的测试集来检查我们的预测的准确性,以验证我们的模型是否可以使用或需要调整。

为了方便起见,我已经将训练集和测试集拆分为两个 JSON 文件:

  • 测试集: testing.json
[{"sepal_length":6,"sepal_width":2.9,"petal_length":4.5,"petal_width":1.5,"species":"versicolor"},{"sepal_length":5.2,"sepal_width":3.4,"petal_length":1.4,"petal_width":0.2,"species":"setosa"},{"sepal_length":6.5,"sepal_width":3,"petal_length":5.8,"petal_width":2.2,"species":"virginica"},{"sepal_length":5.9,"sepal_width":3.2,"petal_length":4.8,"petal_width":1.8,"species":"versicolor"},{"sepal_length":5.1,"sepal_width":3.8,"petal_length":1.9,"petal_width":0.4,"species":"setosa"},{"sepal_length":5.4,"sepal_width":3,"petal_length":4.5,"petal_width":1.5,"species":"versicolor"},{"sepal_length":7,"sepal_width":3.2,"petal_length":4.7,"petal_width":1.4,"species":"versicolor"},{"sepal_length":5.7,"sepal_width":2.8,"petal_length":4.5,"petal_width":1.3,"species":"versicolor"},{"sepal_length":5.1,"sepal_width":2.5,"petal_length":3,"petal_width":1.1,"species":"versicolor"},{"sepal_length":4.9,"sepal_width":2.4,"petal_length":3.3,"petal_width":1,"species":"versicolor"},{"sepal_length":5.1,"sepal_width":3.7,"petal_length":1.5,"petal_width":0.4,"species":"setosa"},{"sepal_length":5.7,"sepal_width":2.8,"petal_length":4.1,"petal_width":1.3,"species":"versicolor"},{"sepal_length":5.6,"sepal_width":3,"petal_length":4.5,"petal_width":1.5,"species":"versicolor"},{"sepal_length":6.1,"sepal_width":3,"petal_length":4.6,"petal_width":1.4,"species":"versicolor"}]
  • 训练集: training.json
[{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.9,"sepal_width":3,"petal_length":1.4,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.7,"sepal_width":3.2,"petal_length":1.3,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.6,"sepal_width":3.1,"petal_length":1.5,"petal_width":0.2,"species":"setosa"},{"sepal_length":5,"sepal_width":3.6,"petal_length":1.4,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.6,"sepal_width":3.4,"petal_length":1.4,"petal_width":0.3,"species":"setosa"},{"sepal_length":5,"sepal_width":3.4,"petal_length":1.5,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.4,"sepal_width":2.9,"petal_length":1.4,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.9,"sepal_width":3.1,"petal_length":1.5,"petal_width":0.1,"species":"setosa"},{"sepal_length":5.4,"sepal_width":3.7,"petal_length":1.5,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.8,"sepal_width":3.4,"petal_length":1.6,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.8,"sepal_width":3,"petal_length":1.4,"petal_width":0.1,"species":"setosa"},{"sepal_length":4.3,"sepal_width":3,"petal_length":1.1,"petal_width":0.1,"species":"setosa"},{"sepal_length":5.8,"sepal_width":4,"petal_length":1.2,"petal_width":0.2,"species":"setosa"},{"sepal_length":5.7,"sepal_width":4.4,"petal_length":1.5,"petal_width":0.4,"species":"setosa"},{"sepal_length":5.4,"sepal_width":3.9,"petal_length":1.3,"petal_width":0.4,"species":"setosa"},{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4,"petal_width":0.3,"species":"setosa"},{"sepal_length":5.7,"sepal_width":3.8,"petal_length":1.7,"petal_width":0.3,"species":"setosa"},{"sepal_length":5.1,"sepal_width":3.8,"petal_length":1.5,"petal_width":0.3,"species":"setosa"},{"sepal_length":5.4,"sepal_width":3.4,"petal_length":1.7,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.6,"sepal_width":3.6,"petal_length":1,"petal_width":0.2,"species":"setosa"},{"sepal_length":5.1,"sepal_width":3.3,"petal_length":1.7,"petal_width":0.5,"species":"setosa"},{"sepal_length":4.8,"sepal_width":3.4,"petal_length":1.9,"petal_width":0.2,"species":"setosa"},{"sepal_length":5,"sepal_width":3,"petal_length":1.6,"petal_width":0.2,"species":"setosa"},{"sepal_length":5,"sepal_width":3.4,"petal_length":1.6,"petal_width":0.4,"species":"setosa"},{"sepal_length":5.2,"sepal_width":3.5,"petal_length":1.5,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.7,"sepal_width":3.2,"petal_length":1.6,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.8,"sepal_width":3.1,"petal_length":1.6,"petal_width":0.2,"species":"setosa"},{"sepal_length":5.4,"sepal_width":3.4,"petal_length":1.5,"petal_width":0.4,"species":"setosa"},{"sepal_length":5.2,"sepal_width":4.1,"petal_length":1.5,"petal_width":0.1,"species":"setosa"},{"sepal_length":5.5,"sepal_width":4.2,"petal_length":1.4,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.9,"sepal_width":3.1,"petal_length":1.5,"petal_width":0.1,"species":"setosa"},{"sepal_length":5,"sepal_width":3.2,"petal_length":1.2,"petal_width":0.2,"species":"setosa"},{"sepal_length":5.5,"sepal_width":3.5,"petal_length":1.3,"petal_width":0.2,"species":"setosa"},{"sepal_length":4.9,"sepal_width":3.1,"petal_length":1.5,"petal_width":0.1,"species":"setosa"},{"sepal_length":4.4,"sepal_width":3,"petal_length":1.3,"petal_width":0.2,"species":"setosa"},{"sepal_length":5.1,"sepal_width":3.4,"petal_length":1.5,"petal_width":0.2,"species":"setosa"},{"sepal_length":5,"sepal_width":3.5,"petal_length":1.3,"petal_width":0.3,"species":"setosa"},{"sepal_length":4.5,"sepal_width":2.3,"petal_length":1.3,"petal_width":0.3,"species":"setosa"},{"sepal_length":4.4,"sepal_width":3.2,"petal_length":1.3,"petal_width":0.2,"species":"setosa"},{"sepal_length":5,"sepal_width":3.5,"petal_length":1.6,"petal_width":0.6,"species":"setosa"},{"sepal_length":4.8,"sepal_width":3,"petal_length":1.4,"petal_width":0.3,"species":"setosa"},{"sepal_length":5.1,"sepal_width":3.8,"petal_length":1.6,"petal_width":0.2,"species":"setosa"},{"sepal_length":5.3,"sepal_width":3.7,"petal_length":1.5,"petal_width":0.2,"species":"setosa"},{"sepal_length":5,"sepal_width":3.3,"petal_length":1.4,"petal_width":0.2,"species":"setosa"},{"sepal_length":6.4,"sepal_width":3.2,"petal_length":4.5,"petal_width":1.5,"species":"versicolor"},{"sepal_length":5.5,"sepal_width":2.3,"petal_length":4,"petal_width":1.3,"species":"versicolor"},{"sepal_length":6.5,"sepal_width":2.8,"petal_length":4.6,"petal_width":1.5,"species":"versicolor"},{"sepal_length":6.3,"sepal_width":3.3,"petal_length":4.7,"petal_width":1.6,"species":"versicolor"},{"sepal_length":6.6,"sepal_width":2.9,"petal_length":4.6,"petal_width":1.3,"species":"versicolor"},{"sepal_length":5.2,"sepal_width":2.7,"petal_length":3.9,"petal_width":1.4,"species":"versicolor"},{"sepal_length":5,"sepal_width":2,"petal_length":3.5,"petal_width":1,"species":"versicolor"},{"sepal_length":5.9,"sepal_width":3,"petal_length":4.2,"petal_width":1.5,"species":"versicolor"},{"sepal_length":6,"sepal_width":2.2,"petal_length":4,"petal_width":1,"species":"versicolor"},{"sepal_length":6.1,"sepal_width":2.9,"petal_length":4.7,"petal_width":1.4,"species":"versicolor"},{"sepal_length":5.6,"sepal_width":2.9,"petal_length":3.6,"petal_width":1.3,"species":"versicolor"},{"sepal_length":6.7,"sepal_width":3.1,"petal_length":4.4,"petal_width":1.4,"species":"versicolor"},{"sepal_length":5.8,"sepal_width":2.7,"petal_length":4.1,"petal_width":1,"species":"versicolor"},{"sepal_length":6.2,"sepal_width":2.2,"petal_length":4.5,"petal_width":1.5,"species":"versicolor"},{"sepal_length":5.6,"sepal_width":2.5,"petal_length":3.9,"petal_width":1.1,"species":"versicolor"},{"sepal_length":6.1,"sepal_width":2.8,"petal_length":4,"petal_width":1.3,"species":"versicolor"},{"sepal_length":6.3,"sepal_width":2.5,"petal_length":4.9,"petal_width":1.5,"species":"versicolor"},{"sepal_length":6.1,"sepal_width":2.8,"petal_length":4.7,"petal_width":1.2,"species":"versicolor"},{"sepal_length":6.4,"sepal_width":2.9,"petal_length":4.3,"petal_width":1.3,"species":"versicolor"},{"sepal_length":6.6,"sepal_width":3,"petal_length":4.4,"petal_width":1.4,"species":"versicolor"},{"sepal_length":6.8,"sepal_width":2.8,"petal_length":4.8,"petal_width":1.4,"species":"versicolor"},{"sepal_length":6.7,"sepal_width":3,"petal_length":5,"petal_width":1.7,"species":"versicolor"},{"sepal_length":5.7,"sepal_width":2.6,"petal_length":3.5,"petal_width":1,"species":"versicolor"},{"sepal_length":5.5,"sepal_width":2.4,"petal_length":3.8,"petal_width":1.1,"species":"versicolor"},{"sepal_length":5.5,"sepal_width":2.4,"petal_length":3.7,"petal_width":1,"species":"versicolor"},{"sepal_length":5.8,"sepal_width":2.7,"petal_length":3.9,"petal_width":1.2,"species":"versicolor"},{"sepal_length":6,"sepal_width":2.7,"petal_length":5.1,"petal_width":1.6,"species":"versicolor"},{"sepal_length":6,"sepal_width":3.4,"petal_length":4.5,"petal_width":1.6,"species":"versicolor"},{"sepal_length":6.7,"sepal_width":3.1,"petal_length":4.7,"petal_width":1.5,"species":"versicolor"},{"sepal_length":6.3,"sepal_width":2.3,"petal_length":4.4,"petal_width":1.3,"species":"versicolor"},{"sepal_length":5.6,"sepal_width":3,"petal_length":4.1,"petal_width":1.3,"species":"versicolor"},{"sepal_length":5.5,"sepal_width":2.5,"petal_length":4,"petal_width":1.3,"species":"versicolor"},{"sepal_length":5.5,"sepal_width":2.6,"petal_length":4.4,"petal_width":1.2,"species":"versicolor"},{"sepal_length":5.8,"sepal_width":2.6,"petal_length":4,"petal_width":1.2,"species":"versicolor"},{"sepal_length":5,"sepal_width":2.3,"petal_length":3.3,"petal_width":1,"species":"versicolor"},{"sepal_length":5.6,"sepal_width":2.7,"petal_length":4.2,"petal_width":1.3,"species":"versicolor"},{"sepal_length":5.7,"sepal_width":3,"petal_length":4.2,"petal_width":1.2,"species":"versicolor"},{"sepal_length":6.2,"sepal_width":2.9,"petal_length":4.3,"petal_width":1.3,"species":"versicolor"},{"sepal_length":6.3,"sepal_width":3.3,"petal_length":6,"petal_width":2.5,"species":"virginica"},{"sepal_length":5.8,"sepal_width":2.7,"petal_length":5.1,"petal_width":1.9,"species":"virginica"},{"sepal_length":7.1,"sepal_width":3,"petal_length":5.9,"petal_width":2.1,"species":"virginica"},{"sepal_length":6.3,"sepal_width":2.9,"petal_length":5.6,"petal_width":1.8,"species":"virginica"},{"sepal_length":7.6,"sepal_width":3,"petal_length":6.6,"petal_width":2.1,"species":"virginica"},{"sepal_length":4.9,"sepal_width":2.5,"petal_length":4.5,"petal_width":1.7,"species":"virginica"},{"sepal_length":7.3,"sepal_width":2.9,"petal_length":6.3,"petal_width":1.8,"species":"virginica"},{"sepal_length":6.7,"sepal_width":2.5,"petal_length":5.8,"petal_width":1.8,"species":"virginica"},{"sepal_length":7.2,"sepal_width":3.6,"petal_length":6.1,"petal_width":2.5,"species":"virginica"},{"sepal_length":6.5,"sepal_width":3.2,"petal_length":5.1,"petal_width":2,"species":"virginica"},{"sepal_length":6.4,"sepal_width":2.7,"petal_length":5.3,"petal_width":1.9,"species":"virginica"},{"sepal_length":6.8,"sepal_width":3,"petal_length":5.5,"petal_width":2.1,"species":"virginica"},{"sepal_length":5.7,"sepal_width":2.5,"petal_length":5,"petal_width":2,"species":"virginica"},{"sepal_length":5.8,"sepal_width":2.8,"petal_length":5.1,"petal_width":2.4,"species":"virginica"},{"sepal_length":6.4,"sepal_width":3.2,"petal_length":5.3,"petal_width":2.3,"species":"virginica"},{"sepal_length":6.5,"sepal_width":3,"petal_length":5.5,"petal_width":1.8,"species":"virginica"},{"sepal_length":7.7,"sepal_width":3.8,"petal_length":6.7,"petal_width":2.2,"species":"virginica"},{"sepal_length":7.7,"sepal_width":2.6,"petal_length":6.9,"petal_width":2.3,"species":"virginica"},{"sepal_length":6,"sepal_width":2.2,"petal_length":5,"petal_width":1.5,"species":"virginica"},{"sepal_length":6.9,"sepal_width":3.2,"petal_length":5.7,"petal_width":2.3,"species":"virginica"},{"sepal_length":5.6,"sepal_width":2.8,"petal_length":4.9,"petal_width":2,"species":"virginica"},{"sepal_length":7.7,"sepal_width":2.8,"petal_length":6.7,"petal_width":2,"species":"virginica"},{"sepal_length":6.3,"sepal_width":2.7,"petal_length":4.9,"petal_width":1.8,"species":"virginica"},{"sepal_length":6.7,"sepal_width":3.3,"petal_length":5.7,"petal_width":2.1,"species":"virginica"},{"sepal_length":7.2,"sepal_width":3.2,"petal_length":6,"petal_width":1.8,"species":"virginica"},{"sepal_length":6.2,"sepal_width":2.8,"petal_length":4.8,"petal_width":1.8,"species":"virginica"},{"sepal_length":6.1,"sepal_width":3,"petal_length":4.9,"petal_width":1.8,"species":"virginica"},{"sepal_length":6.4,"sepal_width":2.8,"petal_length":5.6,"petal_width":2.1,"species":"virginica"},{"sepal_length":7.2,"sepal_width":3,"petal_length":5.8,"petal_width":1.6,"species":"virginica"},{"sepal_length":7.9,"sepal_width":3.8,"petal_length":6.4,"petal_width":2,"species":"virginica"},{"sepal_length":6.4,"sepal_width":2.8,"petal_length":5.6,"petal_width":2.2,"species":"virginica"},{"sepal_length":6.3,"sepal_width":2.8,"petal_length":5.1,"petal_width":1.5,"species":"virginica"},{"sepal_length":6.1,"sepal_width":2.6,"petal_length":5.6,"petal_width":1.4,"species":"virginica"},{"sepal_length":7.7,"sepal_width":3,"petal_length":6.1,"petal_width":2.3,"species":"virginica"},{"sepal_length":6.3,"sepal_width":3.4,"petal_length":5.6,"petal_width":2.4,"species":"virginica"},{"sepal_length":6.4,"sepal_width":3.1,"petal_length":5.5,"petal_width":1.8,"species":"virginica"},{"sepal_length":6,"sepal_width":3,"petal_length":4.8,"petal_width":1.8,"species":"virginica"},{"sepal_length":6.9,"sepal_width":3.1,"petal_length":5.4,"petal_width":2.1,"species":"virginica"},{"sepal_length":6.7,"sepal_width":3.1,"petal_length":5.6,"petal_width":2.4,"species":"virginica"},{"sepal_length":6.9,"sepal_width":3.1,"petal_length":5.1,"petal_width":2.3,"species":"virginica"},{"sepal_length":5.8,"sepal_width":2.7,"petal_length":5.1,"petal_width":1.9,"species":"virginica"},{"sepal_length":6.8,"sepal_width":3.2,"petal_length":5.9,"petal_width":2.3,"species":"virginica"},{"sepal_length":6.7,"sepal_width":3.3,"petal_length":5.7,"petal_width":2.5,"species":"virginica"},{"sepal_length":6.7,"sepal_width":3,"petal_length":5.2,"petal_width":2.3,"species":"virginica"},{"sepal_length":6.3,"sepal_width":2.5,"petal_length":5,"petal_width":1.9,"species":"virginica"},{"sepal_length":6.5,"sepal_width":3,"petal_length":5.2,"petal_width":2,"species":"virginica"},{"sepal_length":6.2,"sepal_width":3.4,"petal_length":5.4,"petal_width":2.3,"species":"virginica"}]

其中,训练集包含 130 个项目,测试集包含 14 个。如果你看看这些数据是什么样子,你会看到如下内容:

{
  "sepal_length": 5.1,
  "sepal_width": 3.5,
  "petal_length": 1.4,
  "petal_width": 0.2,
  "species": "setosa"
}

我们可以看到萼片和花瓣的长度和宽度的四个不同特征,以及物种的标签。

为了能够将它与 Tensorflow.js 一起使用,我们需要将这些数据塑造成框架能够理解的格式,在这种情况下,对于训练数据,它将是 [130, 4] 的 130 个样本,每个样本有四个特征。

import * as trainingSet from "training.json";
import * as testSet from "testing.json";

const trainingData = tf.tensor2d(
  trainingSet.map(item => [
    item.sepal_length,
    item.sepal_width,
    item.petal_length,
    item.petal_width
  ]),
  [130, 4]
);

const testData = tf.tensor2d(
  testSet.map(item => [
    item.sepal_length,
    item.sepal_width,
    item.petal_length,
    item.petal_width
  ]),
  [14, 4]
);

接下来,我们还需要对输出数据进行整形:

const output = tf.tensor2d(trainingSet.map(item => [
    item.species === 'setosa' ? 1 : 0,
    item.species === 'virginica' ? 1 : 0,
    item.species === 'versicolor' ? 1 : 0

]), [130,3])

然后,一旦我们的数据准备就绪,我们就可以继续创建模型:

const model = tf.sequential();

model.add(tf.layers.dense(
    {
        inputShape: 4,
        activation: 'sigmoid',
        units: 10
    }
));

model.add(tf.layers.dense(
    {
        inputShape: 10,
        units: 3,
        activation: 'softmax'
    }
));

在上面的代码示例中,我们首先实例化一个顺序模型,添加一个输入和输出层。

你可以看到内部使用的参数(inputShape, activation, and units)超出了本文的范围,因为它们可能会根据你创建的模型、使用的数据类型等而有所不同。

一旦我们的模型准备就绪,我们就可以使用我们的数据对其进行训练:

async function train_data(){
    for(let i=0;i<15;i++){
      const res = await model.fit(trainingData, outputData,{epochs: 40});
    }
}

async function main() {
  await train_data();
  model.predict(testSet).print();
}

如果这运作良好,你可以开始用自定义用户输入替换测试数据。

一旦我们调用我们的 main 函数,预测的输出将看起来像以下三个选项之一:

[1,0,0] // Setosa
[0,1,0] // Virginica
[0,0,1] // Versicolor

预测返回一个由三个数字组成的数组,表示数据属于三个类别之一的概率。 最接近 1 的数字是最高预测值。

例如,如果分类的输出为 [0.0002, 0.9494, 0.0503],则数组的第二个元素最高,因此模型预测新的输入很可能是 Virginica。

这就是 Tensorflow.js 中的简单神经网络!

我们只讨论了 Irises 的一个小数据集,但如果您想继续使用更大的数据集或处理图像,步骤将是相同的:

  1. 收集数据;
  2. 在训练集和测试集之间拆分;
  3. 重新格式化数据以便 Tensorflow.js 可以理解它;
  4. 选择你的算法;
  5. 拟合数据;
  6. 预测。

如果你想保存创建的模型以便能够在另一个应用程序中加载它并预测新数据,你可以使用以下行来执行此操作:

await model.save('file:///path/to/my-model'); // in Node.js

完整代码

  • index.html
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Tensorflow.js</title>
	<link rel="stylesheet" href="src/styles.css">
</head>

<body>
	<h1>使用 Tensorflow.js 在 JavaScript 中定义、训练和运行机器学习模型</h1>

	<section class="data-inputs">
		<h3>鸢尾花分类</h3>
		<p>正在训练中...</p>
		<p class="training-steps"></p>
		<div class="input-block">
			<label for="sepal-length">Sepal lenth:</label>
			<input name="sepal-length" type="number" min="0" max="100" placeholder="1.5">
		</div>

		<div class="input-block">
			<label for="sepal-width">Sepal width:</label>
			<input name="sepal-width" type="number" min="0" max="100" placeholder="0.4">
		</div>

		<div class="input-block">
			<label for="petal-length">Petal length:</label>
			<input name="petal-length" type="number" min="0" max="100" placeholder="1.0">
		</div>

		<div class="input-block">
			<label for="petal-width">Petal width:</label>
			<input name="petal-width" type="number" min="0" max="100" placeholder="0.7">
		</div>

		<button class="predict" disabled>预测</button>
	</section>

	<section class="prediction-block">
		<p>鸢尾花 预测:</p>
		<p class="prediction"></p>
	</section>

	<script src="src/index.js"></script>
</body>

</html>
  • index.js
import * as tf from "@tensorflow/tfjs";
import trainingSet from "./training.json";
import testSet from "./testing.json";

let trainingData, testingData, outputData, model;
let training = true;
let predictButton = document.getElementsByClassName("predict")[0];

const init = async () => {
  splitData();
  createModel();
  await trainData();
  if (!training) {
    predictButton.disabled = false;
    predictButton.onclick = () => {
      const inputData = getInputData();
      predict(inputData);
    };
  }
};

const splitData = () => {
  trainingData = tf.tensor2d(
    trainingSet.map(item => [
      item.sepal_length,
      item.sepal_width,
      item.petal_length,
      item.petal_width
    ]),
    [130, 4]
  );

  testingData = tf.tensor2d(
    testSet.map(item => [
      item.sepal_length,
      item.sepal_width,
      item.petal_length,
      item.petal_width
    ]),
    [14, 4]
  );

  outputData = tf.tensor2d(
    trainingSet.map(item => [
      item.species === "setosa" ? 1 : 0,
      item.species === "virginica" ? 1 : 0,
      item.species === "versicolor" ? 1 : 0
    ]),
    [130, 3]
  );
};

const createModel = () => {
  model = tf.sequential();
  model.add(
    tf.layers.dense({ inputShape: 4, activation: "sigmoid", units: 10 })
  );

  model.add(
    tf.layers.dense({
      inputShape: 10,
      units: 3,
      activation: "softmax"
    })
  );

  model.compile({
    loss: "categoricalCrossentropy",
    optimizer: tf.train.adam()
  });
};

const trainData = async () => {
  let numSteps = 15;
  let trainingStepsDiv = document.getElementsByClassName("training-steps")[0];
  for (let i = 0; i < numSteps; i++) {
    let res = await model.fit(trainingData, outputData, { epochs: 40 });
    trainingStepsDiv.innerHTML = `Training step: ${i}/${numSteps - 1}, loss: ${
      res.history.loss[0]
    }`;
    if (i === numSteps - 1) {
      training = false;
    }
  }
};

const predict = async inputData => {
  for (let [key, value] of Object.entries(inputData)) {
    inputData[key] = parseFloat(value);
  }
  inputData = [inputData];

  let newDataTensor = tf.tensor2d(
    inputData.map(item => [
      item.sepal_length,
      item.sepal_width,
      item.petal_length,
      item.petal_width
    ]),
    [1, 4]
  );

  let prediction = model.predict(newDataTensor);

  displayPrediction(prediction);
};

const getInputData = () => {
  let sepalLength = document.getElementsByName("sepal-length")[0].value;
  let sepalWidth = document.getElementsByName("sepal-width")[0].value;
  let petalLength = document.getElementsByName("petal-length")[0].value;
  let petalWidth = document.getElementsByName("petal-width")[0].value;

  return {
    sepal_length: sepalLength,
    sepal_width: sepalWidth,
    petal_length: petalLength,
    petal_width: petalWidth
  };
};

const displayPrediction = prediction => {
  let predictionDiv = document.getElementsByClassName("prediction")[0];
  let predictionSection = document.getElementsByClassName(
    "prediction-block"
  )[0];

  let maxProbability = Math.max(...prediction.dataSync());
  let predictionIndex = prediction.dataSync().indexOf(maxProbability);
  let irisPrediction;

  switch (predictionIndex) {
    case 0:
      irisPrediction = "Setosa";
      break;
    case 1:
      irisPrediction = "Virginica";
      break;
    case 2:
      irisPrediction = "Versicolor";
      break;
    default:
      irisPrediction = "";
      break;
  }
  predictionDiv.innerHTML = irisPrediction;
  predictionSection.style.display = "block";
};

init();
  • styles.css
body {
  font-family: "Avenir";
}

h1 {
  text-align: center;
  width: 80%;
  margin: 0 auto;
}

.data-inputs {
  display: block;
  width: 80%;
  margin: 0 auto;
}

.input-block {
  display: inline-block;
  width: fit-content;
  margin: 1em 0.5em 2em 0.5em;
}

.input-block:first-of-type {
  margin-left: 0;
}

.input-block input {
  width: 7em;
  height: 2em;
}

.input-block input::placeholder {
  color: rgba(0, 0, 0, 0.3);
}

button {
  display: block;
  padding: 0.5em 1em;
  border-radius: 5px;
  font-size: 14px;
}

.prediction-block {
  display: none;
  width: 80%;
  margin: 0 auto;
}
  • package.json
{
  "name": "Irises Classficaton",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "scripts": {
    "start": "parcel index.html --open",
    "build": "parcel build index.html"
  },
  "dependencies": {
    "@tensorflow/tfjs": "1.1.2"
  },
  "devDependencies": {
    "@babel/core": "7.2.0",
    "parcel-bundler": "^1.6.1"
  },
  "keywords": []
}

效果如下:

在这里插入图片描述

如果你觉得这篇文章还不错,请点击下方小红心 👍🏻 ❤️,鼓励一下!我会继续为你带来优质的内容~我是前端修罗场,一个独立运行的专业技术社区,感谢你关注我!

YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases) 如有错误,恳请指出。与其说是yolov5的训练技巧,这篇博客更多的记录如何使用wandb这个在线模型训练可视化工具,感受到了yolov5作者对其的充分喜爱。所以下面内容更多的记录下如何最简单的使用这个工具,而不是在介绍他在yolov5中的使用,后者具体可以见官方资料:Weights & Biases with YOLOv5Wandb是Weights & Biases的缩写,这款工具能够帮助跟踪你的机器学习项目。它能够自动记录模型训练过程中的超参数和输出指标,然后可视化和比较结果,并快速与同事共享结果。(感受 阅读详情

相关推荐

23、机器学习模型部署与浏览器端AI技术解析

本文深入探讨了机器学习模型的部署流程与浏览器端AI技术的应用。内容涵盖机器学习管道、Fairing和KubeFlow等部署工具的使用,对比了推理即服务与自建堆栈在成本和性能上的差异,并介绍了JMeter性能测试方法。在浏览器端AI部分,详细解析了TensorFlow.js、ml5.js、ONNX.js等多个JavaScript深度学习库的发展与架构,展示了如何在浏览器中运行预训练模型、进行实时交互及模型训练。文章还提供了人体姿态检测、手绘涂鸦转图像等实际应用案例,展望了浏览器端AI的未来发展方向。

pp12345的博客 79

Tensorflow学习笔记(九)TensorFlow Object Detection API 训练并调用自己的模型

TensorFlow Object Detection API 训练并调用自己的模型1.准备训练样本并标注安装使用labelImg工具标注 生成xml文件labelImg工具的安装使用labelImg工具标注并生成xml文件把xml文件转换成 TFRecords Format 格式把xml文件转换成csv表格把csv文件转换成 TFRecords Format 格式使用自己的数据集训练模型调用验证...

学习小白 3871

Android 的端智能学习路径

基础层:先补齐 Android 进阶(JNI / 性能优化)和 AI 基础概念,无需深入算法开发;核心层:掌握 TFLite 的模型加载、推理、量化,能完成基础端侧推理;落地层:聚焦实战场景(图像 / OCR / 语音),解决工程化问题(模型管理、异常处理);优化层:通过模型量化、硬件加速、工程优化,提升推理性能和用户体验。

hqiangtai的博客 349

在设计GUI时,如何调用已训练好的深度学习模型,作为api接口实现一定功能?

在设计GUI时,如何调用已训练好的深度学习模型,作为api接口实现一定功能?

weixin_44511755的博客 1319

保姆级教程:用Labelme标注车辆关键点,并一键转成YOLOv8-pose训练格式

本文提供了一份详细的车辆底盘关键点标注教程,从使用Labelme进行标注到一键转换为YOLOv8-pose训练格式的全流程指南。特别针对工业检测和自动驾驶场景,分享了标注规范、格式转换脚本及常见问题解决方案,帮助开发者高效准备训练数据并优化模型性能。

weixin_27419879的博客 314

好书分享:Control and Estimation with MATLAB(5th Edition)pdf

基于MATLAB的控制与估计》(第5版)由八章组成,包括控制算法和设计。主要内容包括状态估计和数据平滑、RLS和卡尔曼滤波器状态估计、控制系统设计、自适应控制和PID。书中的概念已经使用MATLAB和Simulink实现。提供了可下载的MATLAB和Simulink文件。适合人群:大学生、研究生、控制类工程研究人员。需要电子版pdf,回复索取。

jixianhu的博客 1515

嵌入不同源的页面_神经网络嵌入的解释,非常有用的东西,一看就明白

作者:Will Koehrsen编译:ronghuaiyang导读深度学习的一个显著成功应用是嵌入,这是一种将离散变量表示为连续向量的方法。这项技术已经有了实际的应用,其中有在机器翻译中使用词嵌入和类别变量中使用实体嵌入。近年来,神经网络在图像分割、自然语言处理、时间序列预测等方面的应用有了很大的发展。深度学习的一个显著成功应用是嵌入,这是一种将离散变量表示为连续向量的方法。这项技术已经有了实际的...

weixin_39557199的博客 241

多传感器融合SLAM指南:如何结合视觉与激光雷达提升建图精度

本文深入探讨了多传感器融合SLAM技术,详细解析了如何将视觉传感器与激光雷达(特别是3D激光雷达)进行深度融合以提升建图精度与鲁棒性。文章从传感器特性分析、数据同步标定、前端融合策略(松耦合与紧耦合)到后端优化与地图构建,提供了完整的实战指南,并重点介绍了V-LOAM等经典融合框架的核心思想,旨在帮助开发者构建适应复杂动态环境的高精度SLAM系统。

weixin_29056145的博客 549

开源项目推荐:ml-classifier

开源项目推荐:ml-classifier ml-classifier 是一个开源项目,旨在提供一个在浏览器中快速训练图像分类模型的机器学习引擎。该项目主要使用 JavaScript 编程语言,并依赖于 TensorFlow.js 来实现核心的机器学习功能。 项目基础介绍 ml-classifier 提供了一个简单易用的界面,允许用户上传数据,训练模型,并查看结果。它的设计初衷是为了提供一个轻量级的...

gitblog_00350的博客 282

tuts4you-lena151-Crackme-tutorial06

一、前言      本次研究的是lena151-Crackme-tutorial06,也是一个应用程序,但因为平台迭代的关系,程序不能正常运行,只能完成部分破解。 (一)·本次我们要破解的目标是个应用程序,如下图 (二)、使用工具:x32dbg 二、过程 这个程序的破解的方法很多,首先最简单的一种不外乎就是搜索字符串,我就按照这个思路来完成。 (一)查找字符串&lt;Unregis...

h123120的专栏 524

我终于停止写 JUnit 了!用 JavaParser + GPT-4 自动生成 90% 覆盖率的单元测试

本文介绍了一种基于JavaParser和GPT-4的智能单元测试生成方案。针对开发者在单元测试编写中面临的繁琐、无聊和硬指标等问题,提出通过AST语法树分析代码结构,提取关键信息后生成精准提示词,再调用GPT-4自动生成高质量的单元测试代码。文章详细阐述了技术原理、实现步骤(包括JavaParser解析、上下文感知提示词构建和GPT-4调用),并展示了该方法能自动识别依赖、Mock对象和覆盖边界条件,显著提升测试效率。同时指出了在复杂数据结构、私有方法测试等方面的局限性,最终强调这种技术组合可以解放开发者,

BUG猿的博客 1133

ML Classifier:浏览器中的图像分类利器

ML Classifier:浏览器中的图像分类利器 项目介绍 ML Classifier 是一个专为浏览器设计的机器学习引擎,旨在帮助开发者快速训练图像分类模型。通过这个项目,用户可以在浏览器中直接进行模型的训练、保存和预测,无需复杂的配置和环境搭建。项目特别适合与 ml-classifier-ui 结合使用,后者提供了一个基于 React 的 Web 前端,用于上传数据和查看结果。 项目技术分析...

gitblog_00735的博客 638

04-协议基础03:USB 2.0信号与电气特性

USB 2.0使用两根差分信号线D+/D-进行数据传输,支持低速(1.5Mbps)、全速(12Mbps)和高速(480Mbps)三种速率。USB总线为主从模式,所有事务由Host发起。端电阻值作用HostRpd(D+)15KΩ±5%下拉,默认低电平HostRpd(D-)15KΩ±5%下拉,默认低电平全速设备Rpu(D+)1.5KΩ±5%上拉到3.3V,标识全速低速设备Rpu(D-)1.5KΩ±5%上拉到3.3V,标识低速USB 2.0硬件要点。

内核笔记 123

下一代机器学习-在浏览器中训练深度学习模型Next Generation Machine Learning - Training Deep Learning Models in a Browser

We recently caught up with Andrej Karpathy, Machine Learning PhD student at Stanford and the man behind the innovative ConvNetJS - a JS library for training Deep Learning models (mainly Neural Netwo

GarfieldEr007的专栏 3657

RK3568平台(USB篇)USB HUB集线器

hub,集线器,连接在host与device之间的一种用于usb接口扩展的usb设备。可以将一个usb上行接口扩展为多个下行接口,使得一个host可以同时与多个device连接。一般来说,一块hub桥接芯片可扩展4个usb接口,而市面上的一拖七hub,其实使用了两块hub桥接芯片,其中一块hub桥接芯片的上行端口连接到另一块hub桥接芯片的下行端口。hub的上行端口面向host,下行端口面向device。在下行端口上,hub提供了设备接入检测和设备移除检测的能力,并给下行端口供电。

weixin_49303682的博客 969

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

浏览器中的机器学习:使用预训练模型

在上一篇文章《浏览器中的手写数字识别》中,讲到在浏览器中训练出一个卷积神经网络模型,用来识别手写数字。值得注意的是,这个训练过程是在浏览器中完成的,使用的是客户端的资源。 虽然TensorFlow.js的愿景是机器学习无处不在,即使是在手机、嵌入式设备上,只要运行有浏览器,都可以训练人工智能模型,但是考虑到手机、嵌入式设备有限的计算能力(虽然手机性能不断飞跃),复杂的人工智能模型还是交给更为强大的...

weixin_33830216的博客 182

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

TensorFlow.js 浏览器端机器学习实战:从模型推理到端侧训练

端侧推理与浏览器端机器学习正在成为前端智能化的重要方向。其核心原理是将模型计算下沉到用户设备,利用 WebGL 或 WebGPU 调用本地 GPU 完成张量运算,从而避免数据上传云端带来的隐私风险与网络延迟。这种方案的技术价值在于低延迟、数据不出端、部署成本低,尤其适合实时视频处理、隐私敏感场景和个性化小模型训练。TensorFlow.js 作为浏览器端机器学习的代表框架,支持模型加载、推理与迁移学习,并可通过 Web Worker 隔离计算、手动管理张量内存来优化性能。

weixin_32830601的博客 232

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

如何快速上手ML2Scratch:面向初学者的完整机器学习实践指南

ML2Scratch(机器学习与Scratch编程)是一个创新的开源项目,它将TensorFlow.js的强大机器学习能力无缝集成到Scratch编程环境中,让初学者也能轻松体验和运用人工智能技术。这个工具特别适合教育场景,通过可视化的编程界面,让用户无需编写复杂代码就能实现图像识别、手势检测等机器学习应用,所有数据处理都在本地浏览器中完成,保护用户隐私的同时提供了极佳的学习体验。 ## ✨ 核

gitblog_00030的博客 799

使用ML Classifier UI 构建浏览器端图像分类应用

使用ML Classifier UI 构建浏览器端图像分类应用 想要在浏览器中快速构建一个图像分类模型?ML Classifier UI 是您的理想选择。这是一个基于React的前端框架,它与机器学习引擎集成,让您可以在浏览器内轻松训练和保存图像分类模型。 项目介绍 ML Classifier UI 是对ml-classifier库的图形界面,提供了一种直观的方法来操作和使用机器学习模型。它包括了...

gitblog_00077的博客 471

TensorFlow.js发布:使用JS进行机器学习并在浏览器中运行

文章来源:ATYUN AI平台 TensorFlow官方团队近日发布多个重大更新,其中包括:为python开发者提供的,可以立即评估操作并且无需额外图形构建步骤的eager execution;可以促进机器学习模型可重用部分的发布、发现和使用的TensorFlow Hub;针对移动和嵌入式设备的轻量级解决方案TensorFlow Lite;Swift的TensorFlow开源;面向JavaS...

whale52hertz的博客 717

如何用ml5.js快速构建机器学习项目:10个提升代码质量的实用技巧

ml5.js是一个友好的Web机器学习库,它让开发者能够轻松地在浏览器中实现各种机器学习功能。无论是图像分类、姿态检测还是神经网络训练,ml5.js都提供了简洁的API和丰富的示例,帮助新手快速入门机器学习项目开发。本文将分享10个实用技巧,帮助你提升ml5.js项目的代码质量和开发效率。 ## 1. 合理选择模型初始化方式 ml5.js提供了多种模型初始化方式,选择合适的方式可以显著提升代码

gitblog_00399的博客 818

零代码AI训练指南:5步掌握浏览器内机器学习模型创建

想要让电脑识别你的手势、区分不同声音,甚至理解你的表情吗?现在,无需任何编程基础,你就能在浏览器中轻松创建属于自己的AI识别模型!Teachable Machine 正是这样一个革命性的工具,它让机器学习变得像玩游戏一样简单有趣。无论你是教育工作者、创意设计师,还是对AI充满好奇的普通人,都能在几分钟内开始你的AI探索之旅。 ## 🎯 为什么选择浏览器内AI训练? 传统的机器学习需要复杂的编

gitblog_01162的博客 416

【机器学习】在浏览器端实践机器学习,需要考虑哪些注意事项

我们有必要了解一下在浏览器中使用机器学习需要考虑哪些注意事项。也给后来想要学习机器学习的同学一个参考。从外部源导入预训练模型可能会对您的应用程序产生性能影响。 例如,某些对象检测模型超过 10MB,这会大大降低你的网站速度。 确保考虑你的用户体验并优化资产的加载以提高你的感知性能。如果您从头开始构建模型,你将不得不收集自己的数据或找到一些开源数据集。在进行任何类型的数据处理或尝试不同的算法之前,请务必检查输入数据的质量。 例如,如果你尝试构建情绪分析模型来识别文本中的情绪,请确保用于训练模型的数据准确且多样

前端进阶之路 | 中大厂、外企、国企内推 | 面试培训 | 简历修改 995

ML-For-Beginners 分类实战:用 Scikit-learn 训练菜品分类模型,转 ONNX 后在浏览器中跑推荐 Web 应用

本文基于 [ML-For-Beginners 课程第 4 节"分类"的 Applied 课](https://link.gitcode.com/i/058c00f25046b3e91c58256ce91a81d0),完整走通一条"Python 训练 → ONNX 转换 → 浏览器端 ONNX Runtime 推理"的模型落地链路:用 380 个食材特征训练一个五国菜系分类器,把 Scikit-le

gitblog_00127的博客 460

JavaScript玩转机器学习:在浏览器和 Node.js 训练和部署机器学习模型

开始 TensorFlow.js 是一个 JavaScript 库,用于在浏览器和 Node.js 训练和部署机器学习模型。 了解开始的更多方式,请参阅下面的部分。 在不直接处理张量的情况下编写 ML 程序 想要开始机器学习,同时不用担心任何类似张量或优化器的低级细节吗? ml5.js 库构建在 TensorFlow.js 之上,通过简洁的、可利用的 API,可以在浏览器中访问机器...

YunWisdom 3463
上一篇: 【机器学习】Tensorflow.js:我在浏览器中实现了迁移学习
下一篇: 【机器学习】在浏览器端实践机器学习,需要考虑哪些注意事项
蟹蟹蟹风流
蟹蟹蟹风流 优质创作者: 编程框架技术领域 优质创作者: 编程框架技术领域 领域专家: 前端开发技术领域 领域专家: 前端开发技术领域
博客等级 码龄11年 3万+粉丝 396原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹蟹蟹风流

期望和你分享一杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址