我的数据科学之路-用pytorch做深度学习
上一篇文章介绍了深度学习的基本原理,核心过程就是正向传播与反向传播,正向传播由输入到输出,完成一次预测,反向传播是由计算所得输出算出与真实值的差距,然后反向计算梯度,优化模型。实际使用深度学习的时候这个过程相对是比较机械的,使用一些集成库可以简化这个操作,不用每次自己写代码完成所有细节。本篇介绍一个相对比较新也比较好用的库pytorch。pytorch是一个facebook团队研发的开源机器学习库,使用它可以很方便的完成深度学习的过程,本篇使用它完成一个基本的构建模型-训练-测量的过程。
#第一步,导包
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
#第二步,导入数据,此步骤中因为现实数据通常是各种各样的,所以可以使用transforms来对数据进行一些#变换,如旋转剪裁等,是数据达到统一标准
train_transforms =transforms.Compose([transforms.RandomRotation(30),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
#颜色通道
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
test_transforms =transforms.Compose([transforms.Resize(255),
transforms.CenterCrop(224), transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
# Pass transforms in here, then run the next cell to see how the transforms look
train_data = datasets.ImageFolder(data_dir + '/train', transform=train_transforms)
test_data = datasets.ImageFolder(data_dir + '/test', transform=test_transforms)
trainloader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)
testloader = torch.utils.data.DataLoader(test_data, batch_size=32)
#第三步,构建神经网络,可以完全自己定义,层数,节点,函数等,也可以使用经过预训练的模型
class Classifier(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(150528,1024)
self.fc2 = nn.Linear(1024,784)
self.fc3 = nn.Linear(784, 256)
self.fc4 = nn.Linear(256, 128)
self.fc5 = nn.Linear(128, 64)
self.fc6 = nn.Linear(64, 2)
self.dropout = nn.Dropout(p=0.2)
def forward(self, x):
# make sure input tensor is flattened
x = x.view(x.shape[0], -1)
x = self.dropout(F.relu(self.fc1(x)))
x = self.dropout(F.relu(self.fc2(x)))
x = self.dropout(F.relu(self.fc3(x)))
x = self.dropout(F.relu(self.fc4(x)))
x = self.dropout(F.relu(self.fc5(x)))
x = F.log_softmax(self.fc6(x), dim=1)
return x
model = Classifier()
model = models.densenet121(pretrained=True)
for param in model.parameters():
param.requires_grad = False
from collections import OrderedDict
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(1024, 500)),
('relu', nn.ReLU()),
('fc2', nn.Linear(500, 2)),
('output', nn.LogSoftmax(dim=1))
]))
#保持隐层参数不变,修改输出层
model.classifier = classifier
#第四步,定义损失函数和训练方法
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=0.003)
#第五步,循环训练模型并输出性能变化
for e in range(epochs):
running_loss=0
for images,labels in trainloader:
optimizer.zero_grad()
log_ps=model(images)
loss=criterion(log_ps,labels)
loss.backward()
running_loss+=loss.item()
optimizer.step()
steps+=1
if steps%3==0:
test_loss=0
accuracy=0
model.eval()
with torch.no_grad():
for inputs,labels in testloader:
log_ps=model(inputs)
loss=criterion(log_ps,labels)
test_loss+=loss.item()
ps=torch.exp(log_ps)
top_p, top_class = ps.topk(1, dim=1)
equals=top_class==labels.view(*top_class.shape)
accuracy+=torch.mean(equals.type(torch.FloatTensor)).item()
print(f"epoch:{e+1}/{epochs}\ntrain_loss:{running_loss/5:.3f}\ntest_loss:{test_loss/5:.3f}\n.. "
f"accuracy:{accuracy/len(testloader):.3f}")
running_loss=0
model.train()