Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ TensorFlow โหลดชุดข้อมูล StackOverflow Questions ใน Python

บทนำ

TensorFlow ให้ Utility text_dataset_from_directory โหลดข้อความจากโครงสร้างโฟลเดอร์ สร้าง Label ตามชื่อโฟลเดอร์

การติดตั้ง

pip install tensorflow

ตัวอย่างโค้ด

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])

ผลลัพธ์ตัวอย่าง

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
... (8 more samples)
Labels: 0=csharp, 1=java, 2=javascript, 3=python

คำอธิบาย

  • โครงสร้างโฟลเดอร์: train/csharp/, train/java/, ...

  • validation_split=0.25 แยก 25% เป็น Validation

  • ใช้ tf.data.Dataset pipeline