TensorFlow เฟรมเวิร์ก ML จาก Google Open Source ใช้กับ Python อัลกอริทึม Deep Learning งานวิจัยและ Production
Tensor โครงสร้างข้อมูลใน TensorFlow (Data Flow Graph) เป็นอาร์เรย์หลายมิติ/ลิสต์ มี 3 แอตทริบิวต์: Rank (มิติ), Type (ชนิดข้อมูล), Shape (ขนาดแถว-คอลัมน์)
ใช้ชุดข้อมูล Iliad (ข้อความแปล 3 ชิ้น) โมเดลระบุผู้แปลจากบรรทัดเดี่ยว ข้อมูล Preprocessed แล้ว ใช้ Google Colab รันโค้ด (Python บนเบราว์เซอร์ ฟรี GPU บน Jupyter Notebook)
ตัวอย่างโค้ด
def labeler(example, index):
return example, tf.cast(index, tf.int64)
print("An empty list has been created")
labeled_data_sets = []
print("Iterate through the file names and create a dataset from text file using 'TextLineDataset' method")
for i, file_name in enumerate(FILE_NAMES):
lines_dataset = tf.data.TextLineDataset(str(parent_dir/file_name))
labeled_dataset = lines_dataset.map(lambda ex: labeler(ex, i))
labeled_data_sets.append(labeled_dataset)
Credit: TensorFlow Tutorials
ผลลัพธ์
An empty list has been created Iterate through the file names and create a dataset from text file using 'TextLineDataset' method
คำอธิบาย
TextLineDatasetสร้าง tf.data.Dataset จากไฟล์ข้อความแต่ละ Example คือบรรทัดข้อความจากไฟล์ต้นฉบับ
text_dataset_from_directoryใช้เนื้อหาไฟล์ทั้งหมดเป็น Example เดียวTextLineDataset เหมาะกับข้อมูลข้อความแบบบรรทัด (Line-based)
วนลูปไฟล์ โหลดแต่ละบรรทัดเข้า Dataset แยกกัน
แต่ละ Example ต้องมี Label แยกกัน ใช้
tf.data.Dataset.mapใส่ Labeler Function ทุกบรรทัดจะวนทุก Example คืนค่า (Example, Label) Pairs