当前位置:网站首页>Are realrange and einsum really elegant
Are realrange and einsum really elegant
2022-04-23 07:27:00 【wujpbb7】
The conclusion is that , O.k. .
In terms of the amount of code , almost :
# -*- coding:utf-8 -*-
import torch
from torch import nn
from torch import einsum
from einops import rearrange
class SimpleQKV(nn.Module):
def __init__(self, dim, use_ein):
super().__init__()
self.proj = nn.Linear(dim, dim*3, bias=False)
self.dim = dim
self.scale = self.dim ** -0.5
self.use_ein = use_ein
torch.manual_seed(777) # To make the weights the same , Easy to compare output
nn.init.xavier_uniform_(self.proj.weight)
def forward(self, x):
n,c,h,w = x.shape
#assert c==self.dim
if (self.use_ein):
x = rearrange(x, 'n c h w -> n (h w) c')
else:
x = x.permute(0,2,3,1).view(n, -1, c)
qkv = self.proj(x)
q,k,v = qkv.chunk(chunks=3,dim=-1)
if (self.use_ein):
attn = (einsum('n i c, n j c -> n i j', q, k) * self.scale).softmax(dim=-1)
v = einsum('n i j, n j c -> n i c', attn, v)
output = rearrange(v, 'n (h w) c -> n c h w', h=h)
else:
attn = (torch.matmul(q, k.transpose(1,2)) * self.scale).softmax(dim=-1)
v = torch.matmul(attn, v)
output = v.permute(0,2,1).view(n,c,h,w)
return output
batch, chan, height, width = 1, 20, 7, 7
simple_qkv_ein = SimpleQKV(chan, True)
simple_qkv_noein = SimpleQKV(chan, False)
x = torch.randn(batch, chan, height, width, device='cpu')
out1 = simple_qkv_ein(x)
out2 = simple_qkv_noein(x)
assert(out1.equal(out2))
# preservation onnx
simple_qkv_ein.eval()
onnx_filename = './simple_qkv_ein.onnx'
torch.onnx.export(simple_qkv_ein, x, onnx_filename,
input_names=['input'], output_names=['ouput'],
export_params=True, verbose=False, opset_version=12)
simple_qkv_noein.eval()
onnx_filename = './simple_qkv_noein.onnx'
torch.onnx.export(simple_qkv_noein, x, onnx_filename,
input_names=['input'], output_names=['ouput'],
export_params=True, verbose=False, opset_version=12)
print('save onnx succ.')
From saved onnx see ( after onnxsim Optimize ), Also almost :
版权声明
本文为[wujpbb7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230611550455.html
边栏推荐
- 多机多卡训练时的错误
- 面试总结之特征工程
- MySQL的安装与配置——详细教程
- GIS实用小技巧(三)-CASS怎么添加图例?
- unhandled system error, NCCL version 2.7.8
- Systrace 解析
- 初探智能指针之std::shared_ptr、std::unique_ptr
- torch.where能否传递梯度
- Machine learning notes 1: learning ideas
- [3D shape reconstruction series] implicit functions in feature space for 3D shape reconstruction and completion
猜你喜欢
基于openmv的无人机Apriltag动态追踪降落完整项目资料(labview+openmv+apriltag+正点原子四轴)
CMSIS CM3源码注解
Résolution du système
FATFS FAT32学习小记
[3D shape reconstruction series] implicit functions in feature space for 3D shape reconstruction and completion
GIS实战应用案例100篇(五十一)-ArcGIS中根据指定的范围计算nc文件逐时次空间平均值的方法
MySQL installation and configuration - detailed tutorial
【点云系列】Pointfilter: Point Cloud Filtering via Encoder-Decoder Modeling
机器学习——PCA与LDA
Paddleocr image text extraction
随机推荐
直观理解 torch.nn.Unfold
基于51单片机的三路超声波测距系统(定时器方式测距)
PyMySQL连接数据库
如何利用qemu搭建SOC protoype:80行代码实现一个Cortex M4 模拟器
Solution to slow compilation speed of Xcode
初探智能指针之std::shared_ptr、std::unique_ptr
How to standardize multidimensional matrix (based on numpy)
ARMCC/GCC下的stack protector
scons 搭建嵌入式arm编译
Pymysql connection database
Pytorch model pruning example tutorial III. multi parameter and global pruning
Use originpro express for free
MySQL的安装与配置——详细教程
《Attention in Natural Language Processing》翻译
enforce fail at inline_container.cc:222
Summary of image classification white box anti attack technology
PyTorch 17. GPU并发
. net encountered failed to decode downloaded font while loading font:
STM32多路测温无线传输报警系统设计(工业定时测温/机舱温度定时检测等)
【点云系列】PnP-3D: A Plug-and-Play for 3D Point Clouds