您可能已經(jīng)注意到,在回歸的情況下,從頭開(kāi)始的實(shí)現(xiàn)和使用框架功能的簡(jiǎn)潔實(shí)現(xiàn)非常相似。分類(lèi)也是如此。由于本書(shū)中的許多模型都處理分類(lèi),因此值得添加專門(mén)支持此設(shè)置的功能。本節(jié)為分類(lèi)模型提供了一個(gè)基類(lèi),以簡(jiǎn)化以后的代碼。
import tensorflow as tf
from d2l import tensorflow as d2l
4.3.1. 類(lèi)Classifier_
我們?cè)谙旅娑xClassifier類(lèi)。在中,validation_step我們報(bào)告了驗(yàn)證批次的損失值和分類(lèi)準(zhǔn)確度。我們?yōu)槊總€(gè)批次繪制一個(gè)更新num_val_batches 。這有利于在整個(gè)驗(yàn)證數(shù)據(jù)上生成平均損失和準(zhǔn)確性。如果最后一批包含的示例較少,則這些平均數(shù)并不完全正確,但我們忽略了這一微小差異以保持代碼簡(jiǎn)單。
We define the Classifier class below. In the validation_step we report both the loss value and the classification accuracy on a validation batch. We draw an update for every num_val_batches batches. This has the benefit of generating the averaged loss and accuracy on the whole validation data. These average numbers are not exactly correct if the last batch contains fewer examples, but we ignore this minor difference to keep the code simple.
We define the Classifier class below. In the validation_step we report both the loss value and the classification accuracy on a validation batch. We draw an update for every num_val_batches batches. This has the benefit of generating the averaged loss and accuracy on the whole validation data. These average numbers are not exactly correct if the last batch contains fewer examples, but we ignore this minor difference to keep the code simple.
We also redefine the training_step method for JAX since all models that will subclass Classifier later will have a loss that returns auxiliary data. This auxiliary data can be used for models with batch normalization (to be explained in Section 8.5), while in all other cases we will make the loss also return a placeholder (empty dictionary) to represent the auxiliary data.
class Classifier(d2l.Module): #@save
"""The base class of classification models."""
def training_step(self, params, batch, state):
# Here value is a tuple since models with BatchNorm layers require
# the loss to return auxiliary data
value, grads = jax.value_and_grad(
self.loss, has_aux=True)(params, batch[:-1], batch[-1], state)
l, _ = value
self.plot("loss", l, train=True)
return value, grads
def validation_step(self, params, batch, state):
# Discard the second returned value. It is used for training models
# with BatchNorm layers since loss also returns auxiliary data
l, _ = self.loss(params, batch[:-1], batch[-1], state)
self.plot('loss', l, train=False)
self.plot('acc', self.accuracy(params, batch[:-1], batch[-1], state),
train=False)
We define the Classifier class below. In the validation_step we report both the loss value and the classification accuracy on a validation batch. We draw an update for every num_val_batches batches. This has the benefit of generating the averaged loss and accuracy on the whole validation data. These average numbers are not exactly correct if the last batch contains fewer examples, but we ignore this minor difference to keep the code simple.
默認(rèn)情況下,我們使用隨機(jī)梯度下降優(yōu)化器,在小批量上運(yùn)行,就像我們?cè)诰€性回歸的上下文中所做的那樣。
@d2l.add_to_class(d2l.Module) #@save
def configure_optimizers(self):
return torch.optim.SGD(self.parameters(), lr=self.lr)
4.3.2. 準(zhǔn)確性
給定預(yù)測(cè)概率分布y_hat,每當(dāng)我們必須輸出硬預(yù)測(cè)時(shí),我們通常會(huì)選擇預(yù)測(cè)概率最高的類(lèi)別。事實(shí)上,許多應(yīng)用程序需要我們做出選擇。例如,Gmail 必須將電子郵件分類(lèi)為“主要”、“社交”、“更新”、“論壇”或“垃圾郵件”。它可能會(huì)在內(nèi)部估計(jì)概率,但最終它必須在類(lèi)別中選擇一個(gè)。
當(dāng)預(yù)測(cè)與標(biāo)簽 class 一致時(shí)y,它們是正確的。分類(lèi)準(zhǔn)確度是所有正確預(yù)測(cè)的分?jǐn)?shù)。盡管直接優(yōu)化精度可能很困難(不可微分),但它通常是我們最關(guān)心的性能指標(biāo)。它通常是基準(zhǔn)測(cè)試中的相關(guān)數(shù)量。因此,我們幾乎總是在訓(xùn)練分類(lèi)器時(shí)報(bào)告它。
準(zhǔn)確度計(jì)算如下。首先,如果y_hat是一個(gè)矩陣,我們假設(shè)第二個(gè)維度存儲(chǔ)每個(gè)類(lèi)別的預(yù)測(cè)分?jǐn)?shù)。我們使用argmax每行中最大條目的索引來(lái)獲取預(yù)測(cè)類(lèi)。然后我們將預(yù)測(cè)的類(lèi)別與真實(shí)的元素進(jìn)行比較y。由于相等運(yùn)算符== 對(duì)數(shù)據(jù)類(lèi)型敏感,因此我們轉(zhuǎn)換 的y_hat數(shù)據(jù)類(lèi)型以匹配 的數(shù)據(jù)類(lèi)型y。結(jié)果是一個(gè)包含條目 0(假)和 1(真)的張量。求和得出正確預(yù)測(cè)的數(shù)量。
@d2l.add_to_class(Classifier) #@save
def accuracy(self, Y_hat, Y, averaged=True):
"""Compute the number of correct predictions."""
Y_hat = Y_hat.reshape((-1, Y_hat.shape[-1]))
preds = Y_hat.argmax(axis=1).type(Y.dtype)
compare = (preds == Y.reshape(-1)).type(torch.float32)
return compare.mean() if averaged else compare
@d2l.add_to_class(Classifier) #@save
def accuracy(self, Y_hat, Y, averaged=True):
"""Compute the number of correct predictions."""
Y_hat = Y_hat.reshape((-1, Y_hat.shape[-1]))
preds = Y_hat.argmax(axis=1).astype(Y.dtype)
compare = (preds == Y.reshape(-1)).astype(np.float32)
return compare.mean() if averaged else compare
@d2l.add_to_class(d2l.Module) #@save
def get_scratch_params(self):
params = []
for attr in dir(self):
a = getattr(self, attr)
if isinstance(a, np.ndarray):
params.append(a)
if isinstance(a, d2l.Module):
params.extend(a.get_scratch_params())
return params
@d2l.add_to_class(d2l.Module) #@save
def parameters(self):
params = self.collect_params()
return params if isinstance(params, gluon.parameter.ParameterDict) and
電子發(fā)燒友App






創(chuàng)作
發(fā)文章
發(fā)帖
提問(wèn)
發(fā)資料
發(fā)視頻
上傳資料賺積分
評(píng)論