前面幾篇文章介紹了特徵歸一化和張量,接下來開始寫兩篇PyTorch簡明教程,主要介紹PyTorch簡單實踐。
import torcha = torch.tensor([2, 3, 4])b = torch.tensor([3, 4, 5])print("a + b: ", (a + b).numpy())print("a - b: ", (a - b).numpy())print("a * b: ", (a * b).numpy())print("a / b: ", (a / b).numpy())
加減乘除就不用多解釋了,輸出為:
a + b:[5 7 9]a - b:[-1 -1 -1]a * b:[ 6 12 20]a / b:[0.6666667 0.750.8]
線性迴歸是找到一條直線盡可能接近已知點,如圖:
圖1
import torchfrom torch import optimdef build_model1():return torch.nn.Sequential(torch.nn.Linear(1, 1, bias=False))def build_model2():model = torch.nn.Sequential()model.add_module("linear", torch.nn.Linear(1, 1, bias=False))return modeldef train(model, loss, optimizer, x, y):model.train()optimizer.zero_grad()fx = model.forward(x.view(len(x), 1)).squeeze()output = loss.forward(fx, y)output.backward()optimizer.step()return output.item()def main():torch.manual_seed(42)X = torch.linspace(-1, 1, 101, requires_grad=False)Y = 2 * X + torch.randn(X.size()) * 0.33print("X: ", X.numpy(), ", Y: ", Y.numpy())model = build_model1()loss = torch.nn.MSELoss(reductinotallow='mean')optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)batch_size = 10for i in range(100):cost = 0.num_batches = len(X) // batch_sizefor k in range(num_batches):start, end = k * batch_size, (k + 1) * batch_sizecost += train(model, loss, optimizer, X[start:end], Y[start:end])print("Epoch = %d, cost = %s" % (i + 1, cost / num_batches))w = next(model.parameters()).dataprint("w = %.2f" % w.numpy())if __name__ == "__main__":main()
(1)先從main函數開始,torch.manual_seed(42 )用於設定隨機數產生器的種子,以確保在每次運行時產生的隨機數序列相同,該函數接受一個整數參數作為種子,可以在訓練神經網路等需要隨機數的場景中使用,以確保結果的可重複性;
(2)torch.linspace(-1, 1, 101, requires_grad=False)用於在指定的區間內產生一組等間隔的數值,該函數接受三個參數:起始值、終止值和元素個數,傳回一個張量,其中包含了指定個數的等間隔數值;
(3)build_model1的內部實作:
#(4)torch.nn.MSELoss (reductinotallow='mean')定義損失函數;
使用optim.SGD(model.parameters(), lr=0.01, momentum=0.9)可以實現隨機梯度下降(Stochastic Gradient Descent,SGD)優化演算法
將訓練集透過批次大小拆分,循環100次
(7)接下來是訓練函數train,用於訓練一個神經網路模型,具體來說,該函數接受以下參數:
(8)train是PyTorch訓練過程中常用的方法,其步驟如下:
(9)print("輪次= %d, 損失值= %s" % (i 1, cost / num_batches)) 最後列印目前訓練的輪次和損失值,上述的程式碼輸出如下:
...Epoch = 95, cost = 0.10514946877956391Epoch = 96, cost = 0.10514946877956391Epoch = 97, cost = 0.10514946877956391Epoch = 98, cost = 0.10514946877956391Epoch = 99, cost = 0.10514946877956391Epoch = 100, cost = 0.10514946877956391w = 1.98
邏輯迴歸即用一條曲線近似表示一堆離散點的軌跡,如圖:
圖2
import numpy as npimport torchfrom torch import optimfrom data_util import load_mnistdef build_model(input_dim, output_dim):return torch.nn.Sequential(torch.nn.Linear(input_dim, output_dim, bias=False))def train(model, loss, optimizer, x_val, y_val):model.train()optimizer.zero_grad()fx = model.forward(x_val)output = loss.forward(fx, y_val)output.backward()optimizer.step()return output.item()def predict(model, x_val):model.eval()output = model.forward(x_val)return output.data.numpy().argmax(axis=1)def main():torch.manual_seed(42)trX, teX, trY, teY = load_mnist(notallow=False)trX = torch.from_numpy(trX).float()teX = torch.from_numpy(teX).float()trY = torch.tensor(trY)n_examples, n_features = trX.size()n_classes = 10model = build_model(n_features, n_classes)loss = torch.nn.CrossEntropyLoss(reductinotallow='mean')optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)batch_size = 100for i in range(100):cost = 0.num_batches = n_examples // batch_sizefor k in range(num_batches):start, end = k * batch_size, (k + 1) * batch_sizecost += train(model, loss, optimizer,trX[start:end], trY[start:end])predY = predict(model, teX)print("Epoch %d, cost = %f, acc = %.2f%%"% (i + 1, cost / num_batches, 100. * np.mean(predY == teY)))if __name__ == "__main__":main()
(1)先從main函數開始,torch.manual_seed(42)上面有介紹,在此略過;
(2)load_mnist是自己實作下載mnist資料集,回傳trX和teX是輸入數據,trY和teY是標籤資料;
(3)build_model內部實作:torch.nn .Sequential(torch.nn.Linear(input_dim, output_dim, bias=False)) 用於建構一個包含一個線性層的神經網路模型,模型的輸入特徵數量為input_dim,輸出特徵數量為output_dim,且該線性層沒有偏置項,其中n_classes=10表示輸出10個分類; 重寫後: (3)build_model內部實作:使用torch.nn.Sequential(torch.nn.Linear(input_dim, output_dim, bias=False)) 建構一個包含一個線性層的神經網路模型,該模型的輸入特徵數量為input_dim,輸出特徵數量為output_dim,且該線性層沒有偏壓項。其中n_classes=10表示輸出10個分類;
(4)其他的步驟就是定義損失函數,梯度下降優化器,透過batch_size將訓練集拆分,循環100次進行train;
使用optim.SGD(model.parameters(), lr=0.01, momentum=0.9)可以實現隨機梯度下降(Stochastic Gradient Descent,SGD)優化演算法
(6)在每一輪訓練結束後,需要執行predict函數來進行預測。函數接受兩個參數model(已經訓練好的模型)和teX(需要進行預測的資料)。具體步驟如下:
(7)print("Epoch %d, cost = %f, acc = %.2f%%" % (i + 1, cost / num_batches, 100. * np.mean(predY == teY)))最后打印当前训练的轮次,损失值和acc,上述的代码输出如下(执行很快,但是准确率偏低):
...Epoch 91, cost = 0.252863, acc = 92.52%Epoch 92, cost = 0.252717, acc = 92.51%Epoch 93, cost = 0.252573, acc = 92.50%Epoch 94, cost = 0.252431, acc = 92.50%Epoch 95, cost = 0.252291, acc = 92.52%Epoch 96, cost = 0.252153, acc = 92.52%Epoch 97, cost = 0.252016, acc = 92.51%Epoch 98, cost = 0.251882, acc = 92.51%Epoch 99, cost = 0.251749, acc = 92.51%Epoch 100, cost = 0.251617, acc = 92.51%
一个经典的LeNet网络,用于对字符进行分类,如图:
图3
import numpy as npimport torchfrom torch import optimfrom data_util import load_mnistdef build_model(input_dim, output_dim):return torch.nn.Sequential(torch.nn.Linear(input_dim, 512, bias=False),torch.nn.Sigmoid(),torch.nn.Linear(512, output_dim, bias=False))def train(model, loss, optimizer, x_val, y_val):model.train()optimizer.zero_grad()fx = model.forward(x_val)output = loss.forward(fx, y_val)output.backward()optimizer.step()return output.item()def predict(model, x_val):model.eval()output = model.forward(x_val)return output.data.numpy().argmax(axis=1)def main():torch.manual_seed(42)trX, teX, trY, teY = load_mnist(notallow=False)trX = torch.from_numpy(trX).float()teX = torch.from_numpy(teX).float()trY = torch.tensor(trY)n_examples, n_features = trX.size()n_classes = 10model = build_model(n_features, n_classes)loss = torch.nn.CrossEntropyLoss(reductinotallow='mean')optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)batch_size = 100for i in range(100):cost = 0.num_batches = n_examples // batch_sizefor k in range(num_batches):start, end = k * batch_size, (k + 1) * batch_sizecost += train(model, loss, optimizer,trX[start:end], trY[start:end])predY = predict(model, teX)print("Epoch %d, cost = %f, acc = %.2f%%"% (i + 1, cost / num_batches, 100. * np.mean(predY == teY)))if __name__ == "__main__":main()
(1)以上这段神经网络的代码与逻辑回归没有太多的差异,区别的地方是build_model,这里是构建一个包含两个线性层和一个Sigmoid激活函数的神经网络模型,该模型包含一个输入特征数量为input_dim,输出特征数量为output_dim的线性层,一个Sigmoid激活函数,以及一个输入特征数量为512,输出特征数量为output_dim的线性层;
(2)print("Epoch %d, cost = %f, acc = %.2f%%" % (i + 1, cost / num_batches, 100. * np.mean(predY == teY)))最后打印当前训练的轮次,损失值和acc,上述的代码输入如下(执行时间比逻辑回归要长,但是准确率要高很多):
第91个时期,费用= 0.054484,准确率= 97.58%第92个时期,费用= 0.053753,准确率= 97.56%第93个时期,费用= 0.053036,准确率= 97.60%第94个时期,费用= 0.052332,准确率= 97.61%第95个时期,费用= 0.051641,准确率= 97.63%第96个时期,费用= 0.050964,准确率= 97.66%第97个时期,费用= 0.050298,准确率= 97.66%第98个时期,费用= 0.049645,准确率= 97.67%第99个时期,费用= 0.049003,准确率= 97.67%第100个时期,费用= 0.048373,准确率= 97.68%
以上是機器學習 | PyTorch簡明教學上篇的詳細內容。更多資訊請關注PHP中文網其他相關文章!