Tạo bản dịch với NMT

Machine Translation với Keras

Thushan Ganegedara

Data Scientist and Author

Động lực

  • Bạn đã có mô hình đã huấn luyện
  • Cần hỗ trợ con người trong tác vụ dịch
  • Kiểm thử mô hình trên dữ liệu chưa thấy

  • Cách làm?

    • Tập kiểm thử giữ lại để đánh giá mô hình
    • Bạn sẽ yêu cầu mô hình dự đoán bản dịch cho một câu.
Machine Translation với Keras

Biến đổi đầu vào

  • Câu tiếng Anh
en_st = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .']
  • Biến đổi câu cho 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 với Keras

Sinh bản dịch

  • Tạo dự đoán

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

    • [sentences, seq len, vocab size]
  • Lấy lớp dự đoán
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 với Keras

Chuyển dự đoán thành câu

  • Chuyển ID từ sang câu bằng 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 với Keras

List comprehension chi tiết hơn

  • List comprehension
word_list = [fr_tok.index_word[i] for i in fr_seq if i != 0]
  • Vòng lặp for
word_list = []
for i in fr_seq:
  if i != 0:
    word_list.append(fr_tok.index_word[i])
Machine Translation với Keras

Ayo berlatih!

Machine Translation với Keras

Preparing Video For Download...