model.summary()如何像在 Keras 中那样在 PyTorch 中打印模型摘要:
model.summary()
Model Summary: ____________________________________________________________________________________________________ Layer (type) Output Shape Param # Connected to ==================================================================================================== input_1 (InputLayer) (None, 1, 15, 27) 0 ____________________________________________________________________________________________________ convolution2d_1 (Convolution2D) (None, 8, 15, 27) 872 input_1[0][0] ____________________________________________________________________________________________________ maxpooling2d_1 (MaxPooling2D) (None, 8, 7, 27) 0 convolution2d_1[0][0] ____________________________________________________________________________________________________ flatten_1 (Flatten) (None, 1512) 0 maxpooling2d_1[0][0] ____________________________________________________________________________________________________ dense_1 (Dense) (None, 1) 1513 flatten_1[0][0] ==================================================================================================== Total params: 2,385 Trainable params: 2,385 Non-trainable params: 0
在 PyTorch 中,您可以使用该torchsummary库torchinfo来打印model.summary()。
torchsummary
torchinfo
pip install torchsummary
summary()
``` from torchsummary import summary
im import torch
impo import torch.nn as nn
impo import torch.nn.functional as F
cl
class SimpleCNN(nn.Module):
def init(self):
supe
super(SimpleCNN, self).init() self.conv1 = nn.Conv2d( self.conv1
self.con self.
1, 8, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(kernel_size= self.pool = nn.MaxPool2d(kernel_size=
self.pool = nn.MaxPool2d(kernel_size self.pool = nn.MaxPool2d(kernel self.pool = nn.MaxPool2 self.pool = nn.MaxPool self.pool = nn self
2, stride=2) self.fc1 = nn.Linear( self.fc1 = nn.Linear(
self.fc1 =
8 * 7 * 13, 1) # Adjust according to input size
def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = x.view(- x = self.pool(F.relu(self.conv1(x))) x =
x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv x = self.pool x =
1, 8 * 7 * 13) # Flatten for fully connected layer x = self.fc1(x)
x = self.fc1(x) x = self.fc1(x) x = self.fc1(x x = self
return x
# Create an instance of your model model = SimpleCNN()
model = # Print summary (input size is (batch_size, channels, height, width)) summary(model, input_size=( su 1, 1, 15, 27)) ```
输出
```
Layer (type) Output Shape Param #
================================================================ Conv2d-1 [-1, 8, 15, 27] 80 MaxPool2d-2 [-1, 8, 7, 13] 0 Linear-3 [-1, 1] 7,297 ================================================================ Total params: 7,377 Trainable params: 7,377 Non-trainable params: 0
PyTorch 现在有一个名为的内置替代方案torchinfo,它
pip install torchinfo
``` from torchinfo import summary import torch.nn as nn import torch.nn.functional as F
class SimpleCNN(nn.Module): def init(self):
super(SimpleCNN, self).init() self.conv1 = nn.Conv2d( self.conv1 = nn.Conv2d
self.conv1 self.conv
self.pool = nn.MaxPool2d(kernel_size self.pool = nn.MaxPool2 self.pool = nn self.pool
self.fc1 = self.fc1
8 * 7 * 13, 1)
def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = x.view(- x = self.pool(F.relu(self.conv1(x))) x = x.view(-
x = self.pool(F.relu(self.conv1(x))) x x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self x = self.pool x =
1, 8 * 7 * 13) x = self.fc1(x)
x = self.fc1(x)
model = SimpleCNN()
summary(model, input_size=(
summary(model,
summary(model
model = Simple
1, 1, 15, 27)) ```