数据准备:
import torch
from torchvision import datasets, transforms# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# Download and load the training data
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)# Download and load the test data
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)
Pytorch构建BP神经网络
class Classifier(nn.Module):def __init__(self):super().__init__() self.fc1 = nn.Linear(784, 256)self.fc2 = nn.Linear(256, 128)self.fc3 = nn.Linear(128, 64)self.fc4 = nn.Linear(64, 10)# Dropout module with 0.2 drop probabilityself.dropout = nn.Dropout(p=0.2)def forward(self, x):# make sure input tensor is flattenedx = x.view(x.shape[0], -1) #修改输入x的size# Now with dropoutx = self.dropout(F.relu(self.fc1(x)))x = self.dropout(F.relu(self.fc2(x)))x = self.dropout(F.relu(self.fc3(x)))# output so no dropout herex = F.log_softmax(self.fc4(x), dim=1)return x
BP神经网络训练
model = Classifier()
criterion = nn.NLLLoss() #损失函数
optimizer = optim.Adam(model.parameters(), lr=0.003) #优化器epochs = 30
steps = 0train_losses, test_losses = [], []
for e in range(epochs):running_loss = 0for images, labels in trainloader:optimizer.zero_grad() #损失梯度清0log_ps = model(images) #模型输入,获得输出loss = criterion(log_ps, labels) #获得损失误差loss.backward() #反向传播optimizer.step() #更新权重running_loss += loss.item() #误差累加else:test_loss = 0accuracy = 0# Turn off gradients for validation, saves memory and computationswith torch.no_grad(): #梯度不回传model.eval() for images, labels in testloader:log_ps = model(images)test_loss += criterion(log_ps, labels)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))model.train()train_losses.append(running_loss/len(trainloader))test_losses.append(test_loss/len(testloader))print("Epoch: {}/{}.. ".format(e+1, epochs),"Training Loss: {:.3f}.. ".format(running_loss/len(trainloader)),"Test Loss: {:.3f}.. ".format(test_loss/len(testloader)),"Test Accuracy: {:.3f}".format(accuracy/len(testloader)))
查看训练后的误差损失对比
%matplotlib inline
%config InlineBackend.figure_format = 'retina'import matplotlib.pyplot as pltplt.plot(train_losses, label='Training loss')
plt.plot(test_losses, label='Validation loss')
plt.legend(frameon=False)
推理
# Import helper module (should be in the repo)
import helper# Test out your network!model.eval()dataiter = iter(testloader)
images, labels = dataiter.next()
img = images[0]
# Convert 2D image to 1D vector
img = img.view(1, 784)# Calculate the class probabilities (softmax) for img
with torch.no_grad():output = model.forward(img)ps = torch.exp(output)# Plot the image and probabilities
helper.view_classify(img.view(1, 28, 28), ps, version='Fashion')