tensorFlow.js出现于2018年,2019年发布第一个稳定版本 1.0版本,2020年5月发布2.0版本,2021年发布3.0版本
张量和操作
1. 张量(tensor)
tensorFlow.js中数据的中央单元为tf.tensor:一组形状为一维或多维数组的值。tf.tensor与多维数组非常相似。tf.tensor包含一下属性
- rank:定义张量包含的维度
- shape:定义数据每个维度的大小
- dtype:定义张量的数据类型(默认为float32,还有bool,int32,complex64和字符串数据类型)
使用tf.tensor()方法从数组创建tf.tensor
// Create a rank-2 tensor (matrix) matrix tensor from a multidimensional array.
const a = tf.tensor([[1, 2], [3, 4]]);
console.log('shape:', a.shape);
a.print();
// Or you can create a tensor from a flat array and specify a shape.
const shape = [2, 2];
const b = tf.tensor([1, 2, 3, 4], shape);
console.log('shape:', b.shape);
b.print();
const a = tf.tensor([[1, 2], [3, 4]], [2, 2], 'int32');
console.log('shape:', a.shape);
console.log('dtype', a.dtype);
a.print();
更改张量的形状
tf.Tensor 中元素的数量是其形状大小的乘积。由于通常可以有多个具有相同大小的形状,因此将 tf.Tensor 重塑为具有相同大小的其他形状通常非常实用。这可以通过 reshape() 方法实现:
const a = tf.tensor([[1, 2], [3, 4]]);
console.log('a shape:', a.shape);
a.print();
const b = a.reshape([4, 1]);
console.log('b shape:', b.shape);
b.print();
从张量中获取值
可以使用 Tensor.array() 或 Tensor.data() 方法从 tf.Tensor 中获取值:
const a = tf.tensor([[1, 2], [3, 4]]);
// Returns the multi dimensional array of values.
a.array().then(array => console.log(array));
// Returns the flattened data that backs the tensor.
a.data().then(data => console.log(data));
同步获取
const a = tf.tensor([[1, 2], [3, 4]]);
// Returns the multi dimensional array of values.
console.log(a.arraySync());
// Returns the flattened data that backs the tensor.
console.log(a.dataSync());
2. 运算
张量可用于存储数据,而运算则可以用于操作改数据。TensorFlow.js 还提供了可对张量执行的适用于线性代数和机器学习的多种运算
对 tf.Tensor 中的所有元素执行 x2 计算:
const x = tf.tensor([1, 2, 3, 4]);
const y = x.square(); // equivalent to tf.square(x)
y.print();
对两个 tf.Tensor 的元素执行逐元素相加:
const a = tf.tensor([1, 2, 3, 4]);
const b = tf.tensor([10, 20, 30, 40]);
const y = a.add(b); // equivalent to tf.add(a, b)
y.print();
由于张量是不可变的,因此这些运算不会改变其值。相反,return 运算总会返回新的 tf.Tensor。
- tf.add() (加法)
- tf.sub() (减法)
- tf.mul() (乘法)
- tf.div() (除法)
3. 内存
使用WebGL后端时,必须显示管理tf.Tensor内存(即使 tf.Tensor 超出范围也不足以释放其内存)。
要销毁 tf.Tensor 的内存,您可以使用 dispose() 方法或 tf.dispose():
const a = tf.tensor([[1, 2], [3, 4]]);
a.dispose(); // Equivalent to tf.dispose(a)
模型
模型和层
在机器学习中,模型是一个带有可学习参数的函数,可将输入映射至输出。通过在数据上训练模型获得最佳参数。训练好的模型可以提供从输入到所需输出的准确映射。
可以通过两种方式创建机器学习模型:
- 使用Layers API(使用层构建模型)
- 使用Core API(借助低级运算,例如tf.matMul()、tf.add()等)
1、使用Layers API创建模型
Layers API模型分为:序贯模型和函数模式
序贯模型
最常见的模型是 <a href="https://js.tensorflow.org/api/0.15.1/#class:Sequential" data-md-type="link">Sequential</a> 模型,序贯模型是层的线性堆叠。您可以通过将层列表传递到 <a href="https://js.tensorflow.org/api/0.15.1/#sequential" data-md-type="link">sequential()</a> 函数来创建 Sequential 模型:
训练模型
在TensorFlow.js 中,可以通过两种方式训练机器学习模型:
- 使用 Layers API 与 <a href="https://js.tensorflow.org/api/latest/#tf.Model.fit" data-md-type="link">LayersModel.fit()</a> 或 <a href="https://js.tensorflow.org/api/latest/#tf.Model.fitDataset" data-md-type="link">LayersModel.fitDataset()</a>。
- 使用 Core API 与 <a href="https://js.tensorflow.org/api/latest/#tf.train.Optimizer.minimize" data-md-type="link">Optimizer.minimize()</a>。
简介
机器学习模型是一种具有可学习参数的函数,可将输入映射到所需输出。基于数据训练模型可以获得最佳参数。
训练模型涉及到的步骤:
- 获取一批次数据来训练模型
- 让模型做出预测
- 将该预测与真实值进行对比
- 确定每个参数的更改幅度,使模型在未来能够针对改批次数据做出更好的预测