訓練 NMT 模型

使用 Keras 進行機器翻譯

Thushan Ganegedara

Data Scientist and Author

再看一次模型

  • 編碼器 GRU
    • 讀入英文單字
    • 輸出情境向量
  • 解碼器 GRU
    • 讀入情境向量
    • 輸出 GRU 序列
  • 解碼器預測層
    • 讀入 GRU 輸出序列
    • 輸出法文字的預測機率

使用 Keras 進行機器翻譯

參數最佳化

  • GRU 與 Dense 層都有參數
  • 常以 W(權重)與 b(偏差)表示(以隨機值初始化)
  • 用來把輸入轉成有用的輸出
  • 透過最佳化器隨時間調整,以最小化指定損失
    • 損失:
      • 模型的預測(例如模型產生的法文字)與
      • 真實輸出(例如實際法文字)之差。
  • 在模型編譯時指定
nmt.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
使用 Keras 進行機器翻譯

訓練模型

  • 訓練迭代
    for ei in range(n_epochs): # Single traverse through the dataset
      for i in range(0,data_size,bsize): # Processing a single batch
    
  • 取得一個訓練批次
      en_x = sents2seqs('source', en_text[i:i+bsize], onehot=True, reverse=True)
      de_y = sents2seqs('target', en_text[i:i+bsize], onehot=True)
    
  • 以單一批次進行訓練
      nmt.train_on_batch(en_x, de_y)
    
  • 評估模型
      res = nmt.evaluate(en_x, de_y, batch_size=bsize, verbose=0)
    
使用 Keras 進行機器翻譯

訓練模型

  • 取得訓練損失與準確率
      res = nmt.evaluate(en_x, de_y, batch_size=bsize, verbose=0)
      print("Epoch {} => Train Loss:{}, Train Acc: {}".format(
        ei+1,res[0], res[1]*100.0))
    
Epoch 1 => Train Loss:4.8036723136901855, Train Acc: 5.215999856591225
...
Epoch 1 => Train Loss:4.718592643737793, Train Acc: 47.0880001783371
...
Epoch 5 => Train Loss:2.8161656856536865, Train Acc: 56.40000104904175
Epoch 5 => Train Loss:2.527724266052246, Train Acc: 54.368001222610474
Epoch 5 => Train Loss:2.2689621448516846, Train Acc: 54.57599759101868
Epoch 5 => Train Loss:1.9934935569763184, Train Acc: 56.51199817657471
Epoch 5 => Train Loss:1.7581449747085571, Train Acc: 55.184000730514526
Epoch 5 => Train Loss:1.5613118410110474, Train Acc: 55.11999726295471
使用 Keras 進行機器翻譯

避免過度擬合

  • 將資料集分成兩部分
    • 訓練集-用來訓練模型
    • 驗證集-用來監控模型準確率
  • 當驗證準確率不再上升,就停止訓練。

過度擬合點

使用 Keras 進行機器翻譯

切分資料集

  • 定義訓練集與驗證集的大小

    train_size, valid_size = 800, 200
    
  • 隨機打亂資料索引

    inds = np.arange(len(en_text))
    np.random.shuffle(inds)
    
  • 取得訓練與驗證索引

    train_inds = inds[:train_size]
    valid_inds = inds[train_size:train_size+valid_size]
    
使用 Keras 進行機器翻譯

切分資料集

  • 依索引切分資料集:
    • 具有訓練索引的資料 → 訓練集
    • 具有驗證索引的資料 → 驗證集
tr_en = [en_text[ti] for ti in train_inds]
tr_fr = [fr_text[ti] for ti in train_inds]

v_en = [en_text[ti] for ti in valid_inds]
v_fr = [fr_text[ti] for ti in valid_inds]
使用 Keras 進行機器翻譯

含驗證的模型訓練

n_epochs, bsize = 5, 250
for ei in range(n_epochs):

for i in range(0,train_size,bsize): en_x = sents2seqs('source', tr_en[i:i+bsize], onehot=True, pad_type='pre') de_y = sents2seqs('target', tr_fr[i:i+bsize], onehot=True) nmt.train_on_batch(en_x, de_y)
v_en_x = sents2seqs('source', v_en, onehot=True, pad_type='pre') v_de_y = sents2seqs('target', v_fr, onehot=True)
res = nmt.evaluate(v_en_x, v_de_y, batch_size=valid_size, verbose=0) print("Epoch: {} => Loss:{}, Val Acc: {}".format(ei+1,res[0], res[1]*100.0))
Epoch 1 => Train Loss:4.8036723136901855, Train Acc: 5.215999856591225
使用 Keras 進行機器翻譯

一起來練習吧!

使用 Keras 進行機器翻譯

Preparing Video For Download...