TensorFlow cung cấp text_dataset_from_directory để tải dataset văn bản có nhãn từ cấu trúc thư mục.
Cài đặt
pip install tensorflow
Ví dụ: Load và in 10 câu hỏi đầu
batch_size = 32
seed = 42
raw_train_ds = preprocessing.text_dataset_from_directory(
train_dir, batch_size=batch_size,
validation_split=0.25, subset='training', seed=seed
)
for text_batch, label_batch in raw_train_ds.take(1):
for i in range(10):
print("Question:", text_batch.numpy()[i][:100], '...')
print("Label:", label_batch.numpy()[i])
Kết quả mẫu
The training parameters have been defined
Found 8000 files belonging to 4 classes. Using 6000 files for training.
Question: b'"my tester is going to the wrong constructor i am new to programming..."'
Label: 1
Question: b'"blank code slow skin detection this code changes the color space to lab..."'
Label: 3
Question: b'"option and validation in blank i want to add a new option on my system..."'
Label: 1
... (7 câu hỏi khác)
Giải thích
- Thư mục
train/chứa 4 thư mục con:csharp,java,javascript,python. validation_split=0.25tách 25% làm validation set.- Label: 0=csharp, 1=java, 2=javascript, 3=python.
tf.data.Datasetpipeline mạnh mẽ cho training.
Nguồn: TensorFlow Text Tutorial