我是深度学习的新手,我在学习时遇到了这个。
有没有办法将这里的 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
您可以将线性层与分类器分开定义为独立层:
self.linear = nn.Linear(in_features=hidden_units*7*7,
out_features=output_shape))
那么在forward函数中,等效的实现是:
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