NMTで翻訳を生成する

Kerasで学ぶMachine Translation

Thushan Ganegedara

Data Scientist and Author

動機

  • 学習済みモデルがある
  • 翻訳タスクで人を支援する必要がある
  • 未知データでモデルを評価する

  • 方法

    • ホールドアウトのテストセットで評価
    • 1文の翻訳を予測させてテストする。
Kerasで学ぶMachine Translation

入力の変換

  • 英文
en_st = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .']
  • エンコーダ用に変換
en_seq = sents2seqs('source', en_st, onehot=True, reverse=True)
print(np.argmax(en_seq, axis=-1)
English: ['the united states is sometimes chilly during december , 
           but it is sometimes freezing in june .']
Reversed sentence: ['june in freezing sometimes is it ...'] 
Reversed sequence: [[34   3  54       10        2  4  7 45  5 69 10  2 23 22  6]]
Kerasで学ぶMachine Translation

翻訳の生成

  • 予測を生成

    fr_pred = model.predict(en_seq)
    
  • fr_pred.shape

    • [sentences, seq len, vocab size]
  • 予測クラスを取得
fr_seq = np.argmax(fr_pred, axis=-1)[0]
[[ 3  7 35 34  2 ...  5  4  4  0  0]] # <= fr_seq
  • fr_seq.shape
    • [num sentences, sequence len]

Kerasで学ぶMachine Translation

予測を文に変換

  • 生成された単語IDをリスト内包表記で文に変換
fr_sentence = ' '.join([fr_id2word[i] for i in fr_seq if i != 0])
English: the united states is sometimes chilly during december , but it is sometimes freezing in june .

French: les états unis est parfois froid en décembre mais il est parfois le gel en
French (Google Translate): les etats-unis sont parfois froids en décembre, mais parfois gelés en juin
Kerasで学ぶMachine Translation

リスト内包表記を詳しく

  • リスト内包表記
word_list = [fr_tok.index_word[i] for i in fr_seq if i != 0]
  • forループ
word_list = []
for i in fr_seq:
  if i != 0:
    word_list.append(fr_tok.index_word[i])
Kerasで学ぶMachine Translation

Passons à la pratique !

Kerasで学ぶMachine Translation

Preparing Video For Download...