Teacher Forcing 소개

Keras로 배우는 Machine Translation

Thushan Ganegedara

Data Scientist and Author

이전 기계번역기 모델

  • 이전 모델

Encoder Decoder full model

  • 인코더 GRU
    • 영어 단어를 입력으로 받음
    • 컨텍스트 벡터를 출력
  • 디코더 GRU
    • 컨텍스트 벡터를 입력으로 받음
    • GRU 출력 시퀀스를 생성
  • 디코더 예측 레이어
    • GRU 출력 시퀀스를 입력으로 받음
    • 프랑스어 단어 확률을 출력
Keras로 배우는 Machine Translation

비유: Teacher Forcing 없이 학습하기

No teacher forcing 1

Keras로 배우는 Machine Translation

비유: Teacher Forcing 없이 학습하기

No teacher forcing 2

Keras로 배우는 Machine Translation

비유: Teacher Forcing 없이 학습하기

No teacher forcing 3

Keras로 배우는 Machine Translation

비유: Teacher Forcing으로 학습하기

Teacher forcing 1

Keras로 배우는 Machine Translation

비유: Teacher Forcing으로 학습하기

Teacher forcing 2

Keras로 배우는 Machine Translation

비유: Teacher Forcing으로 학습하기

Teacher forcing 3

Keras로 배우는 Machine Translation

비유: Teacher Forcing으로 학습하기

Teacher forcing 4

Keras로 배우는 Machine Translation

이전 기계번역기 모델

  • 이전 모델

Encoder decoder full model

  • Teacher Forcing 적용 모델

Encoder decoder model with teacher forcing

Keras로 배우는 Machine Translation

Teacher Forcing 모델 구현

  • 인코더
    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)
    
  • 디코더 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)
    
Keras로 배우는 Machine Translation

입력과 출력

  • 인코더 입력 - 예: I, like, dogs
  • 디코더 입력 - 예: J'aime, les
  • 디코더 출력 - 예: les, chiens

Teacher forcing inputs and outputs

Keras로 배우는 Machine Translation

Teacher Forcing 모델 구현

  • 인코더
    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)
    
  • 디코더 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)
    
  • 디코더 예측
    de_dense = layers.TimeDistributed(layers.Dense(fr_vocab, activation='softmax'))
    de_pred = de_dense(de_out)
    
Keras로 배우는 Machine Translation

모델 컴파일

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

데이터 전처리

  • 인코더

    • 입력 - 모든 영어 단어(원-핫 인코딩)
      • en_x = sents2seqs('source', en_text, onehot=True, reverse=True)
  • 디코더

    de_xy = sents2seqs('target', fr_text, onehot=True)
    
    • 입력 - 마지막 단어 제외 모든 프랑스어 단어(원-핫)
      • de_x = de_xy[:,:-1,:]
    • 출력/타깃 - 첫 단어 제외 모든 프랑스어 단어(원-핫)
      • de_y = de_xy[:,1:,:]
Keras로 배우는 Machine Translation

연습해 봅시다!

Keras로 배우는 Machine Translation

Preparing Video For Download...