小能豆

如何在 PyTorch 中打印模型摘要?

py

model.summary()如何像在 Keras 中那样在 PyTorch 中打印模型摘要:

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

阅读 40

收藏
2024-10-12

共1个答案

小能豆

在 PyTorch 中,您可以使用该torchsummarytorchinfo来打印model.summary()

使用torchsummary

  1. torchsummary如果您尚未安装该包,请安装它:

pip install torchsummary

  1. 使用summary()乐趣torchsummary

```
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


```

使用torchinfo(ne

PyTorch 现在有一个名为的内置替代方案torchinfo,它

  1. 安装torchinfo

pip install torchinfo

  1. 使用summary()以下函数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

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.MaxPool2

       self.pool = nn

       self.pool

2, stride=2)
self.fc1 = nn.Linear(
self.fc1 = nn.Linear(

       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)

return x

model = SimpleCNN()

summary(model, input_size=(

model = SimpleCNN()

summary(model,

model = SimpleCNN()

summary(model

model = SimpleCNN()

model = Simple

1, 1, 15, 27))
```

2024-10-12