PyTorch基础练习-task7
# task7
import torch
import numpy as np
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
%matplotlib inline
# 准备数据
batch_size = 64
train_dataset = datasets.MNIST(root = './data/',
train = True,
transform = transforms.ToTensor(),
download = True)
test_dataset = datasets.MNIST(root = './data/',
train = False,
transform = transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset = train_dataset,
batch_size = batch_size,
shuffle = True)
test_loader = torch.utils.data.DataLoader(dataset = test_dataset,
batch_size = batch_size,
shuffle = False)
# 可视化数据图像
for i in range(5):
plt.figure()
plt.imshow(train_loader.dataset.train_data[i].numpy())
x = torch.randn(2, 2, 2)
x.view(-1, 1, 4)
# 创建类
class LeNet5(nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5, padding = 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.max_pool2d(F.tanh(self.conv1(x)), (2, 2))
x = F.dropout(x, p = 0.3, training = self.training)
x = F.max_pool2d(F.tanh(self.conv2(x)), (2, 2))
x = F.dropout(x, p = 0.3, training = self.training)
x = x.view(-1, self.num_flat_features(x))
x = F.tanh(self.fc1(x))
x = F.dropout(x, p = 0.3, training = self.training)
x = F.tanh(self.fc2(x))
x = F.dropout(x, p = 0.3, training = self.training)
x = self.fc3(x)
return x
def num_flat_features(self, x):
"""进行形状变换"""
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
# 实例化
model = LeNet5()
# 优化器
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1, momentum = 0.9)
criterion = nn.CrossEntropyLoss()
for (data, target) in train_loader:
for i in range(4):
plt.figure()
print(target[i])
plt.imshow(data[i].numpy()[0])
break
def train(epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = Variable(data), Variable(target)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
if batch_idx % 50 == 0:
print('Train Epoch:{} [{}/{} ({:.0f}%)]\tLoss:{:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.data))
return loss.data
def test():
model.eval()
test_loss = 0
correct = 0
for data, target in test_loader:
data, target = Variable(data), Variable(target)
output = model(data)
test_loss += criterion(output, target).data
pred = output.data.max(1, keepdim = True)[1]
correct += pred.eq(target.data.view_as(pred).sum())
test_loss /= len(test_loader.dataset)
print('\nTest set:Average loss:{:.4f}, Auccracy:{}/{}({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
Loss = []
for epoch in range(60):
loss = train(epoch)
Loss.append(loss)
test()