다음은 모델 훈련에 대한 출력 결과입니다.
Epoch 1 Batch 0 Loss 4.5296
Epoch 1 Batch 100 Loss 2.1536
Epoch 1 Batch 200 Loss 1.8774
Epoch 1 Batch 300 Loss 1.7457
...(중간 생략)...
Epoch 10 Batch 0 Loss 0.0844
Epoch 10 Batch 100 Loss 0.0848
Epoch 10 Batch 200 Loss 0.0920
Epoch 10 Batch 300 Loss 0.1320
Epoch 10 Loss 0.1022
Time taken for 1 epoch 902.3556807041168 sec
훈련된 모델 평가를 시각화하여 보여 줍니다.
코드 10-38 모델 평가 및 시각화를 위한 함수
def evaluate(sentence):
attention_plot = np.zeros((max_length_targ, max_length_inp))
sentence = preprocess_sentence(sentence)
inputs = [inp_lang.word_index[i] for i in sentence.split(' ')]
inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs],
maxlen=max_length_inp,
padding='post')
inputs = tf.convert_to_tensor(inputs)
result = ''
hidden = [tf.zeros((1, units))]
enc_out, enc_hidden = encoder(inputs, hidden)
dec_hidden = enc_hidden
dec_input = tf.expand_dims([targ_lang.word_index['<start>']], 0)
for t in range(max_length_targ):
predictions, dec_hidden, attention_weights = decoder(dec_input,
dec_hidden,
enc_out)
attention_weights = tf.reshape(attention_weights, (-1,)) ------ 어텐션 가중치
attention_plot[t] = attention_weights.numpy()
predicted_id = tf.argmax(predictions[0]).numpy()
result += targ_lang.index_word[predicted_id] + ' '
if targ_lang.index_word[predicted_id] == '<end>':
return result, sentence, attention_plot
dec_input = tf.expand_dims([predicted_id], 0) ------ 예측된 ID가 모델에 피드백됩니다.
return result, sentence, attention_plot