【Pytorch实现】——LeNet网络

【Pytorch实现】——LeNet网络

import torch
from torch import nn
from d2l import torch as d2l

# 继承nn.Module类,实现__init__和forward
class Reshape(nn.Module):
  def forward(self,x):
    # 将图片形状变成BxCxHxW
    return x.reshape(-1,1,28,28)

# nn.Sequential可以包装任何继承nn.Module实现的实例
net = nn.Sequential(
    Reshape(),
    # F = 2 * P + 1 特征图尺寸不变
    nn.Conv2d(1,6,kernel_size=5,padding=2),
    nn.Sigmoid(),
    # 平均池化 特征图尺寸减小为原来的一半
    nn.AvgPool2d(kernel_size=2,stride=2),
    nn.Conv2d(6,16,kernel_size=5),
    nn.Sigmoid(),
    nn.AvgPool2d(kernel_size=2,stride=2),
    # nn.Flatten操作仅保留第一个维度将其他维度打平
    nn.Flatten(),
    nn.Linear(16*5*5,120),
    nn.Sigmoid(),
    nn.Linear(120,84),
    nn.Sigmoid(),
    nn.Linear(84,10)
)

X = torch.randn(size=(4,1,28,28),dtype=torch.float32)
# nn.Sequential()包装后相当于将不同层存放到一个list中
# 迭代打印经过每一层后特征图尺寸变化(get)
for layer in net:
  X = layer(X)
  print(layer.__class__.__name__, 'output shape: \t', X.shape)
Reshape output shape: 	 torch.Size([4, 1, 28, 28])
Conv2d output shape: 	 torch.Size([4, 6, 28, 28])
Sigmoid output shape: 	 torch.Size([4, 6, 28, 28])
AvgPool2d output shape: 	 torch.Size([4, 6, 14, 14])
Conv2d output shape: 	 torch.Size([4, 16, 10, 10])
Sigmoid output shape: 	 torch.Size([4, 16, 10, 10])
AvgPool2d output shape: 	 torch.Size([4, 16, 5, 5])
Flatten output shape: 	 torch.Size([4, 400])
Linear output shape: 	 torch.Size([4, 120])
Sigmoid output shape: 	 torch.Size([4, 120])
Linear output shape: 	 torch.Size([4, 84])
Sigmoid output shape: 	 torch.Size([4, 84])
Linear output shape: 	 torch.Size([4, 10])
  • 上面我们是手动打印每一层的名称和参数,其实Pytorch中提供了一种更加便捷的工具summary
import torch
from torch import nn
from d2l import torch as d2l
from torchsummary import summary

# 继承nn.Module类,实现__init__和forward
class Reshape(nn.Module):
  def forward(self,x):
    # 将图片形状变成BxCxHxW
    return x.reshape(-1,1,28,28)

# nn.Sequential可以包装任何继承nn.Module实现的实例
net = nn.Sequential(
    Reshape(),
    # F = 2 * P + 1 特征图尺寸不变
    nn.Conv2d(1,6,kernel_size=5,padding=2),
    nn.Sigmoid(),
    # 平均池化 特征图尺寸减小为原来的一半
    nn.AvgPool2d(kernel_size=2,stride=2),
    nn.Conv2d(6,16,kernel_size=5),
    nn.Sigmoid(),
    nn.AvgPool2d(kernel_size=2,stride=2),
    # nn.Flatten操作仅保留第一个维度将其他维度打平
    nn.Flatten(),
    nn.Linear(16*5*5,120),
    nn.Sigmoid(),
    nn.Linear(120,84),
    nn.Sigmoid(),
    nn.Linear(84,10)
)

X = torch.randn(size=(4,1,28,28),dtype=torch.float32)
summary(net,(4,1,28,28))
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
           Reshape-1            [-1, 1, 28, 28]               0
            Conv2d-2            [-1, 6, 28, 28]             156
           Sigmoid-3            [-1, 6, 28, 28]               0
         AvgPool2d-4            [-1, 6, 14, 14]               0
            Conv2d-5           [-1, 16, 10, 10]           2,416
           Sigmoid-6           [-1, 16, 10, 10]               0
         AvgPool2d-7             [-1, 16, 5, 5]               0
           Flatten-8                  [-1, 400]               0
            Linear-9                  [-1, 120]          48,120
          Sigmoid-10                  [-1, 120]               0
           Linear-11                   [-1, 84]          10,164
          Sigmoid-12                   [-1, 84]               0
           Linear-13                   [-1, 10]             850
================================================================
Total params: 61,706
Trainable params: 61,706
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.01
Forward/backward pass size (MB): 0.12
Params size (MB): 0.24
Estimated Total Size (MB): 0.37
----------------------------------------------------------------
  • 调用summary函数后输出内容包括:
  • 名称Layer
  • 输出特征图大小Output Shape (注意输出大小的Batch维度为-1)
  • 参数量 Param
  • 总参数量 Total params
  • 可训练参数量 Trainable params
  • 不可训练参数量 Non-trainable params
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值