Khi mô hình đầu ra là logits cho từng lớp, cần hàm chuyển đổi từ điểm số sang nhãn văn bản để dễ hiểu kết quả.
Hàm chuyển đổi nhãn
print("Dự đoán điểm số cho mọi nhãn")
def get_string_labels(predicted_scores_batch):
predicted_int_labels = tf.argmax(predicted_scores_batch, axis=1)
predicted_labels = tf.gather(raw_train_ds.class_names, predicted_int_labels)
return predicted_labels
Kết quả
Dự đoán điểm số cho mọi nhãn
Giải thích
tf.argmax(..., axis=1)lấy index có điểm cao nhất cho mỗi mẫu trong batch.tf.gathertra cứu tên lớp từclass_namescủa dataset gốc.- Hàm này dùng cho cả binary và multi-class classification.
- Kết quả là tensor chứa tên nhãn (string) thay vì index số.