女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>PyTorch教程4.3之基本分類模型

PyTorch教程4.3之基本分類模型

2023-06-05 | pdf | 0.13 MB | 次下載 | 免費

資料介紹

您可能已經(jīng)注意到,在回歸的情況下,從頭開始的實現(xiàn)和使用框架功能的簡潔實現(xiàn)非常相似。分類也是如此。由于本書中的許多模型都處理分類,因此值得添加專門支持此設置的功能。本節(jié)為分類模型提供了一個基類,以簡化以后的代碼。

import torch
from d2l import torch as d2l
from mxnet import autograd, gluon, np, npx
from d2l import mxnet as d2l

npx.set_np()
from functools import partial
import jax
import optax
from jax import numpy as jnp
from d2l import jax as d2l
No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
import tensorflow as tf
from d2l import tensorflow as d2l

4.3.1. Classifier_

我們在下面定義Classifier類。在中,validation_step我們報告了驗證批次的損失值和分類準確度。我們?yōu)槊總€批次繪制一個更新num_val_batches這有利于在整個驗證數(shù)據(jù)上生成平均損失和準確性。如果最后一批包含的示例較少,則這些平均數(shù)并不完全正確,但我們忽略了這一微小差異以保持代碼簡單。

class Classifier(d2l.Module): #@save
  """The base class of classification models."""
  def validation_step(self, batch):
    Y_hat = self(*batch[:-1])
    self.plot('loss', self.loss(Y_hat, batch[-1]), train=False)
    self.plot('acc', self.accuracy(Y_hat, batch[-1]), 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.

class Classifier(d2l.Module): #@save
  """The base class of classification models."""
  def validation_step(self, batch):
    Y_hat = self(*batch[:-1])
    self.plot('loss', self.loss(Y_hat, batch[-1]), train=False)
    self.plot('acc', self.accuracy(Y_hat, batch[-1]), 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.

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.

class Classifier(d2l.Module): #@save
  """The base class of classification models."""
  def validation_step(self, batch):
    Y_hat = self(*batch[:-1])
    self.plot('loss', self.loss(Y_hat, batch[-1]), train=False)
    self.plot('acc', self.accuracy(Y_hat, batch[-1]), train=False)

默認情況下,我們使用隨機梯度下降優(yōu)化器,在小批量上運行,就像我們在線性回歸的上下文中所做的那樣。

@d2l.add_to_class(d2l.Module) #@save
def configure_optimizers(self):
  return torch.optim.SGD(self.parameters(), lr=self.lr)
@d2l.add_to_class(d2l.Module) #@save
def configure_optimizers(self):
  params = self.parameters()
  if isinstance(params, list):
    return d2l.SGD(params, self.lr)
  return gluon.Trainer(params, 'sgd', {'learning_rate': self.lr})
@d2l.add_to_class(d2l.Module) #@save
def configure_optimizers(self):
  return optax.sgd(self.lr)
@d2l.add_to_class(d2l.Module) #@save
def configure_optimizers(self):
  return tf.keras.optimizers.SGD(self.lr)

4.3.2. 準確性

給定預測概率分布y_hat,每當我們必須輸出硬預測時,我們通常會選擇預測概率最高的類別。事實上,許多應用程序需要我們做出選擇。例如,Gmail 必須將電子郵件分類為“主要”、“社交”、“更新”、“論壇”或“垃圾郵件”。它可能會在內(nèi)部估計概率,但最終它必須在類別中選擇一個。

當預測與標簽 class 一致時y,它們是正確的。分類準確度是所有正確預測的分數(shù)。盡管直接優(yōu)化精度可能很困難(不可微分),但它通常是我們最關(guān)心的性能指標。它通常是基準測試中的相關(guān)數(shù)量因此,我們幾乎總是在訓練分類器時報告它。

準確度計算如下。首先,如果y_hat是一個矩陣,我們假設第二個維度存儲每個類別的預測分數(shù)。我們使用argmax每行中最大條目的索引來獲取預測類。然后我們將預測的類別與真實的元素進行比較y由于相等運算符== 對數(shù)據(jù)類型敏感,因此我們轉(zhuǎn)換 的y_hat數(shù)據(jù)類型以匹配 的數(shù)據(jù)類型y結(jié)果是一個包含條目 0(假)和 1(真)的張量。求和得出正確預測的數(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

下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1DD3118電路圖紙資料
  2. 0.08 MB   |  1次下載  |  免費
  3. 2AD庫封裝庫安裝教程
  4. 0.49 MB   |  1次下載  |  免費
  5. 3PC6206 300mA低功耗低壓差線性穩(wěn)壓器中文資料
  6. 1.12 MB   |  1次下載  |  免費
  7. 4網(wǎng)絡安全從業(yè)者入門指南
  8. 2.91 MB   |  1次下載  |  免費
  9. 5DS-CS3A P00-CN-V3
  10. 618.05 KB  |  1次下載  |  免費
  11. 6海川SM5701規(guī)格書
  12. 1.48 MB  |  次下載  |  免費
  13. 7H20PR5電磁爐IGBT功率管規(guī)格書
  14. 1.68 MB   |  次下載  |  1 積分
  15. 8IP防護等級說明
  16. 0.08 MB   |  次下載  |  免費

本月

  1. 1貼片三極管上的印字與真實名稱的對照表詳細說明
  2. 0.50 MB   |  103次下載  |  1 積分
  3. 2涂鴉各WiFi模塊原理圖加PCB封裝
  4. 11.75 MB   |  89次下載  |  1 積分
  5. 3錦銳科技CA51F2 SDK開發(fā)包
  6. 24.06 MB   |  43次下載  |  1 積分
  7. 4錦銳CA51F005 SDK開發(fā)包
  8. 19.47 MB   |  19次下載  |  1 積分
  9. 5PCB的EMC設計指南
  10. 2.47 MB   |  16次下載  |  1 積分
  11. 6HC05藍牙原理圖加PCB
  12. 15.76 MB   |  13次下載  |  1 積分
  13. 7802.11_Wireless_Networks
  14. 4.17 MB   |  12次下載  |  免費
  15. 8蘋果iphone 11電路原理圖
  16. 4.98 MB   |  6次下載  |  2 積分

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935127次下載  |  10 積分
  3. 2開源硬件-PMP21529.1-4 開關(guān)降壓/升壓雙向直流/直流轉(zhuǎn)換器 PCB layout 設計
  4. 1.48MB  |  420064次下載  |  10 積分
  5. 3Altium DXP2002下載入口
  6. 未知  |  233089次下載  |  10 積分
  7. 4電路仿真軟件multisim 10.0免費下載
  8. 340992  |  191390次下載  |  10 積分
  9. 5十天學會AVR單片機與C語言視頻教程 下載
  10. 158M  |  183342次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81588次下載  |  10 積分
  13. 7Keil工具MDK-Arm免費下載
  14. 0.02 MB  |  73815次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65989次下載  |  10 積分