我在卷积后面接gmlp,我写的有错误吗?错误在哪里?

forward 方法中,你调用了 self.gmlp(x),但是在初始化方法中并没有定义 self.gmlp。你需要在初始化方法中添加 self.gmlp = gMLP( d_model=40, d_ffn=80, seq_len=10, num_layers=6) 来定义 gmlp 模型。另外,在 forward 方法中,你还需要将 x 传递给 gmlp 模型进行处理。以下是修改后的代码:

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__()
        self.conv1 = nn.Sequential(
            nn.Conv2d(
                in_channels=1,
                out_channels=5,
                kernel_size=(200, 3),
                stride=(50, 1),
                padding=1,
            ),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, padding=1),
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(5, 10, (20, 2), (4, 1), 1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2),
        )
        self.gmlp = gMLP( d_model=40, d_ffn=80, seq_len=10, num_layers=6)  # 添加gmlp模型
        self.linear = nn.Linear(in_features=40, out_features=10)
        self.flatten = nn.Flatten()
        self.out = nn.Linear(10 * 10 * 4, 6)

    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)  # 使用gmlp模型处理x
        x = self.linear(x)
        x = self.flatten(x)
        feature = x
        output = self.out(x)
        return feature, output

这样修改后,你就可以在卷积层之后接入gmlp模型了。

在卷积神经网络中使用gMLP模型

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

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