最简单朴素的CNN模型
import torch.nn as nn
import torch.nn.functional as F
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
class CNNNet(nn.Module):
def __init__(self):
super(CNNNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3,out_channels=16,kernel_size=5,stride=1)
self.pool1 = nn.MaxPool2d(kernel_size=2,stride=2)
self.conv2 = nn.Conv2d(in_channels=16,out_channels=36,kernel_size=3,stride=1)
self.pool2 = nn.MaxPool2d(kernel_size=2,stride=2)
#self.aap = nn.AdaptiveAvgPool2d(1)
self.fc1 = nn.Linear(1296,128)
self.fc2 = nn.Linear(128,10)
#self.fc3 = nn.Linear(36,10)
def forward(self,x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x)))
#x = self.aap(x)
#x = x.view(x.shape[0],-1)
#x = self.fc3(x)
x = x.view(-1,36*6*6)
#print("x.shape:{}".format(x.shape))
x = F.relu(self.fc2(F.relu(self.fc1(x))))
return x
model = CNNNet()
model = model.to(device)
print('--------------查看网络结构-----------')
print(model)