Atte nti o n注意力机制..pptx

上传人:zf 文档编号:30861606 上传时间:2024-05-02 格式:PPTX 页数:12 大小:1.52MB
下载 相关 举报
Atte nti o n注意力机制..pptx_第1页
第1页 / 共12页
Atte nti o n注意力机制..pptx_第2页
第2页 / 共12页
Atte nti o n注意力机制..pptx_第3页
第3页 / 共12页
Atte nti o n注意力机制..pptx_第4页
第4页 / 共12页
Atte nti o n注意力机制..pptx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

Atte nti o n注意力机制..pptx

《Atte nti o n注意力机制..pptx》由会员分享,可在线阅读,更多相关《Atte nti o n注意力机制..pptx(12页珍藏版)》请在冰豆网上搜索。

Atte nti o n注意力机制..pptx

Attention1注意力机制Sequence2Sequence2NLPCVself-attentionq=linear(query,_w,_b)k=linear(key,_w,_b)v=linear(value,_w,_b)head_dim=embed_dim/num_headsscaling=float(head_dim)*-0.5q=q*scalingattn_output_weights=torch.bmm(q,k.transpose(1,2)attn_output_weights=softmax(attn_output_weights,dim=-1)attn_output=torch.bmm(attn_output_weights,v)XXXX1Soft-maxqk0垂直内积为0,相关度小内积越大,关系越近3Mult-headself-attention4Architectureflatten(C,H*W)NN匈牙利算法:

一一匹配GT5PositionalEncoding注入,绝对位置和相对次序信息。

1.体现同一个单词在不同位置的区别2.体现一定的先后次序关系,并且在一定范围内的编码差异不应该依赖于文本长度,具有一定不变性方法1:

直接编码,PE=0,1,2,*,T-1方法2:

归一化编码,PE=pos/(T-1),不同长度文本的位置编码步长是不同的pos:

isthepositioni:

isthedimension(channel)0,1,2,3,.,127/2d_model超参数,默认256(生成的维度)/2=128T偶数奇数i:

channeli=0,T=10000*0=1,波长=2pi一个振动周期内传播的距离i=127/2,T=10000*1=10000,波长=10000*2pi注意:

区分了奇偶数维度的函数形式。

这使得每一维度上都包含了一定的位置信息,而各个位置字符的位置编码又各不相同。

(不同维度使用不同频率的正/余弦公式进而生成不同位置的高维位置向量。

)使编码更丰富https:

/sin和cos:

transformer-Encodersrchw,b,256srcsrc2posq=k=self.with_pos_embed(src,pos)src2=self.self_attn(q,k,value=src,attn_mask=src_mask,key_padding_mask=src_key_padding_mask)0src=src+self.dropout1(src2)src=self.norm1(src)src2=self.linear2(self.dropout(self.activation(self.linear1(src)src=src+self.dropout2(src2)src=self.norm2(src)returnsrchw,b,2567q=k=self.with_pos_embed(tgt,query_pos)tgt2=self.self_attn(q,k,value=tgt,attn_mask=tgt_mask,key_padding_mask=tgt_key_padding_mask)0tgt=tgt+self.dropout1(tgt2)tgt=self.norm1(tgt)tgt2=self.multihead_attn(query=self.with_pos_embed(tgt,query_pos),key=self.with_pos_embed(memory,pos),value=memory,attn_mask=memory_mask,key_padding_mask=memory_key_padding_mask)0tgt=tgt+self.dropout2(tgt2)tgt=self.norm2(tgt)tgt2=self.linear2(self.dropout(self.activation(self.linear1(tgt)tgt=tgt+self.dropout3(tgt2)tgt=self.norm3(tgt)returntgttgtposquery_pos(100,b,256)init=random(论文0)可训练tgttgttgt2memoryaddtgt28transformer-DecodertransformerencoderencoderencoderencoderencoderencoderdecoderdecoderdecoderdecoderdecoderdecoderspatialpositionencodingImagefeaturemapdefforward(self,src,mask,query_embed,pos_embed):

#flattenNxCxHxWtoHWxNxCbs,c,h,w=src.shapesrc=src.flatten

(2).permute(2,0,1)pos_embed=pos_embed.flatten

(2).permute(2,0,1)query_embed=query_embed.unsqueeze

(1).repeat(1,bs,1)mask=mask.flatten

(1)tgt=torch.zeros_like(query_embed)memory=self.encoder(src,src_key_padding_mask=mask,pos=pos_embed)hs=self.decoder(tgt,memory,memory_key_padding_mask=mask,pos=pos_embed,query_pos=query_embed)returnhs.transpose(1,2),memory.permute(1,2,0).view(bs,c,h,w)memorypos_embedquery_embedtgtsrcmask9FFNFFNnonoobjectobjectclass,class,boxboxclass,class,boxboxnonoobjectobjectclass,class,boxboxself.bbox_embed:

ModuleList(0):

Linear(in_features=256,out_features=256,bias=True)

(1):

Linear(in_features=256,out_features=256,bias=True)

(2):

Linear(in_features=256,out_features=4,bias=True)self.class_embed=nn.Linear(hidden_dim,num_classes+1)10训练11gitclonehttps:

/install-Ugit+https:

/installgit+https:

/jsonfilestrain2017/#trainimagesval2017/#valimagespython-mtorch.distributed.launch-nproc_per_node=8-use_envmain.py-coco_path/path/to/cocopython-mtorch.distributed.launch-nproc_per_node=8-use_envmain.py-masks-coco_path/path/to/coco1.transform-attention与CNN的区别2.transform-attention与全连接fc的区别讨论12

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 中医中药

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1