From c431d75e6098361ab01da8a646ed7bff87b5f541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=A4=E4=BB=A3=E4=B8=AD=E4=BA=8C=E9=BE=99?= <32593426+c834613101@users.noreply.github.com> Date: Tue, 19 Nov 2019 16:07:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E4=BA=86=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chapter3/3.2-mnist.ipynb | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/chapter3/3.2-mnist.ipynb b/chapter3/3.2-mnist.ipynb index 657040a..a2c2b14 100644 --- a/chapter3/3.2-mnist.ipynb +++ b/chapter3/3.2-mnist.ipynb @@ -133,23 +133,25 @@ "class ConvNet(nn.Module):\n", " def __init__(self):\n", " super().__init__()\n", - " # 1,28x28\n", - " self.conv1=nn.Conv2d(1,10,5) # 10, 24x24\n", - " self.conv2=nn.Conv2d(10,20,3) # 128, 10x10\n", - " self.fc1 = nn.Linear(20*10*10,500)\n", - " self.fc2 = nn.Linear(500,10)\n", + " # batch*1*28*28(每次会送入batch个样本,输入通道数1(黑白图像),图像分辨率是28x28)\n", + " # 下面的卷积层Conv2d的第一个参数指输入通道数,第二个参数指输出通道数,第三个参数指卷积核的大小\n", + " self.conv1 = nn.Conv2d(1, 10, 5) # 输入通道数1,输出通道数10,核的大小5\n", + " self.conv2 = nn.Conv2d(10, 20, 3) # 输入通道数10,输出通道数20,核的大小3\n", + " # 下面的全连接层Linear的第一个参数指输入通道数,第二个参数指输出通道数\n", + " self.fc1 = nn.Linear(20*10*10, 500) # 输入通道数是2000,输出通道数是500\n", + " self.fc2 = nn.Linear(500, 10) # 输入通道数是500,输出通道数是10,即10分类\n", " def forward(self,x):\n", - " in_size = x.size(0)\n", - " out = self.conv1(x) #24\n", - " out = F.relu(out)\n", - " out = F.max_pool2d(out, 2, 2) #12\n", - " out = self.conv2(out) #10\n", - " out = F.relu(out)\n", - " out = out.view(in_size,-1)\n", - " out = self.fc1(out)\n", - " out = F.relu(out)\n", - " out = self.fc2(out)\n", - " out = F.log_softmax(out,dim=1)\n", + " in_size = x.size(0) # 在本例中in_size=512,也就是BATCH_SIZE的值。输入的x可以看成是512*1*28*28的张量。\n", + " out = self.conv1(x) # batch*1*28*28 -> batch*10*24*24(28x28的图像经过一次核为5x5的卷积,输出变为24x24)\n", + " out = F.relu(out) # batch*10*24*24(激活函数ReLU不改变形状))\n", + " out = F.max_pool2d(out, 2, 2) # batch*10*24*24 -> batch*10*12*12(2*2的池化层会减半)\n", + " out = self.conv2(out) # batch*10*12*12 -> batch*20*10*10(再卷积一次,核的大小是3)\n", + " out = F.relu(out) # batch*20*10*10\n", + " out = out.view(in_size, -1) # batch*20*10*10 -> batch*2000(out的第二维是-1,说明是自动推算,本例中第二维是20*10*10)\n", + " out = self.fc1(out) # batch*2000 -> batch*500\n", + " out = F.relu(out) # batch*500\n", + " out = self.fc2(out) # batch*500 -> batch*10\n", + " out = F.log_softmax(out, dim=1) # 计算log(softmax(x))\n", " return out" ] },