如何将nn.Sequential代码转换为nn.Linear

0 投票
2 回答
37 浏览
提问于 2025-04-12 07:34

我刚开始学习深度学习,遇到了一些问题。

有没有办法把这里的 nn.Sequential() 函数转换成 nn.Linear() 呢?因为使用 nn.Linear() 函数会更灵活。

class FashionMNISTModelV2(nn.Module):
def __init__(self, input_shape: int, hidden_units: int, output_shape: int):
    super().__init__()
    self.block_1 = nn.Sequential(
        nn.Conv2d(in_channels=input_shape, 
                  out_channels=hidden_units, 
                  kernel_size=3,
                  stride=1,
                  padding=1),
        nn.ReLU(),
        nn.Conv2d(in_channels=hidden_units, 
                  out_channels=hidden_units,
                  kernel_size=3,
                  stride=1,
                  padding=1),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2,
                     stride=2)
    )
    self.block_2 = nn.Sequential(
        nn.Conv2d(hidden_units, hidden_units, 3, padding=1),
        nn.ReLU(),
        nn.Conv2d(hidden_units, hidden_units, 3, padding=1),
        nn.ReLU(),
        nn.MaxPool2d(2)
    )
    self.classifier = nn.Sequential(
        nn.Flatten(),
        nn.Linear(in_features=hidden_units*7*7, 
                  out_features=output_shape)
    )

def forward(self, x: torch.Tensor):
    x = self.block_1(x)
    x = self.block_2(x)
    x = self.classifier(x)
    return x

2 个回答

0

试试这个:

self.flatten = nn.Flatten()
self.linear1 = nn.Linear(in_features=hidden_units*7*7, out_features=output_shape))

def forward(self, x:torch.Tensor): 
x = self.block_1(x)
x = self.block_2(x)
x = self.flatten(x)
x = self.linear1(x)
return x

如果你不想在你的模型中添加扁平层,可以直接在前向传播函数里处理:

def forward(self, x:torch.Tensor):
x = self.block_1(x)
x = self.block_2(x)
x = x.view(x.size(0), -1)
x = self.linear1(x)
return x
0

你可以把线性层单独定义出来,作为一个独立的层,而不是和分类器放在一起:

self.linear = nn.Linear(in_features=hidden_units*7*7, 
                        out_features=output_shape))

然后在前向传播的函数中,相应的实现方式可以是:

def forward(self, x: torch.Tensor):
    x = self.block_1(x)
    x = self.block_2(x)
    x = x.flatten(1)
    x = self.linear(x)
    return x

撰写回答