1. 程式人生 > 實用技巧 >PyTorch Quick Start

PyTorch Quick Start

聽說PyTorch比Tensorflow更加直觀,更容易理解,就試一試

一、安裝

開啟官網

按自己的情況選擇,我是在windows上且只使用cpu,下載最新的穩定版。

直接複製命令執行即可。

但是Torch有點大,直接用pip下載有點大。我們可以找到命令執行時所下載的xx.whl,拿出來用FDM等多執行緒下載器下載,然後再安裝,例如

https://download.pytorch.org/whl/cpu/torch-1.6.0%2Bcpu-cp38-cp38-win_amd64.whl

>pip uninstall "torch-1.6.0+cpu-cp38-cp38-win_amd64.whl
"

注意,torchvision版本與torch版本是對應的,不能隨意匹配。所以我們用之前的命令把torchvision下完(已經下好的torch會自動跳過)

二、示例

import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt

# torch.manual_seed(1)    # reproducible

x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)  # x data (tensor), shape=(100, 1)
y = x.pow(2) + 0.2*torch.rand(x.size())                 #
noisy y data (tensor), shape=(100, 1) # torch can only train on Variable, so convert them to Variable # The code below is deprecated in Pytorch 0.4. Now, autograd directly supports tensors # x, y = Variable(x), Variable(y) # plt.scatter(x.data.numpy(), y.data.numpy()) # plt.show() class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output): super(Net, self).__init__() self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer self.predict = torch.nn.Linear(n_hidden, n_output) # output layer def forward(self, x): x = F.relu(self.hidden(x)) # activation function for hidden layer x = self.predict(x) # linear output return x net = Net(n_feature=1, n_hidden=10, n_output=1) # define the network print(net) # net architecture optimizer = torch.optim.SGD(net.parameters(), lr=0.2) loss_func = torch.nn.MSELoss() # this is for regression mean squared loss plt.ion() # something about plotting for t in range(200): prediction = net(x) # input x and predict based on x loss = loss_func(prediction, y) # must be (1. nn output, 2. target) optimizer.zero_grad() # clear gradients for next train loss.backward() # backpropagation, compute gradients optimizer.step() # apply gradients if t % 5 == 0: # plot and show learning process plt.cla() plt.scatter(x.data.numpy(), y.data.numpy()) plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5) plt.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'}) plt.pause(0.1) plt.ioff() plt.show()

三、其他

增加一層中間層,並使用啟用函式,得到3個不同的net,進行橫向對比

"""
View more, visit my tutorial page: https://morvanzhou.github.io/tutorials/
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
Dependencies:
torch: 0.4
matplotlib
"""
import torch
from torch import nn
import torch.nn.functional as F
import matplotlib.pyplot as plt


x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)  # x data (tensor), shape=(100, 1)
y = x.pow(2) + 0.2*torch.rand(x.size())                 # noisy y data (tensor), shape=(100, 1)

# 1 10 1
class Net(torch.nn.Module):
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)   # hidden layer
        self.predict = torch.nn.Linear(n_hidden, n_output)   # output layer

    def forward(self, x):
        x = F.relu(self.hidden(x))      # activation function for hidden layer
        x = self.predict(x)             # linear output
        return x

# 1 10 10 1
class simpleNet(nn.Module):
    """
    定義了一個簡單的三層全連線神經網路,每一層都是線性的
    """
    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super(simpleNet, self).__init__()
        self.layer1 = nn.Linear(in_dim, n_hidden_1)
        self.layer2 = nn.Linear(n_hidden_1, n_hidden_2)
        self.layer3 = nn.Linear(n_hidden_2, out_dim)

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        return x

class Activation_Net(nn.Module):
    """
    在上面的simpleNet的基礎上,在每層的輸出部分添加了啟用函式
    """
    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super(Activation_Net, self).__init__()
        self.layer1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.ReLU(True))
        self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.ReLU(True))
        self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, out_dim))
        """
        這裡的Sequential()函式的功能是將網路的層組合到一起。
        """

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        return x

class Batch_Net(nn.Module):
    """
    在上面的Activation_Net的基礎上,增加了一個加快收斂速度的方法——批標準化
    """
    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super(Batch_Net, self).__init__()
        self.layer1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.BatchNorm1d(n_hidden_1), nn.ReLU(True))
        self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.BatchNorm1d(n_hidden_2), nn.ReLU(True))
        self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, out_dim))

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        return x



def Use(net):
    # print(net)
    optimizer = torch.optim.SGD(net.parameters(), lr=0.2)
    loss_func = torch.nn.MSELoss()  # this is for regression mean squared loss

    prediction = net(x)     # input x and predict based on x

    loss = loss_func(prediction, y)     # must be (1. nn output, 2. target)

    optimizer.zero_grad()   # clear gradients for next train
    loss.backward()         # backpropagation, compute gradients
    optimizer.step()

    return prediction, loss

def Draw(prediction, loss, ax):
    # plt.figure(figsize=(8,6), dpi=80)
    # ax = plt.subplot(1,2,num)
    ax.cla()
    ax.scatter(x.data.numpy(), y.data.numpy())
    ax.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
    ax.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 15, 'color':  'red'})
    plt.pause(0.1)


def start(ax1, ax2, ax3, ax4, net1, net2, net3, net4):
    for t in range(50):
        print("Generation %d" % t)
        prediction, loss = Use(net1)
        Draw(prediction, loss, ax1)
        prediction, loss = Use(net2)
        Draw(prediction, loss, ax2)
        prediction, loss = Use(net3)
        Draw(prediction, loss, ax3)
        prediction, loss = Use(net4)
        Draw(prediction, loss, ax4)


    plt.show()


plt.figure(figsize=(8, 8), dpi=80)
ax1 = plt.subplot(2,2,1)
ax2 = plt.subplot(2,2,2)
ax3 = plt.subplot(2,2,3)
ax4 = plt.subplot(2,2,4)

net1 = Net(n_feature=1, n_hidden=10, n_output=1)
net2 = simpleNet(in_dim=1, n_hidden_1=10, n_hidden_2=15, out_dim=1)
net3 = Activation_Net(in_dim=1, n_hidden_1=10, n_hidden_2=15, out_dim=1)
net4 = Batch_Net(in_dim=1, n_hidden_1=10, n_hidden_2=15, out_dim=1)

start(ax1, ax2, ax3, ax4, net1, net2, net3, net4)

參考連結:

1.https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/301_regression.py

2.https://blog.csdn.net/out_of_memory_error/article/details/81414986