TensorFlow.js: javascript界的机器学习大神

2025年10月01日 16:45:27
70242
机器学习 AI模型 AI框架 Nodejs
tfjs tensorflow/tfjs

TensorFlow.js 把机器学习能力带进了 JavaScript。训练模型、运行推理,甚至搞实时 AI Web 或移动端、Node 后端。谷歌用,创业公司用,爱好者用,用过都说好。

项目大小 169.78 KB
涉及语言 TypeScript 80.16% JavaScript 8.52% C++ 4.51% Python 3.75% Starlark 1.33% HTML 0.92% Shell 0.50% CSS 0.19% Objective-C 0.04% Java 0.03% Batchfile 0.01% Ruby 0.01% Dockerfile 0.01% C 0.01%
许可协议 LICENSE
仓库同步说明
  • • 同步需要仓库写入权限以创建目标仓库
  • • 使用平台账号授权登录后将同步到您平台下的个人仓库

TensorFlow.js

TensorFlow.js is an open-source hardware-accelerated JavaScript library for
training and deploying machine learning models.

Develop ML in the Browser

Use flexible and intuitive APIs to build models from scratch using the low-level
JavaScript linear algebra library or the high-level layers API.

Develop ML in Node.js

Execute native TensorFlow with the same TensorFlow.js API under the Node.js
runtime.

Run Existing models

Use TensorFlow.js model converters to run pre-existing TensorFlow models right
in the browser.

Retrain Existing models

Retrain pre-existing ML models using sensor data connected to the browser or
other client-side data.

About this repo

This repository contains the logic and scripts that combine
several packages.

APIs:

Backends/Platforms:

If you care about bundle size, you can import those packages individually.

If you are looking for Node.js support, check out the TensorFlow.js Node directory.

Examples

Check out our
examples repository
and our tutorials.

Gallery

Be sure to check out the gallery of all projects related to TensorFlow.js.

Pre-trained models

Be sure to also check out our models repository where we host pre-trained models
on NPM.

Benchmarks

  • Local benchmark tool. Use this webpage tool to collect the performance related metrics (speed, memory, etc) of TensorFlow.js models and kernels on your local device with CPU, WebGL or WASM backends. You can benchmark custom models by following this guide.
  • Multi-device benchmark tool. Use this tool to collect the same performance related metrics on a collection of remote devices.

Getting started

There are two main ways to get TensorFlow.js in your JavaScript project:
via script tags or by installing it from NPM
and using a build tool like Parcel,
WebPack, or Rollup.

via Script Tag

Add the following code to an HTML file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<html>
  <head>
     
 
 
     
    <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>

Open up that HTML file in your browser, and the code should run!

via NPM

Add TensorFlow.js to your project using yarn or npm. Note: Because
we use ES2017 syntax (such as import), this workflow assumes you are using a modern browser or a bundler/transpiler
to convert your code to something older browsers understand. See our
examples
to see how we use Parcel to build
our code. However, you are free to use any build tool that you prefer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
});

See our tutorials, examples
and documentation for more details.

Importing pre-trained models

We support porting pre-trained models from:

Various ops supported in different backends

Please refer below :

Find out more

TensorFlow.js is a part of the
TensorFlow ecosystem. For more info:

Thanks, BrowserStack, for providing testing support.


                

                

免责声明 © 2026 - 虚宝阁

本站部分源码来源于网络,版权归属原开发者,用户仅获得使用权。依据《计算机软件保护条例》第十六条,禁止:

  • 逆向工程破解技术保护措施
  • 未经许可的分发行为
  • 去除源码中的原始版权标识

※ 本站源码仅用于学习和研究,禁止用于商业用途。如有侵权, 请及时联系我们进行处理。

侵权举报请提供: 侵权页面URL | 权属证明模板

响应时效:收到完整材料后48小时内处理

相关推荐

markdown-it

markdown-it

markdown-it是一款快速且易扩展的Markdown解析器,遵循CommonMark规范并添加语法扩展。它配置灵活、速度快且安全,还有丰富的社区插件。

20415 2025-09-09
Stable Diffusion AI绘画界的 扛把子

Stable Diffusion AI绘画界的 扛把子

提到AI画图,没人能绕开Stable Diffusion!由Stability AI开源,支持文本生成图像、图像修复、风格迁移,关键是完全免费商用(非商用更没问题),普通电脑装个WebUI就能玩到飞起。

41831 2025-09-13
n8n: AI自动化工作流工具

n8n: AI自动化工作流工具

基于节点的自动化工作流工具,能帮助用户轻松创建和管理复杂的自动化流程,无需编写大量代码。并且内置了AI能力,支持 400+ 应用和服务!

146989 2025-09-26
fastapi: Python web api 框架

fastapi: Python web api 框架

一个基于 Python 3.6+ 版本的异步 WEB 应用框架,使用 Python 类型注解构建 web API 。主要特点有: 高性能:与 Node JS 和 Go 相当。 编码快:将开发功能的速度提高 2~3 倍。 Bug少:减少大约 40% 的由开发人员导致的错误。 直观:强大的编辑器支持,可智能感知和补全代码。 简单:易于学习和使用,减少文档阅读时间。 简短:尽量减少代码重复。 健壮:获得可用于生产的代码,具有自动交互文档。 基于标准:基于 OpenAPI 和 JSON Schema 。

90539 2025-09-16
YOLOv8-目标检测界的闪电侠

YOLOv8-目标检测界的闪电侠

YOLO系列的最新版,主打“又快又准”的目标检测。能瞬间识别图片/视频里的人、车、动物、物体,在普通显卡上就能实时处理视频流,工业级场景都在用它。

47034 2025-09-13
refine: 企业级React后台框架

refine: 企业级React后台框架

这是一个专为构建 CRUD(增删改查)应用设计的 Web 框架,只需一行命令即可生成项目骨架,内置登录、列表、详情、编辑页面等功能。业务逻辑与 UI、路由完全解耦,可灵活集成 Ant Design、Material-UI 等设计系统,适用于快速开发管理后台、仪表盘、内部工具和 B2B 应用。

32865 2025-09-13

仓库下载

gitee

GitHub 下载代理

文件信息

文件名
文件大小
文件类型
代理耗时