怎样做网站pptseo排名软件免费
前言:
前面重点讲了self-attention, mulitHead self-attention.
目录:
- self-attention
- positional Encoding
- 语音处理例子
- 跟CNN区别
- 跟 RNN 区别
一 self-attention 回顾
优点
1 解决了长序列依赖问题
2 并行计算
缺点
1 开销变大 增加了 Q,K,V attention-score,B 矩阵
2 输入之间的位置信息丢失了 (PositionEncoding 可以解决)
RNN : 是按照 seq_num ,依次输入.能看到词与词之间的顺序关系
1.1 Q,K,V 矩阵生成,I 为输入张量
1.2 计算 self-attention 的 attention score
1.3 self-attention 输出
最后用矩阵表示可以看出来
二 Positional Encoding
2.1 作用:
把位置信息添加到当前的向量中.
2.2 主要流程
1: 计算位置编码信息
2: 当前向量加上对应的位置信息
,
通过sincos 产生的(也有其它方案)
2.3 例子
其中: 是通过下面PE编码得到.
偶数位置编码为
奇数位置编码为:
2.4 原理
利用和差化积定义
pos+k 位置的编码: 可以表示为pos 位置与k位置正线余弦的线性组合.从而蕴含了相对位置信息
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 26 17:29:07 2024@author: chengxf2
"""
import numpy as npdef pos_encoding(pos ,dmodel):N = int(dmodel/2)e = []for i in range(N):a1 = (2*i)/dmodela2 = (2*i+1)/dmodelw1 = pos/np.power(10000, a1)w2 = pos/np.power(10000, a2)even = round(np.sin(w1),4)odd = round(np.cos(w2),4)#print("\n i: %d %4.2f %4.2f"%(i,even, odd))e.append(even)e.append(odd)print(e)pos_encoding(1,512)
三 语音识别的例子
1 语音线分帧 10ms 一帧,1个列向量
2 1s 对应100帧,100个向量
3 一段语音对应的seq_len 是非常大的,10s 就对应seq_len=1000
2.1 直接使用selt-attention 计算量非常大
Q,K,V 总共需要 seq_len* [input_dim, hidden_dim]*3 内存大小
可以使用truncated self-attention, 每个向量只计算一小段
或者间隔采样.
四 self-attention Image Vs CNN
4.1 CNN
如上图,一张R,G,B 图片,输入 shape [batch, input_channel=3, height=5,width=3]
4.2 self-attention
输入 shape[batch, seq_len=width*height=10*5, input_dim= channel=3]
self-attention,依次对每个通道的图片做Q,K,V,然后计算出attention-score.
4.3 例子
https://arxiv.org/pdf/1805.08318v2
4.4 跟CNN区别
4.5 对比结果
self-attention 当数据集小的时候,容易过拟合.
低于1000W,self-attention 低于CNN
数据集大于CNN
五 self-attention VS RNN
5.1 RNN
RNN 每个vector 考虑了左边的输入,
RNN 没办法并行处理所有output
5.2 slef-attention
考虑了整个vector, 可以从非常远的位置抽取相关信息。
可以并行输出
运算速度更快。
参考:
self-attention下_哔哩哔哩_bilibili
14 Transformer之位置编码Positional Encoding (为什么 Self-Attention 需要位置编码)_哔哩哔哩_bilibili
Curated Color Palettes with Search and Tags Support
14 Transformer之位置编码Positional Encoding (为什么 Self-Attention 需要位置编码)_哔哩哔哩_bilibili