การสร้างคำแปลด้วย NMT

Machine Translation ด้วย Keras

Thushan Ganegedara

Data Scientist and Author

แรงจูงใจ

  • มีโมเดลที่ฝึกแล้ว
  • ต้องการช่วยมนุษย์ในงานแปลภาษา
  • ทดสอบโมเดลกับข้อมูลที่ยังไม่เคยเห็น

  • จะทำอย่างไร?

    • ใช้ชุดทดสอบแบบ hold-out เพื่อประเมินโมเดล
    • ทดสอบโมเดลโดยให้พยากรณ์คำแปลของประโยคหนึ่ง
Machine Translation ด้วย Keras

การแปลงข้อมูลอินพุต

  • ประโยคภาษาอังกฤษ
en_st = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .']
  • แปลงประโยค encoder
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]]
Machine Translation ด้วย Keras

การสร้างคำแปล

  • การสร้างการพยากรณ์

    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]

Machine Translation ด้วย Keras

การแปลงผลพยากรณ์เป็นประโยค

  • แปลง word ID ที่ได้เป็นประโยคโดยใช้ list comprehension
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
Machine Translation ด้วย Keras

List comprehension อย่างละเอียด

  • List comprehension
word_list = [fr_tok.index_word[i] for i in fr_seq if i != 0]
  • For loop
word_list = []
for i in fr_seq:
  if i != 0:
    word_list.append(fr_tok.index_word[i])
Machine Translation ด้วย Keras

มาฝึกกันเถอะ!

Machine Translation ด้วย Keras

Preparing Video For Download...