시작하기무료로 시작하기

임베딩으로 성능 향상하기

임베딩 레이어를 사용하면 모델의 정확도가 더 좋아질까요? 같은 IMDB 데이터로 확인해 볼게요.

이 모델은 이전에 simpleRNN 셀을 사용한 모델과 마찬가지로 에포크 10으로 이미 학습되어 있어요. 모델을 비교할 수 있도록 테스트 세트 (X_test, y_test)가 환경에 준비되어 있으며, 이전 모델 simpleRNN_model도 제공됩니다. 이전 모델의 정확도는 변수 acc_SimpleRNN에 로드되어 있어요.

필요한 모듈과 함수는 모두 환경에 로드되어 있습니다: keras.modelsSequential(), keras.layersEmbeddingDense, 그리고 keras.layersSimpleRNN.

이 연습은 강의의 일부입니다

Keras로 배우는 언어 모델링을 위한 순환 신경망(RNN)

강의 보기

연습 안내

  • 모델에 임베딩 레이어를 추가하세요.
  • 모델의 정확도를 계산해 변수 acc_embeddings에 저장하세요.
  • 이전 모델과 새로운 모델의 정확도를 출력하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Create the model with embedding
model = Sequential(name="emb_model")
model.add(____(input_dim=max_vocabulary, output_dim=wordvec_dim, input_length=max_len))
model.add(SimpleRNN(units=128))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Load pre-trained weights
model.load_weights('embedding_model_weights.h5')

# Evaluate the models' performance (ignore the loss value)
_, ____ = model.evaluate(X_test, y_test, verbose=0)

# Print the results
print("SimpleRNN model's accuracy:\t{0}\nEmbeddings model's accuracy:\t{1}".format(acc_simpleRNN, ____))
코드 편집 및 실행