class ConvNet(nn.Module):
def __init__(self):
# model layers
def forward(self, x):
# model structure
nn.Conv2d
跟 nn.MaxPool2d
,那位啥这边是使用 MaxPool 的原因我们昨天有解释过了,那我们先看 code 在解释一下里面的参数import torch
import torch.nn as nn
import torch.nn.functional as F
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
# image shape is 1 * 28 * 28, where 1 is one color channel
# 28 * 28 is the image size
self.conv1 = nn.Conv2d(in_channels=1, out_channels=3, kernel_size=5) # output shape = 3 * 24 * 24
self.pool = nn.MaxPool2d(kernel_size=2, stride=2) # output shape = 3 * 12 * 12
# intput shape is 3 * 12 * 12
self.conv2 = nn.Conv2d(in_channels=3, out_channels=9, kernel_size=5) # output shape = 9 * 8 * 8
# add another max pooling, output shape = 9 * 4 * 4
self.fc1 = nn.Linear(9*4*4, 100)
self.fc2 = nn.Linear(100, 50)
# last fully connected layer output should be same as classes
self.fc3 = nn.Linear(50, 10)
def forward(self, x):
# first conv
x = self.pool(F.relu(self.conv1(x)))
# second conv
x = self.pool(F.relu(self.conv2(x)))
# flatten all dimensions except batch
x = torch.flatten(x, 1)
# fully connected layers
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
in_channels
:输入的图片通道数,这边大部分会是图片的颜色状况,例如灰阶图片只有一个通道,RGB 的图片会有三个通道out_channels
:输出的通道数,这边基本上没有特别的限制,可以视自己喜好给予,只要记住往下一层送时,这边会变成另外一层的 in_channels
kernel_size
:这也就是我们的 feature filter 的 size,有两种表达参数的方式,
kernel_size = (num1, num2)
就会是 num1 * num2 的 feature filter,请注意,feature filter 并没有规定一定要是正方形,因此可以视喜好调整stride
:在 feature filter 遍历图片时,每次移动的步数可以不是一格一格移动,此参数就会设定每次卷积移动的步伐大小,那预设会是 1,因此我们上面没有特别调整kernal_size
:同上叙述的,也就是 pooling 每次要覆盖的资料量stride
:同上叙述,也就是池化每次的步数
<<: [ Day 25 ] 实作一个 React.js 网站 1/5
>>: Day25 :【TypeScript 学起来】Class 的继承、修饰符、abstract、static
“I need solitude for my writing; not 'like a herm...
接下来几篇都会是有线网路的应用,让设备与树莓派都可以连上网路,当成主路由、无线分享器或区网无线接入的...
缓存对於服务器的好处,在於使用者会将数据加载到内存,减少了对资料库的访问 终端输入 python m...
这是我们内建的 suspend 函式第三篇,让我们看看有哪些吧: joinAll() 还记得 joi...
问题回答 使用 Vuex 是为了当元件之间都需要共用资料时,使用一个像是公用容器来管理资料,我们把所...