组装 CNN 图像分类器
将卷积、ReLU 和池化模块组合成可运行的模型
组装 CNN 图像分类器 是 CoddyKit 上的免费 Deep Learning Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Deep Learning Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Deep Learning Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The CNN Recipe
A classic image classifier stacks conv-ReLU-pool blocks to extract features, then ends with dense layers that predict the class.
One Building Block
Each block follows the same rhythm: a conv layer, a ReLU activation, then a pool. This is the basic conv block you repeat.
block = nn.Sequential(
nn.Conv2d(3, 16, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
)ReLU Adds Nonlinearity
Without an activation, stacked convolutions collapse into one linear step. ReLU after each conv lets the network learn complex shapes.
Stack Blocks to Go Deeper
Repeat the block, growing the channels each time. More blocks mean a wider receptive field and richer learned features.
Flatten Before the Head
After the conv blocks you have a stack of small maps. Flatten them into one vector so a dense layer can read them.
x = torch.flatten(x, start_dim=1)The Classifier Head
A Linear layer maps the flattened features to one score per class. For ten classes, it outputs ten numbers.
head = nn.Linear(64, 10)Define the Model
Wrap the features and head in an nn.Module. The forward method runs the convs, flattens, then the classifier.
class CNN(nn.Module):
def __init__(self):
super().__init__()
self.features = block
self.head = headWrite the Forward Pass
In forward, pass the image through features, flatten, and feed the head. The output is one raw score per class.
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1)
return self.head(x)Outputs Are Logits
The head returns raw scores called logits, not probabilities. Cross-entropy loss expects exactly these raw values during training.
Pick the Loss
For multiclass images, use CrossEntropyLoss. It applies softmax internally and compares against the true label index.
loss_fn = nn.CrossEntropyLoss()Predict a Class
At inference, take the index of the largest logit. That argmax is the model's predicted class for the image. 🖼️
pred = logits.argmax(dim=1)Quick Check
Let us check the order of a CNN classifier's pieces.
Recap: A Working CNN
You assembled a CNN: conv-ReLU-pool blocks extract features, flatten feeds a Linear head, and argmax over logits gives the predicted class. 🎉
常见问题解答
「组装 CNN 图像分类器」课时是免费的吗?
是的 — 「组装 CNN 图像分类器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Deep Learning Academy 课程的其余内容,请升级到 CoddyKit PRO。 Deep Learning Academy 课程共包含 4 节课。
「组装 CNN 图像分类器」这节课中我会学到什么?
将卷积、ReLU 和池化模块组合成可运行的模型 你通过在浏览器中直接运行的动手代码来练习 Deep Learning Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Deep Learning Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Deep Learning Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「组装 CNN 图像分类器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Deep Learning Academy 课中编写并运行代码吗?
能。每节 Deep Learning Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 卷积:卷积核在像素上滑动
- 步幅、填充与池化
- 通道、特征图与感受野
- 组装 CNN 图像分类器