首先准备数据集,在pytorch团队中已经为我们编写快速记载MNIST数据集(MNIST是一个手写数字数据库)的方法
from torchvision import datasets, transforms
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])#自定义一个数据
#处理的方法
train_set = datasets.MNIST('data', train=True, download=True, transform=transform)
text_set = datasets.MNIST('data', train=False, download=True, transform=transform)
transform函数是自定义进行数据预处理,包括将数据转化为tensor对象和进行数据归一化处理,在datasets.MNIST函数中,train='ture'代表训练集,‘false’代表测试集
需要的包
import torch
import torch.nn as nn
from torchvision import datasets, transforms
import torch.nn.functional as F
import torch.optim as optim
接下来是我们的letnet网络结构示意图
定义Letnet网络模型,在卷积操作中使用Module提供的conv2d()函数(第一个参数代表输入灰度图数量,第二个代表输出特征图数量,第三个参数代表卷积核大小),池化操作则用max_pool12d()函数
class LetNet(nn.Module):
def __init__(self):
super(LetNet, self).__init__()
self.c1 = nn.Conv2d(1, 6, 5)
self.c3 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 4 * 4, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.c1(x)), 2)
x = F.max_pool2d(F.relu(self.c3(x)), 2)
x = x.view(-1, num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
然后我们自定义了个num_flat_features(x)函数,来计算x的特征点总点数,由于pytorch只接受批数据输入方式(同时输入好几张图片进行操作)所以在view之前是几个维度的,通过view后x转化为2维
def num_flat_features(x):
size =x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
然后就是将数据转化到GPU上运算
cuda = torch.cuda.is_available()
if cuda:
let_net = LetNet().cuda()
else:
let_net = LetNet()
定义优化函数sge和损失函数criterion还有数据加载函数(参数shuffle =ture是打乱数据顺序)
训练函数,我们每运行1000次打印损失值,一共训练两次
def train(model, criterion, sge, epochs):
for epoch in range(epochs):
running_loss = 0.0
for i, data in enumerate(train_loder, 0):
inputs, labels = data
if cuda:
inputs, labels = inputs.cuda(), labels.cuda()
sge.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
sge.step()
running_loss += loss.item()
if i % 1000 == 999:
print('[e=%d,b=%5d] loss=%.3f' % (epoch + 1, i + 1, running_loss / 1000))
running_loss = 0.0
print('train_over')
if __name__ == '__main__':
train(let_net, criterion, sge, epochs=2)
sge = optim.SGD(let_net.parameters(), lr=0.001, momentum=0.9)
criterion = nn.CrossEntropyLoss()
train_loder = DataLoader(train_set, batch_size=4, shuffle=True, num_workers=2)