当前位置:   article > 正文

由浅入深,走进深度学习(2)_卷积层

卷积层

今天分享的学习内容主要就是神经网络里面的知识啦,用到的框架就是torch

在这里我也是对自己做一个学习记录,如果不符合大家的口味,大家划走就可以啦

可能没有什么文字或者原理上的讲解,基本上都是代码,但是我还是想说,如果基础不是很好,认认真真敲一遍,会有不一样的感受!!

在这里还有一篇相关内容的补充,大家也可以看一看:

由浅入深,走进深度学习(补充篇:神经网络基础)-CSDN博客

由浅入深,走进深度学习(补充篇:神经网络结构层基础)-CSDN博客

主要内容

目录

内容六 卷积原理、卷积层、卷积层处理图片

内容七 最大池化层

内容八 非线性激活

内容九 线性层以及其他层

内容十 实战,搭建一个小型的神经网络


正片开始

内容六 卷积原理、卷积层、卷积层处理图片

  1. import torch
  2. import torch.nn.functional as F
  3. input = torch.tensor([[1, 2, 0, 3, 1],
  4. [0, 1, 2, 3, 1],
  5. [1, 2, 1, 0, 0],
  6. [5, 6, 2, 2, 1],
  7. [3, 2, 3, 5, 1]])
  8. kernel = torch.tensor([[1, 2, 1],
  9. [2, 3, 1],
  10. [3, 0, 1]])
  11. print(input.shape)
  12. print(kernel.shape)
  13. input = torch.reshape(input, (1, 1, 5, 5))
  14. kernel = torch.reshape(kernel, (1, 1, 3, 3))
  15. print(input.shape)
  16. print(input)
  17. print(kernel.shape)
  18. print(kernel)
  19. output = F.conv2d(input, kernel, stride = 1)
  20. print(output.shape)
  21. print(output)
  22. import torch
  23. import torch.nn.functional as F
  24. input = torch.tensor([[1, 2, 0, 3, 1],
  25. [0, 1, 2, 3, 1],
  26. [1, 2, 1, 0, 0],
  27. [5, 6, 2, 2, 1],
  28. [3, 2, 3, 5, 1]])
  29. kernel = torch.tensor([[1, 2, 1],
  30. [2, 3, 1],
  31. [3, 0, 1]])
  32. print(input.shape)
  33. print(kernel.shape)
  34. input = torch.reshape(input, (1, 1, 5, 5))
  35. kernel = torch.reshape(kernel, (1, 1, 3, 3))
  36. print(input.shape)
  37. print(input)
  38. print(kernel.shape)
  39. print(kernel)
  40. output = F.conv2d(input, kernel, stride = 2)
  41. print(output.shape)
  42. print(output)
  43. # 步幅、填充原理
  44. # 步幅:卷积核经过输入特征图的采样间隔。设置步幅的目的:希望减小输入参数的数目,减少计算量
  45. # 填充:在输入特征图的每一边添加一定数目的行列。设置填充的目的:希望每个输入方块都能作为卷积窗口的中心,或使得输出的特征图的长、宽 = 输入的特征图的长、宽。
  46. # 一个尺寸 a * a 的特征图,经过 b * b 的卷积层,步幅(stride)= c,填充(padding)= d,若d等于0,也就是不填充,输出的特征图的尺寸 =(a-b)/ c+1;若d不等于0,也就是填充,输出的特征图的尺寸 =(a+2d-b)/ c+1
  47. import torch
  48. import torch.nn.functional as F
  49. input = torch.tensor([[1, 2, 0, 3, 1],
  50. [0, 1, 2, 3, 1],
  51. [1, 2, 1, 0, 0],
  52. [5, 6, 2, 2, 1],
  53. [3, 2, 3, 5, 1]])
  54. kernel = torch.tensor([[1, 2, 1],
  55. [2, 3, 1],
  56. [3, 0, 1]])
  57. print(input.shape)
  58. print(kernel.shape)
  59. input = torch.reshape(input, (1, 1, 5, 5))
  60. kernel = torch.reshape(kernel, (1, 1, 3, 3))
  61. print(input.shape)
  62. print(input)
  63. print(kernel.shape)
  64. print(kernel)
  65. output = F.conv2d(input, kernel, stride = 1, padding = 1) # 周围只填充一层
  66. print(output.shape)
  67. print(output)
  68. # 内容六 卷积层
  69. # Conv1d代表一维卷积,Conv2d代表二维卷积,Conv3d代表三维卷积
  70. # kernel_size在训练过程中不断调整,定义为3就是3 * 3的卷积核,实际我们在训练神经网络过程中其实就是对kernel_size不断调整
  71. import torch
  72. from torch import nn
  73. from torch.nn import Conv2d
  74. from torch.utils.data import DataLoader
  75. import torchvision
  76. # dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
  77. # dataloader = DataLoader(dataset, batch_size = 64)
  78. class net(nn.Module):
  79. def __init__(self):
  80. super(net, self).__init__()
  81. self.conv1 = Conv2d(in_channels = 3, out_channels = 6, kernel_size = 3, stride = 1, padding = 0) # 彩色图像输入为3层,我们想让它的输出为6层,选3 * 3 的卷积
  82. def forward(self, x):
  83. x = self.conv1
  84. return x
  85. model = net()
  86. print(model)
  87. # 卷积层处理图片
  88. import torch
  89. from torch import nn
  90. from torch.nn import Conv2d
  91. from torch.utils.data import DataLoader
  92. import torchvision
  93. dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
  94. dataloader = DataLoader(dataset, batch_size = 64)
  95. class net(nn.Module):
  96. def __init__(self):
  97. super(net, self).__init__()
  98. self.conv1 = Conv2d(in_channels = 3, out_channels = 6, kernel_size = 3, stride = 1, padding = 0)
  99. def forward(self, x):
  100. x = self.conv1(x)
  101. return x
  102. model = net()
  103. for data in dataloader:
  104. img, targets = data
  105. output = model(img)
  106. # print(img.shape)
  107. # print(output.shape) # 输入为3通道32×32的64张图片
  108. # print(targets.shape) # 输出为6通道30×30的64张图片

内容七 最大池化层

  1. # 最大池化层有时也被称为下采样 dilation为空洞卷积
  2. # Ceil_model为当超出区域时,只取最左上角的值
  3. # 池化使得数据由5 * 5 变为3 * 3,甚至1 * 1的,这样导致计算的参数会大大减小。例如1080P的电影经过池化的转为720P的电影、或360P的电影后,同样的网速下,视频更为不卡
  4. import torch
  5. from torch import nn
  6. from torch.nn import MaxPool2d
  7. input = torch.tensor([[3, 4, 6, 1, 8],
  8. [4, 0, 8, 0, 1],
  9. [1, 2, 4, 5, 1],
  10. [2, 3, 1, 5, 1],
  11. [3, 3, 1, 5, 0]], dtype = torch.float32)
  12. input = torch.reshape(input, (-1, 1, 5, 5))
  13. print(input.shape)
  14. class net(nn.Module):
  15. def __init__(self):
  16. super(net, self).__init__()
  17. self.maxpool = MaxPool2d(kernel_size = 3, ceil_mode = True)
  18. def forward(self, x):
  19. x = self.maxpool(x)
  20. return x
  21. model = net()
  22. output = model(input)
  23. print(output.shape)
  24. print(output)
  25. import torch
  26. import torchvision
  27. from torch import nn
  28. from torch.nn import MaxPool2d
  29. from torch.utils.data import DataLoader
  30. dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
  31. dataloader = DataLoader(dataset, batch_size = 64)
  32. class net(nn.Module):
  33. def __init__(self):
  34. super(net, self).__init__()
  35. self.maxpool = MaxPool2d(kernel_size = 3, ceil_mode = True)
  36. def forward(self, x):
  37. x = self.maxpool(x)
  38. return x
  39. model = net()
  40. epoch = 0
  41. for data in dataloader:
  42. img, tagets = data
  43. # print('input', img, epoch)
  44. output = model(img)
  45. # print('output', output, epoch)
  46. epoch = epoch + 1

内容八 非线性激活

  1. # inplace为原地替换,若为True,则变量的值被替换。若为False,则会创建一个新变量,将函数处理后的值赋值给新变量,原始变量的值没有修改
  2. import torch
  3. from torch import nn
  4. from torch.nn import ReLU
  5. input = torch.tensor([[1, -2],
  6. [-0.7, 3]])
  7. input = torch.reshape(input, (-1, 1, 2, 2))
  8. print(input.shape)
  9. class net(nn.Module):
  10. def __init__(self):
  11. super(net, self).__init__()
  12. self.relu = ReLU()
  13. def forward(self, x):
  14. x = self.relu(x)
  15. return x
  16. model = net()
  17. output = model(input)
  18. print(output.shape)
  19. print(output)
  20. print(output[0][0][1][1])
  21. import torch
  22. import torchvision
  23. from torch import nn
  24. from torch.nn import ReLU, Sigmoid
  25. from torch.utils.data import DataLoader
  26. dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
  27. dataloader = DataLoader(dataset, batch_size = 64)
  28. class net(nn.Module):
  29. def __init__(self):
  30. super(net, self).__init__()
  31. self.relu = ReLU()
  32. self.sigmoid = Sigmoid()
  33. def forward(self, x):
  34. x1 = self.relu(x)
  35. x2 = self.sigmoid(x1)
  36. return x2
  37. model = net()
  38. epoch = 0
  39. for data in dataloader:
  40. imgs, targets = data
  41. output = model(imgs)
  42. # print(output.shape)
  43. epoch = epoch + 1

内容九 线性层以及其他层

  1. # 线性拉平
  2. import torch
  3. import torchvision
  4. from torch import nn
  5. from torch.nn import ReLU, Sigmoid
  6. from torch.utils.data import DataLoader
  7. dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
  8. dataloader = DataLoader(dataset, batch_size = 64)
  9. for data in dataloader:
  10. imgs, targets = data
  11. # print(imgs.shape)
  12. output = torch.reshape(imgs, (1, 1, 1, -1))
  13. # print(output.shape)
  14. # 线性层
  15. import torch
  16. import torchvision
  17. from torch import nn
  18. from torch.nn import Linear
  19. from torch.utils.data import DataLoader
  20. dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
  21. dataloader = DataLoader(dataset, batch_size = 64, drop_last=True)
  22. # drop_last=True:如果设置为 True,则当数据集的大小不能被 batch_size 整除时,会丢弃最后一个不足一个批次的数据
  23. # drop_last=False:如果设置为 False(也是默认值),则当数据集的大小不能被 batch_size 整除时,最后一个批次会包含剩下的样本,可能少于 batch_size
  24. class net(nn.Module):
  25. def __init__(self):
  26. super(net, self).__init__()
  27. self.linear = Linear(196608, 10)
  28. def forward(self, x):
  29. x = self.linear(x)
  30. return x
  31. model = net()
  32. epoch = 0
  33. for data in dataloader:
  34. imgs, targets = data
  35. # print(imgs.shape)
  36. imgs_reshape = torch.reshape(imgs, (1, 1, 1, -1)) # 方法一 拉平
  37. # print(imgs_reshape.shape)
  38. output = model(imgs_reshape)
  39. # print(output.shape)
  40. # epoch = epoch + 1
  41. # 线性层
  42. import torch
  43. import torchvision
  44. from torch import nn
  45. from torch.nn import Linear
  46. from torch.utils.data import DataLoader
  47. dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
  48. dataloader = DataLoader(dataset, batch_size = 64, drop_last=True)
  49. # drop_last=True:如果设置为 True,则当数据集的大小不能被 batch_size 整除时,会丢弃最后一个不足一个批次的数据
  50. # drop_last=False:如果设置为 False(也是默认值),则当数据集的大小不能被 batch_size 整除时,最后一个批次会包含剩下的样本,可能少于 batch_size
  51. class net(nn.Module):
  52. def __init__(self):
  53. super(net, self).__init__()
  54. self.linear = Linear(196608, 20)
  55. def forward(self, x):
  56. x = self.linear(x)
  57. return x
  58. model = net()
  59. epoch = 0
  60. for data in dataloader:
  61. imgs, targets = data
  62. # print(imgs.shape)
  63. imgs_flatten = torch.flatten(imgs) # 方法二 拉平展为一维
  64. # print(imgs_flatten.shape)
  65. output = model(imgs_flatten)
  66. # print(output.shape)
  67. # epoch = epoch + 1

内容十 实战,搭建一个小型的神经网络

  1. # 把网络结构放在Sequential里面,好处就是代码写起来比较简介、易懂
  2. # 可以根据神经网络每层的尺寸,根据下图的公式计算出神经网络中的参数
  3. import torch
  4. import torchvision
  5. from torch import nn
  6. from torch.nn import Linear, Conv2d, MaxPool2d, Flatten
  7. from torch.utils.data import DataLoader
  8. # dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
  9. # dataloader = DataLoader(dataset, batch_size = 64, drop_last=True)
  10. class net(nn.Module):
  11. def __init__(self):
  12. super(net, self).__init__()
  13. self.conv1 = Conv2d(in_channels = 3, out_channels = 32, kernel_size = 5, stride = 1, padding = 2)
  14. self.maxpool1 = MaxPool2d(kernel_size = 2, ceil_mode = True)
  15. self.conv2 = Conv2d(in_channels = 32, out_channels = 32, kernel_size = 5, stride = 1, padding = 2)
  16. self.maxpool2 = MaxPool2d(kernel_size = 2, ceil_mode = True)
  17. self.conv3 = Conv2d(in_channels = 32, out_channels = 64, kernel_size = 5, stride = 1, padding = 2)
  18. self.maxpool3 = MaxPool2d(kernel_size = 2, ceil_mode = True)
  19. self.flatten = Flatten()
  20. self.linear1 = Linear(1024, 64)
  21. self.linear2 = Linear(64, 10)
  22. def forward(self, x):
  23. x = self.conv1(x)
  24. print(x.shape)
  25. x = self.maxpool1(x)
  26. print(x.shape)
  27. x = self.conv2(x)
  28. print(x.shape)
  29. x = self.maxpool2(x)
  30. print(x.shape)
  31. x = self.conv3(x)
  32. print(x.shape)
  33. x = self.maxpool3(x)
  34. print(x.shape)
  35. x = self.flatten(x)
  36. print(x.shape)
  37. x = self.linear1(x)
  38. print(x.shape)
  39. x = self.linear2(x)
  40. print(x.shape)
  41. return x
  42. model = net()
  43. print(model)
  44. input = torch.ones((64, 3, 32, 32))
  45. output = model(input)
  46. print(output.shape)
  1. # Sequential神经网络
  2. import torch
  3. import torchvision
  4. from torch import nn
  5. from torch.nn import Linear, Conv2d, MaxPool2d, Flatten, Sequential
  6. from torch.utils.data import DataLoader
  7. class net(nn.Module):
  8. def __init__(self):
  9. super(net, self).__init__()
  10. self.model = Sequential(
  11. Conv2d(in_channels = 3, out_channels = 32, kernel_size = 5, stride = 1, padding = 2),
  12. MaxPool2d(kernel_size = 2, ceil_mode = True),
  13. Conv2d(in_channels = 32, out_channels = 32, kernel_size = 5, stride = 1, padding = 2),
  14. MaxPool2d(kernel_size = 2, ceil_mode = True),
  15. Conv2d(in_channels = 32, out_channels = 64, kernel_size = 5, stride = 1, padding = 2),
  16. MaxPool2d(kernel_size = 2, ceil_mode = True),
  17. Flatten(),
  18. Linear(1024, 64),
  19. Linear(64, 10))
  20. def forward(self, x):
  21. x = self.model(x)
  22. return x
  23. model = net()
  24. print(model)
  25. input = torch.ones((64, 3, 32, 32))
  26. output = model(input)
  27. print(output.shape)

注:上述内容参考b站up主“我是土堆”的视频!!!

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