【tensorflow】张量,tensor,数组的定义和声明

张量(tensor):可以表示0阶到n阶的数组

  • 0阶张量(标量):单独的一个数
  • 1阶张量(向量):一维数组
  • 2阶张量(矩阵):二维数组
  • n阶张量(张量):n维数组

tensorflow 中几种常见的数据类型:

  • tf.int,tf.float

tf.int32

tf.float32

tf.float64

  • tf.bool

tf.constant(True, False)

  • tf.string

tf.constant("Hello world")

声明一个张量 tensor

1.tf.constant(张量内容, dtype=数据类型(可选))

如:a = tf.constant([1, 5], dtype=tf.int32)

2.将numpy数据类型转换为tensor数据类型

tf.convert_to_tensor(数据名, dtype=数据类型(可选))

如:a = np.array([3.6, 5.2])

b = tf.convert_to_tensor(a, dtype=tf.int32)

3.创建全为0的张量

tf.zeros(维度)

4.创建全为1的张量

tf.ones(维度)

5.创建全为指定值的张量

tf.fill(维度, 指定值)

维度:

一维,直接写个数

二维,[行, 列]

多维,[n, m, k ...]

6.生成正态分布的随机数,默认均值为0,标准差为1

tf.random.normal(维度, mean=均值, stddev=标准差)

7.生成截断式正态分布的随机数,取值范围为(μ-2σ,μ+2σ)

tf.random.truncated_normal(维度, mean=均值, stddev=标准差)

8.生成一定范围内的随机数

tf.random.uniform(维度, minval=最小值, maxval=最大值)