Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[summary] 显示的shape第一维的值和pdb调试打印的 shape 不一致 ? #2

Closed
vfdff opened this issue Nov 2, 2024 · 1 comment

Comments

@vfdff
Copy link

vfdff commented Nov 2, 2024

根据 summary 描述进行了简单的验证,确实对于其中的测试用例输出了 forward() 部份印出的模型

import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
# Model
class CNN(nn.Module):
    def __init__(self, classes):
        super(CNN, self).__init__()
        self.conv_1 = nn.Conv2d(in_channels=1,
                              out_channels=16,
                              kernel_size=5,
                              stride=1,
                              padding=0)

        self.conv_2 = nn.Conv2d(in_channels=16,
                                out_channels=32,
                                kernel_size=5,
                                stride=1,
                                padding=0)
        self.relu = nn.ReLU()
        self.max_pool = nn.MaxPool2d(kernel_size=2)
        self.fc = nn.Linear(32*4*4, classes)

    def forward(self, x):
        x = self.conv_1(x)
        x = self.relu(x)
        x = self.max_pool(x)
        x = self.conv_2(x)
        x = self.relu(x)
        x = self.max_pool(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x


if __name__ == '__main__':
    cnn = CNN(3000)
    print(cnn)
    summary(cnn, (1, 28, 28))

当前的显示结果中第一维使用了 -1,即无法推断吗 ?

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
            Conv2d-1           [-1, 16, 24, 24]             416
              ReLU-2           [-1, 16, 24, 24]               0
         MaxPool2d-3           [-1, 16, 12, 12]               0
            Conv2d-4             [-1, 32, 8, 8]          12,832
              ReLU-5             [-1, 32, 8, 8]               0
         MaxPool2d-6             [-1, 32, 4, 4]               0
            Linear-7                 [-1, 3000]       1,539,000

但是,直接基于pdb调试打印,我们能够看到第一维的大小是 2,不知道这个差异是否可以改进?

(Pdb) l
 27  	         self.max_pool = nn.MaxPool2d(kernel_size=2)
 28  	         self.fc = nn.Linear(32*4*4, classes)
 29  	
 30  	     def forward(self, x):
 31 B	         x = self.conv_1(x)
 32  ->	         x = self.relu(x)
 33  	         x = self.max_pool(x)
 34  	         x = self.conv_2(x)
 35  	         x = self.relu(x)
 36  	         x = self.max_pool(x)
 37  	         x = x.view(x.size(0), -1)
(Pdb) p x.shape
torch.Size([2, 16, 24, 24])
@vfdff
Copy link
Author

vfdff commented Nov 2, 2024

oh, I think this is similar to sksq96/pytorch-summary#168

@vfdff vfdff closed this as completed Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant