파라미터 수 비교
원-핫 표현은 매우 희소해서 단어를 표현하기에 좋지 않다는 것을 보셨죠. Embedding 레이어를 사용하면 벡터의 밀집 표현을 만들 수 있지만, 학습해야 하는 파라미터 수도 많아져요.
이 연습 문제에서는 embeddings와 one-hot 인코딩을 사용하는 두 모델의 파라미터 수를 비교해 차이를 확인해 보겠습니다.
환경에는 이미 model_onehot 모델이 로드되어 있고, keras에서 Sequential, Dense, GRU도 사용할 수 있어요. 마지막으로, vocabulary_size=80000과 sentence_len=200 파라미터도 로드되어 있습니다.
이 연습은 강의의 일부입니다
Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)
연습 안내
keras.layers에서Embedding레이어를 임포트하세요.- 임베딩 레이어에서 입력 차원은 어휘 크기에 1을 더한 값으로, 입력 길이는 문장 길이로 설정하세요.
- 모델을 컴파일하세요.
- 임베딩을 포함한 모델의 summary를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import the embedding layer
from tensorflow.keras.layers import ____
# Create a model with embeddings
model = Sequential(name="emb_model")
model.add(Embedding(input_dim=____, output_dim=wordvec_dim, input_length=____, trainable=True))
model.add(GRU(128))
model.add(Dense(1))
model.____(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Print the summaries of the one-hot model
model_onehot.summary()
# Print the summaries of the model with embeddings
model.____