Lenet
import torch
from torch import nn
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
'''
这里搭建卷积层,需要按顺序定义卷积层、
激活函数、最大池化层、卷积层、激活函数、最大池化层,
具体形状见测试说明
'''
self.conv = nn.Sequential(
nn.Conv2d(1,6,5),
nn.Sigmoid(),
nn.MaxPool2d(2,2),
nn.Conv2d(6,16,5),
nn.Sigmoid(),
nn.MaxPool2d(2,2)
)
'''
这里搭建全连接层,需要按顺序定义全连接层、
激活函数、全连接层、激活函数、全连接层,
具体形状见测试说明
'''
self.fc = nn.Sequential(
nn.Linear(256,120),
nn.Sigmoid(),
nn.Linear(120,84),
nn.Sigmoid(),
nn.Linear(84,10)
)
def forward(self, img):
'''
这里需要定义前向计算
'''
feature=self.conv(img)
output=self.fc(feature.view(img.shape[0],-1))
return output
Alexet
import torch
from torch import nn
class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
'''
这里搭建卷积层,需要按顺序定义卷积层、
激活函数、最大池化层、卷积层、激活函数、
最大池化层、卷积层、激活函数、卷积层、
激活函数、卷积层、激活函数、最大池化层,
具体形状见测试说明
'''
self.conv = nn.Sequential(
nn.Conv2d(1, 96, kernel_size=(11, 11), stride=(4, 4)),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False),
nn.Conv2d(96, 256, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False),
nn.Conv2d(256, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
nn.ReLU(),
nn.Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
nn.ReLU(),
nn.Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
)
'''
这里搭建全连接层,需要按顺序定义
全连接层、激活函数、丢弃法、
全连接层、激活函数、丢弃法、全连接层,
具体形状见测试说明
'''
self.fc = nn.Sequential(
nn.Linear(in_features=6400, out_features=4096, bias=True),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(in_features=4096, out_features=4096, bias=True),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(in_features=4096, out_features=10, bias=True)
)
def forward(self, img):
'''
这里需要定义前向计算
'''
feature=self.conv(img)
output=self.fc(feature.view(img.shape[0],-1))
return output