1.用ReLU得到非线性,用ReLU非线性函数作为激活函数
2.使用dropout的trick在训练期间有选择性的忽略隐藏层神经元,来减缓学习模型的过拟合
3.使用重叠最大池进行池化操作,避免平均池的平均效果
4.使用GPU可以减少训练时间,这比CPU处理快了10倍,所以可以被用于更大的数据集和图像上。
import torch.nn as nn
import torch.nn.functional as F
class Alexnet(nn.Module):
def __init__(self):
super(Alexnet, self).__init__()
self.conv1 = nn.Conv2d(3, 96,11,4,0)
self.pool = nn.MaxPool2d(3, 2)
self.conv2 = nn.Conv2d(96, 256, 5, 1, 2)
self.Conv3 = nn.Conv2d(256, 384, 3, 1, 1)
self.Conv4 = nn.Conv2d(384,384, 3, 1, 1)
self.Conv5 = nn.Conv2d(384,256, 3, 1, 1)
self.drop = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 4096)
self.fc2 = nn.Linear(4096, 4096)
self.fc3 = nn.Linear(4096, 100)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = self.pool(F.relu(self.conv5(x)))
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = self.drop(F.relu(self.fc1(x)))
x = self.drop(F.relu(self.fc2(x)))
x = self.fc3(x)
return x
net = Alexnet()
print(net)
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import torch.nn.functional as F
torch.manual_seed(1)
batch_size = 64
learning_rate = 1e-2
num_epoches = 100
# 下载数据集
train_data = torchvision.datasets.MNIST(
root='./data/', # 保存或提取的位置
train=True, # true说明是用于训练的数据,false说明是用于测试的数据
transform=torchvision.transforms.ToTensor(), # #转换成PIL.Image or numpy.ndarray
download=False
)
test_data = torchvision.datasets.MNIST(
root='./data',
train=False, # 表明是测试集
transform=torchvision.transforms.ToTensor()
)
# 批训练50个samples,1 channel,28*28(50,1,28,28)
# torch中DataLoader是包装数据的工具,能够帮我们有效迭代数据,这样就可以批训练
train_loader = Data.DataLoader(
dataset=train_data,
batch_size=batch_size,
shuffle=True, # 是否打乱数据,一般都打乱
)
test_loader = Data.DataLoader(
dataset=test_data,
batch_size=batch_size,
shuffle=True, # 是否打乱数据,一般都打乱
)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1,32,3,1,1)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, 1, 1)
self.conv3 = nn.Conv2d(64, 128, 3, 1, 1)
self.conv4 = nn.Conv2d(128,256, 3, 1, 1)
self.conv5 = nn.Conv2d(256,256, 3, 1, 1)
self.pool2 = nn.MaxPool2d(3, 2)
self.drop = nn.Dropout(0.5)
self.fc1 = nn.Linear(256*3*3, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 10)
def forward(self, x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool1(F.relu(self.conv2(x)))
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = self.pool2(F.relu(self.conv5(x)))
x = x.view(-1,256*3*3)
x = F.relu(self.fc1(x))
x = self.drop(F.relu(self.fc1(x)))
x = self.drop(F.relu(self.fc2(x)))
x = self.fc3(x)
return x
net = Net()
model = Net()
# 定义loss和optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# 开始训练
for i, data in enumerate(train_loader, 1):
img, label = data
img = torch.autograd.Variable(img)
label = torch.autograd.Variable(label)
print(label.size(0))
#开始训练
for epoch in range (num_epoches) :
running_loss = 0.0
running_acc= 0.0
for i,data in enumerate(train_loader, 1):
img,label = data
img = torch.autograd.Variable(img)
label = torch.autograd.Variable(label)
out = model (img)
loss = criterion(out, label) # loss
a = loss.item() * label.size(0)
running_loss=running_loss+a
_,pred=torch.max(out,1)
num_correct=(pred==label).sum()
accuracy = (pred == label).float().mean()
b= num_correct.item()
running_acc=running_acc+b
#后向传
optimizer.zero_grad()
loss.backward() #后向传播,计算梯度
optimizer.step() #利用梯度更新W、b参数
# #打印一个循环后,训练集合上的loss和正确率
print('Train {} epoch, Loss:{:.6f}, Acс:{:.6f}'.format(epoch + 1,running_loss/(len(train_data)),running_acc / (len(train_data))))