문장 뒤집기
여기서는 인코더 모델을 위해 문장을 뒤집는 방법을 배웁니다. 앞서 설명했듯이, 소스 문장을 뒤집으면 인코더와 디코더 사이에 초기 연결을 더 견고하게 만들어 모델 성능을 높여 줍니다. 다만 이 이점은 번역하는 두 언어쌍에 따라 달라질 수 있다는 점을 항상 기억하세요. 두 언어가 주어, 동사, 목적어의 어순이 같다면 모델에 도움이 됩니다.
이 연습 문제에서는 필요할 때 문장을 뒤집을 수 있도록 sents2seqs() 함수를 수정합니다. 사용자는 불리언 키워드 인수 reverse를 지정해 텍스트 뒤집기를 수행할 수 있습니다.
이 연습은 강의의 일부입니다
Keras로 배우는 Machine Translation
연습 안내
sents2seqs()함수 시그니처에 기본값이False인 새 키워드 인수reverse를 추가하세요.- 반환되는 시퀀스 ID를 시간 차원에서(
::-1문법 사용) 뒤집어 첫 번째 단어 ID가 마지막이 되도록 하세요. sents2seqs()를 호출해 주어진sentences를 뒤집고, 다른 기본 매개변수 값은 그대로 두세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
sentences = ["california is never rainy during july ."]
# Add new keyword parameter reverse which defaults to False
def ____(input_type, sentences, onehot=False, pad_type='post', ____=____):
encoded_text = en_tok.texts_to_sequences(sentences)
preproc_text = pad_sequences(encoded_text, padding=pad_type, truncating='post', maxlen=en_len)
if reverse:
# Reverse the text using numpy axis reversing
preproc_text = preproc_text[:, ____]
if onehot:
preproc_text = to_categorical(preproc_text, num_classes=en_vocab)
return preproc_text
# Call sents2seqs to get the padded and reversed sequence of IDs
pad_seq = ____('source', ____, ____=____)
rev_sent = [en_tok.index_word[wid] for wid in pad_seq[0][-6:]]
print('\tReversed: ',' '.join(rev_sent))