当前位置:网站首页>向量和矩阵的范数
向量和矩阵的范数
2022-07-27 05:13:00 【Mr_health】
1. 基本定义
具体可以参考这篇文章:https://blog.csdn.net/qq_42138662/article/details/109258028
对于我自己的理解,希望强调一下:
向量的范数≠矩阵的范数
2. 类比记忆
| 定义 | 等价于 | 对应的归一化方法 | |
| L0范数:向量中非0元素的个数 | |||
L1范数:向量元素绝对值之和![]() | 向量到原点的曼哈顿距离 | L1损失函数:
多了求平均 | |
L2范数:向量元素平方和再开方![]() | 向量到原点的欧氏距离 | L2范数归一化 | L2损失函数:
少了开方 多了求平均 |
2. pytorch实现L1范数和L2范数
img1 = torch.randn((4,3,16,16))
img2 = torch.randn((4,3,16,16))
#L1 范数是tensor的所有元素的绝对值之和,可以理解为tensor到0的曼哈顿距离
img1_L1 = torch.norm(img1,p = 1) #求img1的L1范数,即img1所有元素的绝对值之和
img1_L1_my = torch.abs(img1).sum()
print(img1_L1.equal(img1_L1_my)) #返回False 略微有一点不一样
print(img1_L1 - img1_L1_my)
#L1 范数是tensor的所有元素的平方之和开根号,其实可以理解为tensor到0的欧氏距离
img2_L2 = torch.norm(img2,p = 2) #求img2的L2范数,即img2所有元素平方和开根号
img2_L2_my = torch.sqrt(img2.pow(2).sum())
print(img2_L2.equal(img2_L2_my)) #返回False 略微有一点不一样
print(img2_L2 - img2_L2_my) #相差非常小 小数点五位
3. L2范数归一化
- 定义
L2范数归一化处理操作是对向量X的每个维度数据x1, x2, …, xn都除以||x||2得到一个新向量,即:

- 为什么要进行L2范数归一化?(优势)
L2范数有一大优势:经过L2范数归一化后,一组向量的欧式距离和它们的余弦相似度可以等价
一个向量X经过L2范数归一化得到向量X2,同时另一个向量Y经过L2范数归一化得到向量Y2。此时X2和Y2的欧式距离和余弦相似度是等价的。具体具体的证明见博客L2范数归一化概念和优势
也就是说,我们对向量进行L2范数归一化,在求取它们之间的欧氏距离,就相当于求取他们之间的相似度了。因此对它们的欧氏距离进行优化(作为loss),就相当于优化它们的相似度,让它们更相近。具体的应用在蒸馏的论文中:Paying More Attention to Attention: Improving the Performance of Convolutional Neural Netkworks wia Attention Transfer
在论文中,学生模型和老师模型features maps间的loss为:
其中Qs为学生的feature maps,Qt为老师的feature maps。首先对Qs和Qt均进行L2范数归一化,得到
和
。之后令p=2,即是求经过归一化的Qs和Qt之间的欧氏距离,相当于求Qs和Qt的相似度。通过优化这个loss,等价于让学生的特征图更接近于老师的特征图,进而完成蒸馏。
边栏推荐
- 1. Introduction to pytorch
- GBASE 8C——SQL参考6 sql语法(9)
- 难道Redis真的变慢了吗?
- golang封装mysql涉及到的包以及sqlx和gorm的区别
- How MySQL and redis ensure data consistency
- Seven enabling schemes of m-dao help Dao ecology move towards mode and standardization
- 3. Classification problems - initial experience of handwritten digit recognition
- 14.实例-多分类问题
- Count the quantity in parallel after MySQL grouping
- Day 2. Depressive symptoms, post-traumatic stress symptoms and suicide risk among graduate students
猜你喜欢

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business

新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic

万字解析MySQL索引原理——InnoDB索引结构与读取

关于pytorch反向传播的思考

MySQL如何执行查询语句

12.优化问题实战

11.感知机的梯度推导

【好文种草】根域名的知识 - 阮一峰的网络日志

Day 6.重大医疗伤害事件网络舆情能量传播过程分析*———以“魏则西事件”为例
随机推荐
8.数学运算与属性统计
How to not overwrite the target source data when dBSwitch data migrates data increments
Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
Day 17.The role of news sentiment in oil futures returns and volatility forecasting
6.维度变换和Broadcasting
新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic
Gbase 8C - SQL reference 6 SQL syntax (11)
dpdk 网络协议栈 vpp OvS DDos SDN NFV 虚拟化 高性能专家之路
「中高级试题」:MVCC实现原理是什么?
Day 8.Developing Simplified Chinese Psychological Linguistic Analysis Dictionary for Microblog
andorid检测GPU呈现速度和过度绘制
Andorid detects GPU rendering speed and over rendering
Gbase 8C - SQL reference 6 SQL syntax (5)
Getaverse, a distant bridge to Web3
16.过拟合欠拟合
Docker deploys the stand-alone version of redis - modify the redis password and persistence method
If you encounter oom online, how to solve it?
常用adb命令汇总 性能优化
MySQL索引优化相关原理
MySQL索引失效与解决方法实践



