출력 텍스트 준비하기
이 연습 문제에서는 번역 모델에 사용할 출력 텍스트를 준비해 보겠습니다. 텍스트를 인덱스 시퀀스로 변환하는 것 외에도, 각 인덱스를 원-핫 인코딩해야 해요.
영어 텍스트는 en_sentences 변수에, 학습된 토크나이저는 output_tokenizer 변수에, 영어 어휘 크기는 en_vocab_size 변수에 로드되어 있습니다.
또한 출력 언어를 변환하는 초기 단계(텍스트를 인덱스 시퀀스로 변환)를 수행하는 함수가 이미 만들어져 있어요. 이 함수는 transform_text_to_sequences() 이름으로 환경에 로드되어 있으며, 두 개의 매개변수를 받습니다. sentences는 영어 문장 리스트를, tokenizer는 keras.preprocessing.text 모듈의 학습된 Tokenizer 객체를 기대합니다.
numpy는 np로 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)
연습 안내
transform_text_to_sequences()함수에en_sentences와output_tokenizer변수를 전달해Y변수를 초기화하세요.to_categorical()함수를 사용해 문장을 원-핫 인코딩하세요. 클래스 수로는en_vocab_size변수를 사용합니다.- 임시 리스트를 numpy 배열로 변환한 뒤, 형태를
(num_sentences, sentences_len, en_vocab_size)가 되도록 재구성하세요. - 원본 텍스트와 변환된 결과를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Initialize the variable
Y = transform_text_to_sequences(____, ____)
# Temporary list
ylist = list()
for sequence in Y:
# One-hot encode sentence and append to list
ylist.append(____(sequence, num_classes=____))
# Update the variable
Y = np.array(ylist).reshape(____, Y.shape[1], en_vocab_size)
# Print the raw sentence and its transformed version
print("Raw sentence: {0}\nTransformed: {1}".format(____, Y[0]))