当前位置: 首页 > news >正文

CNN和LSTM的计算复杂度分析

前言:今天做边缘计算的时候,在评估模型性能的时候发现NPU计算的大部分时间都花在了LSTM上,使用的是Bi-LSTM(耗时占比98%),CNN耗时很短,不禁会思考为什么LSTM会花费这么久时间。

 首先声明一下实验条件:这里使用的是振动信号,输入的数据,长度是1024,通道是1通道输入,batchsize也是1

一、CNN计算复杂度公式:

卷积核大小为 K x K,输入通道数为 C_in,输出通道数为 C_out,输入大小为 W x H

卷积操作的复杂度: O(K*K * C_in * C_out * W * H)

举个例子:我的第一个卷积层input:1channel,output:32channels,卷积核大小是1*3,为了保持输入数据长度和输出数据长度保持不变,padding=(k-1)/2=1

输入数据格式:1*1*1024(batchsize、channel、len)

输入数据格式: 1*32*1024

计算复杂度:1*32*3*1024

二、LSTM计算复杂度公式:

假设 LSTM 的隐藏层大小为 H,输入大小为 I,时间步数为 T

每个时间步的计算复杂度为 O(I * H + H^2)(包括矩阵乘法和激活函数)。

LSTM计算复杂度为 O(T * (I * H + H*H))

举个例子:输入大小是指上一层CNN输出的通道数128,隐藏层大小设置为128,时间步数就是数据长度:128

复杂度为:128*(128*128+128*128)=4194304

计算比例:4194304%(32*3*1024)=43%

因为这个是双层lstm:43*2=86符合预期,在实际计算中LSTM花费的时间更长,我估计是NPU对CNN结构的计算优化更好吧,下面是网络的完整结构

Layer: CNN_LSTM_Model
  Input shapes: [torch.Size([32, 1, 1024])]
  Output shape: torch.Size([32, 10])
Layer: Conv1d
  Input shapes: [torch.Size([32, 1, 1024])]
  Output shape: torch.Size([32, 32, 1024])
Layer: ReLU
  Input shapes: [torch.Size([32, 32, 1024])]
  Output shape: torch.Size([32, 32, 1024])
Layer: Conv1d
  Input shapes: [torch.Size([32, 32, 1024])]
  Output shape: torch.Size([32, 32, 1024])
Layer: ReLU
  Input shapes: [torch.Size([32, 32, 1024])]
  Output shape: torch.Size([32, 32, 1024])
Layer: MaxPool1d
  Input shapes: [torch.Size([32, 32, 1024])]
  Output shape: torch.Size([32, 32, 512])
Layer: Conv1d
  Input shapes: [torch.Size([32, 32, 512])]
  Output shape: torch.Size([32, 64, 512])
Layer: ReLU
  Input shapes: [torch.Size([32, 64, 512])]
  Output shape: torch.Size([32, 64, 512])
Layer: MaxPool1d
  Input shapes: [torch.Size([32, 64, 512])]
  Output shape: torch.Size([32, 64, 256])
Layer: Conv1d
  Input shapes: [torch.Size([32, 64, 256])]
  Output shape: torch.Size([32, 128, 256])
Layer: ReLU
  Input shapes: [torch.Size([32, 128, 256])]
  Output shape: torch.Size([32, 128, 256])
Layer: MaxPool1d
  Input shapes: [torch.Size([32, 128, 256])]
  Output shape: torch.Size([32, 128, 128])
Layer: Sequential
  Input shapes: [torch.Size([32, 1, 1024])]
  Output shape: torch.Size([32, 128, 128])
Layer: LSTM
  Input shapes: [torch.Size([32, 128, 128]), <class 'tuple'>]
  Output shapes: [torch.Size([32, 128, 256]), <class 'tuple'>]
Layer: Linear
  Input shapes: [torch.Size([32, 128, 256])]
  Output shape: torch.Size([32, 128, 256])
Layer: Attention
  Input shapes: [torch.Size([32, 128]), torch.Size([32, 128, 256])]
  Output shape: torch.Size([32, 1, 128])
Layer: LayerNorm
  Input shapes: [torch.Size([32, 256])]
  Output shape: torch.Size([32, 256])
Layer: ResidualConnection
  Input shapes: [torch.Size([32, 256]), <class 'function'>]
  Output shape: torch.Size([32, 256])
Layer: Linear
  Input shapes: [torch.Size([32, 256])]
  Output shape: torch.Size([32, 500])
Layer: ReLU
  Input shapes: [torch.Size([32, 500])]
  Output shape: torch.Size([32, 500])
Layer: Dropout
  Input shapes: [torch.Size([32, 500])]
  Output shape: torch.Size([32, 500])
Layer: Linear
  Input shapes: [torch.Size([32, 500])]
  Output shape: torch.Size([32, 10])
Layer: Sequential
  Input shapes: [torch.Size([32, 256])]
  Output shape: torch.Size([32, 10])

相关文章:

  • 5.2 WPF数据绑定简单示例1
  • 力扣DAY29 | 热100 | 删除链表的倒数第N个结点
  • tauri2.0安卓端开发踩坑之旅
  • 云安全之k8s未授权漏洞总结
  • 使用 OCRmyPDF 将扫描 PDF 转为可搜索文档和文本文件
  • 可发1区的超级创新思路(python\matlab实现):基于周期注意力机制的TCN-Informer时间序列预测模型
  • 深入解析 PKI(公钥基础设施):原理、应用与安全保障
  • 【科研绘图系列】R语言绘制重点物种进化树图(taxa phylogenetic tree)
  • el-tree-select选项数据无法回显
  • 简历诊断与面试指导:学校用AI开出“数字处方”,GAI认证助力学生求职
  • AI Agent开发大全第十三课-向量数据库Qdrant集群布署全步骤
  • firewall-cmd --list-all-zones 命令详解
  • 几个工作中常用的网站
  • Go语言nil原理深度解析:底层实现与比较规则
  • MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案
  • 进程通信 system V共享内存 ─── linux第25课
  • Dify实现自然语言生成SQL并执行
  • MySQL--权限管理
  • 性能测试理论基础-性能指标及jmeter中的指标
  • 如何编写单元测试
  • 网页制作网站素材/人力资源培训
  • wordpress站点标题图片/长沙网站快速排名提升
  • 课程资源网站的建设/2023第二波疫情已经到来了
  • wordpress搜索增加条件/海南快速seo排名优化
  • 怎么制作属于自己的网站/2024年新闻摘抄十条
  • 安徽网站建设费用/东莞seo优化