Skip to content

Commit c431d75

Browse files
补充了模型的描述
1 parent ce7d56b commit c431d75

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

chapter3/3.2-mnist.ipynb

+18-16
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,25 @@
133133
"class ConvNet(nn.Module):\n",
134134
" def __init__(self):\n",
135135
" super().__init__()\n",
136-
" # 1,28x28\n",
137-
" self.conv1=nn.Conv2d(1,10,5) # 10, 24x24\n",
138-
" self.conv2=nn.Conv2d(10,20,3) # 128, 10x10\n",
139-
" self.fc1 = nn.Linear(20*10*10,500)\n",
140-
" self.fc2 = nn.Linear(500,10)\n",
136+
" # batch*1*28*28(每次会送入batch个样本,输入通道数1(黑白图像),图像分辨率是28x28)\n",
137+
" # 下面的卷积层Conv2d的第一个参数指输入通道数,第二个参数指输出通道数,第三个参数指卷积核的大小\n",
138+
" self.conv1 = nn.Conv2d(1, 10, 5) # 输入通道数1,输出通道数10,核的大小5\n",
139+
" self.conv2 = nn.Conv2d(10, 20, 3) # 输入通道数10,输出通道数20,核的大小3\n",
140+
" # 下面的全连接层Linear的第一个参数指输入通道数,第二个参数指输出通道数\n",
141+
" self.fc1 = nn.Linear(20*10*10, 500) # 输入通道数是2000,输出通道数是500\n",
142+
" self.fc2 = nn.Linear(500, 10) # 输入通道数是500,输出通道数是10,即10分类\n",
141143
" def forward(self,x):\n",
142-
" in_size = x.size(0)\n",
143-
" out = self.conv1(x) #24\n",
144-
" out = F.relu(out)\n",
145-
" out = F.max_pool2d(out, 2, 2) #12\n",
146-
" out = self.conv2(out) #10\n",
147-
" out = F.relu(out)\n",
148-
" out = out.view(in_size,-1)\n",
149-
" out = self.fc1(out)\n",
150-
" out = F.relu(out)\n",
151-
" out = self.fc2(out)\n",
152-
" out = F.log_softmax(out,dim=1)\n",
144+
" in_size = x.size(0) # 在本例中in_size=512,也就是BATCH_SIZE的值。输入的x可以看成是512*1*28*28的张量。\n",
145+
" out = self.conv1(x) # batch*1*28*28 -> batch*10*24*24(28x28的图像经过一次核为5x5的卷积,输出变为24x24)\n",
146+
" out = F.relu(out) # batch*10*24*24(激活函数ReLU不改变形状))\n",
147+
" out = F.max_pool2d(out, 2, 2) # batch*10*24*24 -> batch*10*12*12(2*2的池化层会减半)\n",
148+
" out = self.conv2(out) # batch*10*12*12 -> batch*20*10*10(再卷积一次,核的大小是3)\n",
149+
" out = F.relu(out) # batch*20*10*10\n",
150+
" out = out.view(in_size, -1) # batch*20*10*10 -> batch*2000(out的第二维是-1,说明是自动推算,本例中第二维是20*10*10)\n",
151+
" out = self.fc1(out) # batch*2000 -> batch*500\n",
152+
" out = F.relu(out) # batch*500\n",
153+
" out = self.fc2(out) # batch*500 -> batch*10\n",
154+
" out = F.log_softmax(out, dim=1) # 计算log(softmax(x))\n",
153155
" return out"
154156
]
155157
},

0 commit comments

Comments
 (0)