시작하기무료로 시작하기

추론 모델의 디코더 정의하기

추론 모델은 실제 서비스 환경에서 사용자가 요청할 때 번역을 수행하는 데 쓰이는 모델이에요. 이 연습 문제에서는 추론 모델의 디코더를 구현해 보세요.

추론 모델의 디코더는 학습 모델의 디코더와는 달라요. 예측하려는 대상이 프랑스어 단어이므로, 프랑스어 단어를 디코더에 그대로 넣을 수 없어요. 다행히도 해결책이 있어요. 바로 이전 시점에서 예측된 프랑스어 단어를 사용해 추론 디코더에 넣는 방법이에요. 따라서 번역을 생성할 때 디코더는 이전 출력 값을 입력으로 사용하면서, 한 번에 한 단어씩 생성해야 해요.

이 연습을 위해 hsize(GRU 레이어의 hidden size), fr_len, fr_vocab 변수가 이미 임포트되어 있어요. 접두사 de는 디코더를 가리킨다는 점을 기억해 주세요.

이 연습은 강의의 일부입니다

Keras로 배우는 Machine Translation

강의 보기

연습 안내

  • 원-핫 인코딩된 프랑스어 단어 시퀀스 배치(시퀀스 길이 1)를 받는 Input 레이어를 정의하세요.
  • 디코더에 이전 상태를 공급하기 위해 hsize 크기의 상태 배치를 받는 또 다른 Input 레이어를 정의하세요.
  • 디코더 GRU의 출력과 상태를 가져오세요.
  • 프랑스어 단어 Input과 이전 상태 Input을 받아 최종 예측과 새로운 GRU 상태를 출력하는 모델을 정의하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

import tensorflow.keras.layers as layers
from tensorflow.keras.models import Model
# Define an input layer that accepts a single onehot encoded word
de_inputs = layers.____(shape=(____, ____))
# Define an input to accept the t-1 state
de_state_in = layers.____(shape=(____,))
de_gru = layers.GRU(hsize, return_state=True)
# Get the output and state from the GRU layer
de_out, de_state_out = ____(de_inputs, initial_state=____)
de_dense = layers.Dense(fr_vocab, activation='softmax')
de_pred = de_dense(de_out)

# Define a model
decoder = Model(inputs=[____, ____], outputs=[____, ____])
print(decoder.summary())
코드 편집 및 실행