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

การเตรียมข้อมูล IMDB สำหรับการฝึกด้วย TensorFlow


บทนำ

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

การติดตั้ง

pip install tensorflow

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

เทนเซอร์ = อาร์เรย์หลายมิติ สำหรับ Data flow graph IMDB: รีวิวหนัง 50,000+ เรื่อง ใช้กับ NLP

ตัวอย่าง: Vectorize Text Data

def vectorize_text(text, label):
    text = tf.expand_dims(text, -1)
    return vectorize_layer(text), label

text_batch, label_batch = next(iter(raw_train_ds))
first_review, first_label = text_batch[0], label_batch[0]

print("Review:", first_review)
print("Label:", raw_train_ds.class_names[first_label])
print("Vectorized review:", vectorize_text(first_review, first_label))
print("1222 -->", vectorize_layer.get_vocabulary()[1222])
print("451 -->", vectorize_layer.get_vocabulary()[451])
print('Vocabulary size:', len(vectorize_layer.get_vocabulary()))

train_ds = raw_train_ds.map(vectorize_text)
val_ds = raw_val_ds.map(vectorize_text)
test_ds = raw_test_ds.map(vectorize_text)

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

ผลลัพธ์

Review: tf.Tensor(b'Silent Night, Deadly Night 5 ...', shape=(), dtype=string)
Label: neg
Vectorized review: (, )
1222 --> stick
451 --> already
Vocabulary size: 10000

คำอธิบาย

  • กำหนดฟังก์ชัน vectorize_text แปลงข้อความเป็นตัวเลข
  • ใช้ IMDB dataset ฝึกโมเดล
  • แสดงตัวอย่าง review, label, และข้อมูลที่ vectorize แล้ว
  • Vectorize ทั้ง Train, Validation, Test data