Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Xáo trộn và kết hợp dữ liệu Iliad với TensorFlow

Sau khi tải và gán nhãn cho từng file, cần gộp 3 tập dữ liệu (tương ứng 3 người dịch) và xáo trộn để đảm bảo phân phối đồng đều khi huấn luyện.

Mã gộp và shuffle

print("Kết hợp các tập dữ liệu đã gán nhãn và xáo trộn")
BUFFER_SIZE = 50000
BATCH_SIZE = 64
VALIDATION_SIZE = 5000

all_labeled_data = labeled_data_sets[0]
for labeled_dataset in labeled_data_sets[1:]:
    all_labeled_data = all_labeled_data.concatenate(labeled_dataset)

all_labeled_data = all_labeled_data.shuffle(
    BUFFER_SIZE, reshuffle_each_iteration=False)

print("Hiển thị vài mẫu dữ liệu đầu vào")
for text, label in all_labeled_data.take(8):
    print("Câu:", text.numpy())
    print("Nhãn:", label.numpy())

Kết quả

Kết hợp các tập dữ liệu đã gán nhãn và xáo trộn
Hiển thị vài mẫu dữ liệu đầu vào
Câu: b'But I have now both tasted food, and given' - Nhãn: 0
Câu: b'All these shall now be thine: but if the Gods' - Nhãn: 1
Câu: b'Their spiry summits waved. There, unperceived' - Nhãn: 0
Câu: b'"I pray you, would you show your love, dear friends,' - Nhãn: 1
Câu: b'Entering beneath the clavicle the point' - Nhãn: 0
Câu: b'But grief, his father lost, awaits him now,' - Nhãn: 1
Câu: b'in the fore-arm where the sinews of the elbow are united, whereon he' - Nhãn: 2
Câu: b'For, as I think, I have already chased' - Nhãn: 0

Giải thích

  • Sử dụng concatenate() để gộp 3 dataset thành một.
  • shuffle(BUFFER_SIZE) xáo trộn với buffer lớn (50k) để đảm bảo tính ngẫu nhiên tốt.
  • reshuffle_each_iteration=False giữ thứ tự cố định qua các epoch.
  • Mỗi entry trong all_labeled_data là một cặp (câu văn bản, nhãn người dịch: 0, 1, 2).

Nguồn: TensorFlow Text Loading Tutorial