TensorFlow: ML Framework จาก Google Open-source ใช้กับ Python ทำ Algorithms, Deep Learning มี Optimization สำหรับคณิตศาสตร์ซับซ้อน Tensor = โครงสร้างข้อมูลหลายมิติ (Multi-dimensional array) เชื่อม Edge ใน Data Flow Graph
Keras: Deep Learning API Python ระดับสูง จากโครงการ ONEIROS รันบน TensorFlow Scalable, Cross-platform เข้าถึงด้วย from tensorflow import keras
Functional API
ยืดหยุ่นกว่า Sequential API รองรับ Non-linear topology, Shared layers, Multiple I/O สร้าง DAG ของเลเยอร์
ตัวอย่าง: เทรนโมเดลหลาย Inputs/Outputs
print("Sample input data")
title_data = np.random.randint(num_words, size=(1280, 10))
body_data = np.random.randint(num_words, size=(1280, 100))
tags_data = np.random.randint(2, size=(1280, num_tags)).astype("float32")
print("Sample target data")
priority_targets = np.random.random(size=(1280, 1))
dept_targets = np.random.randint(2, size=(1280, num_classes))
print("The model is being fit to the data")
model.fit(
{"title": title_data, "body": body_data, "tags": tags_data},
{"priority": priority_targets, "class": dept_targets},
epochs=2,
batch_size=32,
)
Code credit: TensorFlow Keras Functional Guide
ผลลัพธ์
Sample input data Sample target data The model is being fit to the data Epoch 1/2 40/40 [==============================] - 5s 43ms/step - loss: 1.2738 - priority_loss: 0.7043 - class_loss: 2.8477 Epoch 2/2 40/40 [==============================] - 2s 44ms/step - loss: 1.2720 - priority_loss: 0.6997 - class_loss: 2.8612 <tensorflow.python.keras.callbacks.History at 0x7f48d0809e80>
คำอธิบาย
- สร้าง Sample Input และ Target Data แบบสุ่ม
- เทรนโมเดลโดยส่ง Numpy Array ของ Inputs (Dict) และ Targets (Dict)
- รองรับ Multiple Inputs (title, body, tags) และ Multiple Outputs (priority, class)