基于Pytorch實(shí)現(xiàn)分類器的示例詳解
本文實(shí)現(xiàn)兩個(gè)分類器: softmax分類器和感知機(jī)分類器
Softmax分類器
Softmax分類是一種常用的多類別分類算法,它可以將輸入數(shù)據(jù)映射到一個(gè)概率分布上。Softmax分類首先將輸入數(shù)據(jù)通過線性變換得到一個(gè)向量,然后將向量中的每個(gè)元素進(jìn)行指數(shù)函數(shù)運(yùn)算,最后將指數(shù)運(yùn)算結(jié)果歸一化得到一個(gè)概率分布。這個(gè)概率分布可以被解釋為每個(gè)類別的概率估計(jì)。
定義
定義一個(gè)softmax分類器類:
class SoftmaxClassifier(nn.Module):
def __init__(self,input_size,output_size):
# 調(diào)用父類的__init__()方法進(jìn)行初始化
super(SoftmaxClassifier,self).__init__()
# 定義一個(gè)nn.Linear對象,用于將輸入特征映射到輸出類別
self.linear = nn.Linear(input_size,output_size)
def forward(self,x):
x = self.linear(x) # 傳遞給線性層
return nn.functional.softmax(x,dim=1) # 得到概率分布
def compute_accuracy(self,output,labels):
preds = torch.argmax(output,dim=1) # 獲取每個(gè)樣本的預(yù)測標(biāo)簽
correct = torch.sum(preds == labels).item() # 計(jì)算正確預(yù)測的數(shù)量
accuracy = correct / len(labels) # 除以總樣本數(shù)得到準(zhǔn)確率
return accuracy
如上定義三個(gè)方法:
- __init__(self):構(gòu)造函數(shù),在類初始化時(shí)運(yùn)行,調(diào)用父類的__init__()方法進(jìn)行初始化
- forward(self):模型前向計(jì)算過程
- compute_accuracy(self):計(jì)算模型的預(yù)測準(zhǔn)確率
訓(xùn)練
生成訓(xùn)練數(shù)據(jù):
import numpy as np
# 生成隨機(jī)樣本(包含訓(xùn)練數(shù)據(jù)和測試數(shù)據(jù))
def generate_rand_samples(dot_num=100):
x_p = np.random.normal(3., 1, dot_num)
y_p = np.random.normal(3., 1, dot_num)
y = np.zeros(dot_num)
C1 = np.array([x_p, y_p, y]).T
x_n = np.random.normal(7., 1, dot_num)
y_n = np.random.normal(7., 1, dot_num)
y = np.ones(dot_num)
C2 = np.array([x_n, y_n, y]).T
x_n = np.random.normal(3., 1, dot_num)
y_n = np.random.normal(7., 1, dot_num)
y = np.ones(dot_num)*2
C3 = np.array([x_n, y_n, y]).T
x_n = np.random.normal(7, 1, dot_num)
y_n = np.random.normal(3, 1, dot_num)
y = np.ones(dot_num)*3
C4 = np.array([x_n, y_n, y]).T
data_set = np.concatenate((C1, C2, C3, C4), axis=0)
np.random.shuffle(data_set)
return data_set[:,:2].astype(np.float32),data_set[:,2].astype(np.int32)
X_train,y_train = generate_rand_samples()
y_train[y_train == -1] = 0
設(shè)置訓(xùn)練前的前置參數(shù),并初始化分類器
num_inputs = 2 # 輸入維度大小 num_outputs = 4 # 輸出維度大小 learning_rate = 0.01 # 學(xué)習(xí)率 num_epochs = 2000 # 訓(xùn)練周期數(shù) # 歸一化數(shù)據(jù) 將數(shù)據(jù)特征減去均值再除以標(biāo)準(zhǔn)差 X_train = (X_train - X_train.mean(axis=0)) / X_train.std(axis=0) y_train = y_train.astype(np.compat.long) # 創(chuàng)建model并初始化 model = SoftmaxClassifier(num_inputs, num_outputs) criterion = nn.CrossEntropyLoss() # 交叉熵?fù)p失 optimizer = optim.SGD(model.parameters(), lr=learning_rate) # SGD優(yōu)化器
訓(xùn)練:
# 遍歷訓(xùn)練周期數(shù)
for epoch in range(num_epochs):
outputs = model(torch.tensor(X_train)) # 前向傳遞計(jì)算
loss = criterion(outputs,torch.tensor(y_train)) # 計(jì)算預(yù)測輸出和真實(shí)標(biāo)簽之間的損失
train_accuracy = model.compute_accuracy(outputs,torch.tensor(y_train)) # 計(jì)算模型當(dāng)前訓(xùn)練周期中準(zhǔn)確率
optimizer.zero_grad() # 清楚優(yōu)化器中梯度
loss.backward() # 計(jì)算損失對模型參數(shù)的梯度
optimizer.step()
# 打印信息
if (epoch + 1) % 10 == 0:
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}, Accuracy: {train_accuracy:.4f}")
運(yùn)行:
Epoch [1820/2000], Loss: 0.9947, Accuracy: 0.9575
Epoch [1830/2000], Loss: 0.9940, Accuracy: 0.9600
Epoch [1840/2000], Loss: 0.9932, Accuracy: 0.9600
Epoch [1850/2000], Loss: 0.9925, Accuracy: 0.9600
Epoch [1860/2000], Loss: 0.9917, Accuracy: 0.9600
....
測試
生成測試并測試:
X_test, y_test = generate_rand_samples() # 生成測試數(shù)據(jù)
X_test = (X_test- np.mean(X_test)) / np.std(X_test) # 歸一化
y_test = y_test.astype(np.compat.long)
predicts = model(torch.tensor(X_test)) # 獲取模型輸出
accuracy = model.compute_accuracy(predicts,torch.tensor(y_test)) # 計(jì)算準(zhǔn)確度
print(f'Test Accuracy: {accuracy:.4f}')
輸出:
Test Accuracy: 0.9725
繪制圖像:
# 繪制圖像 x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1 y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1)) Z = model(torch.tensor(np.c_[xx.ravel(), yy.ravel()], dtype=torch.float32)).argmax(dim=1).numpy() Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, alpha=0.4) plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=20, edgecolor='k') plt.show()

感知機(jī)分類器
實(shí)現(xiàn)與上述softmax分類器相似,此處實(shí)現(xiàn)sigmod感知機(jī),采用sigmod作為分類函數(shù),該函數(shù)可以將線性變換的結(jié)果映射為0到1之間的實(shí)數(shù)值,通常被用作神經(jīng)網(wǎng)絡(luò)中的激活函數(shù)
sigmoid感知機(jī)的學(xué)習(xí)算法與普通的感知機(jī)類似,也是采用隨機(jī)梯度下降(SGD)的方式進(jìn)行更新。不同之處在于,sigmoid感知機(jī)的輸出是一個(gè)概率值,需要將其轉(zhuǎn)化為類別標(biāo)簽。
通常使用閾值來決定輸出值所屬的類別,如將輸出值大于0.5的樣本歸為正類,小于等于0.5的樣本歸為負(fù)類。
定義
# 感知機(jī)分類器
class PerceptronClassifier(nn.Module):
def __init__(self, input_size,output_size):
super(PerceptronClassifier, self).__init__()
self.linear = nn.Linear(input_size,output_size)
def forward(self, x):
logits = self.linear(x)
return torch.sigmoid(logits)
def compute_accuracy(self, pred, target):
pred = torch.where(pred >= 0.5, 1, -1)
accuracy = (pred == target).sum().item() / target.size(0)
return accuracy
給定一個(gè)輸入向量(x1,x2,x3...xn),輸出為y=σ(w⋅x+b)=1/(e^−(w⋅x+b))
訓(xùn)練
生成訓(xùn)練集:
def generate_rand_samples(dot_num=100):
x_p = np.random.normal(3., 1, dot_num)
y_p = np.random.normal(3., 1, dot_num)
y = np.ones(dot_num)
C1 = np.array([x_p, y_p, y]).T
x_n = np.random.normal(6., 1, dot_num)
y_n = np.random.normal(0., 1, dot_num)
y = np.ones(dot_num)*-1
C2 = np.array([x_n, y_n, y]).T
data_set = np.concatenate((C1, C2), axis=0)
np.random.shuffle(data_set)
return data_set[:,:2].astype(np.float32),data_set[:,2].astype(np.int32)
X_train,y_train = generate_rand_samples()
X_test,y_test = generate_rand_samples()
該過程與上述softmax分類器相似:
num_inputs = 2 num_outputs = 1 learning_rate = 0.01 num_epochs = 200 # 歸一化數(shù)據(jù) 將數(shù)據(jù)特征減去均值再除以標(biāo)準(zhǔn)差 X_train = (X_train - X_train.mean(axis=0)) / X_train.std(axis=0) # 創(chuàng)建model并初始化 model = PerceptronClassifier(num_inputs, num_outputs) optimizer = optim.SGD(model.parameters(), lr=learning_rate) # SGD優(yōu)化器 criterion = nn.functional.binary_cross_entropy
訓(xùn)練:
# 遍歷訓(xùn)練周期數(shù)
for epoch in range(num_epochs):
outputs = model(torch.tensor(X_train))
labels = torch.tensor(y_train).unsqueeze(1)
loss = criterion(outputs,labels.float())
train_accuracy = model.compute_accuracy(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 10 == 0:
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}, Accuracy: {train_accuracy:.4f}")
輸出:
Epoch [80/200], Loss: -0.5429, Accuracy: 0.9550
Epoch [90/200], Loss: -0.6235, Accuracy: 0.9550
Epoch [100/200], Loss: -0.7015, Accuracy: 0.9500
Epoch [110/200], Loss: -0.7773, Accuracy: 0.9400
....
測試
X_test, y_test = generate_rand_samples() # 生成測試集
X_test = (X_test - X_test.mean(axis=0)) / X_test.std(axis=0)
test_inputs = torch.tensor(X_test)
test_labels = torch.tensor(y_test).unsqueeze(1)
with torch.no_grad():
outputs = model(test_inputs)
accuracy = model.compute_accuracy(outputs, test_labels)
print(f"Test Accuracy: {accuracy:.4f}")
繪圖:
x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1 y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1 xx, yy = torch.meshgrid(torch.linspace(x_min, x_max, 100), torch.linspace(y_min, y_max, 100)) # 預(yù)測每個(gè)點(diǎn)的類別 Z = torch.argmax(model(torch.cat((xx.reshape(-1,1), yy.reshape(-1,1)), 1)), 1) Z = Z.reshape(xx.shape) # 繪制分類圖 plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral,alpha=0.0) # 繪制分界線 w = model.linear.weight.detach().numpy() # 權(quán)重 b = model.linear.bias.detach().numpy() # 偏置 x1 = np.linspace(x_min, x_max, 100) x2 = (-b - w[0][0]*x1) / w[0][1] plt.plot(x1, x2, 'k-') # 繪制樣本點(diǎn) plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.Spectral) plt.show()

以上就是基于Pytorch實(shí)現(xiàn)分類器的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Pytorch分類器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python學(xué)習(xí)筆記之函數(shù)的參數(shù)和返回值的使用
這篇文章主要介紹了Python學(xué)習(xí)筆記之函數(shù)的參數(shù)和返回值的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
python 使用re.search()篩選后 選取部分結(jié)果的方法
今天小編就為大家分享一篇python 使用re.search()篩選后 選取部分結(jié)果的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Django+Vue實(shí)現(xiàn)文件上傳下載的項(xiàng)目實(shí)踐
本文主要介紹了Django+Vue實(shí)現(xiàn)文件上傳下載的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Python實(shí)現(xiàn)的樸素貝葉斯算法經(jīng)典示例【測試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)的樸素貝葉斯算法,結(jié)合實(shí)例形式詳細(xì)分析了Python實(shí)現(xiàn)與使用樸素貝葉斯算法的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06
Python協(xié)程的實(shí)現(xiàn)方式小結(jié)
協(xié)程是Python中強(qiáng)大的并發(fā)編程工具,允許開發(fā)者編寫異步代碼以提高程序的性能和效率,在本文中,我們將深入探討Python中協(xié)程的實(shí)現(xiàn)方式,包括生成器、asyncio庫和async/await關(guān)鍵字,我們還會提供詳細(xì)的示例代碼,幫助您理解和應(yīng)用協(xié)程,需要的朋友可以參考下2023-11-11
教你學(xué)會通過python的matplotlib庫繪圖
今天教大家如何學(xué)會通過python的matplotlib庫繪圖,文中有非常詳細(xì)的圖文解說及代碼示例,對正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
手把手教你搭建python+selenium自動(dòng)化環(huán)境(圖文)
本文主要介紹了手把手教你搭建python+selenium自動(dòng)化環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Python使用PyQt5實(shí)現(xiàn)與DeepSeek聊天的圖形化小軟件
在?PyQt5?中,菜單欄(QMenuBar)、工具欄(QToolBar)和狀態(tài)欄(QStatusBar)是?QMainWindow?提供的標(biāo)準(zhǔn)控件,用于幫助用戶更好地與應(yīng)用程序交互,所以本文給大家介紹了Python使用PyQt5實(shí)現(xiàn)與DeepSeek聊天的圖形化小軟件,需要的朋友可以參考下2025-03-03
python concurrent.futures模塊的使用測試
大家都知道concurrent.futures 是 3.2 中引入的新模塊,它為異步執(zhí)行可調(diào)用對象提供了高層接口,今天通過本文給大家介紹python concurrent.futures模塊的使用測試 ,感興趣的朋友一起看看吧2021-07-07

