• 分享3个深度学习练手的小案例


    3f6a7ab0347a4af1a75e6ebadee63fc1.gif

    🤵‍♂️ 个人主页:@艾派森的个人主页

    ✍🏻作者简介:Python学习者
    🐋 希望大家多多支持,我们一起进步!😄
    如果文章对你有帮助的话,
    欢迎评论 💬点赞👍🏻 收藏 📂加关注+


    目录

    前言

    问题一

    问题二

    问题三 


     

    前言

            最近有粉丝找到我问了三个关于深度学习的问题,也算是小作业吧,做完之后我便写下这篇文章分享给大家,适合初学或有一定基础到小伙伴练手,我的答案仅供参考学习,如有疑问或建议欢迎提出!

    问题一

    请设计一个多层感知机(mlp)网络。

    1. 该网络执行如下操作:

        - 将输入32×32的图像拉伸为1×1024;

        - 将拉伸后的数据传入第一个隐藏层,该隐藏层为全连接层,包含2048个隐藏单元,并使用Sigmoid激活函数;

        - 将第一个隐藏层的输出传入第二个隐藏层,第二个隐藏层为全连接层,包含512个隐藏单元,使用ReLU激活函数;

        - 将第二个隐藏层的输出传入最后一层,最后一层也为全连接层,输出20维特征,不使用激活函数。

    2. 该网络的全连接层权重初始化方案为:全连接层权重服从[0,1]区间上的均匀分布(uniform); 全连接层偏差为常值0.

    最终输出要求:

    1. # 代码块的输出需与下面结果保持一致:
    2. """
    3. Flatten output shape: torch.Size([1, 1024])
    4. Linear output shape: torch.Size([1, 2048])
    5. Linear weight's mean: tensor(0.8631)
    6. Linear bias's mean: tensor(0.)
    7. Sigmoid output shape: torch.Size([1, 2048])
    8. Linear output shape: torch.Size([1, 512])
    9. Linear weight's mean: tensor(0.0675)
    10. Linear bias's mean: tensor(0.)
    11. ReLU output shape: torch.Size([1, 512])
    12. Linear output shape: torch.Size([1, 20])
    13. Linear weight's mean: tensor(0.2539)
    14. Linear bias's mean: tensor(0.)
    15. """

     参考答案:

    1. # 导入需要的torch及相关库
    2. import torch
    3. from torch import nn
    4. import torch.nn.functional as F
    5. # 打印每一层大小信息函数 print_layer_info()
    6. def print_layer_info(net,X):
    7. for layer in net:
    8. X = layer(X)
    9. print(layer.__class__.__name__,'output shape: \t',X.shape)
    10. if type(layer) == nn.Linear:
    11. print('\t',layer.__class__.__name__,'weight\'s mean: \t',torch.mean(layer.weight[0][0].data))
    12. print('\t',layer.__class__.__name__,'bias\'s mean: \t',torch.mean(layer.bias[0].data))
    13. # 你设计的网络,网络名为net
    14. net = nn.Sequential(
    15. # 将输入32×32的图像拉伸为1×1024将拉伸后的数据传入第一个隐藏层,该隐藏层为全连接层,包含2048个隐藏单元,并使用Sigmoid激活函数;
    16. nn.Flatten(),
    17. nn.Linear(in_features=1024, out_features=2048),
    18. nn.Sigmoid(),
    19. # 将第一个隐藏层的输出传入第二个隐藏层,第二个隐藏层为全连接层,包含512个隐藏单元,使用ReLU激活函数
    20. nn.Linear(in_features=2048, out_features=512),
    21. nn.ReLU(),
    22. # 将第二个隐藏层的输出传入最后一层,最后一层也为全连接层,输出20维特征,不使用激活函数
    23. nn.Linear(in_features=512, out_features=20)
    24. )
    25. # 在这里按要求将网络权重初始化
    26. for layer in net:
    27. if isinstance(layer, nn.Linear):
    28. nn.init.uniform_(layer.weight, 0, 1)
    29. nn.init.constant_(layer.bias, 0)
    30. # 测试网络net是否按要求定义
    31. X = torch.rand(size=(1, 1, 32, 32), dtype=torch.float32)
    32. print_layer_info(net,X)

    输出结果:

    1. Flatten output shape: torch.Size([1, 1024])
    2. Linear output shape: torch.Size([1, 2048])
    3. Linear weight's mean: tensor(0.8667)
    4. Linear bias's mean: tensor(0.)
    5. Sigmoid output shape: torch.Size([1, 2048])
    6. Linear output shape: torch.Size([1, 512])
    7. Linear weight's mean: tensor(0.9883)
    8. Linear bias's mean: tensor(0.)
    9. ReLU output shape: torch.Size([1, 512])
    10. Linear output shape: torch.Size([1, 20])
    11. Linear weight's mean: tensor(0.7355)
    12. Linear bias's mean: tensor(0.)

    注:由于X是随机生成的,所以有些数值不可能完全跟要求一模一样。

    问题二

    请设计一个卷积神经网络(CNN)。

     

    1. 该网络执行如下操作:

        - 第一层: 使用96个大小为11×11、步长为4、填充为2的卷积核,将输入3×224×224的图像输出为96×55×55的图像, 使用ReLU为激活函数;

        - 第二层: 大小为3×3、 步长为2、无填充的极大值池化层,将96×55×55的图像输出为96×27×27;

        - 第三层: 使用256个大小为5×5、步长为1、填充为2的卷积核,将输入96×27×27的图像输出为256×27×27的图像, 使用ReLU为激活函数;

        - 第四层: 大小为3×3、步长为2、无填充的极大值池化层,将256×27×27的图像输出为256×13×13;

        - 第五层: 使用384个大小为3×3、步长为1、填充为1的卷积核,将输入256×13×13的图像输出为384×13×13的图像, 使用ReLU为激活函数;

        - 第六层: 使用256个大小为3×3、步长为1、填充为1的卷积核,将输入384×13×13的图像输出为256×13×13的图像, 使用ReLU为激活函数;

        - 第七层: 大小为3×3、步长为1、填充为1的极大值池化层,将256×13×13的图像输出为256×13×13;

        - 第八层: 将上一层的输入拉伸为行向量;

        - 第九层: 全连接层:将上一层拉伸后的向量变成4096维向量;使用ReLU为激活函数;

        - 第十层: 全连接层:将上一层输出得向量变成1000维向量;

    最后输出要求:

    1. # 代码块的输出需与下面结果保持一致:
    2. """
    3. Conv2d output shape: torch.Size([1, 96, 55, 55])
    4. ReLU output shape: torch.Size([1, 96, 55, 55])
    5. MaxPool2d output shape: torch.Size([1, 96, 27, 27])
    6. Conv2d output shape: torch.Size([1, 256, 27, 27])
    7. ReLU output shape: torch.Size([1, 256, 27, 27])
    8. MaxPool2d output shape: torch.Size([1, 256, 13, 13])
    9. Conv2d output shape: torch.Size([1, 384, 13, 13])
    10. ReLU output shape: torch.Size([1, 384, 13, 13])
    11. Conv2d output shape: torch.Size([1, 256, 13, 13])
    12. ReLU output shape: torch.Size([1, 256, 13, 13])
    13. MaxPool2d output shape: torch.Size([1, 256, 13, 13])
    14. Flatten output shape: torch.Size([1, 43264])
    15. Linear output shape: torch.Size([1, 4096])
    16. ReLU output shape: torch.Size([1, 4096])
    17. Linear output shape: torch.Size([1, 1000])
    18. """

     参考答案:

    1. import torch
    2. import torch.nn as nn
    3. # 你设计的网络,网络名为net
    4. net = nn.Sequential(
    5. # layer 1
    6. nn.Conv2d(in_channels=3, out_channels=96, kernel_size=11, stride=4, padding=2),
    7. nn.ReLU(),
    8. # layer 2
    9. nn.MaxPool2d(kernel_size=3, stride=2),
    10. # layer 3
    11. nn.Conv2d(in_channels=96, out_channels=256, kernel_size=5, stride=1, padding=2),
    12. nn.ReLU(),
    13. # layer 4
    14. nn.MaxPool2d(kernel_size=3, stride=2),
    15. # layer 5
    16. nn.Conv2d(in_channels=256, out_channels=384, kernel_size=3, stride=1, padding=1),
    17. nn.ReLU(),
    18. # layer 6
    19. nn.Conv2d(in_channels=384, out_channels=256, kernel_size=3, stride=1, padding=1),
    20. nn.ReLU(),
    21. # layer 7
    22. nn.MaxPool2d(kernel_size=3, stride=2),
    23. # layer 8
    24. nn.Flatten(),
    25. # layer 9
    26. nn.Linear(in_features=9216, out_features=4096),
    27. nn.ReLU(),
    28. # layer 10
    29. nn.Linear(in_features=4096, out_features=1000)
    30. )
    31. # 检查设计的网络net是否符合要求:
    32. X = torch.randn(1, 3, 224, 224)
    33. for layer in net:
    34. X = layer(X)
    35. print(layer.__class__.__name__,'output shape: \t',X.shape)

    问题三 

    nn.MaxPool2d()函数通过对图像进行最大值下采样,可以将图像尺寸便小。与之相对应得nn.MaxUnpool2d()函数可以实现对图像进行上采样,将图像尺寸变大。

     

    请查询nn.MaxUnpool2d()函数的使用手册,并设计一个先降维后升维的卷积神经网络(u-net)。

     

    1. 该网络执行如下 **降维** 操作:

        - 第一层: 使用8个大小为3×3、步长为1、填充为1的卷积核,将输入1×224×224的图像输出为8×224×224的图像, 使用ReLU为激活函数;

        - 第二层: 大小为2×2、 步长为2、无填充的极大值池化层,将8×224×224的图像输出为8×112×112;

        - 第三层: 使用16个大小为3×3、步长为1、填充为1的卷积核,将输入8×112×112的图像输出为16×112×112的图像, 使用ReLU为激活函数;

        - 第四层: 大小为2×2、步长为2、无填充的极大值池化层,将16×112×112的图像输出为16×56×56;

        - 第五层: 使用32个大小为3×3、步长为1、填充为1的卷积核,将输入16×56×56的图像输出为32×56×56的图像, 使用ReLU为激活函数;

        - 第六层: 大小为2×2、步长为2、填充为1的极大值池化层,将32×56×56的图像输出为32×28×28;

       

    2. 该网络接着执行如下 **升维** 操作:

        - 第七层: 大小为2×2、步长为2、填充为1的**反极大值池化层**,将32×28×28的图像输出为32×56×56;

        - 第八层: 使用16个大小为3×3、步长为1、填充为1的卷积核,将输入32×56×56的图像输出为16×56×56的图像, 使用ReLU为激活函数;

        - 第九层: 大小为2×2、步长为2、填充为1的**反极大值池化层**,将16×56×56的图像输出为16×112×112;

        - 第十层: 使用8个大小为3×3、步长为1、填充为1的卷积核,将输入16×112×112的图像输出为8×112×112的图像, 使用ReLU为激活函数;

        - 第十一层: 大小为2×2、步长为2、填充为1的**反极大值池化层**,将8×112×112的图像输出为8×224×224;

        - 第十二层: 使用1个大小为3×3、步长为1、填充为1的卷积核,将输入8×224×224的图像输出为1×224×224的图像, 使用ReLU为激活函数;

    最终输出要求:

    1. # 代码块的输出需与下面结果保持一致:
    2. """
    3. 降维过程:
    4. Conv2d output shape: torch.Size([1, 8, 224, 224])
    5. ReLU output shape: torch.Size([1, 8, 224, 224])
    6. MaxPool2d_Indices output shape: torch.Size([1, 8, 112, 112])
    7. Conv2d output shape: torch.Size([1, 16, 112, 112])
    8. ReLU output shape: torch.Size([1, 16, 112, 112])
    9. MaxPool2d_Indices output shape: torch.Size([1, 16, 56, 56])
    10. Conv2d output shape: torch.Size([1, 32, 56, 56])
    11. ReLU output shape: torch.Size([1, 32, 56, 56])
    12. MaxPool2d_Indices output shape: torch.Size([1, 32, 28, 28])
    13. 升维过程:
    14. MaxUnpool2d_Indices output shape: torch.Size([1, 32, 56, 56])
    15. Conv2d output shape: torch.Size([1, 16, 56, 56])
    16. ReLU output shape: torch.Size([1, 16, 56, 56])
    17. MaxUnpool2d_Indices output shape: torch.Size([1, 16, 112, 112])
    18. Conv2d output shape: torch.Size([1, 8, 112, 112])
    19. ReLU output shape: torch.Size([1, 8, 112, 112])
    20. MaxUnpool2d_Indices output shape: torch.Size([1, 8, 224, 224])
    21. Conv2d output shape: torch.Size([1, 1, 224, 224])
    22. ReLU output shape: torch.Size([1, 1, 224, 224])
    23. """

     参考答案:

    1. import torch
    2. import torch.nn as nn
    3. # 定义down_net按要求实现图像卷积和下采样
    4. down_net = nn.Sequential(
    5. nn.Conv2d(1, 8, kernel_size=3, stride=1, padding=1),
    6. nn.ReLU(),
    7. nn.MaxPool2d(kernel_size=2, stride=2),
    8. nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=1),
    9. nn.ReLU(),
    10. nn.MaxPool2d(kernel_size=2, stride=2),
    11. nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
    12. nn.ReLU(),
    13. nn.MaxPool2d(kernel_size=2, stride=2)
    14. )
    15. # 定义up_net按要求实现图像上采样和卷积
    16. up_net = nn.Sequential(
    17. nn.ConvTranspose2d(32, 32, kernel_size=2, stride=2),
    18. nn.Conv2d(32, 16, kernel_size=3, stride=1, padding=1),
    19. nn.ReLU(),
    20. nn.ConvTranspose2d(16, 16, kernel_size=2, stride=2),
    21. nn.Conv2d(16, 8, kernel_size=3, stride=1, padding=1),
    22. nn.ReLU(),
    23. nn.ConvTranspose2d(8, 8, kernel_size=2, stride=2),
    24. nn.Conv2d(8, 1, kernel_size=3, stride=1, padding=1),
    25. nn.ReLU()
    26. )
    27. # 测试网络down_net是否符合按要求将图像进行下采样:
    28. X = torch.randn(1, 1, 224, 224) #测试输入数据
    29. print('降维过程:')
    30. for layer in down_net:
    31. X = layer(X)
    32. print(layer.__class__.__name__,'output shape: \t',X.shape)
    33. # 测试网络up_net是否按要求将图像进行上采样:
    34. print('升维过程:')
    35. for layer in up_net:
    36. X = layer(X)
    37. print(layer.__class__.__name__,'output shape: \t',X.shape)

    输出结果:

    1. 降维过程:
    2. Conv2d output shape: torch.Size([1, 8, 224, 224])
    3. ReLU output shape: torch.Size([1, 8, 224, 224])
    4. MaxPool2d output shape: torch.Size([1, 8, 112, 112])
    5. Conv2d output shape: torch.Size([1, 16, 112, 112])
    6. ReLU output shape: torch.Size([1, 16, 112, 112])
    7. MaxPool2d output shape: torch.Size([1, 16, 56, 56])
    8. Conv2d output shape: torch.Size([1, 32, 56, 56])
    9. ReLU output shape: torch.Size([1, 32, 56, 56])
    10. MaxPool2d output shape: torch.Size([1, 32, 28, 28])
    11. 升维过程:
    12. ConvTranspose2d output shape: torch.Size([1, 32, 56, 56])
    13. Conv2d output shape: torch.Size([1, 16, 56, 56])
    14. ReLU output shape: torch.Size([1, 16, 56, 56])
    15. ConvTranspose2d output shape: torch.Size([1, 16, 112, 112])
    16. Conv2d output shape: torch.Size([1, 8, 112, 112])
    17. ReLU output shape: torch.Size([1, 8, 112, 112])
    18. ConvTranspose2d output shape: torch.Size([1, 8, 224, 224])
    19. Conv2d output shape: torch.Size([1, 1, 224, 224])
    20. ReLU output shape: torch.Size([1, 1, 224, 224])

     

  • 相关阅读:
    k线图分析法
    SaaS 营销怎么做?几点思考
    第一次使用腾讯云轻量应用服务器配置宝塔面板部署前端vue项目
    Synchronized锁的升级
    3-3数据链路层-介质访问控制
    全志V853 NPU 系统介绍
    嵌入式Linux 调试常用工具与知识
    linux下mysql主从复制
    c++中std::endl 和“\n“ 这两个换行符有什么区别
    零基础自学Java的网站有哪些?
  • 原文地址:https://blog.csdn.net/m0_64336780/article/details/130561439