अनुवाद जनरेट करना
अब आप Teacher Forcing से प्रशिक्षित एक inference मॉडल का उपयोग करके फ्रेंच अनुवाद जनरेट करेंगे.
यह मॉडल (nmt_tf) को 50 epochs तक 100,000 वाक्यों पर ट्रेन किया गया है और 35,000+ validation सेट पर लगभग 98% accuracy हासिल की है. इस अभ्यास के initialize होने में थोड़ा समय लग सकता है क्योंकि trained मॉडल को लोड करना होगा. आपको sents2seqs() फंक्शन दिया गया है. साथ ही आपको दो नए फंक्शन भी दिए गए हैं:
word2onehot(tokenizer, word, vocab_size) जो स्वीकार करता है:
- tokenizer - Keras का
Tokenizerऑब्जेक्ट - word - vocabulary से लिया गया शब्द दर्शाने वाली string (जैसे,
'apple') - vocab_size - vocabulary का आकार
probs2word(probs, tok) जो स्वीकार करता है:
- probs - मॉडल का आउटपुट, आकार
[1,<French Vocab Size>] - tok - Keras का
Tokenizerऑब्जेक्ट
आप इन फंक्शनों के सोर्स कोड पर एक नज़र डाल सकते हैं. इसके लिए कंसोल में print(inspect.getsource(word2onehot)) और print(inspect.getsource(probs2word)) टाइप करें.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Keras के साथ Machine Translation
अभ्यास निर्देश
- एनकोडर से initial डिकोडर state (
de_s_t) की भविष्यवाणी करें. - पिछले prediction (आउटपुट) और पिछली state को इनपुट मानकर डिकोडर से आउटपुट और नई state प्रेडिक्ट करें. ध्यान रखें कि नई state को recursive तरीके से जनरेट करें.
probs2word()फंक्शन का उपयोग करके probability आउटपुट से शब्द का string प्राप्त करें.word2onehot()फंक्शन से उस शब्द string को one-hot sequence में बदलें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
en_sent = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .']
print('English: {}'.format(en_sent))
en_seq = sents2seqs('source', en_sent, onehot=True, reverse=True)
# Predict the initial decoder state with the encoder
de_s_t = ____.predict(____)
de_seq = word2onehot(fr_tok, 'sos', fr_vocab)
fr_sent = ''
for i in range(fr_len):
# Predict from the decoder and recursively assign the new state to de_s_t
de_prob, ____ = ____.predict([____,____])
# Get the word from the probability output using probs2word
de_w = probs2word(____, fr_tok)
# Convert the word to a onehot sequence using word2onehot
de_seq = word2onehot(fr_tok, ____, fr_vocab)
if de_w == 'eos': break
fr_sent += de_w + ' '
print("French (Ours): {}".format(fr_sent))
print("French (Google Translate): les etats-unis sont parfois froids en décembre, mais parfois gelés en juin")