PyTorch Tutorials 摘要(1) Deep Learning with PyTorch: A 60 minute blitz


0. 前言

  • 官網鏈接
  • 本文目標:
    • 總體瞭解PyTorch的Tensor以及神經網絡相關內容。
    • 訓練一個簡單的圖像分類網絡。
  • 主要內容(對我來說的要點):
    • What is PyTorch:PyTorch的基本功能以及Tensor相關的基本操作。
      • in place操作,如x.add_(y)
    • Autograd: Automatic differentiation:自動微分相關API。
      • PyTorch中不推薦使用inplace操作,可能會導致自動求導時出錯。
    • Neural Networks:簡單介紹使用PyTorch構建神經網絡的過程。
    • Training a classifier:自定義神經網絡結構,訓練cifar10。

1. What is PyTorch

  • 本節鏈接
  • PyTorch 是什麼?
    • numpy 的GPU版實現。
    • 深度學習研究平臺
  • 介紹了PyTorch中tensor的幾種新建方式:
    • torch.empty(5, 3)
    • torch.rand(5, 3)
    • torch.zeros(5, 3, dtype=torch.long)
    • torch.tensor([5.5, 3])
    • x.new_ones(5, 3, dtype=torch.double)
    • torch.randn_like(x, dtype=torch.float)
  • Tensor相關的基本操作:
    • x.size():獲取tensor尺寸,返回值是 torch.Size 對象。
      • torch.Size([5, 3])
      • 是指是一個元組tuple,支持所有元組相關操作。
    • 向量加法:
      • x + y
      • z = torch.add(x, y)
      • torch.add(x, y, result)
      • y.add_(x):in place 操作,即 y += x
    • reshape操作:x.view(-1, 8)
    • 如果tensor只有一個元素(不管是不是標量),都可以通過 x.item() 來獲取Python類型的結果。
      • 有什麼用?可用於獲取loss,不用考慮CPU/GPU。
  • tensor與numpy的相互轉換
    • tonsor -> numpy: x.numpy()
    • numpy -> tensor: torch.from_numpy(ndarray)
  • CUDA tensors
    • 準備工作:device = torch.device("cuda")
    • 直接定義CUDA tensor(指定device參數):torch.ones_like(x, device=device)
    • CPU Tensor轉換爲CUDA tensor:cpu_tensor.to(device)
    • CUDA Tensor轉換爲CPU Tensor:cuda_tensor.to("cpu", torch.double)

2. Autograd: Automatic differentiation

  • 本節鏈接本節翻譯
  • 擴展閱讀:
  • 背景:神經網絡的基礎就是BP算法,而在各類深度學習框架中,一個基本實現BP的基本方法就是 Autograd(即自動求導)。
  • 本章主要內容就是介紹了PyTorch中自動求導的應用。
  • torch.Tensor是實現自動微分的核心。
  • 如果在定義某tensor的時候設置 .requires_grad=True,則調用相關tensor的 .backward() 方法時就會計算之前tensor對應的提取,獲取提取的方法是通過屬性 .grad
  • 如果想要令某個tensor不再參與BP,則可以調用 .detach()
  • 如果想要令某些tensor不再參與BP,可以使用 with torch.no_grad(): 定義網絡。
  • 自動求導中很重要的一個類就是 Function,每個tensor都有一個 .grad_fn 屬性,本質就是 Function的實例。
    • 手動實現的tensor的.grad_fn爲None,自動生成的tensor都有設置該參數。
    • Function 指的是在計算圖中某個節點(node)所進行的運算,比如加減乘除卷積等等之類的,Function 內部有 forward() 和 backward() 兩個方法,分別應用於正向、反向傳播。
  • 調用 .backward() 方法時需要注意,如果tensor是一個標量(或者不是標量,但只有一個元素)則不需要輸入任何參數,如果不是標量就需要輸入一個張量,要求shape與該tensor相同。

3. Neural Networks

  • 本節鏈接
  • PyTorch中構建神經網絡:
    • 一般基於 torch.nn 類。
    • 所有的模型(以及各種layer)都繼承了 nn.Module,這個類中包含了前向過程forward(input)
  • 典型的卷積神經網絡訓練過程:
    • 定義一個神經網絡,包括了一些需要訓練的參數。
    • 遍歷某個Dataset獲取訓練輸入數據。
    • 通過正向傳到得到模型輸出結果。
    • 計算損失函數。
    • 通過自動微分求導。
    • 更新網絡模型中的參數。
  • 一個實例
import torch
import torch.nn as nn
import torch.nn.functional as F

# 定義一個模型
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 3x3 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 3)
        self.conv2 = nn.Conv2d(6, 16, 3)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 6 * 6, 120)  # 6*6 from image dimension
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

# 定義了模型還能輸出查看
net = Net()
print(net)

# 獲取模型參數
params = list(net.parameters())
print(len(params))
print(params[0].size())  # conv1's .weight

# 模型前向傳遞,獲得模型結果
input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)

# 清零梯度,並計算梯度
net.zero_grad()
out.backward(torch.randn(1, 10))

# 計算損失函數
output = net(input)
target = torch.randn(10)  # a dummy target, for example
target = target.view(1, -1)  # make it the same shape as output
criterion = nn.MSELoss()
loss = criterion(output, target)
print(loss)

# 手動更新梯度
learning_rate = 0.01
for f in net.parameters():
    f.data.sub_(f.grad.data * learning_rate)

# 優化器自動更新梯度
import torch.optim as optim

# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)

# in your training loop:
optimizer.zero_grad()   # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()    # Does the update

4. Training a classifier

  • 本節連接
  • 使用了torchvision來實現CNN訓練CIFAR10。
import torch
import torchvision
import torchvision.transforms as transforms

# 定義數據預處理方式
transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

# 構建數據集
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=2)

# 類別信息基本情況
classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

# 查看輸入數據
import matplotlib.pyplot as plt
import numpy as np

# functions to show an image


def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

# 定義神經網絡
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 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 = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()

# 定義損失函數
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

# 訓練模型
for epoch in range(2):  # loop over the dataset multiple times
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        
        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0
print('Finished Training')

# 保存模型
PATH = './cifar_net.pth'
torch.save(net.state_dict(), PATH)

# 測試模型
dataiter = iter(testloader)
images, labels = dataiter.next()
# print images
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))

# 導入模型
net = Net()
net.load_state_dict(torch.load(PATH))

# 獲取少量圖片的預測結果
outputs = net(images)
_, predicted = torch.max(outputs, 1)
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
                              for j in range(4)))

# 在整個驗證集上驗證模型結果
correct = 0
total = 0
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (
    100 * correct / total))

# 查看每一類的預測結果
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        for i in range(4):
            label = labels[i]
            class_correct[label] += c[i].item()
            class_total[label] += 1
for i in range(10):
    print('Accuracy of %5s : %2d %%' % (
        classes[i], 100 * class_correct[i] / class_total[i]))

# GPU上訓練
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)
inputs, labels = data[0].to(device), data[1].to(device)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章