import torch import torch.nn as nn import torch.nn.functional as F from gmlp import SpatialGatingUnit, gMLPBlock, gMLP

class CNN(nn.Module): def init(self): # 初始化方法 super(CNN, self).init() # 初始化方法 # 第一层卷积层,输入为 (1,10000,12) self.conv1 = nn.Sequential( # input shape (1,10000,12) nn.Conv2d( # 二维卷积层 in_channels=1, # input height # 输入通道数 out_channels=5, # n_filters # 输出通道数(卷积核数量) kernel_size=(200, 3), # filter size # 卷积核大小 stride=(50, 1), # filter movement/step # 卷积核移动步长 padding=1, # 填充大小 ), nn.ReLU(), nn.MaxPool2d(kernel_size=2, padding=1), # 最大池化层 ) # 第二层卷积层,输入为 (5,99,7) self.conv2 = nn.Sequential( # input shape (5,99,7) nn.Conv2d(5, 10, (20, 2), (4, 1), 1), # output shape # 输出通道数为10,卷积核大小为(20,2),步长为(4,1),填充为1 nn.ReLU(), # activation nn.MaxPool2d(kernel_size=2), # 最大池化层 # output shape (10,10,4) )

    self.gmlp = gMLP(d_model=600, d_ffn=1200, seq_len=200, num_layers=6)  # 修改gMLP参数为第一个gMLP模型的参数
    self.out = nn.Linear(600, 6)  # 全连接层,输出6个类别

# 定义一个名为CNN的类,继承自PyTorch的nn.Module

def forward(self, x):  # 前向传播方法
    x = self.conv1(x)  # 通过第一个卷积层序列
    x = self.conv2(x)  # 通过第二个卷积层序列
    # 四维转三维
    N, C, H, W = x.shape
    x = x.view(N, C, H * W).permute(0, 2, 1)

    x = self.gmlp(x)
    x = x.mean(dim=1)
    feature = x
    output = self.out(x)  # 通过全连接层得到输出
    return feature, output  # 返回特征和输出

原文地址: https://www.cveoy.top/t/topic/phdf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录