AI基础之卷积网络

全连接是AI入门的第一个网络模型,所以其局限性也很大,它在准确度上确实还不是很高。下面就让我们来看看卷积网络是怎么搭建的吧。

import torch.nn as nn

class 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
这就是卷积网络搭建的完整代码啦。

猜你喜欢

转载自www.cnblogs.com/wangyueyyy/p/11875507.html
今日推荐