全连接是AI入门的第一个网络模型,所以其局限性也很大,它在准确度上确实还不是很高。下面就让我们来看看卷积网络是怎么搭建的吧。
import torch.nn as nnclass CNNet(nn.Module): def __init__(self): super(CNNet, self).__init__() self.cnn_layer = nn.Sequential( # input_size : 32*32*3 nn.Conv2d(3,16,3,1), nn.ReLU(), nn.MaxPool2d(2,2), nn.Conv2d(16,32,3,1), nn.ReLU(), nn.MaxPool2d(2,2), nn.Conv2d(32,64,3,1), nn.ReLU() ) self.mlp_layer = nn.Sequential( nn.Linear(4*4*64, 128), nn.ReLU(), # nn.Linear(128,80), # nn.ReLU(), nn.Linear(128,10) ) def forward(self, x): # NaN x = self.cnn_layer(x) # shape: nchw nv x = x.reshape(-1,4*4*64) x = self.mlp_layer(x) return x这就是卷积网络搭建的完整代码啦。