시작하기무료로 시작하기

검증과 함께 모델 학습하기

여기서는 Teacher Forcing을 사용해 모델을 학습하고, 검증 단계도 수행해 보겠습니다. 여러 epoch과 여러 iteration에 걸쳐 모델을 학습한 뒤, 각 epoch이 끝날 때마다 검증을 실행해 결과를 확인합니다.

이를 위해 en_text(영어 문장), fr_text(프랑스어 문장), sents2seqs() 함수, 그리고 nmt_tf(컴파일된 모델)가 제공되어 있습니다. 또한 이미 로드된 학습 데이터 tr_en, tr_fr와 검증 데이터 v_en, v_fr도 사용할 수 있습니다.

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

Keras로 배우는 Machine Translation

강의 보기

연습 안내

  • de_xy에서 디코더 입력(마지막 단어를 제외한 전체)과 디코더 출력(첫 번째 단어를 제외한 전체)을 추출하세요.
  • 단일 배치의 데이터로 모델을 학습하세요.
  • 학습 데이터에서와 동일한 방식으로 검증 데이터로부터 디코더 입력과 출력을 만드세요.
  • 검증 데이터셋에 대해 모델을 평가해 검증 손실과 정확도를 얻으세요.

실습형 인터랙티브 연습

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

for ei in range(n_epochs):
  for i in range(0,train_size,bsize):    
    en_x = sents2seqs('source', tr_en[i:i+bsize], onehot=True, reverse=True)
    de_xy = sents2seqs('target', tr_fr[i:i+bsize], onehot=True)
    # Create a single batch of decoder inputs and outputs
    de_x, de_y = ____[:,____,:], de_xy[:,____,:]
    # Train the model on a single batch of data
    nmt_tf.____([____,____], de_y)      
  v_en_x = sents2seqs('source', v_en, onehot=True, reverse=True)
  # Create a single batch of validation decoder inputs and outputs
  v_de_xy = ____('target', ____, onehot=____)
  v_de_x, v_de_y = ____[____], v_de_xy[____]
  # Evaluate the trained model on the validation data
  res = nmt_tf.evaluate([____,____], ____, batch_size=valid_size, verbose=0)
  print("{} => Loss:{}, Val Acc: {}".format(ei+1,res[0], res[1]*100.0))
코드 편집 및 실행