Lenet和卷积
这里tensor的通道排序(batch,channel,height,width)
batch就是一次传入的处理多少个图片,比如32张图片
channel 彩色图片的channel就是rgb三个通道,3。ps:在cifar10中的数据是彩色图片
height 高 图片的大小32
width 宽 32
在cifar10中是彩色图片所以
上面的a是灰度图片,所一是1@32*32
import torch.nn as nn
import torch.nn.functional as F
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(3, 16, 5)#in_channels,输入特征矩阵的深度,比如第一个卷积层的输入就是rgb彩色图像,那么in_channels就是3
#out_channels就是使用的卷积核的个数,使用多少个卷积核,就会生成特征是多少维的特征矩阵
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(16, 32, 5)#经过第一个卷积层后,channel就变成了16,第二层使用32个卷积核,卷积核大小仍为5*5
self.pool2 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(32*5*5, 120)#全连接层的输入是一个一维向量,所以需要把特征矩阵展平,展成1维向量,所以输入的节点个数为32*5*5,全连接层的节点个数为120
self.fc2 = nn.Linear(120, 84)#第二个全连接层的输入为120,第二层的节点个数为84
self.fc3 = nn.Linear(84, 10)#第三层的输入84,节点个数由于是cifar10,十分类,所以第三层输出为10
#前向传播过程,就是网络的一次一次卷积池化
def forward(self, x):
x = F.relu(self.conv1(x)) # input(3, 32, 32) output(16, 28, 28)
x = self.pool1(x) # 经过池化后output(16, 14, 14)
x = F.relu(self.conv2(x)) # output(32, 10, 10)
x = self.pool2(x) # output(32, 5, 5)
x = x.view(-1, 32*5*5) # output(32*5*5),.view将数据展平成一维数据,-1是展成维度直接推理,根据后面32*5*5可以推出是1维
x = F.relu(self.fc1(x)) # output(120),全连接层1,然后激活函数
x = F.relu(self.fc2(x)) # output(84),全连接层2,然后激活函数
x = self.fc3(x) # output(10)
return x
浙公网安备 33010602011771号