파트 1: to_categorical() 함수 살펴보기
현실 세계의 문제에서는 어휘 크기가 매우 커질 수 있다는 사실을 알고 계셨나요? (예: 수십만 단어 이상)
이번 연습 문제는 두 부분으로 나뉘며, to_categorical() 함수의 num_classes 인자를 설정하는 것이 왜 중요한지 배우게 됩니다. 파트 1에서는 주어진 단어 리스트에 대해 원-핫 벡터를 생성하고, 그 벡터의 길이를 계산하는 compute_onehot_length() 함수를 구현해 보세요.
to_categorical() 함수는 이미 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Keras로 배우는 Machine Translation
연습 안내
compute_onehot_length()안에서words와word2index를 사용해 단어 ID를 만드세요.- 만든 단어 ID로
to_categorical()함수를 호출해 원-핫 벡터를 생성하세요. <array>.shape문법을 사용해 단일 원-핫 벡터의 길이를 반환하세요.- 단어 리스트
He,drank,milk에 대해compute_onehot_length()를 호출해 원-핫 벡터 길이를 계산하고 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
def compute_onehot_length(words, word2index):
# Create word IDs for words
word_ids = [____[w] for w in ____]
# Convert word IDs to onehot vectors
onehot = ____(____)
# Return the length of a single one-hot vector
return onehot.____[1]
word2index = {"He":0, "drank": 1, "milk": 2}
# Compute and print onehot length of a list of words
print(____([____,____,____], ____))