打印神经网络各层的输出形状

在学习神经网络的时候,总是会出现mat1 和 mat2 不能相乘的问题,其实就是神经网络的某一层的输入和上一层的输出不对应,那如何在定义好网络结构之后进行检查呢?

以AlexNet网络为例,代码如下:

# 两种方法实现AlexNet
class AlexNet(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 96, 11, 4, 1)
        self.maxpool1 = nn.MaxPool2d(3, 2)
        self.conv2 = nn.Conv2d(96, 256, 5, padding=2)
        self.conv3 = nn.Conv2d(256, 384, 3, padding=1)
        self.conv4 = nn.Conv2d(384, 384, 3, padding=1)
        self.conv5 = nn.Conv2d(384, 256, 3, padding=1)
        self.flatten = nn.Flatten(1)
        self.linear1 = nn.Linear(6400, 4096)
        self.linear2 = nn.Linear(4096, 4096)
        self.linear3 = nn.Linear(4096, 10)
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout()

    def forward(self, input):
        h1 = self.relu(self.conv1(input))
        h1 = self.maxpool1(h1)
        h2 = self.relu(self.conv2(h1))
        h2 = self.maxpool1(h2)
        h3 = self.relu(self.conv3(h2))
        h4 = self.relu(self.conv4(h3))
        h5 = self.relu(self.conv5(h4))
        h5 = self.maxpool1(h5)
        h5 = self.flatten(h5)
        h6 = self.relu(self.linear1(h5))
        h7 = self.relu(self.linear2(h6))
        h7 = self.dropout(h7)
        h8 = self.linear3(h7)


        return h8

net = nn.Sequential( 
    nn.Conv2d(1, 96, 11, 4, 1),  nn.ReLU(),
    nn.MaxPool2d(3, 2),
    nn.Conv2d(96, 256, 5, padding=2),  nn.ReLU(),
    nn.MaxPool2d(3, 2),
    nn.Conv2d(256, 384, 3, padding=1),  nn.ReLU(),
    nn.Conv2d(384, 384, 3, padding=1),  nn.ReLU(),
    nn.Conv2d(384, 256, 3, padding=1),  nn.ReLU(),
    nn.MaxPool2d(3, 2),
    nn.Flatten(1),
    nn.Linear(6400, 4096),  nn.ReLU(),
    nn.Linear(4096, 4096),  nn.ReLU(),
    nn.Linear(4096, 10),  nn.ReLU(),
    nn.Dropout())
方法一

一种方法是在《动手学深度学习》中提到的,如果使用的是nn.Sequential()对网络进行定义,可以写一个循环,逐层计算和打印,代码如下

X = torch.rand((1, 1, 224, 224))
for layer in net:
    X=layer(X)
    print(layer.__class__.__name__,'output shape:\t',X.shape)

输入是这样的:

Conv2d output shape:	 torch.Size([1, 96, 54, 54])
ReLU output shape:	 torch.Size([1, 96, 54, 54])
MaxPool2d output shape:	 torch.Size([1, 96, 26, 26])
Conv2d output shape:	 torch.Size([1, 256, 26, 26])
ReLU output shape:	 torch.Size([1, 256, 26, 26])
MaxPool2d output shape:	 torch.Size([1, 256, 12, 12])
Conv2d output shape:	 torch.Size([1, 384, 12, 12])
ReLU output shape:	 torch.Size([1, 384, 12, 12])
Conv2d output shape:	 torch.Size([1, 384, 12, 12])
ReLU output shape:	 torch.Size([1, 384, 12, 12])
Conv2d output shape:	 torch.Size([1, 256, 12, 12])
ReLU output shape:	 torch.Size([1, 256, 12, 12])
MaxPool2d output shape:	 torch.Size([1, 256, 5, 5])
Flatten output shape:	 torch.Size([1, 6400])
Linear output shape:	 torch.Size([1, 4096])
ReLU output shape:	 torch.Size([1, 4096])
Linear output shape:	 torch.Size([1, 4096])
ReLU output shape:	 torch.Size([1, 4096])
Linear output shape:	 torch.Size([1, 10])
ReLU output shape:	 torch.Size([1, 10])
Dropout output shape:	 torch.Size([1, 10])

但是如果把这个方法,用到使用class定义的网络中,就会报错

所以就有了方法二

方法二

使用torchinfo这个包,可以对网络进行打印,方法如下:

安装torchinfo,大概20k左右的大小

pip install torchinfo

导入包中的summary函数

from torchinfo import summary

传入需要打印的网络和输入尺寸的形状(可以是一个批量)

net = AlexNet()
print(summary(net, (1, 1, 224, 224)))

结果如下:不仅输出了每层的形状,还包括参数量的情况

==========================================================================================
Layer (type:depth-idx)                   Output Shape              Param #
==========================================================================================
AlexNet                                  [1, 10]                   --
├─Conv2d: 1-1                            [1, 96, 54, 54]           11,712
├─ReLU: 1-2                              [1, 96, 54, 54]           --
├─MaxPool2d: 1-3                         [1, 96, 26, 26]           --
├─Conv2d: 1-4                            [1, 256, 26, 26]          614,656
├─ReLU: 1-5                              [1, 256, 26, 26]          --
├─MaxPool2d: 1-6                         [1, 256, 12, 12]          --
├─Conv2d: 1-7                            [1, 384, 12, 12]          885,120
├─ReLU: 1-8                              [1, 384, 12, 12]          --
├─Conv2d: 1-9                            [1, 384, 12, 12]          1,327,488
├─ReLU: 1-10                             [1, 384, 12, 12]          --
├─Conv2d: 1-11                           [1, 256, 12, 12]          884,992
├─ReLU: 1-12                             [1, 256, 12, 12]          --
├─MaxPool2d: 1-13                        [1, 256, 5, 5]            --
├─Flatten: 1-14                          [1, 6400]                 --
├─Linear: 1-15                           [1, 4096]                 26,218,496
├─ReLU: 1-16                             [1, 4096]                 --
├─Linear: 1-17                           [1, 4096]                 16,781,312
├─ReLU: 1-18                             [1, 4096]                 --
├─Dropout: 1-19                          [1, 4096]                 --
├─Linear: 1-20                           [1, 10]                   40,970
==========================================================================================
...
Forward/backward pass size (MB): 4.87
Params size (MB): 187.06
Estimated Total Size (MB): 192.13

 综上,通过两个方法都可以实现对神经网络中每层的输出情况的打印,当然,使用hook也可以更加灵活的实现,但是相对来说不如上面两种方法简单

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
打印神经网络的结构,可以使用TensorFlow提供的相关函数。可以通过以下步骤来完成: 1. 首先,导入TensorFlow库: ```python import tensorflow as tf ``` 2. 定义神经网络模型,这包括定义输入层、隐藏层和输出层的结构和连接方式。 3. 使用TensorFlow的`tf.keras.utils.plot_model`函数来绘制神经网络的结构图: ```python tf.keras.utils.plot_model(model, to_file='model.png', show_shapes=True) ``` 其中,`model`是你定义的神经网络模型,`to_file`是保存结构图的文件名,`show_shapes`参数用于显示每一层的输入和输出形状。 4. 最后,使用以下代码将生成的结构图打印出来: ```python from PIL import Image img = Image.open('model.png') img.show() ``` 通过上述步骤,你可以打印神经网络的结构图,进一步了解神经网络的组成和连接方式。请注意,这个方法适用于使用TensorFlow构建的神经网络模型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [DL with python(10)——TensorFlow实现神经网络参数的打印保存](https://blog.csdn.net/qq_36108664/article/details/107130565)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值