[TensorFlow 2] 范数计算/数据统计

介绍

以龙良曲老师的《深度学习与TensorFlow 2入门实战》为教材,记录一下我的学习笔记。

代码

运行结果

范数

L 2 n o r m ( E u c l i d e a n n o r m ) ∣ ∣ x ∣ ∣ 2 = ∑ k x k 2 L_2 norm (Euclidean norm) ||x||_2 = \sqrt{\sum_k x_k^2} L2norm(Euclideannorm)x2=kxk2
M a x . N o r m ∣ ∣ x ∣ ∣ ∞ = m a x k ∣ x k ∣ Max. Norm ||x||_{\infty} = max_k |x_k| Max.Normx=maxkxk
L 1 − N o r m ∣ ∣ x ∣ ∣ 1 = ∑ k ∣ x k ∣ L_1-Norm ||x||_{1} = \sum_k |x_k| L1Normx1=kxk

tf.norm

L 2 L_2 L2 Norm

计算下面矩阵欧几里得范数
[ 1 1 1 1 ] \begin{bmatrix} 1 & 1\\ 1 & 1 \end{bmatrix} [1111]
1 2 + 1 2 + 1 2 + 1 2 = 4 = 2 \sqrt{1^2+1^2+1^2+1^2}=\sqrt{4}=2 12+12+12+12 =4 =2

a=tf.ones([2,2])
tf.norm(a)

<tf.Tensor: shape=(), dtype=float32, numpy=2.0>

相当于下面这个

tf.sqrt(tf.reduce_sum(tf.squre(a)))

计算某一个维度的 L 2 L_2 L2 范数
当 axis=1
L2
[ 2 2 ] \begin{bmatrix} \sqrt{2} \\ \sqrt{2} \end{bmatrix} [2 2 ]

tf.nrom(a,ord=2,axis=1)

<tf.Tensor: , shape=(2,), dtype=float32, numpy=array([1.4142135, 1.4142135], dtype=float32) >

L 1 L_1 L1 Norm

计算下面矩阵的 L 1 L_1 L1 范数
[ 1 1 1 1 ] \begin{bmatrix} 1 & 1\\ 1 & 1 \end{bmatrix} [1111]
∣ 1 ∣ + ∣ 1 ∣ + ∣ 1 ∣ + ∣ 1 ∣ = 4 |1|+|1|+|1|+|1|=4 1+1+1+1=4

tf.nrom(a,ord=1)

<tf.Tensor: , shape=(2,), dtype=float32, numpy=4.0>

计算某一个维度的 L 1 L_1 L1 范数
当 axis=1
[ ∣ 1 ∣ + ∣ 1 ∣ ∣ 1 ∣ + ∣ 1 ∣ ] = [ 2 2 ] \begin{bmatrix} |1|+|1| \\ |1|+|1| \end{bmatrix}=\begin{bmatrix} 2 \\ 2 \end{bmatrix} [1+11+1]=[22]

tf.nrom(a,ord=1,axis=1)

<tf.Tensor: , shape=(2,), dtype=float32, numpy=array([2., 2.],dtype=float32)>

tf.reduce_min/max/mean

a=tf.random.normal[4,10]
tf.reduce_min(a).tf.reduce_max(a),tf.reduce_mean(a)

不给维度的情况下,所有数进行降维处理 => 标量

(<tf.Tesnfor: , shape=(), dtype=float32, numpy=-1.1872448>,
<tf.Tesnfor: , shape=(), dtype=float32, numpy=2.1353827>,
<tf.Tesnfor: , shape=(), dtype=float32, numpy=0.3523524>,)

axis=0 => 对每一列运算,得到[110]的向量
axis=1 => 对每一行运算,得到[4
1]的向量

tf.argmin/argmax

a为[4*10]的矩阵

tf.argmax(a)

返回每一列最大值的位置索引

<tf.Tesnfor: , shape=(10,), dtype=int64, numpy=array([0, 0, 2, 3, 3, 1, 0, 1, 3, 0])>

tf.equal

a=tf.constant([1,2,3,2,4])
b=tf.range(5)
tf.equal(a,b)

b为[0,1,2,3,4]

<tf.Tesnfor: , shape=(10,), dtype=bool, numpy=array([False, False, False, False, True])>

举个栗子:求解准确度
准确率
步骤1: 预测结果放在矩阵a中
[ 0.1 0.2 0.7 0.9 0.05 0.05 ] \begin{bmatrix} 0.1 & 0.2 & 0.7 \\ 0.9 & 0.05 & 0.05 \end{bmatrix} [0.10.90.20.050.70.05]
意味着:
第一个预测,预测0的概率为0.1,预测1的概率为0.2,预测2的概率为0.7
第二个预测,预测0的概率为0.9,预测1的概率为0.05,预测2的概率为0.05

找到每一行最大概率值的索引位置,即:第一行为2,第二行为0
再利用tf.cast()函数,将float32数据类型 => int32数据类型
从而得到这样的向量 [ 2 0 ] \begin{bmatrix} 2 \\ 0 \end{bmatrix} [20]

pred=tf.cast(tf.argmax(a,axis=1), dtype=tf.int32)

<tf.Tesnfor: , shape=(2,), dtype=int32, numpy=array([2, 0])>

步骤2: 比较预测值和真实值
真实值放在y向量中,即 y = [ 2 , 1 ] y=[2,1] y=[2,1]
所以第一个预测正确,第二个预测错误,将得到一个[True, False]的向量

tf.qual(y,pred)

<tf.Tesnfor: , shape=(2,), dtype=bool, numpy=array([True, False])>

步骤3:计算准确度

correct=tf.reduce_sum(tf.cast(tf.qual(y,pred),dtype=tf.int32))
correct/2

correct为预测正确的个数
True => 1
False => 0
所以准确率应该为50%

<tf.Tesnfor: , shape=(), dtype=float64, numpy=0.5>

tf.unique

a=tf.constant([4,2,2,4,3])
tf.unique(a)

返回不重复的元素,以及他们的索引

Unique(y=<tf.Tensor , shape=(3,), dtype=int32, numpy=array([4,2,3], dtype=int32)>,
idx=<tf.Tensor , shape(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2]), dtype=int32)>)

unique

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值