一、Rank
Tensorflow将张量定义为多维数组。它可以表示任意维度的数组,维度用rank(阶)表示。它在深度学习中存在的意义是创造更高维度的矩阵、向量。
import pandas as pd
import tensorflow as tf
a1 = [
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]],
[[11, 21, 31],
[41, 51, 61],
[71, 81, 91],
[101, 111, 121]]
]
a2 = [
[1, 2, 3],
[4, 5, 6]
]
t1 = tf.convert_to_tensor(a1, dtype=tf.int32)
t2 = tf.convert_to_tensor(a2, dtype=tf.int32)
print("tf.rank(t1) = {0}; t1 = \n{1}".format(tf.rank(t1), t1))
print("-" * 100)
print("tf.rank(t2) = {0}; t2 = \n{1}".format(tf.rank(t2), t2))
打印结果:
tf.rank(t1) = 3; t1 =
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[ 10 11 12]]
[[ 11 21 31]
[ 41 51 61]
[ 71 81 91]
[101 111 121]]]
----------------------------------------------------------------------------------------------------
tf.rank(t2) = 2; t2 =
[[1 2 3]
[4 5 6]]
Process finished with exit code 0
rank:就是有几个维度,也相当与有几个括号,也是shape元组的长度,rank的idx计数都是从外向里,从大到小,也就是shape的排列方式。
- tf.rank(t1) = 3
- tf.rank(t2) = 2
图中数字表示rank的idx,箭头表示idx的方向。
二、典型的rank
Typical Dim Data
- []
- [d]
- [h, w]
- [b, len, vec]
- [b, h, w, c]
- [t, b, h, w, c]
- …
0、rank = 0维【标量/Scalar】
- []
- 0, 1., 2.2…
- accuracy
- loss = mse(out, y)
1、rank = 1维【向量/Vector】
Bias
- [out_dim]
2、rank = 2维【矩阵/Matrix】
- input x: [b, vec_dim]
- weight: [input_dim, output_dim]
3、rank = 3维【矩阵/Matrix】
x: [b, seq_len, word_dim]
- [b, 5, 5]
4、rank = 4维【矩阵/Matrix】
Image: [b, h, w, 3]
feature maps: [b, h, w, c]
5、rank= 5维【矩阵/Matrix】
Single task: [b, h, w, 3]
meta-learning:[task_b, b, h, w, 3]
参考资料:
tensorflow:理解 rank, shape, type
通俗易懂的理解Tensor(张量)、Rank、Shape