데이터 전처리
이제 입력 2개와 출력 1개를 가지는 새로운 모델을 위해 데이터를 처리해야 해요. 두 입력은 원-핫 인코딩된 영어 단어들과, 마지막 단어를 제외한 원-핫 인코딩된 프랑스어 단어들이에요.
출력은 첫 단어를 제외한 원-핫 인코딩된 프랑스어 단어가 됩니다. 즉, 디코더에서는 각 입력 프랑스어 단어에 대해 다음 단어가 출력으로 나오도록 하죠. 여기서는 이를 어떻게 구현하는지 배워요.
sents2seqs() 함수와 en_text, fr_text가 제공되어 있어요.
이 연습은 강의의 일부입니다
Keras로 배우는 Machine Translation
연습 안내
sents2seqs()함수를 사용해 인코더 입력 배치(i부터i+bsize까지)를 얻으세요(원-핫 인코딩 및 역순 적용).sents2seqs()함수를 사용해 디코더 입력과 출력을 위한 배치(i부터i+bsize까지)를 얻으세요(원-핫 인코딩).- 시간 차원을 기준으로 슬라이싱해
de_xy에서 디코더 입력(마지막 단어를 제외한 모든 프랑스어 단어)을 분리하세요. de_xy에서 디코더 출력(첫 단어를 제외한 모든 프랑스어 단어)을 분리하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
bsize = 250
for i in range(0, len(en_text), bsize):
# Get the encoder inputs using the sents2seqs() function
en_x = ____('source', ____[____:____], onehot=True, reverse=____)
# Get the decoder inputs/outputs using the sents2seqs() function
de_xy = sents2seqs('target', ____[____:____], onehot=True)
# Separate the decoder inputs from de_xy
de_x = de_xy[:,____,:]
# Separate the decoder outputs from de_xy
de_y = de_xy[:,____,:]
print("Data from ", i, " to ", i+bsize)
print("\tnp.argmax() => en_x[0]: ", np.argmax(en_x[0], axis=-1))
print("\tnp.argmax() => de_x[0]: ", np.argmax(de_x[0], axis=-1))
print("\tnp.argmax() => de_y[0]: ", np.argmax(de_y[0], axis=-1))