当前位置:网站首页>Implementation of neural network attention mechanism by pytoch code
Implementation of neural network attention mechanism by pytoch code
2022-04-21 22:56:00 【Graduate students are not late】
List of articles
1 The birth of attention mechanism
- Attention mechanism , It was first known as a work in natural language processing , article
Attention is all you needIn detail “ What is attention mechanism ”, Interested partners can download the original text to see .

SeNet-Squeeze-and-Excitation NetworksIt is one of the early works of the application of attention mechanism in computer vision , And got it. 2017 Year of imagenet, The last one imagenet The champion of the competition .

2 Introduce SeNet Models and Pytorch Code implementation
import numpy as np
import torch
from torch import nn
from torch.nn import init
class SEAttention(nn.Module):
def __init__(self, channel=512, reduction=16):
super().__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1) # Global mean pooling The output is c×1×1
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False), # channel // reduction Represents channel compression
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False), # Restore
nn.Sigmoid()
)
def init_weights(self):
for m in self.modules():
print(m) # Not running here
if isinstance(m, nn.Conv2d): # Judge type function ——:m yes nn.Conv2d Class? ?
init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
init.normal_(m.weight, std=0.001)
if m.bias is not None:
init.constant_(m.bias, 0)
def forward(self, x):
b, c, _, _ = x.size() # 50×512×7×7
y = self.avg_pool(x).view(b, c) # ① maxpool After that :50×512×1×1 ② view Shape get 50×512
y = self.fc(y).view(b, c, 1, 1) # 50×512×1×1
return x * y.expand_as(x) # according to x.size To expand y
if __name__ == '__main__':
input = torch.randn(50, 512, 7, 7)
se = SEAttention(channel=512, reduction=8) # Instantiation model se
output = se(input)
print(output.shape)
- Also have say SeNet This is the model of :( I think it's too simple )
class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
版权声明
本文为[Graduate students are not late]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212033275195.html
边栏推荐
- Event distribution mechanism Android, Android interview summary of large manufacturers, detailed answers
- CV God of war common code ----- kj15
- How does the applet integrate instant messaging with instant messaging
- Cleaning robot - infrared down looking sensor to detect whether there is a sudden change in ground distance
- 【mq】从零开始实现 mq-01-生产者、消费者启动
- 循环队列与扩容
- 1. MySQL workbench 8.0 installation
- 黑盒测试-数据的读取与输出方式
- Languages supported by markdown code blocks
- [summary of some tips and shortcut keys in MATLAB]
猜你喜欢

Pyqt5 + opencv operate local camera

行业分析| 互联网医疗的发展

1957年高考数学题

Collection of some websites

Oracle database 22c insight:_ kgl_ Large_ heap_ assert_ Threshold automatic and manual adjustment

2022 intermediate accounting title examination accounting practice exercises and answers

音视频基本概念和FFmpeg的简单入门

We sincerely invite you to sign up for the first openharmony developer growth plan sharing day

OS实验三【进程通信】

2022r2 mobile pressure vessel filling test exercises and online simulation test
随机推荐
Core component in opencv - input / output XML, yaml (12)
How does the applet integrate instant messaging with instant messaging
2022r2 mobile pressure vessel filling test exercises and online simulation test
6. Example of QT using MySQL
2022 Intermediate Accounting Title Financial Management exercises and answers
Collection of some websites
【中南林业科技大学】【陈】第七周创新题迷宫
Nacos Registry - service registration and tiered storage
VOS6.0安装及源码命令
将模型训练外包真的安全吗?新研究:外包商可能植入后门,控制银行放款
Basic concepts of audio and video and a simple introduction to ffmpeg
行业分析| 互联网医疗的发展
图像采集卡的概念及作用原理
Analysis on the underlying principle of MySQL transaction and isolation level
8.3 create a mobile robot by hand in rodf robot modeling
CV God of war common code ----- kj15
Deploying wikijs using Helm
MySQL 第3章 SQL基础语法
PyQt5+OpenCV操作本地摄像头
Fine grained emotion analysis practice