1.基础知识
张量时具有统一类型(称为dtype)的多维数组。
张量与np.arrays有一定的相似性。
就像python的数值和字符串一样,所有的张量都是不可变的:永远无法更新张量的内容,只能创建新的张量。
标量(0秩张量):标量包含单个值,但没有轴。
向量(1秩张量):像一个值列表,向量有1个轴。
矩阵(2秩张量):有两个轴。
#标量
rank_0_tensor = tf.constant(4)
print(rank_0_tensor)
#标量
rank_1_tensor = tf.constant([2.1,3.2,4.3])
print(rank_1_tensor)
#矩阵
rank_2_tensor = tf.constant([[1,2],[3,4],[5,6]],dtype = tf.float16)
print(rank_2_tensor)
#包含3个轴的张量
rank_3_tensor = tf.constant([
[[0, 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]],])
print(rank_3_tensor)```
```python
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor([2.1 3.2 4.3], shape=(3,), dtype=float32)
tf.Tensor(
[[1. 2.]
[3. 4.]
[5. 6.]]