六子 TensorFlow.js 学习笔记
part一、Tensors
1、Creation:我们具有用于标量,1D,2D,3D和4D张量的常见情况的实用程序功能,以及许多用于以对机器学习有用的方式初始化张量的功能。
- tf.tensor(values, shape?, dtype?)
- 参数:
- values (TypedArray| Array) 张量的值。可以是数字的嵌套数组,平面数组或 TypedArray。如果值为字符串,则将其编码为 utf-8 并保留为 Uint8Array[]。
- shape (number []) 张量的形状。可选的。如果未提供,则从中推断 values。 可选的
- dtype (‘float32’|‘int32’|‘bool’|‘complex64’|‘string’) 数据类型。 可选的
- 返回值: tf.Tensor
- tf.tensor([1, 2, 3, 4]).print(),打印如下信息:
Tensor [1, 2, 3, 4]
- tf.tensor([1, 2, 3, 4], [2, 2]).print(),打印信息如下:
Tensor [[1, 2], [3, 4]]
- 参数:
- tf.scalar (value, dtype?)
- 使用提供的 value 和 dtype 创建 rank-0 的 tf.Tensor(标量)。使用tf.tensor()可以实现相同的功能,但是通常我们建议使用tf.scalar(),因为它使代码更具可读性。
- 参数:
- value (number | boolean | string | Uint8Array) 标量的值。
- dtype (‘float32’|‘int32’|‘bool’|‘complex64’|‘string’) 数据类型。 可选的
- 返回值: tf.Scalar
- tf.scalar(3.14).print(),打印信息如下:
Tensor 3.140000104904175
- tf.tensor1d (values, dtype?)
- 使用提供的 value 和 dtype 创建 rank-1 的 tf.Tensor(标量)。使用tf.tensor()可以实现相同的功能,但是通常我们建议使用tf.tensor1d(),因为它使代码更具可读性。
- 参数:
- values (TypedArray| Array) 张量的值。可以是数字数组或 TypedArray。
- dtype (‘float32’|‘int32’|‘bool’|‘complex64’|‘string’) 数据类型。 可选的
- 返回值: tf.Tensor1D
- tf.tensor1d([1, 2, 3]).print(),打印信息如下:
Tensor [1, 2, 3]
- tf.tensor2d (values, shape?, dtype?)
- 使用提供的 value 和 dtype 创建 rank-2 的 tf.Tensor(标量)。使用tf.tensor()可以实现相同的功能,但是通常我们建议使用tf.tensor2d(),因为它使代码更具可读性。
- 参数:
- values (TypedArray| Array) 张量的值。可以是数字数组或 TypedArray。
- dtype (‘float32’|‘int32’|‘bool’|‘complex64’|‘string’) 数据类型。 可选的
- 返回值: tf.Tensor2D
- tf.tensor2d([[1, 2], [3, 4]]).print(),打印信息如下:
Tensor [[1, 2], [3, 4]]
- tf.tensor2d([1, 2, 3, 4], [2, 2]).print(),打印信息如下:
Tensor [[1, 2],