1.向量范数
向量范数(Vector norm)是表征向量“长度”的一种度量方法,在神经网络中,常用来表示张量的权值大小,梯度大小等。常用的向量范数有:
L1范数:定义为向量所有元素绝对值之和:
L2范数:定义为向量所有元素的平方和再开根号:
∞ −范数:定义为向量x的所有元素绝对值的最大值:
对于矩阵、张量,同样可以利用向量范数的计算公式,等价于将矩阵、张量打平成向量后计算。
在 TensorFlow 中,可以通过 **tf.norm(x, ord)**求解张量的 L1, L2, ∞等范数,其中参数 ord指定为 1,2 时计算 L1, L2 范数,指定为 np.inf 时计算∞ −范数:
import tensorflow as tf
import numpy as np
x = tf.ones([2, 2])
# 计算L1范数
tf.norm(x, ord=1)
<tf.Tensor: id=258, shape=(), dtype=float32, numpy=4.0>
# 计算L2范数
tf.norm(x, ord=2)
<tf.Tensor: id=263, shape=(), dtype=float32, numpy=2.0>
# 计算∞范数
tf.norm(x, ord=np.inf)
<tf.Tensor: id=267, shape=(), dtype=float32, numpy=1.0>
2.最大最小值、均值、和
通过 tf.reduce_max, tf.reduce_min, tf.reduce_mean, tf.reduce_sum 可以求解张量在某个维度上的最大、最小、均值、和,也可以求全局最大、最小、均值、和信息。
# 创建一个张量
x = tf.random.normal([4,10])
# 统计概率维度上的最大值
print(tf.reduce_max(x,axis=1))
# 统计概率维度上的最小值
print(tf.reduce_min(x,axis=1))
# 统计概率维度上的均值
print(tf.reduce_mean(x,axis=1))
# 统计概率维度上的和
print(tf.reduce_sum(x,axis=1))
tf.Tensor([0.89485013 1.1410645 2.0153873 1.087544 ], shape=(4,), dtype=float32)
tf.Tensor([-2.6266034 -1.3534253 -0.80141383 -1.5867873 ], shape=(4,), dtype=float32)
tf.Tensor([-0.8090556 -0.04560368 0.27672786 0.28363663], shape=(4,), dtype=float32)
tf.Tensor([-8.090556 -0.45603678 2.7672787 2.8363662 ], shape=(4,), dtype=float32)
当不指定 axis 参数时,tf.reduce_*函数会求解出全局元素的最大、最小、均值、和:
# 创建一个张量
x = tf.random.normal([4,10])
# 统计全局的最大、最小、均值、和
tf.reduce_max(x),tf.reduce_min(x),tf.reduce_mean(x),tf.reduce_sum(x)
(<tf.Tensor: id=301, shape=(), dtype=float32, numpy=2.293366>,
<tf.Tensor: id=303, shape=(), dtype=float32, numpy=-2.4185007>,
<tf.Tensor: id=305, shape=(), dtype=float32, numpy=0.042759612>,
<tf.Tensor: id=307, shape=(), dtype=float32, numpy=1.7103845>)
除了获得最值信息,如果还希望获得最值所在的索引号。通过 tf.argmax(x, axis),tf.argmin(x, axis)可以求解在 axis 轴上,x 的最大值、最小值所在的索引号。
out = tf.random.normal([2, 10])
# 通过softmax转换成概率值
out = tf.nn.softmax(out, axis=1)
out
<tf.Tensor: id=352, shape=(2, 10), dtype=float32, numpy=
array([[0.04618455, 0.02923056, 0.01868729, 0.07368804, 0.31133097,
0.00967309, 0.2317502 , 0.01934892, 0.14496917, 0.11513729],
[0.11795696, 0.18404637, 0.08513474, 0.09911715, 0.0086707 ,
0.21008565, 0.0501902 , 0.14062625, 0.0694969 , 0.03467504]],
dtype=float32)>
可以看到,第一行概率最大值的索引为5, 第二行概率最大值索引为0。
# 选取概率最大的位置
pred = tf.argmax(out, axis=1)
pred
<tf.Tensor: id=354, shape=(2,), dtype=int64, numpy=array([4, 5], dtype=int64)>
3.张量比较
为了计算分类任务的准确率等指标,一般需要将预测结果和真实标签比较,统计比较结果中正确的数量来就是计算准确率。考虑 100 个样本的预测结果:
import tensorflow as tf
out = tf.random.normal([100, 10])
# 输出转换为概率
out = tf.nn.softmax(out, axis=1)
# 选取预测值
pre = tf.argmax(out, axis=1)
pre
<tf.Tensor: id=8, shape=(100,), dtype=int64, numpy=
array([6, 1, 0, 3, 0, 0, 8, 6, 5, 5, 3, 2, 8, 9, 9, 0, 6, 3, 5, 1, 2, 1,
3, 3, 7, 1, 4, 8, 7, 9, 6, 1, 9, 9, 3, 2, 5, 2, 6, 8, 8, 3, 9, 1,
0, 0, 8, 3, 6, 9, 1, 9, 0, 2, 6, 4, 5, 7, 1, 7, 6, 0, 5, 4, 7, 9,
9, 6, 7, 1, 9, 7, 8, 6, 8, 2, 5, 8, 6, 8, 4, 3, 2, 7, 4, 1, 0, 3,
1, 5, 4, 3, 7, 1, 1, 4, 3, 9, 1, 5], dtype=int64)>
创建100个真实标签:
# 真实标签
y = tf.random.uniform([100], dtype=tf.int64, maxval=10)
y
<tf.Tensor: id=12, shape=(100,), dtype=int64, numpy=
array([2, 2, 0, 4, 0, 2, 2, 3, 4, 5, 7, 2, 1, 4, 4, 9, 9, 2, 7, 1, 7, 7,
1, 1, 5, 0, 4, 3, 4, 0, 8, 0, 6, 6, 1, 8, 6, 0, 2, 9, 6, 5, 5, 6,
4, 2, 8, 6, 4, 2, 7, 4, 0, 2, 0, 5, 6, 1, 1, 6, 9, 8, 5, 3, 1, 7,
4, 5, 5, 1, 6, 7, 2, 4, 9, 6, 1, 1, 3, 5, 5, 0, 1, 2, 2, 7, 2, 8,
7, 5, 2, 8, 5, 4, 0, 9, 3, 2, 5, 0], dtype=int64)>
将预测值和真实值进行比较:
# 预测值和真实值进行比较
out = tf.equal(pre, y)
out
<tf.Tensor: id=13, shape=(100,), dtype=bool, numpy=
array([False, False, True, False, True, False, False, False, False,
True, False, True, False, False, False, False, False, False,
False, True, False, False, False, False, False, False, True,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, True, False, False, False, False, False, True, True,
False, False, False, False, True, False, False, False, True,
False, False, False, False, False, False, True, False, True,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, True,
False, False, False, False, False, False, True, False, False,
False])>
tf.equal()函数返回布尔型的张量比较结果,只需要统计张量中 True 元素的个数,即可知道预测正确的个数。为了达到这个目的,我们先将布尔型转换为整形张量,再求和其中 1 的个数,可以得到比较结果中 True 元素的个数:
# book型转为int型
out = tf.cast(out, dtype=tf.float32)
# 统计True的个数
correct = tf.reduce_sum(out)
correct
<tf.Tensor: id=16, shape=(), dtype=float32, numpy=15.0>
可以明显看出,正确率为15%。
4.填充与复制
填充
填充操作可以通过 tf.pad(x, paddings)函数实现。
paddings 是包含了多个[𝐿𝑒𝑓𝑡 𝑃𝑎𝑑𝑑𝑖𝑛𝑔, 𝑅𝑖𝑔ℎ𝑡 𝑃𝑎𝑑𝑑𝑖𝑛𝑔]的嵌套方案 List,如[[0,0],[2,1],[1,2]]表示第一个维度不填充,第二个维度左边(起始处)填充两个单元,右边(结束处)填充一个单元,第三个维度左边填充一个单元,右边填充两个单元。
右边填充两个单元:
b = tf.constant([7,8,1,6])
b = tf.pad(b, [[0,2]]) # 填充
b
<tf.Tensor: id=19, shape=(6,), dtype=int32, numpy=array([7, 8, 1, 6, 0, 0])>
复制
x = tf.random.normal([4,32,32,3])
tf.tile(x,[2,3,3,1]) # 数据复制
<tf.Tensor: id=27, shape=(8, 96, 96, 3), dtype=float32, numpy=
array([[[[-4.83582795e-01, -1.13574117e-01, -4.29558069e-01],
[-9.31588352e-01, 5.81051886e-01, 1.63413048e+00],
[-1.47222829e+00, 4.97042900e-04, 7.22212493e-01],
...,
5.数据限幅
在 TensorFlow 中,可以通过 tf.maximum(x, a)实现数据的下限幅:𝑥 ∈ [𝑎, +∞);可以通过 tf.minimum(x, a)实现数据的上限幅:𝑥 ∈ (−∞,𝑎]。
x = tf.range(9)
# 下限幅为2
tf.maximum(x, 2)
<tf.Tensor: id=33, shape=(9,), dtype=int32, numpy=array([2, 2, 2, 3, 4, 5, 6, 7, 8])>
# 上限幅为7
tf.minimum(x, 7)
<tf.Tensor: id=35, shape=(9,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 7])>