PyTorch入门学习:10-Basic CNN

10 Basic CNN

1 Intro: Convolutional Neural Network

  • Why full connected layer isn’t enough? An simple explanation: 考虑图像上上下相邻的两个像素,它们在空间上相近,但是在向量中却相距甚远。因此,单纯的全连接层无法捕捉特征。

IntroExample

  • Advanced network:
  1. 如图所示,首先经过一个卷积层,张量的维度信息发生变化。
  2. 经过下采样层,减少运算量。
  3. 经过卷积层和下采样层,张量的维度信息发生变化。
  4. 两个全连接层,结果映射到10个类别。
  • 以上过程中,卷积和下采样层的作用是提取图像的特征,被称为Feature Extraction

2 Deep into Convolution

2.1 Brief Convolution

单一卷积过程 如图,在卷积过程中,每次从原始图像中取一patch,经过处理后其维度信息改变,从转变为

2.2 卷积运算

单通道卷积运算示例 如图所示,图片中展示了一个单通道图像的卷积运算过程,取卷积核大小的块与Kernel进行逐元素相乘并相加,得到Output相应位置的值。 多通道卷积完整 如图所示,对于多通道的图像,则分别对每个通道与不同的kernel进行相乘,最终得到三个output,再将这三个output进行逐元素相加,得到最终的output。 在这个过程中,输入张量维度为,kernel维度,output dimension 显然如果输入是,kernel,那么输出为多通道输出 如上图,展示了更复杂的情形,如果要得到通道数为的输出,那么需要个卷积核分别对输入进行卷积运算得到个输出,再将其拼接到一起。

  • Implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import torch
in_channels, out_channels=5, 10
width, height =100, 100
kernel_size = 3
batch_size = 1

input = torch.randn(batch_size,
in_channels,
width,
height)

conv_layer = torch.nn.Conv2d(in_channels,
out_channels,
kernel_size=kernel_size)

output = conv_layer(input)
print(input.shape)
print(output.shape)
print(conv_layer.weight.shape)

2.3 其他参数

  1. padding 添加padding参数 如果在参数中使用padding,那么会在张量周围填充一圈0,获得更大的输出,如图所示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import torch

input = [3,4,6,5,7,
2,4,6,8,2,
1,6,7,8,4,
9,7,4,6,2,
3,7,5,4,1]
input = torch.Tensor(input).view(1,1,5,5)

conv_layer = torch.nn.Conv2d(1,1,kernel_size=3,padding=1,bias=False)

kernel = torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1,1,3,3)
conv_layer.weight.data = kernel.data

output = conv_layer(input)
print(output)
  1. stride 代表步长 添加stride参数 如图,stride=2,在进行卷积运算时,每次与kernel相乘之后会跳过一次卷积进行下下次运算,这可以快速降低张量的大小。

3 Subsampling - Max Pooling Layer

MaxPooling 如图展示了常见的下采样方法-MaxPooling的运算过程,MaxPooling将会使张量的尺寸减少一半。

  • Implementation
1
maxpooling_layer = torch.nn.MaxPool2d(kernel_size=2)

4 A Simple Convolutional Neural Network

简单的CNN 如图,使用CNN对MNIST数据集进行学习

Implementation

  • Implementation
1
2
3
4
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = ...

5 How to use GPU

  1. Move Model to GPU
1
2
3
4
# 选择设备
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 模型迁移到device
model.to(device)
  1. Move Tensor to GPU
1
inputs, targets = inputs.to(device), targets.to(device)

6 Exercise

Exercise


PyTorch入门学习:10-Basic CNN
https://eleco.top/2026/03/02/learn-torch-10-Basic-CNN/
作者
Eleco
发布于
2026年3月2日
许可协议