开源
tfjs:硬件加速 JS 库
来源:元经纪     阅读:1207
网站管理员
发布于 2023-01-29 07:39
查看主页

概述

TensorFlow.js 是一个开源的硬件加速 JavaScript 库,用于训练和部署机器学习模型。

在浏览器中开发 ML 使用灵活直观的 API,使用低级 JavaScript 线性代数库或高级层 API 从头开始​​构建模型。

在 Node.js 中开发 ML 在 Node.js 运行时下使用相同的 TensorFlow.js API 执行原生 TensorFlow。

运行现有模型 使用 TensorFlow.js 模型转换器在浏览器中运行预先存在的 TensorFlow 模型。

重新训练现有 模型使用连接到浏览器或其他客户端数据的传感器数据重新训练预先存在的 ML 模型。

关于这个repo

该存储库包含组合多个包的逻辑和脚本。

[hidecontent type="logged" desc="隐藏内容:登录后可查看"]

后端/平台:

如果您关心包的大小,您可以单独导入这些包。

如果您正在寻找 Node.js 支持,请查看TensorFlow.js Node 目录

例子

查看我们的 示例存储库 和教程

画廊

请务必查看与 TensorFlow.js 相关的所有项目

预训练模型

请务必查看我们的模型存储库,我们在 NPM 上托管预训练模型。

基准

  • 本地基准工具。使用此网页工具收集具有 CPU、WebGL 或 WASM 后端的本地设备上TensorFlow.js 模型和内核的性能相关指标(速度、内存等) 。您可以按照本指南对自定义模型进行基准测试。
  • 多设备基准测试工具使用此工具在一组远程设备上收集相同的性能相关指标。

入门

在您的 JavaScript 项目中获取 TensorFlow.js 的主要方法有两种:通过脚本标签 NPM安装它并使用Parcel、 WebPackRollup 等构建工具。

通过脚本标签

将以下代码添加到 HTML 文件中:

<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>


    <!-- Place your code in the script tag below. You can also use an external .js file -->
    <script>
      // Notice there is no 'import' statement. 'tf' is available on the index-page
      // because of the script tag above.

      // 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.tensor2d([1, 2, 3, 4], [4, 1]);
      const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

      // 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
        model.predict(tf.tensor2d([5], [1, 1])).print();
      });
    </script>
  </head>

  <body>
  </body>
</html>

在浏览器中打开该 HTML 文件,代码应该可以运行了!

通过 NPM

使用yarn  npm将 TensorFlow.js 添加到您的项目中。注意:因为我们使用 ES2017 语法(例如import),所以此工作流假定您使用现代浏览器或捆绑器/转译器将您的代码转换为旧浏览器可以理解的代码。查看我们的 示例 ,了解我们如何使用Parcel构建代码。但是,您可以自由使用您喜欢的任何构建工具。

import * as tf from '@tensorflow/tfjs';

// 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.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// 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:
  model.predict(tf.tensor2d([5], [1, 1])).print();
});

有关详细信息,请参阅我们的教程示例 和文档

导入预训练模型

我们支持从以下平台移植预训练模型:

不同后端支持的各种操作

请参考以下:

找到更多

TensorFlow.js是 TensorFlow生态系统的一部分。欲了解更多信息:

[/hidecontent]

 
免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 开源
全画幅微单佳能eosr8使用感受
汽车零部件行业如何数字化转型?这场论坛给你答案
第七届大数据应用实践大会暨先进算力赋能制造业“智改数转”主题大会在雅安举行
刘金祥:以数字化赋能优秀传统文化传承
神州信息张劲:金融数字化转型迫在眉睫,核心下移是突破的关键

首页

分类

定制方案

消息

我的