Chainer - 核心元件



Chainer是一個用途廣泛的深度學習框架,旨在簡化神經網路的開發和訓練。Chainer的核心元件為構建複雜模型和執行高效計算提供了堅實的基礎。

在Chainer中,核心元件Chain類用於管理網路層和引數,例如定義和應用模型操作的LinksFunctions,以及處理資料和梯度的Variable類。

此外,Chainer集成了強大的最佳化器(Optimizers)用於更新模型引數,實用程式用於管理資料集(Dataset)DataLoader,以及支援靈活模型架構的動態計算圖(Computational Graph)。所有這些元件共同實現了模型建立、訓練和最佳化的簡化流程,使Chainer成為深度學習任務的全面工具。

以下是Chainer框架的不同核心元件:

變數(Variables)

在Chainer中,Variable類是表示資料及其在神經網路訓練過程中相關梯度的基本構建塊。Variable不僅封裝了資料(例如輸入、輸出或中間計算),還封裝了自動微分所需的資訊,這對於反向傳播至關重要。

Variable的關鍵特性

以下是Chainer框架中變數的關鍵特性:

  • 資料儲存:Variable以多維陣列的形式儲存資料,該陣列通常是NumPy或CuPy陣列,具體取決於計算是在CPU還是GPU上執行。儲存在Variable中的資料可以是輸入資料、輸出預測或在網路前向傳遞過程中計算的任何中間值。
  • 梯度儲存:在反向傳播過程中,Chainer計算損失函式相對於每個Variable的梯度。這些梯度儲存在Variable本身中。Variable的grad屬性包含梯度資料,該資料用於在訓練期間更新模型引數。
  • 自動微分:當對Variable物件應用操作時,Chainer會自動構建計算圖。該圖跟蹤操作序列以及變數之間的依賴關係,從而能夠在反向傳遞過程中高效地計算梯度。可以透過呼叫Variable的backward方法來觸發整個網路的梯度計算。
  • 裝置靈活性:Variable支援使用NumPy的CPU和使用CuPy的GPU陣列,從而可以輕鬆地在裝置之間移動計算。Variable上的操作會自動適應資料所在的裝置。

示例

以下示例演示瞭如何使用Chainer的Variable類執行基本操作並透過反向傳播計算梯度:

import chainer
import numpy as np

# Create a Variable with data
x = chainer.Variable(np.array([1.0, 2.0, 3.0], dtype=np.float32))

# Perform operations on Variable
y = x ** 2 + 2 * x + 1

# Print the result
print("Result:", y.data)  # Output: [4. 9. 16.]

# Assume y is a loss and perform backward propagation
y.grad = np.ones_like(y.data)  # Set gradient of y to 1 for backward pass
y.backward()  # Compute gradients

# Print the gradient of x
print("Gradient of x:", x.grad)  # Output: [4. 6. 8.]

以下是Chainer變數類的輸出:

Result: [ 4.  9. 16.]
Gradient of x: [4. 6. 8.]

函式(Functions)

在Chainer中,函式(Functions)是應用於神經網路內資料的操作。這些函式是執行數學運算、啟用函式、損失計算以及對資料進行其他轉換的基本構建塊,資料在網路中流動時會執行這些轉換。

Chainer在chainer.functions模組中提供廣泛的預定義函式,使使用者能夠輕鬆構建和自定義神經網路。

Chainer中的關鍵函式

啟用函式:神經網路中的這些函式為模型引入了非線性,使其能夠學習資料中的複雜模式。它們應用於每一層的輸出,以確定網路的最終輸出。以下是Chainer中的啟用函式:

  • ReLU(修正線性單元):如果輸入為正數,則ReLU直接輸出輸入,否則輸出零。它廣泛用於神經網路,因為它有助於減輕梯度消失問題,並且計算效率高,使其成為訓練深度模型的有效方法。ReLU的公式為:

    $$ReLU(x) = max(0, x)$$

    chainer.functions模組中ReLU的函式為F.relu(x)

  • sigmoid:此函式將輸入對映到0到1之間的值,使其成為二元分類任務的理想選擇。它提供了一個平滑的梯度,這有助於基於梯度的最佳化,但在深層網路中可能會遭受梯度消失問題。sigmoid的公式為:

    $$Sigmoid(x)=\frac{1}{1+e^{-x}}$$

    chainer.functions模組中Sigmoid的函式為F.sigmoid(x)

  • Tanh(雙曲正切):Chainer中此函式用作神經網路中的啟用函式。它將輸入轉換為-1到1之間的值,從而產生以零為中心的輸出。此特性在訓練中可能是有益的,因為它有助於解決與非中心資料相關的問題,從而潛在地提高模型的收斂速度。Tanh的公式為:

    $$Tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}$$

    我們在chainer.functions模組中有一個函式F.tanh(x)用於計算Chainer中的Tanh。

  • Leaky ReLU:這也被稱為神經網路中的洩漏修正線性單元函式,是標準ReLU啟用函式的一個變體。與ReLU不同的是,ReLU對負輸入輸出零,而Leaky ReLU允許對負輸入進行小的非零梯度。

    此調整有助於防止“死亡ReLU”問題,其中神經元變得不活躍並停止學習,透過確保所有神經元繼續參與模型的學習過程來實現。Leaky ReLU的公式為:

    $$Leaky Relu(x) = max(\alpha x, x)$$

    其中,$\alpha$是一個小常數。chainer.functions模組具有函式F.leaky_relu(x)用於在Chainer中計算Leaky ReLU。

  • Softmax:這是一個啟用函式,通常用於神經網路的輸出層,特別是對於多類分類任務。它將原始預測分數(logits)向量轉換為機率分佈,其中每個機率與其對應的輸入值的指數成比例。

    輸出向量中的機率總和為1,這使得Softmax成為表示分類問題中每個類的可能性理想的選擇。Softmax的公式為:

    $$Softmax(x_{i})=\frac{e^{x_{i}}}{\sum_{j} e^{x_{j}}}$$

    chainer.functions模組具有函式F.softmax(x)用於在Chainer中計算Softmax。

示例

這是一個示例,它演示瞭如何在簡單的網路中使用Chainer中的各種啟用函式:

import chainer
import chainer.links as L
import chainer.functions as F
import numpy as np

# Define a simple neural network using Chainer's Chain class
class SimpleNN(chainer.Chain):
   def __init__(self):
      super(SimpleNN, self).__init__()
      with self.init_scope():
        # Define layers: two linear layers
        self.l1 = L.Linear(4, 3)  # Input layer with 4 features, hidden layer with 3 units
        self.l2 = L.Linear(3, 2)  # Hidden layer with 3 units, output layer with 2 units
      
   def __call__(self, x):
      # Forward pass using different activation functions
      
      # Apply ReLU activation after the first layer
      h = F.relu(self.l1(x))
      
      # Apply Sigmoid activation after the second layer
      y = F.sigmoid(self.l2(h))
      
      return y
      
# Create a sample input data with 4 features
x = np.array([[0.5, -1.2, 3.3, 0.7]], dtype=np.float32)

# Convert input to Chainer's Variable
x_var = chainer.Variable(x)

# Instantiate the neural network
model = SimpleNN()

# Perform a forward pass
output = model(x_var)

# Print the output
print("Network output after applying ReLU and Sigmoid activations:", output.data)

以下是簡單的網路中使用的啟用函式的輸出:

Network output after applying ReLU and Sigmoid activations: [[0.20396319 0.7766712 ]]

Chain和ChainList

在Chainer中,Chain和ChainList是基本類,它們有助於組織和管理神經網路中的層和引數。Chain和ChainList都是從chainer.Link派生的,chainer.Link是負責定義模型引數的基類。但是,它們具有不同的用途,並且用於不同的場景。讓我們詳細瞭解Chain和ChainList:

Chain

Chain類旨在將神經網路或網路中的模組表示為連結(層)的集合。使用Chain時,我們可以透過將每一層顯式地指定為例項變數來定義網路結構。這種方法對於具有固定架構的網路非常有用。

當我們擁有一個定義明確的固定網路架構,並且想要直接訪問和組織模型的每一層或元件時,可以使用Chain。

以下是Chain類的關鍵特性:

  • 命名元件:新增到Chain的層或連結可以透過名稱訪問,這使得引用網路的特定部分變得簡單直接。
  • 靜態架構:Chain的結構通常在初始化時定義,在訓練或推理期間不會動態更改。

示例

以下示例演示了在Chainer框架中使用Chain類:

import chainer
import chainer.links as L
import chainer.functions as F

# Define a simple neural network using Chain
class SimpleChain(chainer.Chain):
   def __init__(self):
      super(SimpleChain, self).__init__()
      with self.init_scope():
        self.l1 = L.Linear(4, 3)  # Linear layer with 4 inputs and 3 outputs
        self.l2 = L.Linear(3, 2)  # Linear layer with 3 inputs and 2 outputs
      
   def forward(self, x):
      h = F.relu(self.l1(x))  # Apply ReLU after the first layer
      y = self.l2(h)        # No activation after the second layer
      return y
      
# Instantiate the model
model = SimpleChain()
print(model)

以下是上述示例的輸出:

SimpleChain(
  (l1): Linear(in_size=4, out_size=3, nobias=False),
  (l2): Linear(in_size=3, out_size=2, nobias=False),
)

ChainList

ChainList類類似於Chain,但我們不是將每一層定義為例項變數,而是將其儲存在類似列表的結構中。當層或元件的數量可能發生變化或架構是動態的時,ChainList非常有用。

當我們的模型具有可變數量的層,或者網路結構可以動態更改時,可以使用ChainList。它也適用於諸如迴圈網路之類的架構,在這些架構中,相同型別的層被多次使用。

以下是ChainList的關鍵特性:

  • 無序元件:新增到ChainList的層或連結透過其索引而不是名稱來訪問。
  • 靈活的架構:它更適合於網路結構可能會發生變化或在迴圈或列表中處理層的情況。

示例

以下示例演示瞭如何在Chainer框架中使用ChainList類:

import chainer
import chainer.links as L
import chainer.functions as F

# Define a neural network using ChainList
class SimpleChainList(chainer.ChainList):
   def __init__(self):
      super(SimpleChainList, self).__init__(
         L.Linear(4, 3),  # Linear layer with 4 inputs and 3 outputs
         L.Linear(3, 2)   # Linear layer with 3 inputs and 2 outputs
      )

   def forward(self, x):
      h = F.relu(self[0](x))  # Apply ReLU after the first layer
      y = self[1](h)        # No activation after the second layer
      return y

# Instantiate the model
model = SimpleChainList()
print(model)

以下是使用Chainer框架中ChainList類的輸出:

SimpleChainList(
  (0): Linear(in_size=4, out_size=3, nobias=False),
  (1): Linear(in_size=3, out_size=2, nobias=False),
)

最佳化器(Optimizers)

在Chainer中,最佳化器(Optimizers)在訓練神經網路中起著至關重要的作用,它透過調整模型的引數(例如權重和偏差)來最小化損失函式。

在訓練期間,在透過反向傳播計算出損失函式相對於引數的梯度後,最佳化器會使用這些梯度來更新引數,從而逐漸減少損失。

Chainer提供了各種內建最佳化器,每個最佳化器都採用不同的引數更新策略,以適應不同型別的模型和任務。以下是Chainer中的關鍵最佳化器:

SGD(隨機梯度下降)

最基本的最佳化器是SGD,它沿其負梯度的方向更新每個引數,並按學習率縮放。它很簡單,但收斂速度可能很慢。

這些通常可以用於更簡單或更小的模型,或者作為與更復雜最佳化器進行比較的基線。

Chainer中計算SGD的函式為chainer.optimizers.SGD

示例

這是一個使用Chainer中的隨機梯度下降(SGD)訓練基本神經網路的簡單示例。我們將使用一個小資料集,定義一個神經網路模型,然後應用SGD最佳化器在訓練期間更新模型的引數:

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import Chain
import numpy as np
from chainer import Variable
from chainer import optimizers

class SimpleNN(Chain):
   def __init__(self):
      super(SimpleNN, self).__init__()
      with self.init_scope():
       self.fc1 = L.Linear(None, 100)  # Fully connected layer with 100 units
       self.fc2 = L.Linear(100, 10)   # Output layer with 10 units (e.g., for 10 classes)

   def forward(self, x):
      h = F.relu(self.fc1(x))  # Apply ReLU activation function
      return self.fc2(h)     # Output layer

# Dummy data: 5 samples, each with 50 features
x_data = np.random.rand(5, 50).astype(np.float32)
# Dummy labels: 5 samples, each with 10 classes (one-hot encoded)
y_data = np.random.randint(0, 10, 5).astype(np.int32)

# Convert to Chainer variables
x = Variable(x_data)
y = Variable(y_data)

# Initialize the model
model = SimpleNN()

# Set up SGD optimizer with a learning rate of 0.01
optimizer = optimizers.SGD(lr=0.01)
optimizer.setup(model)
def loss_func(predictions, targets):
   return F.softmax_cross_entropy(predictions, targets)
# Training loop
for epoch in range(10):  # Number of epochs
   # Zero the gradients
   model.cleargrads()
   
   # Forward pass
   predictions = model(x)
   
   # Calculate loss
   loss = loss_func(predictions, y)
   
   # Backward pass
   loss.backward()
   
   # Update parameters
   optimizer.update()
   
   # Print loss
   print(f'Epoch {epoch + 1}, Loss: {loss.data}')

以下是SGD最佳化器的輸出:

Epoch 1, Loss: 2.3100974559783936
Epoch 2, Loss: 2.233552932739258
Epoch 3, Loss: 2.1598660945892334
Epoch 4, Loss: 2.0888497829437256
Epoch 5, Loss: 2.020642042160034
Epoch 6, Loss: 1.9552147388458252
Epoch 7, Loss: 1.8926388025283813
Epoch 8, Loss: 1.8325523138046265
Epoch 9, Loss: 1.7749309539794922
Epoch 10, Loss: 1.7194255590438843

Momentum SGD

Momentum SGD是SGD的擴充套件,它包括動量,這有助於加速梯度向量向正確的方向移動,從而導致更快的收斂。它累積一個朝梯度方向的速率向量。

這適用於普通SGD難以收斂的模型。我們有一個名為chainer.optimizers.MomentumSGD的函式來執行Momentum SGD最佳化。

動量項:將先前梯度更新的一部分新增到當前更新中。這有助於加速梯度向量向正確的方向移動並抑制振盪。

公式:帶有動量的引數θ的更新規則為:

$$v_{t} = \beta v_{t-1} + (1 - \beta) \nabla L(\theta)$$ $$\theta = \theta-\alpha v_{t}$$

其中:

  • $v_{t}$是速度(或累積梯度)
  • $\beta$是動量係數(通常約為0.9)
  • $\alpha$是學習率
  • $\nabla L(\theta)$是損失函式相對於引數的梯度。

示例

這是一個關於如何在Chainer中使用Momentum SGD最佳化器和簡單神經網路的基本示例:

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import Chain
from chainer import optimizers
import numpy as np
from chainer import Variable

class SimpleNN(Chain):
   def __init__(self):
      super(SimpleNN, self).__init__()
      with self.init_scope():
       self.fc1 = L.Linear(None, 100)  # Fully connected layer with 100 units
       self.fc2 = L.Linear(100, 10)   # Output layer with 10 units (e.g., for 10 classes)

   def forward(self, x):
      h = F.relu(self.fc1(x))  # Apply ReLU activation function
      return self.fc2(h)     # Output layer


# Dummy data: 5 samples, each with 50 features
x_data = np.random.rand(5, 50).astype(np.float32)
# Dummy labels: 5 samples, each with 10 classes (one-hot encoded)
y_data = np.random.randint(0, 10, 5).astype(np.int32)

# Convert to Chainer variables
x = Variable(x_data)
y = Variable(y_data)


# Initialize the model
model = SimpleNN()

# Set up Momentum SGD optimizer with a learning rate of 0.01 and momentum of 0.9
optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)
optimizer.setup(model)
def loss_func(predictions, targets):
   return F.softmax_cross_entropy(predictions, targets)
# Training loop
for epoch in range(10):  # Number of epochs
   # Zero the gradients
   model.cleargrads()
   
   # Forward pass
   predictions = model(x)
   
   # Calculate loss
   loss = loss_func(predictions, y)
   
   # Backward pass
   loss.backward()
   
   # Update parameters
   optimizer.update()
   
   # Print loss
   print(f'Epoch {epoch + 1}, Loss: {loss.data}')

以下是Momentum SGD最佳化器的輸出:

Epoch 1, Loss: 2.4459869861602783
Epoch 2, Loss: 2.4109833240509033
Epoch 3, Loss: 2.346194267272949
Epoch 4, Loss: 2.25825572013855
Epoch 5, Loss: 2.153470754623413
Epoch 6, Loss: 2.0379838943481445
Epoch 7, Loss: 1.9174035787582397
Epoch 8, Loss: 1.7961997985839844
Epoch 9, Loss: 1.677260398864746
Epoch 10, Loss: 1.5634090900421143

Adam

Adam 最佳化器結合了 SGD 的另外兩個擴充套件的優點,即AdaGrad(擅長處理稀疏梯度)和RMSProp(擅長處理非平穩環境)。Adam 保持梯度及其平方的移動平均值,並根據這些平均值更新引數。

由於其在各種任務和模型中的魯棒性和效率,它通常用作預設最佳化器。在 Chainer 中,我們使用函式chainer.optimizers.Adam 執行 Adam 最佳化。

以下是 Adam 最佳化器的關鍵特性:

  • 自適應學習率:Adam 為每個引數動態調整學習率,使其在各種任務中有效。
  • 梯度的矩:它計算梯度的一階矩(均值)和二階矩(無偏方差)以改進最佳化。
  • 偏差校正:Adam 使用偏差校正來解決初始化過程中引入的偏差,尤其是在訓練初期。
  • 公式:Adam 最佳化的公式如下:

    
$$m_t = \beta_1 m_{t-1} + (1 - \beta_1) \nabla L(\theta)$$ $$v_t = \beta_2 v_{t-1} + (1 - \beta_2) (\nabla L(\theta))^2$$ $$\hat{m}_t = \frac{m_t}{1 - \beta_1^t}$$ $$\hat{v}_t = \frac{v_t}{1 - \beta_2^t}$$ $$\theta = \theta - \frac{\alpha\hat{m}_t}{\sqrt{\hat{v}_t} + \epsilon}$$

    其中,$\alpha$ 是學習率,$\beta_1$$\beta_2$ 分別是梯度及其平方的移動平均值的衰減率,通常分別為 0.9 和 0.999,${m_t}$ 和 ${v_t}$ 分別是一階矩和二階矩估計,$\epsilon$ 是為了數值穩定性而新增的小常數。

示例

以下示例展示瞭如何在 Chainer 中使用 Adam 最佳化器和神經網路:

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import Chain
from chainer import optimizers
import numpy as np
from chainer import Variable

class SimpleNN(Chain):
   def __init__(self):
      super(SimpleNN, self).__init__()
      with self.init_scope():
       self.fc1 = L.Linear(None, 100)  # Fully connected layer with 100 units
       self.fc2 = L.Linear(100, 10)   # Output layer with 10 units (e.g., for 10 classes)

   def forward(self, x):
      h = F.relu(self.fc1(x))  # Apply ReLU activation function
      return self.fc2(h)     # Output layer

# Dummy data: 5 samples, each with 50 features
x_data = np.random.rand(5, 50).astype(np.float32)
# Dummy labels: 5 samples, each with 10 classes (one-hot encoded)
y_data = np.random.randint(0, 10, 5).astype(np.int32)

# Convert to Chainer variables
x = Variable(x_data)
y = Variable(y_data)

# Initialize the model
model = SimpleNN()

# Set up Adam optimizer with default parameters
optimizer = optimizers.Adam()
optimizer.setup(model)
def loss_func(predictions, targets):
   return F.softmax_cross_entropy(predictions, targets)
# Training loop
for epoch in range(10):  # Number of epochs
   # Zero the gradients
   model.cleargrads()
   
   # Forward pass
   predictions = model(x)
   
   # Calculate loss
   loss = loss_func(predictions, y)
   
   # Backward pass
   loss.backward()
   
   # Update parameters
   optimizer.update()
   
   # Print loss
   print(f'Epoch {epoch + 1}, Loss: {loss.data}')

以下是將Adam 最佳化器應用於神經網路的輸出:

Epoch 1, Loss: 2.4677982330322266
Epoch 2, Loss: 2.365001678466797
Epoch 3, Loss: 2.2655398845672607
Epoch 4, Loss: 2.1715924739837646
Epoch 5, Loss: 2.082294464111328
Epoch 6, Loss: 1.9973262548446655
Epoch 7, Loss: 1.9164447784423828
Epoch 8, Loss: 1.8396313190460205
Epoch 9, Loss: 1.7676666975021362
Epoch 10, Loss: 1.7006778717041016

AdaGrad

AdaGrad 也稱為自適應梯度演算法,這是一種最佳化演算法,它根據訓練期間累積的梯度歷史記錄為每個引數調整學習率。它對於稀疏資料和特徵頻率或重要性不同的場景特別有效。

這適用於具有稀疏資料的問題,以及處理某些引數需要比其他引數更多調整的模型。函式chainer.optimizers.AdaGrad 用於在 Chainer 中執行 AdaGrad 最佳化。

以下是 AdaGrad 最佳化器的關鍵特性:

  • 自適應學習率:AdaGrad 根據梯度平方累積和分別為每個引數調整學習率。這導致對不頻繁的引數進行較大的更新,對頻繁的引數進行較小的更新。
  • 無需調整學習率:AdaGrad 自動縮放學習率,通常無需手動調整。

公式:AdaGrad 的公式如下:

$$g_t = \nabla L(\theta)$$ $$G_t = G_{t-1} +{g_t}^2$$ $$\theta = \theta - \frac{\alpha}{\sqrt{G_t} + \epsilon} g_t$$

其中:

  • $g_t$ 是 t 時刻的梯度。
  • $G_t$ 是直到 t 時刻梯度平方的累積和。
  • $\alpha$ 是全域性學習率。
  • $\epsilon$ 是一個小的常數,用於防止除以零。

示例

這是一個如何在 Chainer 中使用 AdaGrad 最佳化器和簡單神經網路的示例:

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import Chain
from chainer import optimizers
import numpy as np
from chainer import Variable

class SimpleNN(Chain):
   def __init__(self):
      super(SimpleNN, self).__init__()
      with self.init_scope():
       self.fc1 = L.Linear(None, 100)  # Fully connected layer with 100 units
       self.fc2 = L.Linear(100, 10)   # Output layer with 10 units (e.g., for 10 classes)

   def forward(self, x):
      h = F.relu(self.fc1(x))  # Apply ReLU activation function
      return self.fc2(h)     # Output layer


# Dummy data: 5 samples, each with 50 features
x_data = np.random.rand(5, 50).astype(np.float32)
# Dummy labels: 5 samples, each with 10 classes (one-hot encoded)
y_data = np.random.randint(0, 10, 5).astype(np.int32)

# Convert to Chainer variables
x = Variable(x_data)
y = Variable(y_data)


# Initialize the model
model = SimpleNN()

# Set up AdaGrad optimizer with a learning rate of 0.01
optimizer = optimizers.AdaGrad(lr=0.01)
optimizer.setup(model)
def loss_func(predictions, targets):
   return F.softmax_cross_entropy(predictions, targets)
# Training loop
for epoch in range(10):  # Number of epochs
   # Zero the gradients
   model.cleargrads()
   
   # Forward pass
   predictions = model(x)
   
   # Calculate loss
   loss = loss_func(predictions, y)
   
   # Backward pass
   loss.backward()
   
   # Update parameters
   optimizer.update()
   
   # Print loss
   print(f'Epoch {epoch + 1}, Loss: {loss.data}')

以下是將AdaGrad 最佳化器應用於神經網路的輸出:

Epoch 1, Loss: 2.2596702575683594
Epoch 2, Loss: 1.7732301950454712
Epoch 3, Loss: 1.4647505283355713
Epoch 4, Loss: 1.2398217916488647
Epoch 5, Loss: 1.0716438293457031
Epoch 6, Loss: 0.9412426352500916
Epoch 7, Loss: 0.8350374102592468
Epoch 8, Loss: 0.7446572780609131
Epoch 9, Loss: 0.6654194593429565
Epoch 10, Loss: 0.59764164686203

RMSProp

RMSProp 最佳化器透過引入衰減因子來改進AdaGrad,透過防止學習率下降過多,從而防止學習率過小。它在迴圈神經網路或需要快速適應不同梯度尺度的模型中特別有效。

在 Chainer 中,我們使用函式chainer.optimizers.RMSprop 執行 RMSProp 最佳化。

以下是 RMSProp 最佳化器的關鍵特性:

  • 衰減因子:RMSProp 透過防止學習率變得太小並允許更穩定的收斂,向梯度平方累積和引入了衰減因子。
  • 自適應學習率:與 AdaGrad 一樣,RMSProp 最佳化器根據梯度歷史記錄分別為每個引數調整學習率,但它透過限制過去梯度平方的累積來避免學習率減小的問題。

公式:RMSProp 最佳化器的公式如下:

$$g_t = \nabla L(\theta)$$ $$E[g^2]_t = \gamma E[g^2]_{t-1} + (1 - \gamma){g_t}^2$$ $$\theta = \theta - \frac{\alpha}{\sqrt{E[g^2]_t} + \epsilon} g_t$$

其中:

  • $g_t$ 是 t 時刻的梯度。
  • $E[g^2]_t$ 是梯度平方的移動平均值。
  • $\gamma$ 是衰減因子,通常約為 0.9。
  • $\alpha$ 是全域性學習率。
  • $\epsilon$ 是一個小的常數,用於防止除以零。

示例

以下示例展示瞭如何在 Chainer 中使用RMSProp 最佳化器和簡單神經網路:

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import Chain
import numpy as np
from chainer import Variable
from chainer import optimizers

class SimpleNN(Chain):
   def __init__(self):
      super(SimpleNN, self).__init__()
      with self.init_scope():
       self.fc1 = L.Linear(None, 100)  # Fully connected layer with 100 units
       self.fc2 = L.Linear(100, 10)   # Output layer with 10 units (e.g., for 10 classes)

   def forward(self, x):
      h = F.relu(self.fc1(x))  # Apply ReLU activation function
      return self.fc2(h)     # Output layer

# Dummy data: 5 samples, each with 50 features
x_data = np.random.rand(5, 50).astype(np.float32)
# Dummy labels: 5 samples, each with 10 classes (one-hot encoded)
y_data = np.random.randint(0, 10, 5).astype(np.int32)

# Convert to Chainer variables
x = Variable(x_data)
y = Variable(y_data)

# Initialize the model
model = SimpleNN()

# Set up RMSProp optimizer with a learning rate of 0.01 and decay factor of 0.9
optimizer = optimizers.RMSprop(lr=0.01, alpha=0.9)
optimizer.setup(model)
def loss_func(predictions, targets):
   return F.softmax_cross_entropy(predictions, targets)
# Training loop
for epoch in range(10):  # Number of epochs
   # Zero the gradients
   model.cleargrads()
   
   # Forward pass
   predictions = model(x)
   
   # Calculate loss
   loss = loss_func(predictions, y)
   
   # Backward pass
   loss.backward()
   
   # Update parameters
   optimizer.update()
   
   # Print loss
   print(f'Epoch {epoch + 1}, Loss: {loss.data}')

以下是使用 RMSProp 最佳化的上述示例的輸出:

Epoch 1, Loss: 2.3203792572021484
Epoch 2, Loss: 1.1593462228775024
Epoch 3, Loss: 1.2626817226409912
Epoch 4, Loss: 0.6015896201133728
Epoch 5, Loss: 0.3906801640987396
Epoch 6, Loss: 0.28964582085609436
Epoch 7, Loss: 0.21569299697875977
Epoch 8, Loss: 0.15832018852233887
Epoch 9, Loss: 0.12146510928869247
Epoch 10, Loss: 0.09462013095617294

Chainer 中的資料集和迭代器

在 Chainer 中,高效地處理資料對於訓練神經網路至關重要。為了促進這一點,Chainer 框架提供了兩個基本元件,即資料集迭代器。這些元件透過確保資料以結構化和高效的方式饋送到模型來幫助管理資料。

資料集

Chainer 中的資料集是可以饋送到神經網路進行訓練、驗證或測試的資料樣本的集合。Chainer 提供了一個 Dataset 類,可以擴充套件它來建立自定義資料集,以及用於常見任務的幾個內建資料集類。

Chainer 中的資料集型別

Chainer 提供了幾種型別的資料集來處理各種資料格式和結構。這些資料集可以大致分為內建資料集、自定義資料集和資料集轉換。

內建資料集

Chainer 附帶了一些常用的資料集,通常用於基準測試和實驗。這些資料集隨時可用,可以使用內建函式輕鬆載入。

以下是獲取 Chainer 中所有可用資料集列表的程式碼:

import chainer.datasets as datasets

# Get all attributes in the datasets module
all_datasets = [attr for attr in dir(datasets) if attr.startswith('get_')]

# Print the available datasets
print("Built-in datasets available in Chainer:")
for dataset in all_datasets:
   print(f"- {dataset}")

以下是顯示 Chainer 框架中所有內建資料集的輸出:

Built-in datasets available in Chainer:
- get_cifar10
- get_cifar100
- get_cross_validation_datasets
- get_cross_validation_datasets_random
- get_fashion_mnist
- get_fashion_mnist_labels
- get_kuzushiji_mnist
- get_kuzushiji_mnist_labels
- get_mnist
- get_ptb_words
- get_ptb_words_vocabulary
- get_svhn

自定義資料集

當使用自定義資料時,我們可以透過子類化chainer.dataset.DatasetMixin來建立我們自己的資料集。這允許我們定義如何載入和返回資料。

這是一個使用chainer.dataset.DatasetMixin建立自定義資料集並列印其中第一行的示例:

import chainer
import numpy as np

class MyDataset(chainer.dataset.DatasetMixin):
   def __init__(self, data, labels):
      self.data = data
      self.labels = labels

   def __len__(self):
      return len(self.data)

   def get_example(self, i):
      return self.data[i], self.labels[i]

# Creating a custom dataset
data = np.random.rand(100, 3)
labels = np.random.randint(0, 2, 100)
dataset = MyDataset(data, labels)
print(dataset[0])

以下是自定義資料集第一行的輸出:

(array([0.82744124, 0.33828446, 0.06409377]), 0)

預處理資料集

Chainer 提供了將轉換應用於資料集的工具,例如縮放、歸一化或資料增強。可以使用 TransformDataset 動態應用這些轉換。

這是一個在 Chainer 中使用預處理資料集的示例:

from chainer.datasets import TransformDataset

def transform(data):
   x, t = data
   x = x / 255.0  # Normalize input data
   return x, t

# Apply transformation to dataset
transformed_dataset = TransformDataset(dataset, transform)
print(transformed_dataset[0])

以下是使用 TransformDataset() 函式獲得的預處理資料集的第一行:

(array([0.00324487, 0.00132661, 0.00025135]), 0)

連線資料集

ConcatDataset 用於將多個數據集連線成單個數據集。當我們的資料分佈在不同的來源時,這非常有用。這是一個在 Chainer 框架中使用連線資料集的示例,它打印出連線資料集中每個樣本的資料和標籤。組合資料集包含資料集 1 和資料集 2 的所有樣本:

import numpy as np
from chainer.datasets import ConcatenatedDataset
from chainer.dataset import DatasetMixin

# Define a custom dataset class
class MyDataset(DatasetMixin):
   def __init__(self, data, labels):
      self.data = data
      self.labels = labels

   def __len__(self):
      return len(self.data)

   def get_example(self, i):
      return self.data[i], self.labels[i]

# Sample data arrays
data1 = np.random.rand(5, 3)  # 5 samples, 3 features each
labels1 = np.random.randint(0, 2, 5)  # Binary labels for data1

data2 = np.random.rand(5, 3)  # Another 5 samples, 3 features each
labels2 = np.random.randint(0, 2, 5)  # Binary labels for data2

# Create MyDataset instances
dataset1 = MyDataset(data1, labels1)
dataset2 = MyDataset(data2, labels2)

# Concatenate the datasets
combined_dataset = ConcatenatedDataset(dataset1, dataset2)

# Iterate over the combined dataset and print each example
for i in range(len(combined_dataset)):
   data, label = combined_dataset[i]
   print(f"Sample {i+1}: Data = {data}, Label = {label}")

以下是 Chainer 中連線資料集的輸出:

Sample 1: Data = [0.6153635  0.19185915 0.26029754], Label = 1
Sample 2: Data = [0.69201927 0.70393578 0.85382294], Label = 1
Sample 3: Data = [0.46647242 0.37787839 0.37249345], Label = 0
Sample 4: Data = [0.2975833  0.90399536 0.15978975], Label = 1
Sample 5: Data = [0.29939455 0.21290926 0.97327959], Label = 1
Sample 6: Data = [0.68297438 0.64874375 0.09129224], Label = 1
Sample 7: Data = [0.52026288 0.24197601 0.5239313 ], Label = 0
Sample 8: Data = [0.63250008 0.85023346 0.94985447], Label = 1
Sample 9: Data = [0.75183151 0.01774763 0.66343944], Label = 0
Sample 10: Data = [0.60212864 0.48215319 0.02736618], Label = 0

元組和字典資料集

Chainer 提供了名為TupleDatasetDictDataset 的特殊資料集類,允許我們方便地管理多個數據源。當我們有多種型別的資料(例如特徵和標籤)或我們想要一起處理的多個特徵集時,這些類非常有用。

  • 元組資料集:用於將多個數據集或資料陣列組合成單個數據集,其中每個元素都是來自原始資料集的對應元素的元組。
  • 這是一個展示如何在神經網路中使用元組資料集的示例:

    import numpy as np
    from chainer.datasets import TupleDataset
    
    # Create two datasets (or data arrays)
    data1 = np.random.rand(100, 3)  # 100 samples, 3 features each
    data2 = np.random.rand(100, 5)  # 100 samples, 5 features each
    
    # Create a TupleDataset combining both data arrays
    tuple_dataset = TupleDataset(data1, data2)
    
    # Accessing data from the TupleDataset
    for i in range(5):
       print(f"Sample {i+1}: Data1 = {tuple_dataset[i][0]}, Data2 = {tuple_dataset[i][1]}")
    

    以下是元組資料集的輸出:

     
    Sample 1: Data1 = [0.32992823 0.57362303 0.95586597], Data2 = [0.41455   0.52850591 0.55602243 0.36316931 0.93588697]
    Sample 2: Data1 = [0.37731994 0.00452533 0.67853069], Data2 = [0.71637691 0.04191565 0.54027323 0.68738626 0.01887967]
    Sample 3: Data1 = [0.85808665 0.15863516 0.51649116], Data2 = [0.9596284  0.12417238 0.22897152 0.63822924 0.99434029]
    Sample 4: Data1 = [0.2477932  0.27937585 0.59660463], Data2 = [0.92666318 0.93611279 0.96622103 0.41834484 0.72602107]
    Sample 5: Data1 = [0.71989544 0.46155552 0.31835487], Data2 = [0.27475741 0.33759694 0.22539997 0.40985004 0.00469414]
    
  • 字典資料集:這與TupleDataset類似,但允許我們透過新增鍵來標記每個元素,從而更容易訪問和理解資料。
  • 這是一個展示如何在 Chainer 中使用字典資料集的示例:

    import numpy as np
    from chainer.datasets import DictDataset
    
    # Create two datasets (or data arrays)
    data1 = np.random.rand(100, 3)  # 100 samples, 3 features each
    labels = np.random.randint(0, 2, 100)  # Binary labels for each sample
    
    # Create a DictDataset
    dict_dataset = DictDataset(data=data1, label=labels)
    
    # Accessing data from the DictDataset
    for i in range(5):
       print(f"Sample {i+1}: Data = {dict_dataset[i]['data']}, Label = {dict_dataset[i]['label']}")
    

    以下是元組資料集的輸出:

     
    Sample 1: Data = [0.09362018 0.33198328 0.11421714], Label = 1
    Sample 2: Data = [0.53655817 0.9115115  0.0192754 ], Label = 0
    Sample 3: Data = [0.48746879 0.18567869 0.88030764], Label = 0
    Sample 4: Data = [0.10720832 0.79523399 0.56056922], Label = 0
    Sample 5: Data = [0.76360577 0.69915416 0.64604595], Label = 1
    

迭代器

在 Chainer 中,迭代器對於管理機器學習模型訓練期間的資料至關重要。它們將大型資料集分解成較小的塊,稱為小批次,這些塊可以增量處理。這種方法提高了記憶體效率,並透過允許模型更頻繁地更新其引數來加快訓練過程。

Chainer 中的迭代器型別

Chainer 提供了各種型別的迭代器來處理機器學習模型的訓練和評估期間的資料集。這些迭代器旨在處理不同的場景和需求,例如處理大型資料集、並行資料載入或確保資料混洗以獲得更好的泛化能力。

SerialIterator

這是 Chainer 中最常見的迭代器。它以序列(順序)方式迭代資料集,提供小批次資料。當到達資料集的末尾時,迭代器可以停止或從開頭重新開始,具體取決於重複選項。這在資料順序得到保留的標準訓練中非常理想。

這是一個展示如何在 Chainer 中使用SerialIterator 的示例:

import chainer
import numpy as np
from chainer import datasets, iterators

# Create a simple dataset (e.g., dummy data)
x_data = np.random.rand(100, 2).astype(np.float32)  # 100 samples, 2 features each
y_data = np.random.randint(0, 2, size=(100,)).astype(np.int32)  # 100 binary labels

# Combine the features and labels into a Chainer dataset
dataset = datasets.TupleDataset(x_data, y_data)

# Initialize the SerialIterator
iterator = iterators.SerialIterator(dataset, batch_size=10, repeat=True, shuffle=True)

# Example of iterating over the dataset
for epoch in range(2):  # Run for two epochs
   while True:
      batch = iterator.next()  # Get the next batch
      
      # Unpacking the batch manually
      x_batch = np.array([example[0] for example in batch])  # Extract x data
      y_batch = np.array([example[1] for example in batch])  # Extract y data

      print("X batch:", x_batch)
      print("Y batch:", y_batch)

      if iterator.is_new_epoch:  # Check if a new epoch has started
       print("End of epoch")
       break

# Reset the iterator to the beginning of the dataset (optional)
iterator.reset()

以下是 Chainer 中使用的 SerialIterator 的輸出:

 
X batch: [[0.00603645 0.13716008]
 [0.97394305 0.9035589 ]
 [0.93046355 0.63140464]
 [0.44332692 0.5307854 ]
 [0.48565307 0.845648  ]
 [0.98147005 0.47466147]
 [0.3036461  0.62494874]
 [0.31664708 0.7176309 ]
 [0.14955625 0.65800977]
 [0.72328717 0.33383074]]
Y batch: [1 0 0 1 0 0 1 1 1 0]
----------------------------
----------------------------
----------------------------
X batch: [[0.10038178 0.32700586]
 [0.4653218  0.11713986]
 [0.10589143 0.5662842 ]
 [0.9196327  0.08948212]
 [0.13177629 0.59920484]
 [0.46034923 0.8698121 ]
 [0.24727622 0.8066094 ]
 [0.01744546 0.88371164]
 [0.18966147 0.9189765 ]
 [0.06658458 0.02469426]]
Y batch: [0 1 0 0 0 0 0 0 0 1]
End of epoch

MultiprocessIterator

此迭代器旨在透過使用多個程序來加快資料載入速度。在處理大型資料集或資料預處理耗時的情況下,它特別有用。

以下是如何在 Chainer 框架中使用多程序迭代器的示例:

import chainer
import numpy as np
from chainer import datasets, iterators

# Create a simple dataset (e.g., dummy data)
x_data = np.random.rand(1000, 2).astype(np.float32)  # 1000 samples, 2 features each
y_data = np.random.randint(0, 2, size=(1000,)).astype(np.int32)  # 1000 binary labels

# Combine the features and labels into a Chainer dataset
dataset = datasets.TupleDataset(x_data, y_data)

# Initialize the MultiprocessIterator
# n_processes: Number of worker processes to use
iterator = iterators.MultiprocessIterator(dataset, batch_size=32, n_processes=4, repeat=True, shuffle=True)

# Example of iterating over the dataset
for epoch in range(2):  # Run for two epochs
   while True:
      batch = iterator.next()  # Get the next batch
      
      # Unpacking the batch manually
      x_batch = np.array([example[0] for example in batch])  # Extract x data
      y_batch = np.array([example[1] for example in batch])  # Extract y data

      print("X batch shape:", x_batch.shape)
      print("Y batch shape:", y_batch.shape)

      if iterator.is_new_epoch:  # Check if a new epoch has started
       print("End of epoch")
       break

# Reset the iterator to the beginning of the dataset (optional)
iterator.reset()

以下是多程序迭代器的輸出:

X batch shape: (32, 2)
Y batch shape: (32,)
X batch shape: (32, 2)
Y batch shape: (32,)
X batch shape: (32, 2)
Y batch shape: (32,)
---------------------
---------------------
X batch shape: (32, 2)
Y batch shape: (32,)
X batch shape: (32, 2)
Y batch shape: (32,)
End of epoch

MultithreadIterator

MultithreadIterator 是 Chainer 中的一個迭代器,旨在使用多個執行緒進行並行資料載入。在處理可以從併發資料處理中受益的資料集時,此迭代器特別有用,例如當資料載入或預處理是訓練中的瓶頸時。

與使用多個程序的MultiprocessIterator不同,MultithreadIterator使用執行緒,使其更適合需要共享記憶體訪問或輕量級並行的場景。

以下是Chainer框架中使用MultithreadIterator的示例:

import numpy as np
from chainer.datasets import TupleDataset
from chainer.iterators import MultithreadIterator

# Create sample datasets
data1 = np.random.rand(100, 3)  # 100 samples, 3 features each
data2 = np.random.rand(100, 5)  # 100 samples, 5 features each

# Create a TupleDataset
dataset = TupleDataset(data1, data2)

# Create a MultithreadIterator with 4 threads and a batch size of 10
iterator = MultithreadIterator(dataset, batch_size=10, n_threads=4, repeat=False, shuffle=True)

# Iterate over the dataset
for batch in iterator:
   # Unpack each tuple in the batch
   data_batch_1 = np.array([item[0] for item in batch])  # Extract the first element from each tuple
   data_batch_2 = np.array([item[1] for item in batch])  # Extract the second element from each tuple

   print("Data batch 1:", data_batch_1)
   print("Data batch 2:", data_batch_2)

以下是Multithread Iterator的輸出:

Data batch 1: [[0.38723876 0.66585393 0.74603754]
 [0.136392   0.23425485 0.6053701 ]
 [0.99668734 0.13096871 0.13114792]
 [0.32277508 0.3718192  0.42083016]
 [0.93408236 0.59433832 0.23590596]
 [0.16351005 0.82340571 0.08372471]
 [0.78469682 0.81117013 0.41653794]
 [0.32369538 0.77524528 0.10378537]
 [0.21678887 0.8905319  0.88525376]
 [0.41348068 0.43437296 0.90430938]]
---------------------
---------------------
Data batch 2: [[0.20541319 0.69626397 0.81508325 0.49767042 0.92252953]
 [0.12794664 0.33955336 0.81339754 0.54042266 0.44137714]
 [0.52487615 0.59930116 0.96334436 0.61622956 0.34192033]
 [0.93474439 0.37455884 0.94954379 0.73027705 0.24333167]
 [0.24805745 0.80921792 0.91316062 0.59701139 0.25295744]
 [0.27026875 0.67836862 0.16911597 0.50452568 0.86257208]
 [0.81722752 0.41361153 0.43188091 0.98313524 0.28605503]
 [0.50885091 0.80546812 0.89346966 0.63828489 0.8231125 ]
 [0.78996715 0.05338346 0.16573956 0.89421364 0.54267903]
 [0.05804313 0.5613496  0.09146587 0.79961318 0.02466306]]

ShuffleOrderSampler

ShuffleOrderSampler是Chainer中的一個元件,用於隨機化資料集中索引的順序。它確保每個訓練時期的資料順序不同,這有助於減少過擬合併提高模型的泛化能力。

import numpy as np
from chainer.datasets import TupleDataset
from chainer.iterators import SerialIterator, ShuffleOrderSampler

# Create sample datasets
data = np.random.rand(100, 3)  # 100 samples, 3 features each
labels = np.random.randint(0, 2, size=100)  # 100 binary labels

# Create a TupleDataset
dataset = TupleDataset(data, labels)

# Initialize ShuffleOrderSampler
sampler = ShuffleOrderSampler()

# Create a SerialIterator with the ShuffleOrderSampler
iterator = SerialIterator(dataset, batch_size=10, repeat=False, order_sampler=sampler)

# Iterate over the dataset
for batch in iterator:
   # Since the batch contains tuples, we extract data and labels separately
   data_batch, label_batch = zip(*batch)
   print("Data batch:", np.array(data_batch))
   print("Label batch:", np.array(label_batch))

以下是Chainer中應用ShuffleOrderSampler迭代器的輸出:

Data batch: [[0.93062607 0.68334939 0.73764239]
 [0.87416648 0.50679946 0.17060853]
 [0.19647824 0.2195698  0.5010152 ]
 [0.28589369 0.08394862 0.28748563]
 [0.55498598 0.73032299 0.01946458]
 [0.68907645 0.8920713  0.7224627 ]
 [0.36771187 0.91855943 0.87878009]
 [0.14039665 0.88076789 0.76606626]
 [0.84889666 0.57975573 0.70021538]
 [0.45484641 0.17291856 0.42353947]]
Label batch: [0 1 1 0 1 0 1 1 0 0]
-------------------------------------
-------------------------------------
Data batch: [[0.0692231  0.24701816 0.24603659]
 [0.72014948 0.67211487 0.45648504]
 [0.8625562  0.45570299 0.58156546]
 [0.60350332 0.81757841 0.30411054]
 [0.93224841 0.3055118  0.07809648]
 [0.16425884 0.69060297 0.36452719]
 [0.79252781 0.35895253 0.26741555]
 [0.27568602 0.38510119 0.36718876]
 [0.58806512 0.35221788 0.08439596]
 [0.13015496 0.81817428 0.86631724]]
Label batch: [0 0 1 0 1 0 1 0 0 1]

訓練迴圈

訓練迴圈是機器學習的核心機制,模型透過它從資料中學習。它包含一個重複的過程:將資料輸入模型,計算誤差(損失),調整模型引數以減少誤差,然後重複此過程,直到模型在任務上的表現足夠好。訓練迴圈是訓練神經網路和其他機器學習模型的基礎。

訓練迴圈中的關鍵元件

  • 模型: 你想要訓練的神經網路或機器學習模型。
  • 損失函式: 用於衡量模型預測與實際資料匹配程度的函式,例如均方誤差、交叉熵。
  • 最佳化器: 用於根據計算的梯度更新模型引數的演算法,例如SGD、Adam。
  • 資料: 用於訓練的資料集,通常被劃分為小批次(minibatches)以提高處理效率。

為什麼訓練迴圈很重要?

訓練迴圈在深度學習和機器學習中至關重要,原因如下:

  • 效率: 它們允許模型透過處理小批次資料(即minibatches)來訓練大型資料集。
  • 迭代改進: 透過反覆調整模型引數,訓練迴圈使模型能夠學習並隨著時間的推移提高其準確性。
  • 靈活性: 訓練迴圈可以自定義,包括學習率排程、提前停止或監控指標等附加功能。

訓練迴圈的關鍵步驟

訓練迴圈的步驟如下:

  • 前向傳播: 首先將輸入資料饋入模型,然後模型透過其各層處理資料以產生輸出(預測)。
  • 損失計算: 使用損失函式將輸出與實際目標值進行比較。損失函式計算預測輸出與實際目標之間的誤差或差異。
  • 反向傳播: 計算損失相對於模型每個引數(權重)的梯度。這些梯度指示每個引數對誤差的貢獻程度。
  • 引數更新: 使用最佳化演算法(例如SGD、Adam等)更新模型引數。引數以最小化損失的方式進行調整。
  • 重複: 此過程重複多次迭代(時期),模型多次檢視資料。目標是讓模型透過逐漸減少損失來學習並改進其預測。

示例

在Chainer中,訓練迴圈用於迭代資料集,計算損失並更新模型引數。以下是一個使用Chainer演示基本訓練迴圈的示例。此示例使用在MNIST資料集上訓練的簡單前饋神經網路。

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import Chain, optimizers, training, serializers
from chainer.datasets import TupleDataset
from chainer.iterators import SerialIterator
from chainer.training import extensions
import numpy as np

# Define the neural network model
class SimpleNN(Chain):
   def __init__(self):
      super(SimpleNN, self).__init__()
      with self.init_scope():
       self.l1 = L.Linear(3, 5)  # Input layer to hidden layer
       self.l2 = L.Linear(5, 2)  # Hidden layer to output layer

   def forward(self, x):
      h = F.relu(self.l1(x))  # Apply ReLU activation
      y = self.l2(h)      # Output layer
      return y

   def __call__(self, x, t):
      y = self.forward(x)
      return F.softmax_cross_entropy(y, t)

# Generate synthetic data
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32)
labels = np.array([0, 1, 0], dtype=np.int32)

# Create a dataset and iterator
dataset = TupleDataset(data, labels)
iterator = SerialIterator(dataset, batch_size=1, shuffle=True)

# Initialize the model, optimizer, and updater
model = SimpleNN()
optimizer = optimizers.Adam()
optimizer.setup(model)

# Set up the trainer
updater = training.StandardUpdater(iterator, optimizer, device=-1)
trainer = training.Trainer(updater, (10, 'epoch'), out='result')

# Add extensions to monitor training
trainer.extend(extensions.Evaluator(iterator, model, device=-1))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss']))
trainer.extend(extensions.ProgressBar())

# Start training
trainer.run()

以下是訓練迴圈的輸出:

epoch      main/loss   validation/main/loss
廣告
© . All rights reserved.