加載mnist數據集
one-hot編碼實現
import numpy as np
import torch
x_train = np.load("../dataset/mnist/x_train.npy") # 從網站提前下載數據集,并解壓縮
y_train_label = np.load("../dataset/mnist/y_train_label.npy")
x = torch.tensor(y_train_label[:5],dtype=torch.int64) # 獲取前5個樣本的標簽數據
# 定義一個張量輸入,因為此時有 5 個數值,且最大值為9,類別數為10
# 所以我們可以得到 y 的輸出結果的形狀為 shape=(5,10),即5行12列
y = torch.nn.functional.one_hot(x, 10) # 一個參數張量x,10為類別數
print(y)
對于擁有6000個樣本的MNIST數據集來說,標簽就是一個大小的矩陣張量。
多層感知機模型
#設定的多層感知機網絡模型
class NeuralNetwork(torch.nn.Module):def __init__(self):super(NeuralNetwork, self).__init__()self.flatten = torch.nn.Flatten() # 拉平圖像矩陣self.linear_relu_stack = torch.nn.Sequential(torch.nn.Linear(28*28,312), # 輸入大小為28*28,輸出大小為312維的線性變換層torch.nn.ReLU(), # 激活函數層torch.nn.Linear(312, 256),torch.nn.ReLU(),torch.nn.Linear(256, 10) # 最終輸出大小為10,對應one-hot標簽維度)def forward(self, input): # 構建網絡x = self.flatten(input) #拉平矩陣為1維logits = self.linear_relu_stack(x) # 多層感知機return logits
損失函數
優化函數
model = NeuralNetwork()
loss_fu = torch.nn.CrossEntropyLoss() # 交叉熵損失函數,內置了softmax函數,
optimizer = torch.optim.Adam(model.parameters(), lr=2e-5) #設定優化函數loss = loss_fu(pred,label_batch) # 計算損失
完整模型
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0' #指定GPU編
import torch
import numpy as npbatch_size = 320 #設定每次訓練的批次數
epochs = 1024 #設定訓練次數#device = "cpu" #Pytorch的特性,需要指定計算的硬件,如果沒有GPU的存在,就使用CPU進行計算
device = "cuda" #在這里讀者默認使用GPU,如果讀者出現運行問題可以將其改成cpu模式#設定的多層感知機網絡模型
class NeuralNetwork(torch.nn.Module):def __init__(self):super(NeuralNetwork, self).__init__()self.flatten = torch.nn.Flatten()self.linear_relu_stack = torch.nn.Sequential(torch.nn.Linear(28*28,312),torch.nn.ReLU(),torch.nn.Linear(312, 256),torch.nn.ReLU(),torch.nn.Linear(256, 10))def forward(self, input):x = self.flatten(input)logits = self.linear_relu_stack(x)return logitsmodel = NeuralNetwork()
model = model.to(device) #將計算模型傳入GPU硬件等待計算
torch.save(model, './model.pth')
#model = torch.compile(model) #Pytorch2.0的特性,加速計算速度
loss_fu = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=2e-5) #設定優化函數#載入數據
x_train = np.load("../../dataset/mnist/x_train.npy")
y_train_label = np.load("../../dataset/mnist/y_train_label.npy")train_num = len(x_train)//batch_size#開始計算
for epoch in range(20):train_loss = 0for i in range(train_num):start = i * batch_sizeend = (i + 1) * batch_sizetrain_batch = torch.tensor(x_train[start:end]).to(device)label_batch = torch.tensor(y_train_label[start:end]).to(device)pred = model(train_batch)loss = loss_fu(pred,label_batch)optimizer.zero_grad()loss.backward()optimizer.step()train_loss += loss.item() # 記錄每個批次的損失值# 計算并打印損失值train_loss /= train_numaccuracy = (pred.argmax(1) == label_batch).type(torch.float32).sum().item() / batch_sizeprint("epoch:",epoch,"train_loss:", round(train_loss,2),"accuracy:",round(accuracy,2))
可視化模型結構和參數
model = NeuralNetwork()
print(model)
是對模型具體使用的函數及其對應的參數進行打印。
格式化顯示:
param = list(model.parameters())
k=0
for i in param:l = 1print('該層結構:'+str(list(i.size())))for j in i.size():l*=jprint('該層參數和:'+str(l))k = k+l
print("總參數量:"+str(k))
模型保存
model = NeuralNetwork()
torch.save(model, './model.pth')
netron可視化
安裝:pip install netron
運行:命令行輸入netron
打開:通過網址http://localhost:8080打開
打開保存的模型文件model.pth:
?
?點擊顏色塊,可以顯示詳細信息: