Úvod do teacher forcingu

Machine Translation with Keras

Thushan Ganegedara

Data Scientist and Author

Předchozí model strojového překladače

  • Předchozí model

Plný model enkodér-dekodér

  • Enkodér GRU
    • Zpracovává anglická slova
    • Výstupem je kontextový vektor
  • Dekodér GRU
    • Zpracovává kontextový vektor
    • Výstupem je sekvence výstupů GRU
  • Predikční vrstva dekodéru
    • Zpracovává sekvenci výstupů GRU
    • Výstupem jsou pravděpodobnosti predikce francouzských slov
Machine Translation with Keras

Analogie: Trénování bez teacher forcingu

Bez teacher forcingu 1

Machine Translation with Keras

Analogie: Trénování bez teacher forcingu

Bez teacher forcingu 2

Machine Translation with Keras

Analogie: Trénování bez teacher forcingu

Bez teacher forcingu 3

Machine Translation with Keras

Analogie: Trénování s teacher forcingem

Teacher forcing 1

Machine Translation with Keras

Analogie: Trénování s teacher forcingem

Teacher forcing 2

Machine Translation with Keras

Analogie: Trénování s teacher forcingem

Teacher forcing 3

Machine Translation with Keras

Analogie: Trénování s teacher forcingem

Teacher forcing 4

Machine Translation with Keras

Předchozí model strojového překladače

  • Předchozí model

Plný model enkodér-dekodér

  • Model s teacher forcingem

Model enkodér-dekodér s teacher forcingem

Machine Translation with Keras

Implementace modelu s teacher forcingem

  • Enkodér
    en_inputs = layers.Input(shape=(en_len, en_vocab))
    en_gru = layers.GRU(hsize, return_state=True)
    en_out, en_state = en_gru(en_inputs)
    
  • Dekodér GRU
    de_inputs = layers.Input(shape=(fr_len-1, fr_vocab))
    de_gru = layers.GRU(hsize, return_sequences=True)
    de_out = de_gru(de_inputs, initial_state=en_state)
    
Machine Translation with Keras

Vstupy a výstupy

  • Vstup enkodéru – např. I, like, dogs
  • Vstup dekodéru – např. J'aime, les
  • Výstup dekodéru – např. les, chiens

Vstupy a výstupy teacher forcingu

Machine Translation with Keras

Implementace modelu s teacher forcingem

  • Enkodér
    en_inputs = layers.Input(shape=(en_len, en_vocab))
    en_gru = layers.GRU(hsize, return_state=True)
    en_out, en_state = en_gru(en_inputs)
    
  • Dekodér GRU
    de_inputs = layers.Input(shape=(fr_len-1, fr_vocab))
    de_gru = layers.GRU(hsize, return_sequences=True)
    de_out = de_gru(de_inputs, initial_state=en_state)
    
  • Predikce dekodéru
    de_dense = layers.TimeDistributed(layers.Dense(fr_vocab, activation='softmax'))
    de_pred = de_dense(de_out)
    
Machine Translation with Keras

Kompilace modelu

nmt_tf = Model(inputs=[en_inputs, de_inputs], outputs=de_pred)
nmt_tf.compile(optimizer='adam', loss="categorical_crossentropy", metrics=["acc"])
Machine Translation with Keras

Předzpracování dat

  • Enkodér

    • Vstupy – všechna anglická slova (zakódovaná one-hot)
      • en_x = sents2seqs('source', en_text, onehot=True, reverse=True)
  • Dekodér

    de_xy = sents2seqs('target', fr_text, onehot=True)
    
    • Vstupy – všechna francouzská slova kromě posledního (zakódovaná one-hot)
      • de_x = de_xy[:,:-1,:]
    • Výstupy/Cíle – všechna francouzská slova kromě prvního (zakódovaná one-hot)
      • de_y = de_xy[:,1:,:]
Machine Translation with Keras

Pojďme si procvičit!

Machine Translation with Keras

Preparing Video For Download...