網(wǎng)站建設(shè)過(guò)程論文網(wǎng)站收錄查詢
文章目錄
- 3.2、softmax 回歸
- 3.2.1、softmax運(yùn)算
- 3.2.2、交叉熵?fù)p失函數(shù)
- 3.2.3、PyTorch 從零實(shí)現(xiàn) softmax 回歸
- 3.2.4、簡(jiǎn)單實(shí)現(xiàn) softmax 回歸

3.2、softmax 回歸
3.2.1、softmax運(yùn)算
softmax 函數(shù)是一種常用的激活函數(shù),用于將實(shí)數(shù)向量轉(zhuǎn)換為概率分布向量。它在多類別分類問(wèn)題中起到重要的作用,并與交叉熵?fù)p失函數(shù)結(jié)合使用。
y ^ = s o f t m a x ( o ) 其中???? y ^ i = e x p ( o j ) ∑ k e x p ( o k ) \hat{y} = softmax(o) \ \ \ \ \ 其中\(zhòng) \ \ \ \hat{y}_i = \frac{exp(o_j)}{\sum_{k}exp(o_k)} y^?=softmax(o)?????其中????y^?i?=∑k?exp(ok?)exp(oj?)?
其中,O為小批量的未規(guī)范化的預(yù)測(cè), Y ^ \hat{Y} Y^w為輸出概率,是一個(gè)正確的概率分布【 ∑ y i = 1 \sum{y_i} =1 ∑yi?=1 】
3.2.2、交叉熵?fù)p失函數(shù)
通過(guò)測(cè)量給定模型編碼的比特位,來(lái)衡量?jī)?strong>概率分布之間的差異,是分類問(wèn)題中常用的 loss 函數(shù)。
H ( P , Q ) = ? Σ P ( x ) ? l o g ( Q ( x ) ) H(P, Q) = -Σ P(x) * log(Q(x)) H(P,Q)=?ΣP(x)?log(Q(x))
真實(shí)概率分布是從哪里得知的?
真實(shí)標(biāo)簽的概率分布是由數(shù)據(jù)集中的標(biāo)簽信息提供的,通常使用單熱編碼表示。
softmax() 如何與交叉熵函數(shù)搭配的?
softmax 函數(shù)與交叉熵?fù)p失函數(shù)常用于多分類任務(wù)中。softmax 函數(shù)用于將模型輸出轉(zhuǎn)化為概率分布形式,交叉熵?fù)p失函數(shù)用于衡量模型輸出概率分布與真實(shí)標(biāo)簽的差異,并通過(guò)優(yōu)化算法來(lái)最小化損失函數(shù),從而訓(xùn)練出更準(zhǔn)確的分類模型。
3.2.3、PyTorch 從零實(shí)現(xiàn) softmax 回歸
(非完整代碼)
#在 Notebook 中內(nèi)嵌繪圖
%matplotlib inline
import torch
import torchvision
from torch.utils import data
from torchvision import transforms
from d2l import torch as d2l#,將圖形顯示格式設(shè)置為 SVG 格式,以在 Notebook 中以矢量圖形的形式顯示圖像。這有助于提高圖像的清晰度和可縮放性。
d2l .use_svg_display()
在線下載數(shù)據(jù)集 Fashion-MNIST
#將圖像數(shù)據(jù)轉(zhuǎn)換為張量形式
trans = transforms.ToTensor()
mnist_train = torchvision.datasets.FashionMNIST(root="../data",train=True,transform=trans,download=True)
mnist_test = torchvision.datasets.FashionMNIST(root="../data",train=False,transform =trans,download=True)len(mnist_train),len(mnist_test)
繪圖(略)
讀取小批量數(shù)據(jù)集
batch_size = 256def get_dataloader_workers():"""使用4進(jìn)程讀取"""return 4train_iter = data.DataLoader(mnist_train,batch_size,shuffle=True,num_workers=get_dataloader_workers())
timer = d2l.Timer()
for X,y in train_iter:continue
print(f'{timer.stop():.2f}sec')
定義softmax操作
def softmax(X):X_exp = torch.exp(X)partition = X_exp.sum(1, keepdim=True)return X_exp / partition # 這里應(yīng)用了廣播機(jī)制
定義損失函數(shù)
def cross_entropy(y_hat, y):return - torch.log(y_hat[range(len(y_hat)), y])cross_entropy(y_hat, y)
分類精度
def accuracy(y_hat, y): #@save"""計(jì)算預(yù)測(cè)正確的數(shù)量"""if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:y_hat = y_hat.argmax(axis=1)cmp = y_hat.type(y.dtype) == yreturn float(cmp.type(y.dtype).sum())
評(píng)估
def evaluate_accuracy(net, data_iter): #@save"""計(jì)算在指定數(shù)據(jù)集上模型的精度"""if isinstance(net, torch.nn.Module):net.eval() # 將模型設(shè)置為評(píng)估模式metric = Accumulator(2) # 正確預(yù)測(cè)數(shù)、預(yù)測(cè)總數(shù)with torch.no_grad():for X, y in data_iter:metric.add(accuracy(net(X), y), y.numel())return metric[0] / metric[1]
class Accumulator: #@save"""在n個(gè)變量上累加"""def __init__(self, n):self.data = [0.0] * ndef add(self, *args):self.data = [a + float(b) for a, b in zip(self.data, args)]def reset(self):self.data = [0.0] * len(self.data)def __getitem__(self, idx):return self.data[idx]
3.2.4、簡(jiǎn)單實(shí)現(xiàn) softmax 回歸
導(dǎo)入前面已下載數(shù)據(jù)集 Fashion-MNIST
import torch
from torch import nn
from d2l import torch as d2lbatch_size =256
train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size)
初始化模型
#nn.Flatten() 層的作用是將輸入數(shù)據(jù)展平,將二維輸入(如圖像)轉(zhuǎn)換為一維向量。因?yàn)榫€性層(nn.Linear)通常期望接收一維輸入。
#nn.Linear(784,10) 將輸入特征從 784 維降低到 10 維,用于圖像分類問(wèn)題中的 10 個(gè)類別的預(yù)測(cè) 784維向量->10維向量
net = nn.Sequential(nn.Flatten(),nn.Linear(784,10))def init_weights(m):if type(m) == nn.Linear:nn.init.normal_(m.weight,std=0.01)net.apply(init_weights);
#計(jì)算交叉熵?fù)p失函數(shù),用于衡量模型預(yù)測(cè)與真實(shí)標(biāo)簽之間的差異。參數(shù) reduction 控制了損失的計(jì)算方式。
#reduction='none' 表示不進(jìn)行損失的降維或聚合操作,即返回每個(gè)樣本的獨(dú)立損失值。
loss = nn.CrossEntropyLoss(reduction='none')
優(yōu)化算法
trainer = torch.optim.SGD(net.parameters(),lr=0.1)
訓(xùn)練
num_epochs = 10
d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,trainer)