当前位置:网站首页>Position encoding practice in transformer
Position encoding practice in transformer
2022-07-04 16:39:00 【Beginner Chris】
In recent years ,transformer Because it can realize parallel computing and solve the dependency problem of long sequences, it is in nlp Areas and cv The field is brilliant .
The schematic diagram is as follows :
Here we mainly focus on a small part , namely position encoding part , because transformer Eliminate circular dependency , In order to reflect the location attribute , So encode the position of each element .
The code is as follows , As for why it is written like this , You can refer to the author's original , Or refer to the article .https://zhuanlan.zhihu.com/p/338592312
The code is as follows :
class PositionalEncoding(torch.nn.Module):
def __init__(self, d_model, max_len=5000):
super(PositionalEncoding, self).__init__()
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)#(max-len,1,d_model)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:x.size(1), :].squeeze(1)
#x = x + self.pe[:x.size(1), :]
return x
In order to test , We define two input matrices , Full respectively 0、 whole 1tensor.
d_model = 4
a=torch.zeros(2,3,4)
pos=PositionalEncoding(d_model)
b=pos(a)
c=torch.ones(2,3,4)
b1=pos(c)
Obviously , The input matrix is
Output is b,b1, As shown below :;
It can be seen that , Are based on input , Add a fixed value , And those fixed values are encoded , It's not about input , And d_model of ,d_model It can be understood as a word embedding size .
边栏推荐
- Accounting regulations and professional ethics [9]
- What does IOT engineering learn and work for?
- 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
- Market trend report, technical innovation and market forecast of China's hair repair therapeutic apparatus
- Unity animation day05
- Scientific research cartoon | what else to do after connecting with the subjects?
- AI system content recommendation issue 24
- TypeError: not enough arguments for format string
- .Net 应用考虑x64生成
- China Indonesia adhesive market trend report, technological innovation and market forecast
猜你喜欢
The vscode waveform curve prompts that the header file cannot be found (an error is reported if the header file exists)
Interface test - knowledge points and common interview questions
[North Asia data recovery] a database data recovery case where the partition where the database is located is unrecognized due to the RAID disk failure of HP DL380 server
Model fusion -- stacking principle and Implementation
Book of night sky 53 "stone soup" of Apache open source community
[native JS] optimized text rotation effect
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Scientific research cartoon | what else to do after connecting with the subjects?
Common knowledge of unity Editor Extension
Principle and general steps of SQL injection
随机推荐
Accounting regulations and professional ethics [10]
Research Report on surgical otorhinolaryngology equipment industry - market status analysis and development prospect prediction
Oracle监听器Server端与Client端配置实例
Application of clock wheel in RPC
Socks agent tools earthworm, ssoks
The four most common errors when using pytorch
China's plastic processing machinery market trend report, technological innovation and market forecast
Research Report on plastic recycling machine industry - market status analysis and development prospect forecast
Research Report on market supply and demand and strategy of China's plastics and polymer industry
一图看懂ThreadLocal
D3D11_ Chili_ Tutorial (2): draw a triangle
Interpretation of the champion scheme of CVPR 2020 night target detection challenge
C language: implementation of daffodil number function
Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China
APOC自定义函数和过程
Research Report on market supply and demand and strategy of China's Sodium Tetraphenylborate (cas+143-66-8) industry
Market trend report, technical innovation and market forecast of China's hair repair therapeutic apparatus
Filtered off site request to
Actual combat | use composite material 3 in application
Book of night sky 53 "stone soup" of Apache open source community