栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

学习笔记:动手学深度学习 29 深度卷积神经网络(AlexNet)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习笔记:动手学深度学习 29 深度卷积神经网络(AlexNet)

AlexNet 是更大更深的LeNet,10×参数个数,260×计算复杂度

新进入了丢弃发,ReLU,最大池化层和数据增强

AlexNet 赢下了2012 ImageNet竞赛后,标志着新的一轮神经网络热潮的开始

Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.22.0
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] on win32
import torch
from torch import nn
from d2l import torch as d2l
net = nn.Sequential(
    # 这里,我们使用一个11*11的更大窗口来捕捉对象。
    # 同时,步幅为4,以减少输出的高度和宽度。
    # 另外,输出通道的数目远大于LeNet
    nn.Conv2d(1, 96, kernel_size=11, stride=4, padding=1), nn.ReLU(),
    nn.MaxPool2d(kernel_size=3, stride=2),
    # 减小卷积窗口,使用填充为2来使得输入与输出的高和宽一致,且增大输出通道数
    nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(),
    nn.MaxPool2d(kernel_size=3, stride=2),
    # 使用三个连续的卷积层和较小的卷积窗口。
    # 除了最后的卷积层,输出通道的数量进一步增加。
    # 在前两个卷积层之后,汇聚层不用于减少输入的高度和宽度
    nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(),
    nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(),
    nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(),
    nn.MaxPool2d(kernel_size=3, stride=2),
    nn.Flatten(),
    # 这里,全连接层的输出数量是LeNet中的好几倍。使用dropout层来减轻过度拟合
    nn.Linear(6400, 4096), nn.ReLU(),
    nn.Dropout(p=0.5),
    nn.Linear(4096, 4096), nn.ReLU(),
    nn.Dropout(p=0.5),
    # 最后是输出层。由于这里使用Fashion-MNIST,所以用类别数为10,而非论文中的1000
    nn.Linear(4096, 10))
batch_size = 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
lr, num_epochs = 0.01, 10
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
training on cuda:0
Traceback (most recent call last):
  File "C:ProgramDataAnaconda3libsite-packagesIPythoncoreinteractiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "", line 2, in 
    d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
  File "C:ProgramDataAnaconda3libsite-packagesd2ltorch.py", line 496, in train_ch6
    y_hat = net(X)
  File "C:ProgramDataAnaconda3libsite-packagestorchnnmodulesmodule.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:ProgramDataAnaconda3libsite-packagestorchnnmodulescontainer.py", line 141, in forward
    input = module(input)
  File "C:ProgramDataAnaconda3libsite-packagestorchnnmodulesmodule.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:ProgramDataAnaconda3libsite-packagestorchnnmoduleslinear.py", line 103, in forward
    return F.linear(input, self.weight, self.bias)
  File "C:ProgramDataAnaconda3libsite-packagestorchnnfunctional.py", line 1848, in linear
    return torch._C._nn.linear(input, weight, bias)
RuntimeError: CUDA error: CUBLAS_STATUS_NOT_INITIALIZED when calling `cublasCreate(handle)`

因为显存不足,导致无法运行,调整了网络结构

Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.22.0
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] on win32
import torch
from torch import nn
from d2l import torch as d2l
net = nn.Sequential(
    # 这里,我们使用一个11*11的更大窗口来捕捉对象。
    # 同时,步幅为4,以减少输出的高度和宽度。
    # 另外,输出通道的数目远大于LeNet
    nn.Conv2d(1, 48, kernel_size=11, stride=4, padding=1), nn.ReLU(),
    nn.MaxPool2d(kernel_size=3, stride=2),
    # 减小卷积窗口,使用填充为2来使得输入与输出的高和宽一致,且增大输出通道数
    nn.Conv2d(48, 128, kernel_size=5, padding=2), nn.ReLU(),
    nn.MaxPool2d(kernel_size=3, stride=2),
    # 使用三个连续的卷积层和较小的卷积窗口。
    # 除了最后的卷积层,输出通道的数量进一步增加。
    # 在前两个卷积层之后,汇聚层不用于减少输入的高度和宽度
    nn.Conv2d(128, 192, kernel_size=3, padding=1), nn.ReLU(),
    nn.Conv2d(192, 192, kernel_size=3, padding=1), nn.ReLU(),
    nn.Conv2d(192, 128, kernel_size=3, padding=1), nn.ReLU(),
    nn.MaxPool2d(kernel_size=3, stride=2),
    nn.Flatten(),
    # 这里,全连接层的输出数量是LeNet中的好几倍。使用dropout层来减轻过度拟合
    nn.Linear(3200, 2048), nn.ReLU(),
    nn.Dropout(p=0.5),
    nn.Linear(2048, 2048), nn.ReLU(),
    nn.Dropout(p=0.5),
    # 最后是输出层。由于这里使用Fashion-MNIST,所以用类别数为10,而非论文中的1000
    nn.Linear(2048, 10))
X = torch.randn(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, 48, 54, 54])
ReLU Output shape:	 torch.Size([1, 48, 54, 54])
MaxPool2d Output shape:	 torch.Size([1, 48, 26, 26])
Conv2d Output shape:	 torch.Size([1, 128, 26, 26])
ReLU Output shape:	 torch.Size([1, 128, 26, 26])
MaxPool2d Output shape:	 torch.Size([1, 128, 12, 12])
Conv2d Output shape:	 torch.Size([1, 192, 12, 12])
ReLU Output shape:	 torch.Size([1, 192, 12, 12])
Conv2d Output shape:	 torch.Size([1, 192, 12, 12])
ReLU Output shape:	 torch.Size([1, 192, 12, 12])
Conv2d Output shape:	 torch.Size([1, 128, 12, 12])
ReLU Output shape:	 torch.Size([1, 128, 12, 12])
MaxPool2d Output shape:	 torch.Size([1, 128, 5, 5])
Flatten Output shape:	 torch.Size([1, 3200])
Linear Output shape:	 torch.Size([1, 2048])
ReLU Output shape:	 torch.Size([1, 2048])
Dropout Output shape:	 torch.Size([1, 2048])
Linear Output shape:	 torch.Size([1, 2048])
ReLU Output shape:	 torch.Size([1, 2048])
Dropout Output shape:	 torch.Size([1, 2048])
Linear Output shape:	 torch.Size([1, 10])
batch_size = 32
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
lr, num_epochs = 0.01, 10
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
training on cuda:0
loss 0.243, train acc 0.911, test acc 0.905 511.8 examples/sec on cuda:0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530125.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号