介紹幾個重要操作:
1.范數
a = tf.fill([1,2], value=2.)
b = tf.norm(a)# 二范數#第二種計算方法
# 計算驗證
a = tf.square(a)
log("a的平方:", a)
a = tf.reduce_sum(a)
log("a平方后的和:", a)
b = tf.sqrt(a)
log("a平方和后開根號:", b)#二者結果是一樣的b = tf.norm(a,ord=1)#一范數
print(b)
print(tf.reduce_sum(tf.abs(a)))#所有值得絕對值之和
# 指定計算軸:axis=1
b = tf.norm(a, ord=1, axis=1)
log("a的axis=1的1范數b:", b)
2.最大最小平均值的計算
a = tf.range(12, dtype=tf.float32)
a = tf.reshape(a, (4,3))
log("a數組:", a)b = tf.reduce_min(a)
log("a數組最小值:", b)
b = tf.reduce_max(a)
log("a數組最大值:", b)
b = tf.reduce_mean(a)
log("a數組平均值:", b)b = tf.reduce_min(a, axis=0)
log("a數組axis=0最小值:", b)
b = tf.reduce_max(a, axis=0)
log("a數組axis=0最大值:", b)
b = tf.reduce_mean(a, axis=0)
log("a數組axis=0平均值:", b)b = tf.reduce_min(a, axis=1)
log("a數組axis=1最小值:", b)
b = tf.reduce_max(a, axis=1)
log("a數組axis=1最大值:", b)
b = tf.reduce_mean(a, axis=1)
log("a數組axis=1平均值:", b)
其實都挺簡單的,和numpy和torch都差不多。
3.索引
- tf.argmax
- tf.argmin
def log(prefix="", val=""):print(prefix, val, "\n")# 定義一個隨機數組
a = tf.random.uniform((3,10), minval=0, maxval=10, dtype=tf.int32)
log("a", a)# 取最大索引位置數組,通常用于取得模型預測結果
b = tf.argmax(a, axis=1)
log("a數組axis=1的最大值索引位置:", b)# 取最小索引位置數組
b = tf.argmin(a, axis=1)
log("a數組axis=1的最小值索引位置:", b)
4.數組比較
一些python基礎語法
a = tf.random.uniform((1,10), minval=0, maxval=10, dtype=tf.int32)
b = tf.random.uniform((1,10), minval=0, maxval=10, dtype=tf.int32)log("a:", a)
log("b:", b)# a,b數組比較
log("a==b", a==b)
# 相同元素輸出
log(a[a==b])
# 相同元素索引位置輸出
log(tf.where(a==b))
5.張量排序
- 張量數值排序:tf.sort
- 張量索引排序:tf.argsort
#################################################
# 聲明數組a
a = tf.random.shuffle(tf.range(10))
log("a", a)
# a tf.Tensor([8 2 0 5 7 9 3 1 4 6], shape=(10,), dtype=int32) #################################################
# 升序排列
b = tf.sort(a, direction="ASCENDING")
log("b", b)
# 降序排列
b = tf.sort(a, direction="DESCENDING")
log("b", b)
# b tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
# b tf.Tensor([9 8 7 6 5 4 3 2 1 0], shape=(10,), dtype=int32) #################################################
# 升序排列,返回索引位置
b = tf.argsort(a, direction="ASCENDING")
log("b", b)
# 降序排列,返回索引位置
b = tf.argsort(a, direction="DESCENDING")
log("b", b)
# b tf.Tensor([2 7 1 6 8 3 9 4 0 5], shape=(10,), dtype=int32)
# b tf.Tensor([5 0 4 9 3 8 6 1 7 2], shape=(10,), dtype=int32) #################################################
# 按索引位置b, 從數組a中收集數據
c = tf.gather(a, b)
log("c", c)
# c tf.Tensor([9 8 7 6 5 4 3 2 1 0], shape=(10,), dtype=int32)
6.二維張量排序
- 2維張量數值排序:tf.sort
- 2維張量索引排序:tf.argsort
#################################################
# 聲明2維數組a
a = tf.random.uniform([3,5], maxval=10, dtype=tf.int32)
log("a", a)
# a tf.Tensor(
# [[2 5 8 0 4]
# [1 7 2 4 5]
# [6 0 2 5 0]], shape=(3, 5), dtype=int32) #################################################
# 升序排列
b = tf.sort(a, axis=1, direction="ASCENDING")
log("b", b)
# 降序排列
b = tf.sort(a, axis=1, direction="DESCENDING")
log("b", b)
# b tf.Tensor(
# [[0 2 4 5 8]
# [1 2 4 5 7]
# [0 0 2 5 6]], shape=(3, 5), dtype=int32)
# b tf.Tensor(
# [[8 5 4 2 0]
# [7 5 4 2 1]
# [6 5 2 0 0]], shape=(3, 5), dtype=int32) #################################################
# 升序排列,返回索引位置
b = tf.argsort(a, axis=1, direction="ASCENDING")
log("b", b)
# 降序排列,返回索引位置
b = tf.argsort(a, axis=1, direction="DESCENDING")
log("b", b)
# b tf.Tensor(
# [[3 0 4 1 2]
# [0 2 3 4 1]
# [1 4 2 3 0]], shape=(3, 5), dtype=int32)
# b tf.Tensor(
# [[2 1 4 0 3]
# [1 4 3 2 0]
# [0 3 2 1 4]], shape=(3, 5), dtype=int32)
7.TopK值取得
################################################
# 2維數組定義
a = tf.random.uniform([3,5], maxval=10, dtype=tf.int32)
log("a", a)
# a tf.Tensor(
# [[1 2 5 8 7]
# [6 1 4 3 9]
# [5 9 6 5 5]], shape=(3, 5), dtype=int32) #################################################
# 取數組每行前3位
b = tf.math.top_k(a, k=3, sorted=True)
# 前3位數值
log("b", b.values)
# 前3位數值索引
log("b", b.indices)# b tf.Tensor(
# [[8 7 5]
# [9 6 4]
# [9 6 5]], shape=(3, 3), dtype=int32)
# b tf.Tensor(
# [[3 4 2]
# [4 0 2]
# [1 2 0]], shape=(3, 3), dtype=int32)