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

การดาวน์โหลดและสำรวจชุดข้อมูล IMDB ด้วย TensorFlow


บทนำ

TensorFlow เฟรมเวิร์ก ML จาก Google โค้ดเปิด ใช้กับ Python ใช้ในงานวิจัยและการผลิต มีเทคนิคเพิ่มประสิทธิภาพ ใช้ NumPy และเทนเซอร์ รองรับ Deep Neural Networks สเกลได้ดี มีดาต้ASET ยอดนิยม ใช้ GPU computation จัดการทรัพยากรอัตโนมัติ มีไลบรารี ML มากมาย เอกสารดี

การติดตั้ง

pip install tensorflow

Tensor และชุดข้อมูล IMDB

เทนเซอร์ = อาร์เรย์หลายมิติ ระบุได้ 3 แอตทริบิวต์: Rank, Type, Shape IMDB: รีวิวหนัง 50,000+ เรื่อง ใช้กับ NLP

ตัวอย่าง: ดาวน์โหลด, แสดงตัวอย่าง, แบ่งข้อมูล

import matplotlib.pyplot as plt
import os, re, shutil, string
import tensorflow as tf
from tensorflow.keras import layers, losses, preprocessing
from tensorflow.keras.layers.experimental.preprocessing import TextVectorization

print("TensorFlow version:")
print(tf.__version__)

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"
dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url, untar=True, cache_dir='.', cache_subdir='')

print("กำลังดาวน์โหลดข้อมูล")
dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
print("โฟลเดอร์ในข้อมูล:", os.listdir(dataset_dir))

train_dir = os.path.join(dataset_dir, 'train')
print("โฟลเดอร์ train:", os.listdir(train_dir))

print("ตัวอย่างข้อมูล:")
sample_file = os.path.join(train_dir, 'pos/1181_9.txt')
with open(sample_file) as f:
    print(f.read())

remove_dir = os.path.join(train_dir, 'unsup')
shutil.rmtree(remove_dir)

batch_size = 32
seed = 42
print("Batch size:", batch_size)

raw_train_ds = tf.keras.preprocessing.text_dataset_from_directory(
    'aclImdb/train', batch_size=batch_size, validation_split=0.2, subset='training', seed=seed)

for text_batch, label_batch in raw_train_ds.take(1):
    for i in range(3):
        print("Review:", text_batch.numpy()[i])
        print("Label:", label_batch.numpy()[i])
    print("Label 0 =", raw_train_ds.class_names[0])
    print("Label 1 =", raw_train_ds.class_names[1])

raw_val_ds = tf.keras.preprocessing.text_dataset_from_directory(
    'aclImdb/train', batch_size=batch_size, validation_split=0.2, subset='validation', seed=seed)

raw_test_ds = tf.keras.preprocessing.text_dataset_from_directory(
    'aclImdb/test', batch_size=batch_size)

ที่มาโค้ด: TensorFlow Text Classification Tutorial

ผลลัพธ์

TensorFlow version: 2.4.0
กำลังดาวน์โหลดข้อมูล
โฟลเดอร์ในข้อมูล: ...
ตัวอย่างข้อมูล: Rachel Griffiths writes and directs this award winning short film...
Batch size: 32
Found 25000 files belonging to 2 classes. Using 20000 files for training.
Review: b'"Pandemonium" is a horror movie spoof...' Label: 0
Review: b"David Mamet is a very interesting..." Label: 0
Review: b'Great documentary about the lives of NY firefighters...' Label: 1
Label 0 = neg, Label 1 = pos
Found 25000 files belonging to 2 classes. Using 5000 files for validation.
Found 25000 files belonging to 2 classes.

คำอธิบาย

  • นำเข้าแพ็กเกจและตั้งชื่อย่อ
  • ดาวน์โหลด IMDB dataset จาก Stanford แล้วแตกไฟล์
  • แสดงตัวอย่างรีวิวต้นฉบับ
  • แบ่งข้อมูล Train/Validation/Test
  • ลบโฟลเดอร์ unsup (unsupervised)
  • สร้าง tf.data.Dataset จาก directory โครงสร้าง pos/neg
  • แสดงตัวอย่าง 3 รีวิวแรก และ class names (neg/pos)