卷积神经网络构建
一般卷积层,relu层,池化层写成一个模块
import torch.nn as nn
class CNN(nn.Module):
def __init__(self):
super(CNN,self).__init__()
self.conv1=nn.Sequential(
nn.Conv2d(
in_channels=1,#输入通道数1(灰度图)
out_channels=16,#输出通道数
kernel_size=5,#卷积核尺寸
stride=1,#步长
padding=2,#填充
),
nn.ReLU(),#relu激活函数(16,28,28)
nn.MaxPool2d(kernel_size=2)#进行池化操作(16,14,14)
)
self.conv2=nn.Sequential(
nn.Conv2d(16,32,5,1,2),#第二个卷积
nn.ReLU(),
nn.MaxPool2d(2)
)
self.out=nn.Linear(32*7*7,10)#全连接层
def forword(self,x):
x=self.conv1(x)
x=self.conv2(x)
x=x.view(x.size(0),-1)
output=self.out(x)
return output
卷积WH计算公式