当前位置:   article > 正文

数据科学导引上机(6)神经网络_神经网络nums work

神经网络nums work

这次是简单的神经网络,因为层数比较少

关于环境配置,

  1. Pytorch的安装:安装分为cpu版本和gpu版本,gpu版本的pytorch运行速度更快一些,但gpu版本的安装需要有NVIDIA的显卡,尽量安装cpu版本。cpu版本的安装可以参考网址:在Windows下安装配置CPU版的PyTorch_On the road丶的博客-CSDN博客
  2. tqdm的安装:在base环境下直接pip install tqdm
  3. cv2的安装:python cv2模块怎么安装?-Python学习网

 这次处理的是数字识别,用到手写数字集

lenet模型构建

  1. # 导入相关的包
  2. import torch
  3. from torch import nn
  1. # 判断是否安装gpu版本的torch,若没有则使用cpu运算
  2. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  3. print("using {} device.".format(device))
  1. # 定义Lenet模型
  2. class LeNet(nn.Module):
  3. def __init__(self):
  4. super(LeNet, self).__init__()
  5. ## 定义网络结构
  6. self.conv = nn.Sequential(
  7. #在此处更改网络的结构,使网络在3232的输入大小下,可以输入2828的Mnist数据集。
  8. ## C1卷积层
  9. nn.Conv2d(3,6,(5,5),padding=(2,2)),# 输入通道3,输出通道6,卷积矩阵5x5
  10. # nn.Conv2d(3,6,(5,5)),# 输入通道3,输出通道6,卷积矩阵5x5
  11. nn.Sigmoid(),
  12. nn.MaxPool2d(2,2), # 池化2x2
  13. # NO.2
  14. nn.Conv2d(6,16,5),#
  15. nn.Sigmoid(),
  16. nn.MaxPool2d(2,2)
  17. )
  18. self.fc = nn.Sequential(
  19. nn.Linear(16*5*5,120),# 上面池化层的结果输入,输出
  20. # nn.Linear(16*4*4,120),# 上面池化层的结果输入,输出
  21. nn.Sigmoid(),
  22. nn.Linear(120,84),
  23. nn.Sigmoid(),
  24. nn.Linear(84,10)
  25. )
  26. ## 前向传播
  27. def forward(self, img):
  28. feature = self.conv(img)
  29. img = feature.view(img.shape[0],-1) # 把图矩阵拉成向量
  30. output = self.fc(img) # 得到长度为10 的向量
  31. return output
  1. # 查看模型
  2. net = LeNet()
  3. print(net)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/929537
推荐阅读
相关标签
  

闽ICP备14008679号