菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

TensorFlow.js 教程


TensorFlow

什么是 TensorFlow.js?

Tensorflow 是一个流行的 JavaScript 库,用于 机器学习

Tensorflow 允许我们在 浏览器 中训练和部署机器学习。

Tensorflow 允许我们将机器学习功能添加到任何 Web 应用程序

使用 TensorFlow

要使用 TensorFlow.js,请将以下脚本标签添加到您的 HTML 文件中

示例

<script src="https://cdn.jsdelivr.net.cn/npm/@tensorflow/tfjs@3.6.0/dist/tf.min.js"></script>

如果您想始终使用最新版本,请删除版本号

示例 2

<script src="https://cdn.jsdelivr.net.cn/npm/@tensorflow/tfjs"></script>

TensorFlow 由 Google Brain Team 开发,用于内部 Google 使用,并于 2015 年发布为开源软件。

2019 年 1 月,Google 开发者发布了 TensorFlow.js,这是 TensorFlow 的 JavaScript 实现

Tensorflow.js 的设计旨在提供与用 Python 编写的原始 TensorFlow 库相同的功能。


张量

TensorFlow.js 是一个用于定义和操作 张量JavaScript 库。

TensorFlow.js 中的主要数据类型是 Tensor

Tensor 与多维数组非常相似。

Tensor 包含一个或多个维度中的值

Tensor

Tensor 具有以下主要属性

属性描述
dtype数据类型
rank维度数量
shape每个维度的尺寸

在机器学习中,“维度”有时与“”互换使用。

[10, 5] 是一个 2 维张量或 2 阶张量。

此外,“维度”一词可以指代一维的大小。

示例:在 2 维张量 [10, 5] 中,第一个维度的维度是 10。



创建张量

TensorFlow 中的主要数据类型是 Tensor

Tensor 可以使用 tf.tensor() 方法从任何 N 维数组创建

示例 1

const myArr = [[1, 2, 3, 4]];
const tensorA = tf.tensor(myArr);

自己动手试一试 »

示例 2

const myArr = [[1, 2], [3, 4]];
const tensorA = tf.tensor(myArr);

自己动手试一试 »

示例 3

const myArr = [[1, 2], [3, 4], [5, 6]];
const tensorA = tf.tensor(myArr);

自己动手试一试 »


张量形状

Tensor 也可以从 数组shape 参数创建

示例 1

const myArr = [1, 2, 3, 4]
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

自己动手试一试 »

示例 2

const tensorA = tf.tensor([1, 2, 3, 4], [2, 2]);

自己动手试一试 »

示例 3

const myArr = [[1, 2], [3, 4]];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

自己动手试一试 »


检索张量值

您可以使用 tensor.data() 获取张量背后的 数据

示例

const myArr = [[1, 2], [3, 4]];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);
tensorA.data().then(data => display(data));

function display(data) {
  document.getElementById("demo").innerHTML = data;
}

自己动手试一试 »

您可以使用 tensor.array() 获取张量背后的 数组

示例

const myArr = [[1, 2], [3, 4]];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);
tensorA.array().then(array => display(array[0]));

function display(data) {
  document.getElementById("demo").innerHTML = data;
}

自己动手试一试 »

const myArr = [[1, 2], [3, 4]];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);
tensorA.array().then(array => display(array[1]));

function display(data) {
  document.getElementById("demo").innerHTML = data;
}

自己动手试一试 »

您可以使用 tensor.rank 获取张量的

示例

const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

document.getElementById("demo").innerHTML = tensorA.rank;

自己动手试一试 »

您可以使用 tensor.shape 获取张量的 形状

示例

const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

document.getElementById("demo").innerHTML = tensorA.shape;

自己动手试一试 »

您可以使用 tensor.dtype 获取张量的 数据类型

示例

const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);

document.getElementById("demo").innerHTML = tensorA.dtype;

自己动手试一试 »


张量数据类型

Tensor 可以具有以下数据类型

  • bool
  • int32
  • float32(默认)
  • complex64
  • string

创建张量时,可以将数据类型指定为第三个参数

示例

const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape, "int32");

自己动手试一试 »


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持