TensorFlow เป็นเฟรมเวิร์ก Machine Learning จาก Google เป็น Open Source ใช้กับ Python สำหรับอัลกอริทึม Deep Learning และอื่นๆ ใช้ได้ทั้งวิจัยและ Production
ติดตั้ง tensorflow บน Windows:
pip install tensorflow
Tensor คือโครงสร้างข้อมูลใน TensorFlow เชื่อมขอบใน Data Flow Graph เป็นอาร์เรย์หลายมิติหรือลิสต์ มี 3 แอตทริบิวต์หลัก:
Rank – จำนวนมิติของ Tensor (Order)
Type – ชนิดข้อมูลของสมาชิกใน Tensor (1D, 2D, nD)
Shape – ขนาดแถวและคอลัมน์รวมกัน
ใช้ชุดข้อมูล Iliad (ข้อความแปล 3 ชิ้น) โมเดลระบุผู้แปลจากบรรทัดเดี่ยว ข้อมูลผ่าน Preprocessing (ลบ Header, Footer, หมายเลขบรรทัด, ชื่อบท) ใช้ Google Colab รันโค้ด (รัน Python บนเบราว์เซอร์ ฟรี GPU บน Jupyter Notebook)
ตัวอย่างโค้ด
train_data = all_encoded_data.skip(VALIDATION_SIZE).shuffle(BUFFER_SIZE)
validation_data = all_encoded_data.take(VALIDATION_SIZE)
train_data = train_data.padded_batch(BATCH_SIZE)
validation_data = validation_data.padded_batch(BATCH_SIZE)
sample_text, sample_labels = next(iter(validation_data))
print("The text batch shape is : ", sample_text.shape)
print("The label batch shape is : ", sample_labels.shape)
print("A text example is : ", sample_text[5])
print("A label example is: ", sample_labels[5])
Credit: TensorFlow Tutorials
ผลลัพธ์
The text batch shape is : (64, 18) The label batch shape is : (64,) A text example is : tf.Tensor( [ 20 391 2 11 144 787 2 3498 16 49 2 0 0 0 0 0 0 0], shape=(18,), dtype=int64) A label example is: tf.Tensor(1, shape=(), dtype=int64)
คำอธิบาย
Keras TextVectorization layer ใช้จัดกลุ่ม/แบทช์ และ Pad ข้อมูลที่ Vectorized แล้ว
Padding จำเป็นเพราะตัวอย่างในแบทช์ต้องมีขนาดเท่ากัน แต่อาจต่างกันใน Dataset
แต่ละบรรทัดข้อความอาจมีจำนวนคำต่างกัน
เมธอด
tf.data.Datasetช่วยแบ่งและ Pad-batch Datasetvalidation_dataและtrain_dataเป็นคอลเลกชันของ Batch Dataแต่ละ Batch เป็นคู่ (หลายตัวอย่าง, หลาย Label) แทนด้วยอาร์เรย์